Unify code paths for buying coffee and throwing parties

This commit is contained in:
Staszek Welsh 2022-06-01 23:26:32 +01:00
parent 1ba5902e1e
commit ffaa38d086
5 changed files with 95 additions and 73 deletions

@ -333,14 +333,21 @@ export function UpgradeOfficeSize(corp: ICorporation, office: OfficeSpace, size:
corp.funds = corp.funds - cost; corp.funds = corp.funds - cost;
} }
export function BuyCoffee(corp: ICorporation, office: OfficeSpace): void {
const cost = 500e3 * office.employees.length;
if (corp.funds < cost) { return; }
if (!office.setCoffee()) { return; }
corp.funds -= cost;
}
export function ThrowParty(corp: ICorporation, office: OfficeSpace, costPerEmployee: number): number { export function ThrowParty(corp: ICorporation, office: OfficeSpace, costPerEmployee: number): number {
const totalCost = costPerEmployee * office.employees.length; const mult = 1 + costPerEmployee / 10e6;
if (corp.funds < totalCost) return 0; const cost = costPerEmployee * office.employees.length;
corp.funds = corp.funds - totalCost; if (corp.funds < cost) { return 0; }
let mult = 0;
for (let i = 0; i < office.employees.length; ++i) { if (!office.setParty(mult)) { return 0; }
mult = office.employees[i].throwParty(costPerEmployee); corp.funds -= cost;
}
return mult; return mult;
} }
@ -372,17 +379,6 @@ export function UpgradeWarehouse(corp: ICorporation, division: IIndustry, wareho
corp.funds = corp.funds - sizeUpgradeCost; corp.funds = corp.funds - sizeUpgradeCost;
} }
export function BuyCoffee(corp: ICorporation, division: IIndustry, office: OfficeSpace): void {
const upgrade = IndustryUpgrades[0];
const cost = office.employees.length * upgrade[1];
if (corp.funds < cost) return;
corp.funds = corp.funds - cost;
division.upgrade(upgrade, {
corporation: corp,
office: office,
});
}
export function HireAdVert(corp: ICorporation, division: IIndustry, office: OfficeSpace): void { export function HireAdVert(corp: ICorporation, division: IIndustry, office: OfficeSpace): void {
const upgrade = IndustryUpgrades[1]; const upgrade = IndustryUpgrades[1];
const cost = upgrade[1] * Math.pow(upgrade[2], division.upgrades[1]); const cost = upgrade[1] * Math.pow(upgrade[2], division.upgrades[1]);

@ -58,8 +58,8 @@ export class Employee {
//Returns the amount the employee needs to be paid //Returns the amount the employee needs to be paid
process(marketCycles = 1, office: OfficeSpace): number { process(marketCycles = 1, office: OfficeSpace): number {
const gain = 0.003 * marketCycles, const gain = 0.003 * marketCycles;
det = gain * Math.random(); const det = gain * Math.random();
this.exp += gain; this.exp += gain;
//Training //Training
@ -74,12 +74,6 @@ export class Employee {
this.ene -= det; this.ene -= det;
this.hap -= det; this.hap -= det;
if (this.ene < office.minEne) {
this.ene = office.minEne;
}
if (this.hap < office.minHap) {
this.hap = office.minHap;
}
const salary = this.sal * marketCycles * CorporationConstants.SecsPerMarketCycle; const salary = this.sal * marketCycles * CorporationConstants.SecsPerMarketCycle;
return salary; return salary;
} }
@ -120,16 +114,6 @@ export class Employee {
return prodBase * prodMult; return prodBase * prodMult;
} }
//Process benefits from having an office party thrown
throwParty(money: number): number {
const mult = 1 + money / 10e6;
this.mor *= mult;
this.mor = Math.min(100, this.mor);
this.hap *= mult;
this.hap = Math.min(100, this.hap);
return mult;
}
toJSON(): any { toJSON(): any {
return Generic_toJSON("Employee", this); return Generic_toJSON("Employee", this);
} }

@ -15,11 +15,20 @@ interface IParams {
export class OfficeSpace { export class OfficeSpace {
loc: string; loc: string;
size: number; size: number;
minEne = 0; minEne = 0;
maxEne = 100;
minHap = 0; minHap = 0;
minMor = 0;
maxEne = 100;
maxHap = 100; maxHap = 100;
maxMor = 100; maxMor = 100;
autoCoffee = false;
autoParty = false;
coffeeMult = 0;
partyMult = 0;
employees: Employee[] = []; employees: Employee[] = [];
employeeProd: { [key: string]: number } = { employeeProd: { [key: string]: number } = {
[EmployeePositions.Operations]: 0, [EmployeePositions.Operations]: 0,
@ -66,11 +75,8 @@ export class OfficeSpace {
} }
} }
for (let i = 0; i < this.employees.length; ++i) { // Update employee jobs and job counts
const emp = this.employees[i]; for (const employee of this.employees) { employee.pos = employee.nextPos; }
emp.pos = emp.nextPos;
}
this.calculateTotalEmployees(); this.calculateTotalEmployees();
this.calculateNextEmployees(); this.calculateNextEmployees();
@ -78,6 +84,7 @@ export class OfficeSpace {
this.maxEne = 100; this.maxEne = 100;
this.maxHap = 100; this.maxHap = 100;
this.maxMor = 100; this.maxMor = 100;
if (industry.hasResearch("Go-Juice")) { if (industry.hasResearch("Go-Juice")) {
this.maxEne += 10; this.maxEne += 10;
} }
@ -87,6 +94,12 @@ export class OfficeSpace {
if (industry.hasResearch("Sti.mu")) { if (industry.hasResearch("Sti.mu")) {
this.maxMor += 10; this.maxMor += 10;
} }
if (industry.hasResearch("AutoBrew")) {
this.autoCoffee = true;
}
if (industry.hasResearch("AutoPartyManager")) {
this.autoParty = true;
}
// Calculate changes in Morale/Happiness/Energy for Employees // Calculate changes in Morale/Happiness/Energy for Employees
let perfMult = 1; //Multiplier for employee morale/happiness/energy based on company performance let perfMult = 1; //Multiplier for employee morale/happiness/energy based on company performance
@ -96,35 +109,40 @@ export class OfficeSpace {
perfMult = Math.pow(1.01, marketCycles); perfMult = Math.pow(1.01, marketCycles);
} }
const hasAutobrew = industry.hasResearch("AutoBrew"); let totalSalary = 0;
const hasAutoparty = industry.hasResearch("AutoPartyManager"); for (const employee of this.employees) {
const salary = employee.process(marketCycles, this);
totalSalary += salary;
let salaryPaid = 0; if (this.autoCoffee) {
for (let i = 0; i < this.employees.length; ++i) { employee.ene = this.maxEne;
const emp = this.employees[i]; } else if (this.coffeeMult > 1) {
if (hasAutoparty) { employee.ene *= this.coffeeMult;
emp.mor = this.maxMor;
emp.hap = this.maxHap;
} else { } else {
emp.mor *= perfMult; employee.ene *= perfMult;
emp.hap *= perfMult;
emp.mor = Math.min(emp.mor, this.maxMor);
emp.hap = Math.min(emp.hap, this.maxHap);
} }
if (hasAutobrew) { if (this.autoParty) {
emp.ene = this.maxEne; employee.mor = this.maxMor;
employee.hap = this.maxHap;
} else if (this.partyMult > 1) {
employee.mor *= this.partyMult;
employee.hap *= this.partyMult;
} else { } else {
emp.ene *= perfMult; employee.mor *= perfMult;
emp.ene = Math.min(emp.ene, this.maxEne); employee.hap *= perfMult;
} }
const salary = emp.process(marketCycles, this); employee.ene = Math.max(Math.min(employee.ene, this.maxEne), this.minEne);
salaryPaid += salary; employee.mor = Math.max(Math.min(employee.mor, this.maxMor), this.minMor);
employee.hap = Math.max(Math.min(employee.hap, this.maxHap), this.minHap);
} }
this.coffeeMult = 0;
this.partyMult = 0;
this.calculateEmployeeProductivity(corporation, industry); this.calculateEmployeeProductivity(corporation, industry);
return salaryPaid; return totalSalary;
} }
calculateNextEmployees(): void { calculateNextEmployees(): void {
@ -227,6 +245,24 @@ export class OfficeSpace {
return count === target; return count === target;
} }
setCoffee(mult = 1.05): boolean {
if (mult > 1 && this.coffeeMult === 0 && !this.autoCoffee) {
this.coffeeMult = mult;
return true;
}
return false;
}
setParty(mult: number): boolean {
if (mult > 1 && this.partyMult === 0 && !this.autoParty) {
this.partyMult = mult;
return true;
}
return false;
}
toJSON(): any { toJSON(): any {
return Generic_toJSON("OfficeSpace", this); return Generic_toJSON("OfficeSpace", this);
} }

@ -36,10 +36,10 @@ import {
AssignJob, AssignJob,
AutoAssignJob, AutoAssignJob,
UpgradeOfficeSize, UpgradeOfficeSize,
ThrowParty,
PurchaseWarehouse, PurchaseWarehouse,
UpgradeWarehouse, UpgradeWarehouse,
BuyCoffee, BuyCoffee,
ThrowParty,
HireAdVert, HireAdVert,
MakeProduct, MakeProduct,
Research, Research,
@ -774,16 +774,18 @@ export function NetscriptCorporation(player: IPlayer, workerScript: WorkerScript
const divisionName = ctx.helper.string("divisionName", _divisionName); const divisionName = ctx.helper.string("divisionName", _divisionName);
const cityName = ctx.helper.city("cityName", _cityName); const cityName = ctx.helper.city("cityName", _cityName);
const costPerEmployee = ctx.helper.number("costPerEmployee", _costPerEmployee); const costPerEmployee = ctx.helper.number("costPerEmployee", _costPerEmployee);
if (costPerEmployee < 0)
if (costPerEmployee < 0) {
throw new Error("Invalid value for Cost Per Employee field! Must be numeric and greater than 0"); throw new Error("Invalid value for Cost Per Employee field! Must be numeric and greater than 0");
const office = getOffice(divisionName, cityName); }
const corporation = getCorporation(); const corporation = getCorporation();
return netscriptDelay( const office = getOffice(divisionName, cityName);
(60 * 1000) / (player.hacking_speed_mult * calculateIntelligenceBonus(player.intelligence, 1)),
workerScript, return netscriptDelay(0, workerScript).then(function () {
).then(function () {
return Promise.resolve(ThrowParty(corporation, office, costPerEmployee)); return Promise.resolve(ThrowParty(corporation, office, costPerEmployee));
}); });
//return ThrowParty(corporation, office, costPerEmployee)
}, },
buyCoffee: buyCoffee:
(ctx: NetscriptContext) => (ctx: NetscriptContext) =>
@ -791,13 +793,14 @@ export function NetscriptCorporation(player: IPlayer, workerScript: WorkerScript
checkAccess(ctx, 8); checkAccess(ctx, 8);
const divisionName = ctx.helper.string("divisionName", _divisionName); const divisionName = ctx.helper.string("divisionName", _divisionName);
const cityName = ctx.helper.city("cityName", _cityName); const cityName = ctx.helper.city("cityName", _cityName);
const corporation = getCorporation(); const corporation = getCorporation();
return netscriptDelay( const office = getOffice(divisionName, cityName);
(60 * 1000) / (player.hacking_speed_mult * calculateIntelligenceBonus(player.intelligence, 1)),
workerScript, return netscriptDelay(0, workerScript).then(function () {
).then(function () { return Promise.resolve(BuyCoffee(corporation, office));
return Promise.resolve(BuyCoffee(corporation, getDivision(divisionName), getOffice(divisionName, cityName)));
}); });
//BuyCoffee(corporation, getOffice(divisionName, cityName);
}, },
hireAdVert: hireAdVert:
(ctx: NetscriptContext) => (ctx: NetscriptContext) =>
@ -829,6 +832,7 @@ export function NetscriptCorporation(player: IPlayer, workerScript: WorkerScript
maxEne: office.maxEne, maxEne: office.maxEne,
minHap: office.minHap, minHap: office.minHap,
maxHap: office.maxHap, maxHap: office.maxHap,
minMor: office.minMor,
maxMor: office.maxMor, maxMor: office.maxMor,
employees: office.employees.map((e) => e.name), employees: office.employees.map((e) => e.name),
employeeProd: { employeeProd: {

@ -7222,6 +7222,8 @@ interface Office {
minHap: number; minHap: number;
/** Maximum happiness of the employees */ /** Maximum happiness of the employees */
maxHap: number; maxHap: number;
/** Minimum morale of the employees */
minMor: number;
/** Maximum morale of the employees */ /** Maximum morale of the employees */
maxMor: number; maxMor: number;
/** Name of all the employees */ /** Name of all the employees */