CORPORATION: Fix wrong average price of material (#1227)

This commit is contained in:
catloversg 2024-04-24 07:14:22 +07:00 committed by GitHub
parent eba86e4bf0
commit 7a1fce6f64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 1 deletions

@ -358,6 +358,8 @@ export function BulkPurchase(
const cost = amt * material.marketPrice; const cost = amt * material.marketPrice;
if (corp.funds >= cost) { if (corp.funds >= cost) {
corp.loseFunds(cost, "materials"); corp.loseFunds(cost, "materials");
material.averagePrice =
(material.averagePrice * material.stored + material.marketPrice * amt) / (material.stored + amt);
material.stored += amt; material.stored += amt;
warehouse.sizeUsed = warehouse.sizeUsed + amt * matSize; warehouse.sizeUsed = warehouse.sizeUsed + amt * matSize;
} else { } else {

@ -77,6 +77,7 @@ export class Material {
this.competition = MaterialInfo[this.name].competitionBase; this.competition = MaterialInfo[this.name].competitionBase;
this.competitionRange = MaterialInfo[this.name].competitionRange; this.competitionRange = MaterialInfo[this.name].competitionRange;
this.marketPrice = MaterialInfo[this.name].baseCost; this.marketPrice = MaterialInfo[this.name].baseCost;
this.averagePrice = this.marketPrice;
this.maxVolatility = MaterialInfo[this.name].maxVolatility; this.maxVolatility = MaterialInfo[this.name].maxVolatility;
this.markup = MaterialInfo[this.name].baseMarkup; this.markup = MaterialInfo[this.name].baseMarkup;
} }
@ -135,7 +136,13 @@ export class Material {
// Initializes a Material object from a JSON save state. // Initializes a Material object from a JSON save state.
static fromJSON(value: IReviverValue): Material { static fromJSON(value: IReviverValue): Material {
const material = Generic_fromJSON(Material, value.data); const material = Generic_fromJSON(Material, value.data);
if (isNaN(material.quality)) material.quality = 1; if (isNaN(material.quality)) {
material.quality = 1;
}
// averagePrice has not been initialized properly, so if it is 0 (wrong initial value), we set it to marketPrice.
if (material.averagePrice === 0) {
material.averagePrice = material.marketPrice;
}
return material; return material;
} }
} }