From 51e5d38c7bb9dd09f706b1f810e4b7fb46f6cb6f Mon Sep 17 00:00:00 2001 From: nickofolas <60761231+nickofolas@users.noreply.github.com> Date: Wed, 13 Apr 2022 09:43:27 -0500 Subject: [PATCH] Add grafting API function to get graftable augs --- src/Netscript/RamCostGenerator.ts | 1 + src/NetscriptFunctions/Grafting.ts | 15 +++++++++++---- src/PersonObjects/Grafting/GraftingHelpers.ts | 15 +++++++++++++++ .../Grafting/ui/GraftingRoot.tsx | 19 ++++--------------- src/ScriptEditor/NetscriptDefinitions.d.ts | 12 ++++++++++++ 5 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 src/PersonObjects/Grafting/GraftingHelpers.ts diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index 00e0a3832..d72fef4ac 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -299,6 +299,7 @@ const ui: IMap = { const grafting: IMap = { getAugmentationGraftPrice: 3.75, getAugmentationGraftTime: 3.75, + getGraftableAugmentations: 5, graftAugmentation: 7.5, }; diff --git a/src/NetscriptFunctions/Grafting.ts b/src/NetscriptFunctions/Grafting.ts index ce4947fa9..577336570 100644 --- a/src/NetscriptFunctions/Grafting.ts +++ b/src/NetscriptFunctions/Grafting.ts @@ -4,7 +4,7 @@ import { CityName } from "../Locations/data/CityNames"; import { getRamCost } from "../Netscript/RamCostGenerator"; import { WorkerScript } from "../Netscript/WorkerScript"; import { GraftableAugmentation } from "../PersonObjects/Grafting/GraftableAugmentation"; -import { getAvailableAugs } from "../PersonObjects/Grafting/ui/GraftingRoot"; +import { getGraftingAvailableAugs } from "../PersonObjects/Grafting/GraftingHelpers"; import { IPlayer } from "../PersonObjects/IPlayer"; import { Grafting as IGrafting } from "../ScriptEditor/NetscriptDefinitions"; import { Router } from "../ui/GameRoot"; @@ -28,7 +28,7 @@ export function NetscriptGrafting(player: IPlayer, workerScript: WorkerScript, h updateRam("getAugmentationGraftPrice"); const augName = helper.string("getAugmentationGraftPrice", "augName", _augName); checkGraftingAPIAccess("getAugmentationGraftPrice"); - if (!Augmentations.hasOwnProperty(augName)) { + if (!getGraftingAvailableAugs(player).includes(augName) || !Augmentations.hasOwnProperty(augName)) { throw helper.makeRuntimeErrorMsg("grafting.getAugmentationGraftPrice", `Invalid aug: ${augName}`); } const craftableAug = new GraftableAugmentation(Augmentations[augName]); @@ -39,13 +39,20 @@ export function NetscriptGrafting(player: IPlayer, workerScript: WorkerScript, h updateRam("getAugmentationGraftTime"); const augName = helper.string("getAugmentationGraftTime", "augName", _augName); checkGraftingAPIAccess("getAugmentationGraftTime"); - if (!Augmentations.hasOwnProperty(augName)) { + if (!getGraftingAvailableAugs(player).includes(augName) || !Augmentations.hasOwnProperty(augName)) { throw helper.makeRuntimeErrorMsg("grafting.getAugmentationGraftTime", `Invalid aug: ${augName}`); } const craftableAug = new GraftableAugmentation(Augmentations[augName]); return craftableAug.time; }, + getGraftableAugmentations: (): string[] => { + updateRam("getGraftableAugmentations"); + checkGraftingAPIAccess("getGraftableAugmentations"); + const graftableAugs = getGraftingAvailableAugs(player); + return graftableAugs; + }, + graftAugmentation: (_augName: string, _focus: unknown = true): boolean => { updateRam("graftAugmentation"); const augName = helper.string("graftAugmentation", "augName", _augName); @@ -57,7 +64,7 @@ export function NetscriptGrafting(player: IPlayer, workerScript: WorkerScript, h "You must be in New Tokyo to begin grafting an Augmentation.", ); } - if (!getAvailableAugs(player).includes(augName)) { + if (!getGraftingAvailableAugs(player).includes(augName) || !Augmentations.hasOwnProperty(augName)) { workerScript.log("grafting.graftAugmentation", () => `Invalid aug: ${augName}`); return false; } diff --git a/src/PersonObjects/Grafting/GraftingHelpers.ts b/src/PersonObjects/Grafting/GraftingHelpers.ts new file mode 100644 index 000000000..35d4d7946 --- /dev/null +++ b/src/PersonObjects/Grafting/GraftingHelpers.ts @@ -0,0 +1,15 @@ +import { Augmentations } from "../../Augmentation/Augmentations"; +import { AugmentationNames } from "../../Augmentation/data/AugmentationNames"; +import { IPlayer } from "../IPlayer"; + +export const getGraftingAvailableAugs = (player: IPlayer): string[] => { + const augs: string[] = []; + + for (const [augName, aug] of Object.entries(Augmentations)) { + if (augName === AugmentationNames.NeuroFluxGovernor || augName === AugmentationNames.TheRedPill || aug.isSpecial) + continue; + augs.push(augName); + } + + return augs.filter((augmentation: string) => !player.hasAugmentation(augmentation)); +}; diff --git a/src/PersonObjects/Grafting/ui/GraftingRoot.tsx b/src/PersonObjects/Grafting/ui/GraftingRoot.tsx index 1122f73fa..34e3a1a61 100644 --- a/src/PersonObjects/Grafting/ui/GraftingRoot.tsx +++ b/src/PersonObjects/Grafting/ui/GraftingRoot.tsx @@ -15,22 +15,11 @@ import { ConfirmationModal } from "../../../ui/React/ConfirmationModal"; import { Money } from "../../../ui/React/Money"; import { convertTimeMsToTimeElapsedString, formatNumber } from "../../../utils/StringHelperFunctions"; import { IPlayer } from "../../IPlayer"; +import { getGraftingAvailableAugs } from "../GraftingHelpers"; import { GraftableAugmentation } from "../GraftableAugmentation"; const GraftableAugmentations: IMap = {}; -export const getAvailableAugs = (player: IPlayer): string[] => { - const augs: string[] = []; - - for (const [augName, aug] of Object.entries(Augmentations)) { - if (augName === AugmentationNames.NeuroFluxGovernor || augName === AugmentationNames.TheRedPill || aug.isSpecial) - continue; - augs.push(augName); - } - - return augs.filter((augmentation: string) => !player.hasAugmentation(augmentation)); -}; - const canGraft = (player: IPlayer, aug: GraftableAugmentation): boolean => { if (player.money < aug.cost) { return false; @@ -71,7 +60,7 @@ export const GraftingRoot = (): React.ReactElement => { GraftableAugmentations[name] = graftableAug; } - const [selectedAug, setSelectedAug] = useState(getAvailableAugs(player)[0]); + const [selectedAug, setSelectedAug] = useState(getGraftingAvailableAugs(player)[0]); const [graftOpen, setGraftOpen] = useState(false); return ( @@ -92,10 +81,10 @@ export const GraftingRoot = (): React.ReactElement => { Graft Augmentations - {getAvailableAugs(player).length > 0 ? ( + {getGraftingAvailableAugs(player).length > 0 ? ( - {getAvailableAugs(player).map((k, i) => ( + {getGraftingAvailableAugs(player).map((k, i) => ( setSelectedAug(k)} selected={selectedAug === k}> {k} diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 569bb8478..dc3a214b7 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -3784,6 +3784,18 @@ export interface Grafting { */ getAugmentationGraftTime(augName: string): number; + /** + * Retrieves a list of Augmentations that can be grafted. + * @remarks + * RAM cost: 5 GB + * + * Note that this function returns a list of currently graftable Augmentations, + * based off of the Augmentations that you already own. + * + * @returns An array of graftable Augmentations. + */ + getGraftableAugmentations(): string[]; + /** * Begins grafting the named aug. You must be in New Tokyo to use this. * @remarks