This commit is contained in:
TheMas3212
2022-04-08 10:02:59 +10:00
parent 9b53896732
commit b2e7817a0b
2 changed files with 86 additions and 86 deletions
src
Netscript
NetscriptFunctions

@ -11,7 +11,7 @@ type ExternalAPI = {
}; };
type InternalFunction<F extends (...args: unknown[]) => unknown> = ( type InternalFunction<F extends (...args: unknown[]) => unknown> = (
ctx: NetscriptContext ctx: NetscriptContext,
) => (...args: unknown[]) => ReturnType<F>; ) => (...args: unknown[]) => ReturnType<F>;
export type InternalAPI<API> = { export type InternalAPI<API> = {
[Property in keyof API]: API[Property] extends ExternalFunction [Property in keyof API]: API[Property] extends ExternalFunction
@ -65,7 +65,7 @@ function wrapFunction(
func: (_ctx: NetscriptContext) => (...args: unknown[]) => unknown, func: (_ctx: NetscriptContext) => (...args: unknown[]) => unknown,
...tree: string[] ...tree: string[]
): void { ): void {
const functionPath = tree.join('.'); const functionPath = tree.join(".");
const functionName = tree.pop(); const functionName = tree.pop();
if (typeof functionName !== "string") { if (typeof functionName !== "string") {
throw makeRuntimeRejectMsg(workerScript, "Failure occured while wrapping netscript api"); throw makeRuntimeRejectMsg(workerScript, "Failure occured while wrapping netscript api");
@ -99,7 +99,7 @@ function wrapFunction(
Object.defineProperty(parent, functionName, { Object.defineProperty(parent, functionName, {
value: wrappedFunction, value: wrappedFunction,
writable: true, writable: true,
enumerable: true enumerable: true,
}); });
} }

@ -26,15 +26,18 @@ export function NetscriptStanek(
} }
return { return {
giftWidth: (_ctx: NetscriptContext) => function (): number { giftWidth: (_ctx: NetscriptContext) =>
function (): number {
checkStanekAPIAccess("giftWidth"); checkStanekAPIAccess("giftWidth");
return staneksGift.width(); return staneksGift.width();
}, },
giftHeight: (_ctx: NetscriptContext) => function (): number { giftHeight: (_ctx: NetscriptContext) =>
function (): number {
checkStanekAPIAccess("giftHeight"); checkStanekAPIAccess("giftHeight");
return staneksGift.height(); return staneksGift.height();
}, },
chargeFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): Promise<void> { chargeFragment: (_ctx: NetscriptContext) =>
function (_rootX: unknown, _rootY: unknown): Promise<void> {
const rootX = _ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("chargeFragment"); checkStanekAPIAccess("chargeFragment");
@ -47,29 +50,28 @@ export function NetscriptStanek(
return Promise.resolve(); return Promise.resolve();
}); });
}, },
fragmentDefinitions: (_ctx: NetscriptContext) => function (): IFragment[] { fragmentDefinitions: (_ctx: NetscriptContext) =>
function (): IFragment[] {
checkStanekAPIAccess("fragmentDefinitions"); checkStanekAPIAccess("fragmentDefinitions");
_ctx.log(() => `Returned ${Fragments.length} fragments`); _ctx.log(() => `Returned ${Fragments.length} fragments`);
return Fragments.map((f) => f.copy()); return Fragments.map((f) => f.copy());
}, },
activeFragments: (_ctx: NetscriptContext) => function (): IActiveFragment[] { activeFragments: (_ctx: NetscriptContext) =>
function (): IActiveFragment[] {
checkStanekAPIAccess("activeFragments"); checkStanekAPIAccess("activeFragments");
_ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`); _ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`);
return staneksGift.fragments.map((af) => { return staneksGift.fragments.map((af) => {
return { ...af.copy(), ...af.fragment().copy() }; return { ...af.copy(), ...af.fragment().copy() };
}); });
}, },
clearGift: (_ctx: NetscriptContext) => function (): void { clearGift: (_ctx: NetscriptContext) =>
function (): void {
checkStanekAPIAccess("clearGift"); checkStanekAPIAccess("clearGift");
_ctx.log(() => `Cleared Stanek's Gift.`); _ctx.log(() => `Cleared Stanek's Gift.`);
staneksGift.clear(); staneksGift.clear();
}, },
canPlaceFragment: (_ctx: NetscriptContext) => function ( canPlaceFragment: (_ctx: NetscriptContext) =>
_rootX: unknown, function (_rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
_rootY: unknown,
_rotation: unknown,
_fragmentId: unknown,
): boolean {
const rootX = _ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
const rotation = _ctx.helper.number("rotation", _rotation); const rotation = _ctx.helper.number("rotation", _rotation);
@ -80,12 +82,8 @@ export function NetscriptStanek(
const can = staneksGift.canPlace(rootX, rootY, rotation, fragment); const can = staneksGift.canPlace(rootX, rootY, rotation, fragment);
return can; return can;
}, },
placeFragment: (_ctx: NetscriptContext) => function ( placeFragment: (_ctx: NetscriptContext) =>
_rootX: unknown, function (_rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
_rootY: unknown,
_rotation: unknown,
_fragmentId: unknown,
): boolean {
const rootX = _ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
const rotation = _ctx.helper.number("rotation", _rotation); const rotation = _ctx.helper.number("rotation", _rotation);
@ -95,7 +93,8 @@ export function NetscriptStanek(
if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`); if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`);
return staneksGift.place(rootX, rootY, rotation, fragment); return staneksGift.place(rootX, rootY, rotation, fragment);
}, },
getFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): IActiveFragment | undefined { getFragment: (_ctx: NetscriptContext) =>
function (_rootX: unknown, _rootY: unknown): IActiveFragment | undefined {
const rootX = _ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("getFragment"); checkStanekAPIAccess("getFragment");
@ -103,7 +102,8 @@ export function NetscriptStanek(
if (fragment !== undefined) return fragment.copy(); if (fragment !== undefined) return fragment.copy();
return undefined; return undefined;
}, },
removeFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): boolean { removeFragment: (_ctx: NetscriptContext) =>
function (_rootX: unknown, _rootY: unknown): boolean {
const rootX = _ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = _ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("removeFragment"); checkStanekAPIAccess("removeFragment");