bitburner-src/src/NetscriptFunctions/Grafting.ts

100 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-05-08 01:08:07 +02:00
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { StaticAugmentations } from "../Augmentation/StaticAugmentations";
2022-03-29 23:09:36 +02:00
import { hasAugmentationPrereqs } from "../Faction/FactionHelpers";
import { CityName } from "../Locations/data/CityNames";
2022-03-29 20:09:17 +02:00
import { GraftableAugmentation } from "../PersonObjects/Grafting/GraftableAugmentation";
import { getGraftingAvailableAugs, calculateGraftingTimeWithBonus } from "../PersonObjects/Grafting/GraftingHelpers";
2022-03-23 18:35:15 +01:00
import { IPlayer } from "../PersonObjects/IPlayer";
import { Grafting as IGrafting } from "../ScriptEditor/NetscriptDefinitions";
import { Router } from "../ui/GameRoot";
2022-07-10 07:37:36 +02:00
import { GraftingWork } from "../Work/GraftingWork";
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
export function NetscriptGrafting(player: IPlayer): InternalAPI<IGrafting> {
const checkGraftingAPIAccess = (ctx: NetscriptContext): void => {
2022-03-23 21:48:39 +01:00
if (!player.canAccessGrafting()) {
2022-05-08 01:08:07 +02:00
throw ctx.makeRuntimeErrorMsg(
2022-03-23 18:35:15 +01:00
"You do not currently have access to the Grafting API. This is either because you are not in BitNode 10 or because you do not have Source-File 10",
);
}
};
return {
2022-05-08 01:08:07 +02:00
getAugmentationGraftPrice:
(ctx: NetscriptContext) =>
(_augName: unknown): number => {
const augName = ctx.helper.string("augName", _augName);
checkGraftingAPIAccess(ctx);
if (!getGraftingAvailableAugs(player).includes(augName) || !StaticAugmentations.hasOwnProperty(augName)) {
throw ctx.makeRuntimeErrorMsg(`Invalid aug: ${augName}`);
}
const graftableAug = new GraftableAugmentation(StaticAugmentations[augName]);
return graftableAug.cost;
},
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
getAugmentationGraftTime:
(ctx: NetscriptContext) =>
(_augName: string): number => {
const augName = ctx.helper.string("augName", _augName);
checkGraftingAPIAccess(ctx);
if (!getGraftingAvailableAugs(player).includes(augName) || !StaticAugmentations.hasOwnProperty(augName)) {
throw ctx.makeRuntimeErrorMsg(`Invalid aug: ${augName}`);
}
const graftableAug = new GraftableAugmentation(StaticAugmentations[augName]);
return calculateGraftingTimeWithBonus(player, graftableAug);
},
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
getGraftableAugmentations: (ctx: NetscriptContext) => (): string[] => {
checkGraftingAPIAccess(ctx);
const graftableAugs = getGraftingAvailableAugs(player);
return graftableAugs;
},
2022-05-08 01:08:07 +02:00
graftAugmentation:
(ctx: NetscriptContext) =>
(_augName: string, _focus: unknown = true): boolean => {
const augName = ctx.helper.string("augName", _augName);
const focus = ctx.helper.boolean(_focus);
checkGraftingAPIAccess(ctx);
if (player.city !== CityName.NewTokyo) {
throw ctx.makeRuntimeErrorMsg("You must be in New Tokyo to begin grafting an Augmentation.");
}
if (!getGraftingAvailableAugs(player).includes(augName) || !StaticAugmentations.hasOwnProperty(augName)) {
ctx.log(() => `Invalid aug: ${augName}`);
return false;
}
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
const wasFocusing = player.focus;
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
const craftableAug = new GraftableAugmentation(StaticAugmentations[augName]);
if (player.money < craftableAug.cost) {
ctx.log(() => `You don't have enough money to craft ${augName}`);
return false;
}
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
if (!hasAugmentationPrereqs(craftableAug.augmentation)) {
ctx.log(() => `You don't have the pre-requisites for ${augName}`);
return false;
}
2022-03-29 23:09:36 +02:00
2022-07-10 07:37:36 +02:00
player.startNEWWork(
new GraftingWork({
singularity: true,
augmentation: augName,
player: player,
}),
);
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
if (focus) {
player.startFocusing();
Router.toWork();
} else if (wasFocusing) {
player.stopFocusing();
Router.toTerminal();
}
2022-03-23 18:35:15 +01:00
2022-05-08 01:08:07 +02:00
ctx.log(() => `Began grafting Augmentation ${augName}.`);
return true;
},
2022-03-23 18:35:15 +01:00
};
}