UI: Added new locale-aware and configurable number formatting (#354)

This commit is contained in:
Snarling
2023-02-11 13:18:50 -05:00
committed by GitHub
parent 1f5546b721
commit b4074328ec
1231 changed files with 4233 additions and 11958 deletions
+8 -7
View File
@@ -7,7 +7,7 @@ type APIFn = (...args: any[]) => unknown;
/** Type for internal, unwrapped ctx function that produces an APIFunction */
type InternalFn<F extends APIFn> = (ctx: NetscriptContext) => ((...args: unknown[]) => ReturnType<F>) & F;
/** Type constraint for an API layer. They must all fit this "shape". */
type GenericAPI = { [key: string]: APIFn | GenericAPI };
type GenericAPI<T> = { [key in keyof T]: APIFn | GenericAPI<T[key]> };
// args, enums, and pid are excluded from the API for typing purposes via the definition of NSFull.
// They do in fact exist on the external API (but are absent on the internal API and ramcost tree)
@@ -21,7 +21,7 @@ export type NetscriptContext = {
functionPath: string;
};
class NSProxyHandler<API extends GenericAPI> {
class NSProxyHandler<API extends GenericAPI<API>> {
ns: API;
ws: WorkerScript;
tree: string[];
@@ -81,17 +81,18 @@ class NSProxyHandler<API extends GenericAPI> {
return ((this.memoed[key] as APIFn) = wrappedFunction);
}
if (typeof field === "object") {
return ((this.memoed[key] as GenericAPI) = NSProxy(this.ws, field as InternalAPI<GenericAPI>, [
...this.tree,
key,
]));
return ((this.memoed[key] as GenericAPI<API[keyof API]>) = NSProxy(
this.ws,
field as InternalAPI<GenericAPI<API[keyof API]>>,
[...this.tree, key],
));
}
console.warn(`Unexpected data while wrapping API.`, "tree:", this.tree, "key:", key, "field:", field);
throw new Error("Error while wrapping netscript API. See console.");
}
}
export function NSProxy<API extends GenericAPI>(
export function NSProxy<API extends GenericAPI<API>>(
ws: WorkerScript,
ns: InternalAPI<API>,
tree: string[],
+10 -10
View File
@@ -3,7 +3,7 @@ import { WorkerScript } from "./WorkerScript";
import { GetAllServers, GetServer } from "../Server/AllServers";
import { Player } from "@player";
import { ScriptDeath } from "./ScriptDeath";
import { numeralWrapper } from "../ui/numeralFormat";
import { formatExp, formatMoney, formatRam, formatThreads } from "../ui/formatNumber";
import { ScriptArg } from "./ScriptArg";
import { CityName } from "../Enums";
import { BasicHGWOptions, RunningScript as IRunningScript, Person as IPerson } from "@nsdefs";
@@ -354,8 +354,8 @@ function updateDynamicRam(ctx: NetscriptContext, ramCost: number): void {
This is probably because you somehow circumvented the static RAM calculation.
Threads: ${threads}
Dynamic RAM Usage: ${numeralWrapper.formatRAM(ws.dynamicRamUsage)} per thread
Static RAM Usage: ${numeralWrapper.formatRAM(ws.ramUsage)} per thread
Dynamic RAM Usage: ${formatRam(ws.dynamicRamUsage)} per thread
Static RAM Usage: ${formatRam(ws.ramUsage)} per thread
One of these could be the reason:
* Using eval() to get a reference to a ns function
@@ -448,7 +448,7 @@ function hack(
`Executing on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(
hackingTime * 1000,
true,
)} (t=${numeralWrapper.formatThreads(threads)})`,
)} (t=${formatThreads(threads)})`,
);
return helpers.netscriptDelay(ctx, hackingTime * 1000).then(function () {
@@ -495,9 +495,9 @@ function hack(
log(
ctx,
() =>
`Successfully hacked '${server.hostname}' for ${numeralWrapper.formatMoney(
moneyGained,
)} and ${numeralWrapper.formatExp(expGainedOnSuccess)} exp (t=${numeralWrapper.formatThreads(threads)})`,
`Successfully hacked '${server.hostname}' for ${formatMoney(moneyGained)} and ${formatExp(
expGainedOnSuccess,
)} exp (t=${formatThreads(threads)})`,
);
server.fortify(CONSTANTS.ServerFortifyAmount * Math.min(threads, maxThreadNeeded));
if (stock) {
@@ -514,9 +514,9 @@ function hack(
log(
ctx,
() =>
`Failed to hack '${server.hostname}'. Gained ${numeralWrapper.formatExp(
expGainedOnFailure,
)} exp (t=${numeralWrapper.formatThreads(threads)})`,
`Failed to hack '${server.hostname}'. Gained ${formatExp(expGainedOnFailure)} exp (t=${formatThreads(
threads,
)})`,
);
return 0;
}
+3
View File
@@ -518,6 +518,9 @@ export const RamCosts: RamCostTree<NSFull> = {
getTotalScriptExpGain: RamCostConstants.GetScript,
getScriptExpGain: RamCostConstants.GetScript,
getRunningScript: RamCostConstants.GetRunningScript,
formatNumber: 0,
formatRam: 0,
formatPercent: 0,
nFormat: 0,
tFormat: 0,
getTimeSinceLastAug: RamCostConstants.GetHackTime,