From 2269f79b15bab4aa1a91171cb0136942df768c97 Mon Sep 17 00:00:00 2001 From: pigalot Date: Thu, 13 Jan 2022 11:42:23 +0000 Subject: [PATCH] Add investments to the api --- src/Corporation/data/Constants.ts | 14 +++++++ src/Corporation/ui/FindInvestorsModal.tsx | 35 ++++------------- src/NetscriptFunctions/Corporation.ts | 45 +++++++++++++++++++++- src/ScriptEditor/NetscriptDefinitions.d.ts | 25 ++++++++++++ 4 files changed, 91 insertions(+), 28 deletions(-) diff --git a/src/Corporation/data/Constants.ts b/src/Corporation/data/Constants.ts index 9139762c1..b20b03de7 100644 --- a/src/Corporation/data/Constants.ts +++ b/src/Corporation/data/Constants.ts @@ -25,6 +25,8 @@ export const CorporationConstants: { BaseMaxProducts: number; AllCorporationStates: string[]; AllMaterials: string[]; + FundingRoundShares: number[]; + FundingRoundMultiplier: number[]; } = { INITIALSHARES: 1e9, //Total number of shares you have at your company SHARESPERPRICEUPDATE: 1e6, //When selling large number of shares, price is dynamically updated for every batch of this amount @@ -71,4 +73,16 @@ export const CorporationConstants: { "AI Cores", "Real Estate", ], + FundingRoundShares: [ + 0.1, + 0.35, + 0.25, + 0.2 + ], + FundingRoundMultiplier: [ + 4, + 3, + 3, + 2.5 + ], }; diff --git a/src/Corporation/ui/FindInvestorsModal.tsx b/src/Corporation/ui/FindInvestorsModal.tsx index 62bf50831..e24b1520b 100644 --- a/src/Corporation/ui/FindInvestorsModal.tsx +++ b/src/Corporation/ui/FindInvestorsModal.tsx @@ -15,37 +15,18 @@ interface IProps { // Create a popup that lets the player manage exports export function FindInvestorsModal(props: IProps): React.ReactElement { - const corp = useCorporation(); - const val = corp.determineValuation(); - let percShares = 0; - let roundMultiplier = 4; - switch (corp.fundingRound) { - case 0: //Seed - percShares = 0.1; - roundMultiplier = 4; - break; - case 1: //Series A - percShares = 0.35; - roundMultiplier = 3; - break; - case 2: //Series B - percShares = 0.25; - roundMultiplier = 3; - break; - case 3: //Series C - percShares = 0.2; - roundMultiplier = 2.5; - break; - default: - return <>; - } + const corporation = useCorporation(); + const val = corporation.determineValuation(); + if (corporation.fundingRound >= CorporationConstants.FundingRoundShares.length || corporation.fundingRound >= CorporationConstants.FundingRoundMultiplier.length) return <>; + const percShares = CorporationConstants.FundingRoundShares[corporation.fundingRound]; + const roundMultiplier = CorporationConstants.FundingRoundMultiplier[corporation.fundingRound]; const funding = val * percShares * roundMultiplier; const investShares = Math.floor(CorporationConstants.INITIALSHARES * percShares); function findInvestors(): void { - corp.fundingRound++; - corp.addFunds(funding); - corp.numShares -= investShares; + corporation.fundingRound++; + corporation.addFunds(funding); + corporation.numShares -= investShares; props.rerender(); props.onClose(); } diff --git a/src/NetscriptFunctions/Corporation.ts b/src/NetscriptFunctions/Corporation.ts index 0e47455b0..469e1157d 100644 --- a/src/NetscriptFunctions/Corporation.ts +++ b/src/NetscriptFunctions/Corporation.ts @@ -21,6 +21,7 @@ import { Division as NSDivision, WarehouseAPI, OfficeAPI, + InvestmentOffer } from "../ScriptEditor/NetscriptDefinitions"; import { @@ -126,6 +127,40 @@ export function NetscriptCorporation( return CorporationConstants.OfficeInitialCost; } + function getInvestmentOffer(): InvestmentOffer { + const corporation = getCorporation(); + if (corporation.fundingRound >= CorporationConstants.FundingRoundShares.length || corporation.fundingRound >= CorporationConstants.FundingRoundMultiplier.length) + 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) 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 getCorporation(): ICorporation { const corporation = player.corporation; if (corporation === null) throw new Error("cannot be called without a corporation"); @@ -632,6 +667,14 @@ export function NetscriptCorporation( getExpandCityCost: function(): number { checkAccess("getExpandCityCost"); return getExpandCityCost(); - } + }, + getInvestmentOffer: function(): InvestmentOffer { + checkAccess("getInvestmentOffer"); + return getInvestmentOffer(); + }, + acceptInvestmentOffer: function(): boolean { + checkAccess("acceptInvestmentOffer"); + return acceptInvestmentOffer(); + }, }; } diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index bec7986cb..14b88ea97 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -6383,6 +6383,18 @@ export interface Corporation extends WarehouseAPI, OfficeAPI { * @returns cost */ getExpandCityCost(): number; + /** + * Get an offer for investment based on you companies current valuation + * @returns An offer of investment + */ + getInvestmentOffer(): InvestmentOffer; + /** + * Accept investment based on you companies current valuation + * @remarks + * Is based on current valuation and will not honer a specific Offer + * @returns An offer of investment + */ + acceptInvestmentOffer(): boolean; /** * Get corporation data * @returns Corporation data @@ -6597,6 +6609,19 @@ interface Division { cities: string[]; } +/** + * Corporation investment offer + * @public + */ +interface InvestmentOffer { + /** Amount of funds you will get from this investment */ + funds: number; + /** Amount of share you will give in exchange for this investment */ + shares: number; + /** Current round of funding (max 4) */ + round: number; +} + /** * Interface Theme * @internal