mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-10 09:43:54 +01:00
add wrapped helpers to ctx
add ramcheck into wrapper
This commit is contained in:
parent
ea2b444b87
commit
40f74e4a98
@ -19,11 +19,11 @@ export type WrappedNetscriptAPI = {
|
||||
}
|
||||
|
||||
export type NetscriptContext = {
|
||||
workerScript: WorkerScript;
|
||||
function: string;
|
||||
makeRuntimeErrorMsg: (message: string) => string;
|
||||
log: (message: () => string) => void;
|
||||
updateDynamicRam: () => void;
|
||||
workerScript: WorkerScript;
|
||||
function: string;
|
||||
helper: WrappedNetscriptHelpers;
|
||||
};
|
||||
|
||||
type NetscriptHelpers = {
|
||||
@ -38,21 +38,42 @@ type NetscriptHelpers = {
|
||||
getValidPort: (funcName: string, port: any) => IPort;
|
||||
}
|
||||
|
||||
type WrappedNetscriptHelpers = {
|
||||
updateDynamicRam: (ramCost: number) => void;
|
||||
makeRuntimeErrorMsg: (msg: string) => string;
|
||||
string: (argName: string, v: unknown) => string;
|
||||
number: (argName: string, v: unknown) => number;
|
||||
boolean: (v: unknown) => boolean;
|
||||
getServer: (hostname: string)=> BaseServer;
|
||||
checkSingularityAccess: () => void;
|
||||
hack: (hostname: any, manual: any, { threads: requestedThreads, stock }?: any) => Promise<number>;
|
||||
getValidPort: (port: any) => IPort;
|
||||
}
|
||||
|
||||
function wrapFunction<T>(helpers: NetscriptHelpers, wrappedAPI: any, workerScript: WorkerScript, func: (ctx: NetscriptContext, ...args: unknown[]) => T, ...tree: string[]): void {
|
||||
const functionName = tree.pop();
|
||||
if (typeof functionName !== 'string') {
|
||||
throw makeRuntimeRejectMsg(workerScript, 'Failure occured while wrapping netscript api');
|
||||
}
|
||||
const ctx = {
|
||||
workerScript,
|
||||
function: functionName,
|
||||
makeRuntimeErrorMsg: (message: string) => { return helpers.makeRuntimeErrorMsg(functionName, message); },
|
||||
log: (message: () => string) => { workerScript.log(functionName, message); },
|
||||
updateDynamicRam: () => {
|
||||
helpers.updateDynamicRam(functionName, getRamCost(Player, ...tree, functionName));
|
||||
workerScript,
|
||||
function: functionName,
|
||||
helper: {
|
||||
updateDynamicRam: (ramCost: number) => helpers.updateDynamicRam(functionName, ramCost),
|
||||
makeRuntimeErrorMsg: (msg: string) => helpers.makeRuntimeErrorMsg(functionName, msg),
|
||||
string: (argName: string, v: unknown) => helpers.string(functionName, argName, v),
|
||||
number: (argName: string, v: unknown) => helpers.number(functionName, argName, v),
|
||||
boolean: helpers.boolean,
|
||||
getServer: (hostname: string) => helpers.getServer(hostname, functionName),
|
||||
checkSingularityAccess: () => helpers.checkSingularityAccess(functionName),
|
||||
hack: helpers.hack,
|
||||
getValidPort: (port: any) => helpers.getValidPort(functionName, port)
|
||||
}
|
||||
};
|
||||
function wrappedFunction(...args: unknown[]): T {
|
||||
helpers.updateDynamicRam(ctx.function, getRamCost(Player, ...tree, ctx.function));
|
||||
return func(ctx, ...args);
|
||||
}
|
||||
const parent = getNestedProperty(wrappedAPI, ...tree);
|
||||
|
@ -21,20 +21,17 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
|
||||
}
|
||||
|
||||
return {
|
||||
giftWidth: function (ctx: NetscriptContext): number {
|
||||
ctx.updateDynamicRam();
|
||||
giftWidth: function (): number {
|
||||
checkStanekAPIAccess("giftWidth");
|
||||
return staneksGift.width();
|
||||
},
|
||||
giftHeight: function (ctx: NetscriptContext): number {
|
||||
ctx.updateDynamicRam();
|
||||
giftHeight: function (): number {
|
||||
checkStanekAPIAccess("giftHeight");
|
||||
return staneksGift.height();
|
||||
},
|
||||
chargeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): Promise<void> {
|
||||
ctx.updateDynamicRam();
|
||||
const rootX = helper.number("stanek.chargeFragment", "rootX", _rootX);
|
||||
const rootY = helper.number("stanek.chargeFragment", "rootY", _rootY);
|
||||
const rootX = ctx.helper.number("rootX", _rootX);
|
||||
const rootY = ctx.helper.number("rootY", _rootY);
|
||||
checkStanekAPIAccess("chargeFragment");
|
||||
const fragment = staneksGift.findFragment(rootX, rootY);
|
||||
if (!fragment) throw ctx.makeRuntimeErrorMsg(`No fragment with root (${rootX}, ${rootY}).`);
|
||||
@ -46,13 +43,11 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
|
||||
});
|
||||
},
|
||||
fragmentDefinitions: function (ctx: NetscriptContext): IFragment[] {
|
||||
ctx.updateDynamicRam();
|
||||
checkStanekAPIAccess("fragmentDefinitions");
|
||||
ctx.log(() => `Returned ${Fragments.length} fragments`);
|
||||
return Fragments.map((f) => f.copy());
|
||||
},
|
||||
activeFragments: function (ctx: NetscriptContext): IActiveFragment[] {
|
||||
ctx.updateDynamicRam();
|
||||
checkStanekAPIAccess("activeFragments");
|
||||
ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`);
|
||||
return staneksGift.fragments.map((af) => {
|
||||
@ -60,17 +55,15 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
|
||||
});
|
||||
},
|
||||
clearGift: function (ctx: NetscriptContext): void {
|
||||
ctx.updateDynamicRam();
|
||||
checkStanekAPIAccess("clearGift");
|
||||
ctx.log(() => `Cleared Stanek's Gift.`);
|
||||
staneksGift.clear();
|
||||
},
|
||||
canPlaceFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
|
||||
ctx.updateDynamicRam();
|
||||
const rootX = helper.number("stanek.canPlaceFragment", "rootX", _rootX);
|
||||
const rootY = helper.number("stanek.canPlaceFragment", "rootY", _rootY);
|
||||
const rotation = helper.number("stanek.canPlaceFragment", "rotation", _rotation);
|
||||
const fragmentId = helper.number("stanek.canPlaceFragment", "fragmentId", _fragmentId);
|
||||
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 fragment = FragmentById(fragmentId);
|
||||
if (!fragment) throw ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`);
|
||||
@ -78,29 +71,26 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
|
||||
return can;
|
||||
},
|
||||
placeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean {
|
||||
ctx.updateDynamicRam();
|
||||
const rootX = helper.number("stanek.placeFragment", "rootX", _rootX);
|
||||
const rootY = helper.number("stanek.placeFragment", "rootY", _rootY);
|
||||
const rotation = helper.number("stanek.placeFragment", "rotation", _rotation);
|
||||
const fragmentId = helper.number("stanek.placeFragment", "fragmentId", _fragmentId);
|
||||
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 fragment = FragmentById(fragmentId);
|
||||
if (!fragment) throw ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`);
|
||||
return staneksGift.place(rootX, rootY, rotation, fragment);
|
||||
},
|
||||
getFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): IActiveFragment | undefined {
|
||||
ctx.updateDynamicRam();
|
||||
const rootX = helper.number("stanek.getFragment", "rootX", _rootX);
|
||||
const rootY = helper.number("stanek.getFragment", "rootY", _rootY);
|
||||
const rootX = ctx.helper.number("rootX", _rootX);
|
||||
const rootY = ctx.helper.number("rootY", _rootY);
|
||||
checkStanekAPIAccess("getFragment");
|
||||
const fragment = staneksGift.findFragment(rootX, rootY);
|
||||
if (fragment !== undefined) return fragment.copy();
|
||||
return undefined;
|
||||
},
|
||||
removeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): boolean {
|
||||
ctx.updateDynamicRam();
|
||||
const rootX = helper.number("stanek.removeFragment", "rootX", _rootX);
|
||||
const rootY = helper.number("stanek.removeFragment", "rootY", _rootY);
|
||||
const rootX = ctx.helper.number("rootX", _rootX);
|
||||
const rootY = ctx.helper.number("rootY", _rootY);
|
||||
checkStanekAPIAccess("removeFragment");
|
||||
return staneksGift.delete(rootX, rootY);
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user