This commit is contained in:
TheMas3212 2022-04-08 10:02:59 +10:00
parent 9b53896732
commit b2e7817a0b
No known key found for this signature in database
GPG Key ID: 62A173A4FDA683CA
2 changed files with 86 additions and 86 deletions

@ -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,88 +26,88 @@ export function NetscriptStanek(
} }
return { return {
giftWidth: (_ctx: NetscriptContext) => function (): number { giftWidth: (_ctx: NetscriptContext) =>
checkStanekAPIAccess("giftWidth"); function (): number {
return staneksGift.width(); checkStanekAPIAccess("giftWidth");
}, return staneksGift.width();
giftHeight: (_ctx: NetscriptContext) => function (): number { },
checkStanekAPIAccess("giftHeight"); giftHeight: (_ctx: NetscriptContext) =>
return staneksGift.height(); function (): number {
}, checkStanekAPIAccess("giftHeight");
chargeFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): Promise<void> { return staneksGift.height();
const rootX = _ctx.helper.number("rootX", _rootX); },
const rootY = _ctx.helper.number("rootY", _rootY); chargeFragment: (_ctx: NetscriptContext) =>
checkStanekAPIAccess("chargeFragment"); function (_rootX: unknown, _rootY: unknown): Promise<void> {
const fragment = staneksGift.findFragment(rootX, rootY); const rootX = _ctx.helper.number("rootX", _rootX);
if (!fragment) throw _ctx.makeRuntimeErrorMsg(`No fragment with root (${rootX}, ${rootY}).`); const rootY = _ctx.helper.number("rootY", _rootY);
const time = staneksGift.inBonus() ? 200 : 1000; checkStanekAPIAccess("chargeFragment");
return netscriptDelay(time, workerScript).then(function () { const fragment = staneksGift.findFragment(rootX, rootY);
const charge = staneksGift.charge(player, fragment, workerScript.scriptRef.threads); if (!fragment) throw _ctx.makeRuntimeErrorMsg(`No fragment with root (${rootX}, ${rootY}).`);
_ctx.log(() => `Charged fragment for ${charge} charge.`); const time = staneksGift.inBonus() ? 200 : 1000;
return Promise.resolve(); return netscriptDelay(time, workerScript).then(function () {
}); const charge = staneksGift.charge(player, fragment, workerScript.scriptRef.threads);
}, _ctx.log(() => `Charged fragment for ${charge} charge.`);
fragmentDefinitions: (_ctx: NetscriptContext) => function (): IFragment[] { return Promise.resolve();
checkStanekAPIAccess("fragmentDefinitions"); });
_ctx.log(() => `Returned ${Fragments.length} fragments`); },
return Fragments.map((f) => f.copy()); fragmentDefinitions: (_ctx: NetscriptContext) =>
}, function (): IFragment[] {
activeFragments: (_ctx: NetscriptContext) => function (): IActiveFragment[] { checkStanekAPIAccess("fragmentDefinitions");
checkStanekAPIAccess("activeFragments"); _ctx.log(() => `Returned ${Fragments.length} fragments`);
_ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`); return Fragments.map((f) => f.copy());
return staneksGift.fragments.map((af) => { },
return { ...af.copy(), ...af.fragment().copy() }; activeFragments: (_ctx: NetscriptContext) =>
}); function (): IActiveFragment[] {
}, checkStanekAPIAccess("activeFragments");
clearGift: (_ctx: NetscriptContext) => function (): void { _ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`);
checkStanekAPIAccess("clearGift"); return staneksGift.fragments.map((af) => {
_ctx.log(() => `Cleared Stanek's Gift.`); return { ...af.copy(), ...af.fragment().copy() };
staneksGift.clear(); });
}, },
canPlaceFragment: (_ctx: NetscriptContext) => function ( clearGift: (_ctx: NetscriptContext) =>
_rootX: unknown, function (): void {
_rootY: unknown, checkStanekAPIAccess("clearGift");
_rotation: unknown, _ctx.log(() => `Cleared Stanek's Gift.`);
_fragmentId: unknown, staneksGift.clear();
): boolean { },
const rootX = _ctx.helper.number("rootX", _rootX); canPlaceFragment: (_ctx: NetscriptContext) =>
const rootY = _ctx.helper.number("rootY", _rootY); function (_rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
const rotation = _ctx.helper.number("rotation", _rotation); const rootX = _ctx.helper.number("rootX", _rootX);
const fragmentId = _ctx.helper.number("fragmentId", _fragmentId); const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("canPlaceFragment"); const rotation = _ctx.helper.number("rotation", _rotation);
const fragment = FragmentById(fragmentId); const fragmentId = _ctx.helper.number("fragmentId", _fragmentId);
if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`); checkStanekAPIAccess("canPlaceFragment");
const can = staneksGift.canPlace(rootX, rootY, rotation, fragment); const fragment = FragmentById(fragmentId);
return can; if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`);
}, const can = staneksGift.canPlace(rootX, rootY, rotation, fragment);
placeFragment: (_ctx: NetscriptContext) => function ( return can;
_rootX: unknown, },
_rootY: unknown, placeFragment: (_ctx: NetscriptContext) =>
_rotation: unknown, function (_rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
_fragmentId: unknown, const rootX = _ctx.helper.number("rootX", _rootX);
): boolean { const rootY = _ctx.helper.number("rootY", _rootY);
const rootX = _ctx.helper.number("rootX", _rootX); const rotation = _ctx.helper.number("rotation", _rotation);
const rootY = _ctx.helper.number("rootY", _rootY); const fragmentId = _ctx.helper.number("fragmentId", _fragmentId);
const rotation = _ctx.helper.number("rotation", _rotation); checkStanekAPIAccess("placeFragment");
const fragmentId = _ctx.helper.number("fragmentId", _fragmentId); const fragment = FragmentById(fragmentId);
checkStanekAPIAccess("placeFragment"); if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`);
const fragment = FragmentById(fragmentId); return staneksGift.place(rootX, rootY, rotation, fragment);
if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`); },
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"); const fragment = staneksGift.findFragment(rootX, rootY);
const fragment = staneksGift.findFragment(rootX, rootY); if (fragment !== undefined) return fragment.copy();
if (fragment !== undefined) return fragment.copy(); return undefined;
return undefined; },
}, removeFragment: (_ctx: NetscriptContext) =>
removeFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): boolean { 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");
return staneksGift.delete(rootX, rootY); return staneksGift.delete(rootX, rootY);
}, },
}; };
} }