Initial commit

This commit is contained in:
Snarling
2022-08-08 13:43:41 -04:00
parent 3a2e676c9b
commit 208495e269
25 changed files with 1592 additions and 1614 deletions
+50 -54
View File
@@ -1,7 +1,5 @@
import { INetscriptHelper } from "./INetscriptHelper";
import { IPlayer } from "../PersonObjects/IPlayer";
import { WorkerScript } from "../Netscript/WorkerScript";
import { netscriptDelay } from "../NetscriptEvaluator";
import { staneksGift } from "../CotMG/Helper";
import { Fragments, FragmentById } from "../CotMG/Fragment";
@@ -18,111 +16,109 @@ import { applyAugmentation } from "../Augmentation/AugmentationHelpers";
import { FactionNames } from "../Faction/data/FactionNames";
import { joinFaction } from "../Faction/FactionHelpers";
import { Factions } from "../Faction/Factions";
import { helpers } from "../Netscript/NetscriptHelpers";
export function NetscriptStanek(
player: IPlayer,
workerScript: WorkerScript,
helper: INetscriptHelper,
): InternalAPI<IStanek> {
function checkStanekAPIAccess(func: string): void {
export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript): InternalAPI<IStanek> {
function checkStanekAPIAccess(ctx: NetscriptContext): void {
if (!player.hasAugmentation(AugmentationNames.StaneksGift1, true)) {
throw helper.makeRuntimeErrorMsg(func, "Stanek's Gift is not installed");
throw helpers.makeRuntimeErrorMsg(ctx, "Stanek's Gift is not installed");
}
}
return {
giftWidth: () =>
giftWidth: (ctx: NetscriptContext) =>
function (): number {
checkStanekAPIAccess("giftWidth");
checkStanekAPIAccess(ctx);
return staneksGift.width();
},
giftHeight: () =>
giftHeight: (ctx: NetscriptContext) =>
function (): number {
checkStanekAPIAccess("giftHeight");
checkStanekAPIAccess(ctx);
return staneksGift.height();
},
chargeFragment: (_ctx: NetscriptContext) =>
chargeFragment: (ctx: NetscriptContext) =>
function (_rootX: unknown, _rootY: unknown): Promise<void> {
//Get the fragment object using the given coordinates
const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("chargeFragment");
const rootX = helpers.number(ctx, "rootX", _rootX);
const rootY = helpers.number(ctx, "rootY", _rootY);
checkStanekAPIAccess(ctx);
const fragment = staneksGift.findFragment(rootX, rootY);
//Check whether the selected fragment can ge charged
if (!fragment) throw _ctx.makeRuntimeErrorMsg(`No fragment with root (${rootX}, ${rootY}).`);
if (!fragment) throw helpers.makeRuntimeErrorMsg(ctx, `No fragment with root (${rootX}, ${rootY}).`);
if (fragment.fragment().type == FragmentType.Booster) {
throw _ctx.makeRuntimeErrorMsg(
throw helpers.makeRuntimeErrorMsg(
ctx,
`The fragment with root (${rootX}, ${rootY}) is a Booster Fragment and thus cannot be charged.`,
);
}
//Charge the fragment
const time = staneksGift.inBonus() ? 200 : 1000;
return netscriptDelay("stanek.charge", time, workerScript).then(function () {
return helpers.netscriptDelay(ctx, time).then(function () {
staneksGift.charge(player, fragment, workerScript.scriptRef.threads);
_ctx.log(() => `Charged fragment with ${_ctx.workerScript.scriptRef.threads} threads.`);
ctx.log(() => `Charged fragment with ${ctx.workerScript.scriptRef.threads} threads.`);
return Promise.resolve();
});
},
fragmentDefinitions: (_ctx: NetscriptContext) =>
fragmentDefinitions: (ctx: NetscriptContext) =>
function (): IFragment[] {
checkStanekAPIAccess("fragmentDefinitions");
_ctx.log(() => `Returned ${Fragments.length} fragments`);
checkStanekAPIAccess(ctx);
ctx.log(() => `Returned ${Fragments.length} fragments`);
return Fragments.map((f) => f.copy());
},
activeFragments: (_ctx: NetscriptContext) =>
activeFragments: (ctx: NetscriptContext) =>
function (): IActiveFragment[] {
checkStanekAPIAccess("activeFragments");
_ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`);
checkStanekAPIAccess(ctx);
ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`);
return staneksGift.fragments.map((af) => {
return { ...af.copy(), ...af.fragment().copy() };
});
},
clearGift: (_ctx: NetscriptContext) =>
clearGift: (ctx: NetscriptContext) =>
function (): void {
checkStanekAPIAccess("clearGift");
_ctx.log(() => `Cleared Stanek's Gift.`);
checkStanekAPIAccess(ctx);
ctx.log(() => `Cleared Stanek's Gift.`);
staneksGift.clear();
},
canPlaceFragment: (_ctx: NetscriptContext) =>
canPlaceFragment: (ctx: NetscriptContext) =>
function (_rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY);
const rotation = _ctx.helper.number("rotation", _rotation);
const fragmentId = _ctx.helper.number("fragmentId", _fragmentId);
checkStanekAPIAccess("canPlaceFragment");
const rootX = helpers.number(ctx, "rootX", _rootX);
const rootY = helpers.number(ctx, "rootY", _rootY);
const rotation = helpers.number(ctx, "rotation", _rotation);
const fragmentId = helpers.number(ctx, "fragmentId", _fragmentId);
checkStanekAPIAccess(ctx);
const fragment = FragmentById(fragmentId);
if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`);
if (!fragment) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid fragment id: ${fragmentId}`);
const can = staneksGift.canPlace(rootX, rootY, rotation, fragment);
return can;
},
placeFragment: (_ctx: NetscriptContext) =>
placeFragment: (ctx: NetscriptContext) =>
function (_rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY);
const rotation = _ctx.helper.number("rotation", _rotation);
const fragmentId = _ctx.helper.number("fragmentId", _fragmentId);
checkStanekAPIAccess("placeFragment");
const rootX = helpers.number(ctx, "rootX", _rootX);
const rootY = helpers.number(ctx, "rootY", _rootY);
const rotation = helpers.number(ctx, "rotation", _rotation);
const fragmentId = helpers.number(ctx, "fragmentId", _fragmentId);
checkStanekAPIAccess(ctx);
const fragment = FragmentById(fragmentId);
if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`);
if (!fragment) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid fragment id: ${fragmentId}`);
return staneksGift.place(rootX, rootY, rotation, fragment);
},
getFragment: (_ctx: NetscriptContext) =>
getFragment: (ctx: NetscriptContext) =>
function (_rootX: unknown, _rootY: unknown): IActiveFragment | undefined {
const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("getFragment");
const rootX = helpers.number(ctx, "rootX", _rootX);
const rootY = helpers.number(ctx, "rootY", _rootY);
checkStanekAPIAccess(ctx);
const fragment = staneksGift.findFragment(rootX, rootY);
if (fragment !== undefined) return fragment.copy();
return undefined;
},
removeFragment: (_ctx: NetscriptContext) =>
removeFragment: (ctx: NetscriptContext) =>
function (_rootX: unknown, _rootY: unknown): boolean {
const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("removeFragment");
const rootX = helpers.number(ctx, "rootX", _rootX);
const rootY = helpers.number(ctx, "rootY", _rootY);
checkStanekAPIAccess(ctx);
return staneksGift.delete(rootX, rootY);
},
acceptGift: (_ctx: NetscriptContext) =>
acceptGift: (ctx: NetscriptContext) =>
function (): boolean {
//Check if the player is eligible to join the church
if (
@@ -138,7 +134,7 @@ export function NetscriptStanek(
!player.queuedAugmentations.some((a) => a.name === AugmentationNames.StaneksGift1)
) {
applyAugmentation({ name: AugmentationNames.StaneksGift1, level: 1 });
_ctx.log(
ctx.log(
() => `'${FactionNames.ChurchOfTheMachineGod}' joined and '${AugmentationNames.StaneksGift1}' installed.`,
);
}