From be081614422bfc37d25c6ecd36b6dad2de6230ac Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Fri, 10 Sep 2021 00:13:28 -0400 Subject: [PATCH] corp api --- package-lock.json | 9 +- src/Corporation/Actions.ts | 45 +++++++ src/Corporation/ui/IndustryWarehouse.tsx | 21 ++-- src/Corporation/ui/ThrowPartyPopup.tsx | 7 +- src/Corporation/ui/UpgradeOfficeSizePopup.tsx | 4 +- src/NetscriptFunctions.js | 114 ++++++++++++------ 6 files changed, 138 insertions(+), 62 deletions(-) diff --git a/package-lock.json b/package-lock.json index cb3013eff..46eb58eab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,11 @@ { "name": "bitburner", - "version": "0.52.9", + "version": "0.53.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "bitburner", - "version": "0.52.9", + "version": "0.53.0", "hasInstallScript": true, "license": "SEE LICENSE IN license.txt", "dependencies": { @@ -39259,7 +39258,9 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/mathjax-react/-/mathjax-react-1.0.6.tgz", "integrity": "sha512-GlkPAhaY0FKc95TMdo33mxNZHb+3xRgfgc3YcnQ0cZxvnM1UaF0o8mRI5y5xIwi3JnioeYuukZJWCbIZkACIVw==", - "requires": {} + "requires": { + "mathjax-full": "^3.0.4" + } }, "mathml-tag-names": { "version": "2.1.0", diff --git a/src/Corporation/Actions.ts b/src/Corporation/Actions.ts index 6edf65f00..c3637dc05 100644 --- a/src/Corporation/Actions.ts +++ b/src/Corporation/Actions.ts @@ -10,6 +10,7 @@ import { Warehouse } from "./Warehouse"; import { CorporationUnlockUpgrade } from "./data/CorporationUnlockUpgrades"; import { CorporationUpgrade } from "./data/CorporationUpgrades"; import { Cities } from "../Locations/Cities"; +import { EmployeePositions } from "./EmployeePositions"; export function NewIndustry(corporation: ICorporation, industry: string, name: string): void { for (let i = 0; i < corporation.divisions.length; ++i) { @@ -232,3 +233,47 @@ export function BuyMaterial(material: Material, amt: number): void { } material.buy = amt; } + +export function AssignJob(employee: Employee, job: string): void { + if (!Object.values(EmployeePositions).includes(job)) throw new Error(`'${job}' is not a valid job.`); + employee.pos = job; +} + +export function UpgradeOfficeSize(corp: ICorporation, office: OfficeSpace, size: number): void { + const initialPriceMult = Math.round(office.size / CorporationConstants.OfficeInitialSize); + const costMultiplier = 1.09; + // Calculate cost to upgrade size by 15 employees + let mult = 0; + for (let i = 0; i < size / CorporationConstants.OfficeInitialSize; ++i) { + mult += Math.pow(costMultiplier, initialPriceMult + i); + } + const cost = CorporationConstants.OfficeInitialCost * mult; + console.log(cost); + if (corp.funds.lt(cost)) return; + office.size += size; + corp.funds = corp.funds.minus(cost); +} + +export function ThrowParty(corp: ICorporation, office: OfficeSpace, costPerEmployee: number): number { + const totalCost = costPerEmployee * office.employees.length; + if (corp.funds.lt(totalCost)) return 0; + corp.funds = corp.funds.minus(totalCost); + let mult = 0; + for (let i = 0; i < office.employees.length; ++i) { + mult = office.employees[i].throwParty(costPerEmployee); + } + + return mult; +} + +export function PurchaseWarehouse(corp: ICorporation, division: IIndustry, city: string): void { + if (corp.funds.lt(CorporationConstants.WarehouseInitialCost)) return; + if (division.warehouses[city] instanceof Warehouse) return; + division.warehouses[city] = new Warehouse({ + corp: corp, + industry: division, + loc: city, + size: CorporationConstants.WarehouseInitialSize, + }); + corp.funds = corp.funds.minus(CorporationConstants.WarehouseInitialCost); +} diff --git a/src/Corporation/ui/IndustryWarehouse.tsx b/src/Corporation/ui/IndustryWarehouse.tsx index 55181ebdb..bedbe1242 100644 --- a/src/Corporation/ui/IndustryWarehouse.tsx +++ b/src/Corporation/ui/IndustryWarehouse.tsx @@ -29,6 +29,7 @@ import { Money } from "../../ui/React/Money"; import { MoneyCost } from "./MoneyCost"; import { isRelevantMaterial } from "./Helpers"; import { IndustryProductEquation } from "./IndustryProductEquation"; +import { PurchaseWarehouse } from "../Actions"; interface IProductProps { corp: ICorporation; @@ -603,18 +604,8 @@ export function IndustryWarehouse(props: IProps): React.ReactElement { } function purchaseWarehouse(division: IIndustry, city: string): void { - if (props.corp.funds.lt(CorporationConstants.WarehouseInitialCost)) { - dialogBoxCreate("You do not have enough funds to do this!"); - } else { - division.warehouses[city] = new Warehouse({ - corp: props.corp, - industry: division, - loc: city, - size: CorporationConstants.WarehouseInitialSize, - }); - props.corp.funds = props.corp.funds.minus(CorporationConstants.WarehouseInitialCost); - props.rerender(); - } + PurchaseWarehouse(props.corp, division, city); + props.rerender(); } if (props.warehouse instanceof Warehouse) { @@ -622,7 +613,11 @@ export function IndustryWarehouse(props: IProps): React.ReactElement { } else { return (
- diff --git a/src/Corporation/ui/ThrowPartyPopup.tsx b/src/Corporation/ui/ThrowPartyPopup.tsx index beb84ab9d..0899eef24 100644 --- a/src/Corporation/ui/ThrowPartyPopup.tsx +++ b/src/Corporation/ui/ThrowPartyPopup.tsx @@ -4,6 +4,7 @@ import { numeralWrapper } from "../../ui/numeralFormat"; import { dialogBoxCreate } from "../../../utils/DialogBox"; import { OfficeSpace } from "../OfficeSpace"; import { ICorporation } from "../ICorporation"; +import { ThrowParty } from "../Actions"; interface IProps { office: OfficeSpace; @@ -26,11 +27,7 @@ export function ThrowPartyPopup(props: IProps): React.ReactElement { if (props.corp.funds.lt(totalCost)) { dialogBoxCreate("You don't have enough company funds to throw a party!"); } else { - props.corp.funds = props.corp.funds.minus(totalCost); - let mult = 0; - for (let i = 0; i < props.office.employees.length; ++i) { - mult = props.office.employees[i].throwParty(cost); - } + const mult = ThrowParty(props.corp, props.office, cost); dialogBoxCreate( "You threw a party for the office! The morale and happiness " + "of each employee increased by " + diff --git a/src/Corporation/ui/UpgradeOfficeSizePopup.tsx b/src/Corporation/ui/UpgradeOfficeSizePopup.tsx index 6acc92c7a..3c344a7dd 100644 --- a/src/Corporation/ui/UpgradeOfficeSizePopup.tsx +++ b/src/Corporation/ui/UpgradeOfficeSizePopup.tsx @@ -6,6 +6,7 @@ import { CorporationConstants } from "../data/Constants"; import { OfficeSpace } from "../OfficeSpace"; import { ICorporation } from "../ICorporation"; import { IPlayer } from "../../PersonObjects/IPlayer"; +import { UpgradeOfficeSize } from "../Actions"; interface IProps { office: OfficeSpace; @@ -48,8 +49,7 @@ export function UpgradeOfficeSizePopup(props: IProps): React.ReactElement { if (props.corp.funds.lt(cost)) { dialogBoxCreate("You don't have enough company funds to purchase this upgrade!"); } else { - props.office.size += size; - props.corp.funds = props.corp.funds.minus(cost); + UpgradeOfficeSize(props.corp, props.office, size); dialogBoxCreate("Office space increased! It can now hold " + props.office.size + " employees"); props.rerender(); } diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 9b256cdd7..48e189ffc 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -27,6 +27,9 @@ import { SellProduct, SetSmartSupply, BuyMaterial, + AssignJob, + UpgradeOfficeSize, + ThrowParty, } from "./Corporation/Actions"; import { CorporationUnlockUpgrades } from "./Corporation/data/CorporationUnlockUpgrades"; import { CorporationUpgrades } from "./Corporation/data/CorporationUpgrades"; @@ -561,6 +564,14 @@ function NetscriptFunctions(workerScript) { return division; } + function getOffice(divisionName, cityName) { + const division = getDivision(divisionName); + if (!(cityName in division.offices)) throw new Error(`Invalid city name '${cityName}'`); + const office = division.offices[cityName]; + if (office === 0) throw new Error(`${division.name} has not expanded to '${cityName}'`); + return office; + } + function getWarehouse(divisionName, cityName) { const division = getDivision(divisionName); if (!(cityName in division.warehouses)) throw new Error(`Invalid city name '${cityName}'`); @@ -583,6 +594,13 @@ function NetscriptFunctions(workerScript) { return product; } + function getEmployee(divisionName, cityName, employeeName) { + const office = getOffice(divisionName, cityName); + const employee = office.employees.find((e) => e.name === employeeName); + if (employee === undefined) throw new Error(`Invalid employee name: '${employeeName}'`); + return employee; + } + const runAfterReset = function (cbScript = null) { //Run a script after reset if (cbScript && isString(cbScript)) { @@ -4479,44 +4497,64 @@ function NetscriptFunctions(workerScript) { }, }, // End Bladeburner - // corporation: { - // expandIndustry: function (industryName, divisionName) { - // NewIndustry(Player.corporation, industryName, divisionName); - // }, - // expandCity: function (divisionName, cityName) { - // const division = getDivision(divisionName); - // NewCity(Player.corporation, division, cityName); - // }, - // unlockUpgrade: function (upgradeName) { - // const upgrade = Object.values(CorporationUnlockUpgrades).find((upgrade) => upgrade[2] === upgradeName); - // if (upgrade === undefined) throw new Error("No upgrade named '${upgradeName}'"); - // UnlockUpgrade(Player.corporation, upgrade); - // }, - // levelUpgrade: function (upgradeName) { - // const upgrade = Object.values(CorporationUpgrades).find((upgrade) => upgrade[4] === upgradeName); - // if (upgrade === undefined) throw new Error("No upgrade named '${upgradeName}'"); - // LevelUpgrade(Player.corporation, upgrade); - // }, - // issueDividends: function (percent) { - // IssueDividends(Player.corporation, percent); - // }, - // sellMaterial: function (divisionName, cityName, materialName, amt, price) { - // const material = getMaterial(divisionName, cityName, materialName); - // SellMaterial(material, amt, price); - // }, - // sellProduct: function (divisionName, cityName, productName, amt, price, all) { - // const product = getProduct(divisionName, productName); - // SellProduct(product, cityName, amt, price, all); - // }, - // setSmartSupply: function (divisionName, cityName, enabled) { - // const warehouse = getWarehouse(divisionName, cityName); - // SetSmartSupply(warehouse, enabled); - // }, - // buyMaterial: function (divisionName, cityName, materialName, amt) { - // const material = getMaterial(divisionName, cityName, materialName); - // BuyMaterial(material, amt); - // }, - // }, // End Corporation API + corporation: { + expandIndustry: function (industryName, divisionName) { + NewIndustry(Player.corporation, industryName, divisionName); + }, + expandCity: function (divisionName, cityName) { + const division = getDivision(divisionName); + NewCity(Player.corporation, division, cityName); + }, + unlockUpgrade: function (upgradeName) { + const upgrade = Object.values(CorporationUnlockUpgrades).find((upgrade) => upgrade[2] === upgradeName); + if (upgrade === undefined) throw new Error("No upgrade named '${upgradeName}'"); + UnlockUpgrade(Player.corporation, upgrade); + }, + levelUpgrade: function (upgradeName) { + const upgrade = Object.values(CorporationUpgrades).find((upgrade) => upgrade[4] === upgradeName); + if (upgrade === undefined) throw new Error("No upgrade named '${upgradeName}'"); + LevelUpgrade(Player.corporation, upgrade); + }, + issueDividends: function (percent) { + IssueDividends(Player.corporation, percent); + }, + sellMaterial: function (divisionName, cityName, materialName, amt, price) { + const material = getMaterial(divisionName, cityName, materialName); + SellMaterial(material, amt, price); + }, + sellProduct: function (divisionName, cityName, productName, amt, price, all) { + const product = getProduct(divisionName, productName); + SellProduct(product, cityName, amt, price, all); + }, + setSmartSupply: function (divisionName, cityName, enabled) { + const warehouse = getWarehouse(divisionName, cityName); + SetSmartSupply(warehouse, enabled); + }, + buyMaterial: function (divisionName, cityName, materialName, amt) { + const material = getMaterial(divisionName, cityName, materialName); + BuyMaterial(material, amt); + }, + employees: function (divisionName, cityName) { + const office = getOffice(divisionName, cityName); + return office.employees.map((e) => Object.assign({}, e)); + }, + assignJob: function (divisionName, cityName, employeeName, job) { + const employee = getEmployee(divisionName, cityName, employeeName); + AssignJob(employee, job); + }, + hireEmployee: function (divisionName, cityName) { + const office = getOffice(divisionName, cityName); + office.hireRandomEmployee(); + }, + upgradeOfficeSize: function (divisionName, cityName, size) { + const office = getOffice(divisionName, cityName); + UpgradeOfficeSize(Player.corporation, office, size); + }, + throwParty: function (divisionName, cityName, costPerEmployee) { + const office = getOffice(divisionName, cityName); + ThrowParty(Player.corporation, office, costPerEmployee); + }, + }, // End Corporation API // Coding Contract API codingcontract: {