2023-06-12 06:34:20 +02:00
|
|
|
import type { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
|
2023-06-16 23:52:42 +02:00
|
|
|
|
|
|
|
import { Player } from "@player";
|
|
|
|
import { Grafting as IGrafting } from "@nsdefs";
|
|
|
|
import { AugmentationName, CityName } from "@enums";
|
|
|
|
import { Augmentations } from "../Augmentation/Augmentations";
|
2022-03-29 23:09:36 +02:00
|
|
|
import { hasAugmentationPrereqs } from "../Faction/FactionHelpers";
|
2022-03-29 20:09:17 +02:00
|
|
|
import { GraftableAugmentation } from "../PersonObjects/Grafting/GraftableAugmentation";
|
2022-04-22 04:48:18 +02:00
|
|
|
import { getGraftingAvailableAugs, calculateGraftingTimeWithBonus } from "../PersonObjects/Grafting/GraftingHelpers";
|
2022-03-23 18:35:15 +01:00
|
|
|
import { Router } from "../ui/GameRoot";
|
2022-12-04 09:14:06 +01:00
|
|
|
import { Page } from "../ui/Router";
|
2022-07-10 07:37:36 +02:00
|
|
|
import { GraftingWork } from "../Work/GraftingWork";
|
2022-08-08 19:43:41 +02:00
|
|
|
import { helpers } from "../Netscript/NetscriptHelpers";
|
2023-06-16 23:52:42 +02:00
|
|
|
import { getEnumHelper } from "../utils/EnumHelper";
|
2022-03-23 18:35:15 +01:00
|
|
|
|
2022-08-09 21:41:47 +02:00
|
|
|
export function NetscriptGrafting(): InternalAPI<IGrafting> {
|
2022-05-08 01:08:07 +02:00
|
|
|
const checkGraftingAPIAccess = (ctx: NetscriptContext): void => {
|
2023-06-12 06:34:20 +02:00
|
|
|
if (!Player.canAccessGrafting()) {
|
2022-08-08 19:43:41 +02:00
|
|
|
throw helpers.makeRuntimeErrorMsg(
|
|
|
|
ctx,
|
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",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-16 23:52:42 +02:00
|
|
|
const isValidGraftingAugName = (augName: AugmentationName) => getGraftingAvailableAugs().includes(augName);
|
2023-05-26 10:14:11 +02:00
|
|
|
|
2022-03-23 18:35:15 +01:00
|
|
|
return {
|
2022-10-12 14:49:27 +02:00
|
|
|
getAugmentationGraftPrice: (ctx) => (_augName) => {
|
2023-06-16 23:52:42 +02:00
|
|
|
const augName = getEnumHelper("AugmentationName").nsGetMember(ctx, _augName);
|
2022-10-12 14:49:27 +02:00
|
|
|
checkGraftingAPIAccess(ctx);
|
2023-05-26 10:14:11 +02:00
|
|
|
if (!isValidGraftingAugName(augName)) {
|
2022-10-12 14:49:27 +02:00
|
|
|
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid aug: ${augName}`);
|
|
|
|
}
|
2023-06-16 23:52:42 +02:00
|
|
|
const graftableAug = new GraftableAugmentation(Augmentations[augName]);
|
2022-10-12 14:49:27 +02:00
|
|
|
return graftableAug.cost;
|
|
|
|
},
|
2022-03-23 18:35:15 +01:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
getAugmentationGraftTime: (ctx) => (_augName) => {
|
2023-06-16 23:52:42 +02:00
|
|
|
const augName = getEnumHelper("AugmentationName").nsGetMember(ctx, _augName);
|
2022-10-12 14:49:27 +02:00
|
|
|
checkGraftingAPIAccess(ctx);
|
2023-05-26 10:14:11 +02:00
|
|
|
if (!isValidGraftingAugName(augName)) {
|
2022-10-12 14:49:27 +02:00
|
|
|
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid aug: ${augName}`);
|
|
|
|
}
|
2023-06-16 23:52:42 +02:00
|
|
|
const graftableAug = new GraftableAugmentation(Augmentations[augName]);
|
2022-10-12 14:49:27 +02:00
|
|
|
return calculateGraftingTimeWithBonus(graftableAug);
|
|
|
|
},
|
2022-03-23 18:35:15 +01:00
|
|
|
|
2022-10-12 14:49:27 +02:00
|
|
|
getGraftableAugmentations: (ctx) => () => {
|
2022-05-08 01:08:07 +02:00
|
|
|
checkGraftingAPIAccess(ctx);
|
2022-09-06 15:07:12 +02:00
|
|
|
const graftableAugs = getGraftingAvailableAugs();
|
2022-04-13 16:43:27 +02:00
|
|
|
return graftableAugs;
|
|
|
|
},
|
|
|
|
|
2022-05-08 01:08:07 +02:00
|
|
|
graftAugmentation:
|
2022-10-12 14:49:27 +02:00
|
|
|
(ctx) =>
|
|
|
|
(_augName, _focus = true) => {
|
2023-06-16 23:52:42 +02:00
|
|
|
const augName = getEnumHelper("AugmentationName").nsGetMember(ctx, _augName);
|
2022-08-08 19:43:41 +02:00
|
|
|
const focus = !!_focus;
|
2022-05-08 01:08:07 +02:00
|
|
|
checkGraftingAPIAccess(ctx);
|
2023-06-12 06:34:20 +02:00
|
|
|
if (Player.city !== CityName.NewTokyo) {
|
2022-08-08 19:43:41 +02:00
|
|
|
throw helpers.makeRuntimeErrorMsg(ctx, "You must be in New Tokyo to begin grafting an Augmentation.");
|
2022-05-08 01:08:07 +02:00
|
|
|
}
|
2023-05-26 10:14:11 +02:00
|
|
|
if (!isValidGraftingAugName(augName)) {
|
2022-08-08 21:51:50 +02:00
|
|
|
helpers.log(ctx, () => `Invalid aug: ${augName}`);
|
2022-05-08 01:08:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-23 18:35:15 +01:00
|
|
|
|
2023-06-12 06:34:20 +02:00
|
|
|
const wasFocusing = Player.focus;
|
2022-03-23 18:35:15 +01:00
|
|
|
|
2023-06-16 23:52:42 +02:00
|
|
|
const craftableAug = new GraftableAugmentation(Augmentations[augName]);
|
2023-06-12 06:34:20 +02:00
|
|
|
if (Player.money < craftableAug.cost) {
|
2022-08-08 21:51:50 +02:00
|
|
|
helpers.log(ctx, () => `You don't have enough money to craft ${augName}`);
|
2022-05-08 01:08:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-23 18:35:15 +01:00
|
|
|
|
2022-05-08 01:08:07 +02:00
|
|
|
if (!hasAugmentationPrereqs(craftableAug.augmentation)) {
|
2022-08-08 21:51:50 +02:00
|
|
|
helpers.log(ctx, () => `You don't have the pre-requisites for ${augName}`);
|
2022-05-08 01:08:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-29 23:09:36 +02:00
|
|
|
|
2023-06-12 06:34:20 +02:00
|
|
|
Player.startWork(
|
2022-07-10 07:37:36 +02:00
|
|
|
new GraftingWork({
|
|
|
|
singularity: true,
|
|
|
|
augmentation: augName,
|
|
|
|
}),
|
|
|
|
);
|
2022-03-23 18:35:15 +01:00
|
|
|
|
2022-05-08 01:08:07 +02:00
|
|
|
if (focus) {
|
2023-06-12 06:34:20 +02:00
|
|
|
Player.startFocusing();
|
2022-12-04 09:14:06 +01:00
|
|
|
Router.toPage(Page.Work);
|
2022-05-08 01:08:07 +02:00
|
|
|
} else if (wasFocusing) {
|
2023-06-12 06:34:20 +02:00
|
|
|
Player.stopFocusing();
|
2022-12-04 09:14:06 +01:00
|
|
|
Router.toPage(Page.Terminal);
|
2022-05-08 01:08:07 +02:00
|
|
|
}
|
2022-03-23 18:35:15 +01:00
|
|
|
|
2022-08-08 21:51:50 +02:00
|
|
|
helpers.log(ctx, () => `Began grafting Augmentation ${augName}.`);
|
2022-05-08 01:08:07 +02:00
|
|
|
return true;
|
|
|
|
},
|
2022-03-23 18:35:15 +01:00
|
|
|
};
|
|
|
|
}
|