mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-23 17:53:00 +02:00
UI: Added new locale-aware and configurable number formatting (#354)
This commit is contained in:
+55
-39
@@ -43,8 +43,17 @@ import { killWorkerScript } from "./Netscript/killWorkerScript";
|
||||
import { workerScripts } from "./Netscript/WorkerScripts";
|
||||
import { WorkerScript } from "./Netscript/WorkerScript";
|
||||
import { helpers, assertObjectType } from "./Netscript/NetscriptHelpers";
|
||||
import { numeralWrapper } from "./ui/numeralFormat";
|
||||
import { convertTimeMsToTimeElapsedString, formatNumber } from "./utils/StringHelperFunctions";
|
||||
import {
|
||||
formatExp,
|
||||
formatNumberNoSuffix,
|
||||
formatMoney,
|
||||
formatPercent,
|
||||
formatRam,
|
||||
formatSecurity,
|
||||
formatThreads,
|
||||
formatNumber,
|
||||
} from "./ui/formatNumber";
|
||||
import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions";
|
||||
import { LogBoxEvents, LogBoxCloserEvents, LogBoxPositionEvents, LogBoxSizeEvents } from "./ui/React/LogBoxManager";
|
||||
import { arrayToString } from "./utils/helpers/arrayToString";
|
||||
import { isString } from "./utils/helpers/isString";
|
||||
@@ -80,6 +89,7 @@ import { assert, arrayAssert, stringAssert, objectAssert } from "./utils/helpers
|
||||
import { CityName, JobName, CrimeType, GymType, LocationName, UniversityClassType } from "./Enums";
|
||||
import { cloneDeep } from "lodash";
|
||||
import { FactionWorkType } from "./Enums";
|
||||
import numeral from "numeral";
|
||||
|
||||
export const enums: NSEnums = {
|
||||
CityName,
|
||||
@@ -261,7 +271,7 @@ export const ns: InternalAPI<NSFull> = {
|
||||
`Executing on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(
|
||||
growTime * 1000,
|
||||
true,
|
||||
)} (t=${numeralWrapper.formatThreads(threads)}).`,
|
||||
)} (t=${formatThreads(threads)}).`,
|
||||
);
|
||||
return helpers.netscriptDelay(ctx, growTime * 1000).then(function () {
|
||||
const moneyBefore = server.moneyAvailable <= 0 ? 1 : server.moneyAvailable;
|
||||
@@ -273,10 +283,9 @@ export const ns: InternalAPI<NSFull> = {
|
||||
helpers.log(
|
||||
ctx,
|
||||
() =>
|
||||
`Available money on '${server.hostname}' grown by ${numeralWrapper.formatPercentage(
|
||||
logGrowPercent,
|
||||
6,
|
||||
)}. Gained ${numeralWrapper.formatExp(expGain)} hacking exp (t=${numeralWrapper.formatThreads(threads)}).`,
|
||||
`Available money on '${server.hostname}' grown by ${formatPercent(logGrowPercent, 6)}. Gained ${formatExp(
|
||||
expGain,
|
||||
)} hacking exp (t=${formatThreads(threads)}).`,
|
||||
);
|
||||
ctx.workerScript.scriptRef.onlineExpGained += expGain;
|
||||
Player.gainHackingExp(expGain);
|
||||
@@ -359,7 +368,7 @@ export const ns: InternalAPI<NSFull> = {
|
||||
`Executing on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(
|
||||
weakenTime * 1000,
|
||||
true,
|
||||
)} (t=${numeralWrapper.formatThreads(threads)})`,
|
||||
)} (t=${formatThreads(threads)})`,
|
||||
);
|
||||
return helpers.netscriptDelay(ctx, weakenTime * 1000).then(function () {
|
||||
const host = GetServer(ctx.workerScript.hostname);
|
||||
@@ -374,9 +383,9 @@ export const ns: InternalAPI<NSFull> = {
|
||||
helpers.log(
|
||||
ctx,
|
||||
() =>
|
||||
`'${server.hostname}' security level weakened to ${
|
||||
server.hackDifficulty
|
||||
}. Gained ${numeralWrapper.formatExp(expGain)} hacking exp (t=${numeralWrapper.formatThreads(threads)})`,
|
||||
`'${server.hostname}' security level weakened to ${server.hackDifficulty}. Gained ${formatExp(
|
||||
expGain,
|
||||
)} hacking exp (t=${formatThreads(threads)})`,
|
||||
);
|
||||
ctx.workerScript.scriptRef.onlineExpGained += expGain;
|
||||
Player.gainHackingExp(expGain);
|
||||
@@ -1070,10 +1079,10 @@ export const ns: InternalAPI<NSFull> = {
|
||||
}
|
||||
if (server.hostname == "home") {
|
||||
// Return player's money
|
||||
helpers.log(ctx, () => `returned player's money: ${numeralWrapper.formatMoney(Player.money)}`);
|
||||
helpers.log(ctx, () => `returned player's money: ${formatMoney(Player.money)}`);
|
||||
return Player.money;
|
||||
}
|
||||
helpers.log(ctx, () => `returned ${numeralWrapper.formatMoney(server.moneyAvailable)} for '${server.hostname}'`);
|
||||
helpers.log(ctx, () => `returned ${formatMoney(server.moneyAvailable)} for '${server.hostname}'`);
|
||||
return server.moneyAvailable;
|
||||
},
|
||||
getServerSecurityLevel: (ctx) => (_hostname) => {
|
||||
@@ -1086,10 +1095,7 @@ export const ns: InternalAPI<NSFull> = {
|
||||
if (helpers.failOnHacknetServer(ctx, server)) {
|
||||
return 1;
|
||||
}
|
||||
helpers.log(
|
||||
ctx,
|
||||
() => `returned ${numeralWrapper.formatServerSecurity(server.hackDifficulty)} for '${server.hostname}'`,
|
||||
);
|
||||
helpers.log(ctx, () => `returned ${formatSecurity(server.hackDifficulty)} for '${server.hostname}'`);
|
||||
return server.hackDifficulty;
|
||||
},
|
||||
getServerBaseSecurityLevel: (ctx) => (_hostname) => {
|
||||
@@ -1103,10 +1109,7 @@ export const ns: InternalAPI<NSFull> = {
|
||||
if (helpers.failOnHacknetServer(ctx, server)) {
|
||||
return 1;
|
||||
}
|
||||
helpers.log(
|
||||
ctx,
|
||||
() => `returned ${numeralWrapper.formatServerSecurity(server.baseDifficulty)} for '${server.hostname}'`,
|
||||
);
|
||||
helpers.log(ctx, () => `returned ${formatSecurity(server.baseDifficulty)} for '${server.hostname}'`);
|
||||
return server.baseDifficulty;
|
||||
},
|
||||
getServerMinSecurityLevel: (ctx) => (_hostname) => {
|
||||
@@ -1119,10 +1122,7 @@ export const ns: InternalAPI<NSFull> = {
|
||||
if (helpers.failOnHacknetServer(ctx, server)) {
|
||||
return 1;
|
||||
}
|
||||
helpers.log(
|
||||
ctx,
|
||||
() => `returned ${numeralWrapper.formatServerSecurity(server.minDifficulty)} for ${server.hostname}`,
|
||||
);
|
||||
helpers.log(ctx, () => `returned ${formatSecurity(server.minDifficulty)} for ${server.hostname}`);
|
||||
return server.minDifficulty;
|
||||
},
|
||||
getServerRequiredHackingLevel: (ctx) => (_hostname) => {
|
||||
@@ -1135,7 +1135,7 @@ export const ns: InternalAPI<NSFull> = {
|
||||
if (helpers.failOnHacknetServer(ctx, server)) {
|
||||
return 1;
|
||||
}
|
||||
helpers.log(ctx, () => `returned ${formatNumber(server.requiredHackingSkill, 0)} for '${server.hostname}'`);
|
||||
helpers.log(ctx, () => `returned ${formatNumberNoSuffix(server.requiredHackingSkill, 0)} for '${server.hostname}'`);
|
||||
return server.requiredHackingSkill;
|
||||
},
|
||||
getServerMaxMoney: (ctx) => (_hostname) => {
|
||||
@@ -1148,7 +1148,7 @@ export const ns: InternalAPI<NSFull> = {
|
||||
if (helpers.failOnHacknetServer(ctx, server)) {
|
||||
return 0;
|
||||
}
|
||||
helpers.log(ctx, () => `returned ${numeralWrapper.formatMoney(server.moneyMax)} for '${server.hostname}'`);
|
||||
helpers.log(ctx, () => `returned ${formatMoney(server.moneyMax)} for '${server.hostname}'`);
|
||||
return server.moneyMax;
|
||||
},
|
||||
getServerGrowth: (ctx) => (_hostname) => {
|
||||
@@ -1180,13 +1180,13 @@ export const ns: InternalAPI<NSFull> = {
|
||||
getServerMaxRam: (ctx) => (_hostname) => {
|
||||
const hostname = helpers.string(ctx, "hostname", _hostname);
|
||||
const server = helpers.getServer(ctx, hostname);
|
||||
helpers.log(ctx, () => `returned ${numeralWrapper.formatRAM(server.maxRam)}`);
|
||||
helpers.log(ctx, () => `returned ${formatRam(server.maxRam)}`);
|
||||
return server.maxRam;
|
||||
},
|
||||
getServerUsedRam: (ctx) => (_hostname) => {
|
||||
const hostname = helpers.string(ctx, "hostname", _hostname);
|
||||
const server = helpers.getServer(ctx, hostname);
|
||||
helpers.log(ctx, () => `returned ${numeralWrapper.formatRAM(server.ramUsed)}`);
|
||||
helpers.log(ctx, () => `returned ${formatRam(server.ramUsed)}`);
|
||||
return server.ramUsed;
|
||||
},
|
||||
serverExists: (ctx) => (_hostname) => {
|
||||
@@ -1273,7 +1273,7 @@ export const ns: InternalAPI<NSFull> = {
|
||||
}
|
||||
|
||||
if (Player.money < cost) {
|
||||
helpers.log(ctx, () => `Not enough money to purchase server. Need ${numeralWrapper.formatMoney(cost)}`);
|
||||
helpers.log(ctx, () => `Not enough money to purchase server. Need ${formatMoney(cost)}`);
|
||||
return "";
|
||||
}
|
||||
const newServ = safelyCreateUniqueServer({
|
||||
@@ -1292,10 +1292,7 @@ export const ns: InternalAPI<NSFull> = {
|
||||
homeComputer.serversOnNetwork.push(newServ.hostname);
|
||||
newServ.serversOnNetwork.push(homeComputer.hostname);
|
||||
Player.loseMoney(cost, "servers");
|
||||
helpers.log(
|
||||
ctx,
|
||||
() => `Purchased new server with hostname '${newServ.hostname}' for ${numeralWrapper.formatMoney(cost)}`,
|
||||
);
|
||||
helpers.log(ctx, () => `Purchased new server with hostname '${newServ.hostname}' for ${formatMoney(cost)}`);
|
||||
return newServ.hostname;
|
||||
},
|
||||
|
||||
@@ -1685,14 +1682,33 @@ export const ns: InternalAPI<NSFull> = {
|
||||
}
|
||||
return runningScript.onlineExpGained / runningScript.onlineRunningTime;
|
||||
},
|
||||
formatNumber:
|
||||
(ctx) =>
|
||||
(_n, _fractionalDigits = 3, _suffixStart = 1000, isInteger) => {
|
||||
const n = helpers.number(ctx, "n", _n);
|
||||
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
|
||||
const suffixStart = helpers.number(ctx, "suffixStart", _suffixStart);
|
||||
return formatNumber(n, fractionalDigits, suffixStart, !!isInteger);
|
||||
},
|
||||
formatRam:
|
||||
(ctx) =>
|
||||
(_n, _fractionalDigits = 2) => {
|
||||
const n = helpers.number(ctx, "n", _n);
|
||||
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
|
||||
return formatRam(n, fractionalDigits);
|
||||
},
|
||||
formatPercent:
|
||||
(ctx) =>
|
||||
(_n, _fractionalDigits = 2) => {
|
||||
const n = helpers.number(ctx, "n", _n);
|
||||
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
|
||||
return formatPercent(n, fractionalDigits);
|
||||
},
|
||||
// Todo: Remove function in 2.3. Until then it just directly wraps numeral.
|
||||
nFormat: (ctx) => (_n, _format) => {
|
||||
const n = helpers.number(ctx, "n", _n);
|
||||
const format = helpers.string(ctx, "format", _format);
|
||||
if (isNaN(n)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return numeralWrapper.format(n, format);
|
||||
return numeral(n).format(format);
|
||||
},
|
||||
tFormat: (ctx) => (_milliseconds, _milliPrecision) => {
|
||||
const milliseconds = helpers.number(ctx, "milliseconds", _milliseconds);
|
||||
|
||||
Reference in New Issue
Block a user