Merge pull request #2618 from pigalot/Corp-Api-Updates

Corp api updates
This commit is contained in:
hydroflame
2022-01-18 18:05:23 -05:00
committed by GitHub
60 changed files with 2749 additions and 89 deletions
+315 -20
View File
@@ -21,6 +21,7 @@ import {
Division as NSDivision,
WarehouseAPI,
OfficeAPI,
InvestmentOffer
} from "../ScriptEditor/NetscriptDefinitions";
import {
@@ -53,12 +54,161 @@ import { CorporationUnlockUpgrades } from "../Corporation/data/CorporationUnlock
import { CorporationUpgrades } from "../Corporation/data/CorporationUpgrades";
import { EmployeePositions } from "../Corporation/EmployeePositions";
import { calculateIntelligenceBonus } from "../PersonObjects/formulas/intelligence";
import { Industry } from "../Corporation/Industry";
import { IndustryResearchTrees, IndustryStartingCosts } from "../Corporation/IndustryData";
import { CorporationConstants } from "../Corporation/data/Constants";
import { IndustryUpgrades } from "../Corporation/IndustryUpgrades";
import { ResearchMap } from "../Corporation/ResearchMap";
import { Factions } from "../Faction/Factions";
export function NetscriptCorporation(
player: IPlayer,
workerScript: WorkerScript,
helper: INetscriptHelper,
): NSCorporation {
function createCorporation(corporationName: string, selfFund = true): boolean {
if (!player.canAccessCorporation() || player.hasCorporation()) return false;
if (!corporationName) return false;
if (player.bitNodeN !== 3 && !selfFund) throw new Error("cannot use seed funds outside of BitNode 3");
if (selfFund) {
if (!player.canAfford(150e9)) return false;
player.startCorporation(corporationName);
player.loseMoney(150e9, "corporation");
} else {
player.startCorporation(corporationName, 500e6);
}
return true;
}
function hasUnlockUpgrade(upgradeName: string): boolean {
const corporation = getCorporation();
const upgrade = Object.values(CorporationUnlockUpgrades).find((upgrade) => upgrade[2] === upgradeName);
if (upgrade === undefined) throw new Error(`No upgrade named '${upgradeName}'`);
const upgN = upgrade[0];
return corporation.unlockUpgrades[upgN] === 1;
}
function getUnlockUpgradeCost(upgradeName: string): number {
const upgrade = Object.values(CorporationUnlockUpgrades).find((upgrade) => upgrade[2] === upgradeName);
if (upgrade === undefined) throw new Error(`No upgrade named '${upgradeName}'`);
return upgrade[1];
}
function getUpgradeLevel(aupgradeName: string): number {
const upgradeName = helper.string("levelUpgrade", "upgradeName", aupgradeName);
const corporation = getCorporation();
const upgrade = Object.values(CorporationUpgrades).find((upgrade) => upgrade[4] === upgradeName);
if (upgrade === undefined) throw new Error(`No upgrade named '${upgradeName}'`);
const upgN = upgrade[0];
return corporation.upgrades[upgN];
}
function getUpgradeLevelCost(aupgradeName: string): number {
const upgradeName = helper.string("levelUpgrade", "upgradeName", aupgradeName);
const corporation = getCorporation();
const upgrade = Object.values(CorporationUpgrades).find((upgrade) => upgrade[4] === upgradeName);
if (upgrade === undefined) throw new Error(`No upgrade named '${upgradeName}'`);
const upgN = upgrade[0];
const baseCost = upgrade[1];
const priceMult = upgrade[2];
const level = corporation.upgrades[upgN];
return baseCost * Math.pow(priceMult, level);
}
function getExpandIndustryCost(industryName: string): number {
const cost = IndustryStartingCosts[industryName];
if (cost === undefined) {
throw new Error(`Invalid industry: '${industryName}'`);
}
return cost;
}
function getExpandCityCost(): number {
return CorporationConstants.OfficeInitialCost;
}
function getInvestmentOffer(): InvestmentOffer {
const corporation = getCorporation();
if (corporation.fundingRound >= CorporationConstants.FundingRoundShares.length || corporation.fundingRound >= CorporationConstants.FundingRoundMultiplier.length || corporation.public)
return {
funds: 0,
shares: 0,
round: corporation.fundingRound + 1 // Make more readable
}; // Don't throw an error here, no reason to have a second function to check if you can get investment.
const val = corporation.determineValuation();
const percShares = CorporationConstants.FundingRoundShares[corporation.fundingRound];
const roundMultiplier = CorporationConstants.FundingRoundMultiplier[corporation.fundingRound];
const funding = val * percShares * roundMultiplier;
const investShares = Math.floor(CorporationConstants.INITIALSHARES * percShares);
return {
funds: funding,
shares: investShares,
round: corporation.fundingRound + 1 // Make more readable
};
}
function acceptInvestmentOffer(): boolean {
const corporation = getCorporation();
if (corporation.fundingRound >= CorporationConstants.FundingRoundShares.length || corporation.fundingRound >= CorporationConstants.FundingRoundMultiplier.length || corporation.public) return false;
const val = corporation.determineValuation();
const percShares = CorporationConstants.FundingRoundShares[corporation.fundingRound];
const roundMultiplier = CorporationConstants.FundingRoundMultiplier[corporation.fundingRound];
const funding = val * percShares * roundMultiplier;
const investShares = Math.floor(CorporationConstants.INITIALSHARES * percShares);
corporation.fundingRound++;
corporation.addFunds(funding);
corporation.numShares -= investShares;
return true;
}
function goPublic(numShares: number): boolean {
const corporation = getCorporation();
const initialSharePrice = corporation.determineValuation() / corporation.totalShares;
if (isNaN(numShares)) throw new Error("Invalid value for number of issued shares");
if (numShares < 0) throw new Error("Invalid value for number of issued shares");
if (numShares > corporation.numShares) throw new Error("You don't have that many shares to issue!");
corporation.public = true;
corporation.sharePrice = initialSharePrice;
corporation.issuedShares = numShares;
corporation.numShares -= numShares;
corporation.addFunds(numShares * initialSharePrice);
return true;
}
function getResearchCost(division: IIndustry, researchName: string): number {
const researchTree = IndustryResearchTrees[division.type];
if (researchTree === undefined) throw new Error(`No research tree for industry '${division.type}'`);
const allResearch = researchTree.getAllNodes();
if (!allResearch.includes(researchName)) throw new Error(`No research named '${researchName}'`);
const research = ResearchMap[researchName];
return research.cost;
}
function hasResearched(division: IIndustry, researchName: string): boolean {
return division.researched[researchName] === undefined ? false : division.researched[researchName] as boolean;
}
function bribe(factionName: string, amountCash: number, amountShares: number): boolean {
if (!player.factions.includes(factionName)) throw new Error("Invalid faction name");
if (isNaN(amountCash) || amountCash < 0 || isNaN(amountShares) || amountShares < 0) throw new Error("Invalid value for amount field! Must be numeric, grater than 0.");
const corporation = getCorporation();
if (corporation.funds < amountCash) return false;
if (corporation.numShares < amountShares) return false;
const faction = Factions[factionName]
const info = faction.getInfo();
if (!info.offersWork()) return false;
if (player.hasGangWith(factionName)) return false;
const repGain = (amountCash + amountShares * corporation.sharePrice) / CorporationConstants.BribeToRepRatio;
faction.playerReputation += repGain;
corporation.funds = corporation.funds - amountCash;
corporation.numShares -= amountShares;
return true;
}
function getCorporation(): ICorporation {
const corporation = player.corporation;
if (corporation === null) throw new Error("cannot be called without a corporation");
@@ -90,7 +240,8 @@ export function NetscriptCorporation(
function getMaterial(divisionName: any, cityName: any, materialName: any): Material {
const warehouse = getWarehouse(divisionName, cityName);
const material = warehouse.materials[materialName];
const matName = (materialName as string).replace(/ /g, "");
const material = warehouse.materials[matName];
if (material === undefined) throw new Error(`Invalid material name: '${materialName}'`);
return material;
}
@@ -117,7 +268,50 @@ export function NetscriptCorporation(
throw helper.makeRuntimeErrorMsg(`corporation.${func}`, "You do not have access to this API.");
}
function getSafeDivision(division: Industry): NSDivision {
const cities: string[] = [];
for (const office of Object.values(division.offices)) {
if (office === 0) continue;
cities.push(office.loc);
}
return {
name: division.name,
type: division.type,
awareness: division.awareness,
popularity: division.popularity,
prodMult: division.prodMult,
research: division.sciResearch.qty,
lastCycleRevenue: division.lastCycleRevenue,
lastCycleExpenses: division.lastCycleExpenses,
thisCycleRevenue: division.thisCycleRevenue,
thisCycleExpenses: division.thisCycleExpenses,
upgrades: division.upgrades,
cities: cities,
products: division.products === undefined ? [] : Object.keys(division.products),
};
}
const warehouseAPI: WarehouseAPI = {
getPurchaseWarehouseCost: function (): number {
checkAccess("getPurchaseWarehouseCost", 7);
return CorporationConstants.WarehouseInitialCost;
},
getUpgradeWarehouseCost: function (adivisionName: any, acityName: any): number {
checkAccess("upgradeWarehouse", 7);
const divisionName = helper.string("getUpgradeWarehouseCost", "divisionName", adivisionName);
const cityName = helper.string("getUpgradeWarehouseCost", "cityName", acityName);
const warehouse = getWarehouse(divisionName, cityName);
return CorporationConstants.WarehouseUpgradeBaseCost * Math.pow(1.07, warehouse.level + 1);
},
hasWarehouse: function (adivisionName: any, acityName: any): boolean {
checkAccess("hasWarehouse", 7);
const divisionName = helper.string("getWarehouse", "divisionName", adivisionName);
const cityName = helper.string("getWarehouse", "cityName", acityName);
const division = getDivision(divisionName);
if (!(cityName in division.warehouses)) throw new Error(`Invalid city name '${cityName}'`);
const warehouse = division.warehouses[cityName];
return warehouse !== 0;
},
getWarehouse: function (adivisionName: any, acityName: any): NSWarehouse {
checkAccess("getWarehouse", 7);
const divisionName = helper.string("getWarehouse", "divisionName", adivisionName);
@@ -128,6 +322,7 @@ export function NetscriptCorporation(
loc: warehouse.loc,
size: warehouse.size,
sizeUsed: warehouse.sizeUsed,
smartSupplyEnabled: warehouse.smartSupplyEnabled
};
},
getMaterial: function (adivisionName: any, acityName: any, amaterialName: any): NSMaterial {
@@ -140,6 +335,8 @@ export function NetscriptCorporation(
name: material.name,
qty: material.qty,
qlt: material.qlt,
prod: material.prd,
sell: material.sll,
};
},
getProduct: function (adivisionName: any, aproductName: any): NSProduct {
@@ -153,6 +350,8 @@ export function NetscriptCorporation(
cmp: product.cmp,
pCost: product.pCost,
sCost: product.sCost,
cityData: product.data,
developmentProgress: product.prog,
};
},
purchaseWarehouse: function (adivisionName: any, acityName: any): void {
@@ -217,6 +416,7 @@ export function NetscriptCorporation(
const cityName = helper.string("buyMaterial", "cityName", acityName);
const materialName = helper.string("buyMaterial", "materialName", amaterialName);
const amt = helper.number("buyMaterial", "amt", aamt);
if (amt < 0) throw new Error("Invalid value for amount field! Must be numeric and grater than 0");
const material = getMaterial(divisionName, cityName, materialName);
BuyMaterial(material, amt);
},
@@ -303,6 +503,61 @@ export function NetscriptCorporation(
};
const officeAPI: OfficeAPI = {
getHireAdVertCost: function (adivisionName: any): number {
checkAccess("getHireAdVertCost", 8);
const divisionName = helper.string("getHireAdVertCost", "divisionName", adivisionName);
const division = getDivision(divisionName);
const upgrade = IndustryUpgrades[1];
return upgrade[1] * Math.pow(upgrade[2], division.upgrades[1]);
},
getHireAdVertCount: function (adivisionName: any): number {
checkAccess("getHireAdVertCount", 8);
const divisionName = helper.string("getHireAdVertCount", "divisionName", adivisionName);
const division = getDivision(divisionName);
return division.upgrades[1]
},
getResearchCost: function (adivisionName: any, aresearchName: any): number {
checkAccess("getResearchCost", 8);
const divisionName = helper.string("getResearchCost", "divisionName", adivisionName);
const researchName = helper.string("getResearchCost", "researchName", aresearchName);
return getResearchCost(getDivision(divisionName), researchName);
},
hasResearched: function (adivisionName: any, aresearchName: any): boolean {
checkAccess("hasResearched", 8);
const divisionName = helper.string("hasResearched", "divisionName", adivisionName);
const researchName = helper.string("hasResearched", "researchName", aresearchName);
return hasResearched(getDivision(divisionName), researchName);
},
setAutoJobAssignment: function (adivisionName: any, acityName: any, ajob: any, aamount: any): Promise<boolean> {
checkAccess("setAutoJobAssignment", 8);
const divisionName = helper.string("setAutoJobAssignment", "divisionName", adivisionName);
const cityName = helper.string("setAutoJobAssignment", "cityName", acityName);
const amount = helper.number("setAutoJobAssignment", "amount", aamount);
const job = helper.string("setAutoJobAssignment", "job", ajob);
const office = getOffice(divisionName, cityName);
if (!Object.values(EmployeePositions).includes(job)) throw new Error(`'${job}' is not a valid job.`);
return netscriptDelay(1000, workerScript).then(function () {
if (workerScript.env.stopFlag) {
return Promise.reject(workerScript);
}
return Promise.resolve(office.setEmployeeToJob(job, amount));
});
},
getOfficeSizeUpgradeCost: function (adivisionName: any, acityName: any, asize: any): number {
checkAccess("getOfficeSizeUpgradeCost", 8);
const divisionName = helper.string("getOfficeSizeUpgradeCost", "divisionName", adivisionName);
const cityName = helper.string("getOfficeSizeUpgradeCost", "cityName", acityName);
const size = helper.number("getOfficeSizeUpgradeCost", "size", asize);
if (size < 0) throw new Error("Invalid value for size field! Must be numeric and grater than 0");
const office = getOffice(divisionName, cityName);
const initialPriceMult = Math.round(office.size / CorporationConstants.OfficeInitialSize);
const costMultiplier = 1.09;
let mult = 0;
for (let i = 0; i < size / CorporationConstants.OfficeInitialSize; ++i) {
mult += Math.pow(costMultiplier, initialPriceMult + i);
}
return CorporationConstants.OfficeInitialCost * mult;
},
assignJob: function (adivisionName: any, acityName: any, aemployeeName: any, ajob: any): Promise<void> {
checkAccess("assignJob", 8);
const divisionName = helper.string("assignJob", "divisionName", adivisionName);
@@ -326,6 +581,7 @@ export function NetscriptCorporation(
const divisionName = helper.string("upgradeOfficeSize", "divisionName", adivisionName);
const cityName = helper.string("upgradeOfficeSize", "cityName", acityName);
const size = helper.number("upgradeOfficeSize", "size", asize);
if (size < 0) throw new Error("Invalid value for size field! Must be numeric and grater than 0");
const office = getOffice(divisionName, cityName);
const corporation = getCorporation();
UpgradeOfficeSize(corporation, office, size);
@@ -335,6 +591,7 @@ export function NetscriptCorporation(
const divisionName = helper.string("throwParty", "divisionName", adivisionName);
const cityName = helper.string("throwParty", "cityName", acityName);
const costPerEmployee = helper.number("throwParty", "costPerEmployee", acostPerEmployee);
if (costPerEmployee < 0) throw new Error("Invalid value for Cost Per Employee field! Must be numeric and grater than 0");
const office = getOffice(divisionName, cityName);
const corporation = getCorporation();
return netscriptDelay(
@@ -429,6 +686,7 @@ export function NetscriptCorporation(
checkAccess("expandCity");
const divisionName = helper.string("expandCity", "divisionName", adivisionName);
const cityName = helper.string("expandCity", "cityName", acityName);
if (!CorporationConstants.Cities.includes(cityName)) throw new Error("Invalid city name");
const corporation = getCorporation();
const division = getDivision(divisionName);
NewCity(corporation, division, cityName);
@@ -452,6 +710,7 @@ export function NetscriptCorporation(
issueDividends: function (apercent: any): void {
checkAccess("issueDividends");
const percent = helper.number("issueDividends", "percent", apercent);
if (percent < 0 || percent > 100) throw new Error("Invalid value for percent field! Must be numeric, grater than 0, and less than 100");
const corporation = getCorporation();
IssueDividends(corporation, percent);
},
@@ -462,25 +721,7 @@ export function NetscriptCorporation(
checkAccess("getDivision");
const divisionName = helper.string("getDivision", "divisionName", adivisionName);
const division = getDivision(divisionName);
const cities: string[] = [];
for (const office of Object.values(division.offices)) {
if (office === 0) continue;
cities.push(office.loc);
}
return {
name: division.name,
type: division.type,
awareness: division.awareness,
popularity: division.popularity,
prodMult: division.prodMult,
research: division.sciResearch.qty,
lastCycleRevenue: division.lastCycleRevenue,
lastCycleExpenses: division.lastCycleExpenses,
thisCycleRevenue: division.thisCycleRevenue,
thisCycleExpenses: division.thisCycleExpenses,
upgrades: division.upgrades,
cities: cities,
};
return getSafeDivision(division);
},
getCorporation: function (): CorporationInfo {
checkAccess("getCorporation");
@@ -497,7 +738,61 @@ export function NetscriptCorporation(
issuedShares: corporation.issuedShares,
sharePrice: corporation.sharePrice,
state: corporation.state.getState(),
divisions: corporation.divisions.map((division): NSDivision => getSafeDivision(division)),
};
},
createCorporation: function (acorporationName: string, selfFund = true): boolean {
const corporationName = helper.string("createCorporation", "corporationName", acorporationName);
return createCorporation(corporationName, selfFund);
},
hasUnlockUpgrade: function (aupgradeName: any): boolean {
checkAccess("hasUnlockUpgrade");
const upgradeName = helper.string("hasUnlockUpgrade", "upgradeName", aupgradeName);
return hasUnlockUpgrade(upgradeName);
},
getUnlockUpgradeCost: function (aupgradeName: any): number {
checkAccess("getUnlockUpgradeCost");
const upgradeName = helper.string("getUnlockUpgradeCost", "upgradeName", aupgradeName);
return getUnlockUpgradeCost(upgradeName);
},
getUpgradeLevel: function (aupgradeName: any): number {
checkAccess("hasUnlockUpgrade");
const upgradeName = helper.string("getUpgradeLevel", "upgradeName", aupgradeName);
return getUpgradeLevel(upgradeName);
},
getUpgradeLevelCost: function (aupgradeName: any): number {
checkAccess("getUpgradeLevelCost");
const upgradeName = helper.string("getUpgradeLevelCost", "upgradeName", aupgradeName);
return getUpgradeLevelCost(upgradeName);
},
getExpandIndustryCost: function (aindustryName: any): number {
checkAccess("getExpandIndustryCost");
const industryName = helper.string("getExpandIndustryCost", "industryName", aindustryName);
return getExpandIndustryCost(industryName);
},
getExpandCityCost: function(): number {
checkAccess("getExpandCityCost");
return getExpandCityCost();
},
getInvestmentOffer: function(): InvestmentOffer {
checkAccess("getInvestmentOffer");
return getInvestmentOffer();
},
acceptInvestmentOffer: function(): boolean {
checkAccess("acceptInvestmentOffer");
return acceptInvestmentOffer();
},
goPublic: function(anumShares: any): boolean {
checkAccess("acceptInvestmentOffer");
const numShares = helper.number("goPublic", "numShares", anumShares);
return goPublic(numShares);
},
bribe: function(afactionName: string, aamountCash: any, aamountShares: any): boolean {
checkAccess("bribe");
const factionName = helper.string("bribe", "factionName", afactionName);
const amountCash = helper.number("bribe", "amountCash", aamountCash);
const amountShares = helper.number("bribe", "amountShares", aamountShares);
return bribe(factionName, amountCash, amountShares);
},
};
}