Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NETSCRIPT: Moved formatting functions to their own interface #1635

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions markdown/bitburner.format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md)

## Format interface

Format API

**Signature:**

```typescript
export interface Format
```

## Methods

| Method | Description |
| --- | --- |
| [number(n, fractionalDigits, suffixStart, isInteger)](./bitburner.format.number.md) | Format a number. |
| [percent(n, fractionalDigits, suffixStart)](./bitburner.format.percent.md) | Format a number as a percentage. |
| [ram(n, fractionalDigits)](./bitburner.format.ram.md) | Format a number as an amount of ram. |
| [time(milliseconds, milliPrecision)](./bitburner.format.time.md) | Format time to a readable string. |

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [formatNumber](./bitburner.ns.formatnumber.md)
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md) &gt; [number](./bitburner.format.number.md)

## NS.formatNumber() method
## Format.number() method

Format a number.

**Signature:**

```typescript
formatNumber(n: number, fractionalDigits?: number, suffixStart?: number, isInteger?: boolean): string;
number(n: number, fractionalDigits?: number, suffixStart?: number, isInteger?: boolean): string;
```

## Parameters
Expand All @@ -31,5 +31,5 @@ Formatted number.

RAM cost: 0 GB

Converts a number into a numeric string with the specified format options. This is the same function that the game itself uses to display numbers. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format ram or percentages, see [formatRam](./bitburner.ns.formatram.md) and [formatPercent](./bitburner.ns.formatpercent.md)
Converts a number into a numeric string with the specified format options. This is the same function that the game itself uses to display numbers. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format ram or percentages, see [format.ram](./bitburner.format.ram.md) and [format.percent](./bitburner.format.percent.md)

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [formatPercent](./bitburner.ns.formatpercent.md)
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md) &gt; [percent](./bitburner.format.percent.md)

## NS.formatPercent() method
## Format.percent() method

Format a number as a percentage.

**Signature:**

```typescript
formatPercent(n: number, fractionalDigits?: number, suffixStart?: number): string;
percent(n: number, fractionalDigits?: number, suffixStart?: number): string;
```

## Parameters
Expand All @@ -30,5 +30,5 @@ Formatted percentage.

RAM cost: 0 GB

Converts a number into a percentage string with the specified number of fractional digits. This is the same function that the game itself uses to display percentages. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format plain numbers or ram, see [formatNumber](./bitburner.ns.formatnumber.md) and [formatRam](./bitburner.ns.formatram.md)
Converts a number into a percentage string with the specified number of fractional digits. This is the same function that the game itself uses to display percentages. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format plain numbers or ram, see [format.number](./bitburner.format.number.md) and [format.ram](./bitburner.format.ram.md)

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [formatRam](./bitburner.ns.formatram.md)
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md) &gt; [ram](./bitburner.format.ram.md)

## NS.formatRam() method
## Format.ram() method

Format a number as an amount of ram.

**Signature:**

```typescript
formatRam(n: number, fractionalDigits?: number): string;
ram(n: number, fractionalDigits?: number): string;
```

## Parameters
Expand All @@ -29,5 +29,5 @@ Formatted ram amount.

RAM cost: 0 GB

Converts a number into a ram string with the specified number of fractional digits. This is the same function that the game itself uses to display ram. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format plain numbers or percentages, see [formatNumber](./bitburner.ns.formatnumber.md) and [formatPercent](./bitburner.ns.formatpercent.md)
Converts a number into a ram string with the specified number of fractional digits. This is the same function that the game itself uses to display ram. The format also depends on the Numeric Display settings (all options on the "Numeric Display" options page) To format plain numbers or percentages, see [format.number](./bitburner.format.number.md) and [format.percent](./bitburner.format.percent.md)

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [tFormat](./bitburner.ns.tformat.md)
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Format](./bitburner.format.md) &gt; [time](./bitburner.format.time.md)

## NS.tFormat() method
## Format.time() method

Format time to a readable string.

**Signature:**

```typescript
tFormat(milliseconds: number, milliPrecision?: boolean): string;
time(milliseconds: number, milliPrecision?: boolean): string;
```

## Parameters
Expand Down
1 change: 1 addition & 0 deletions markdown/bitburner.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
| [Export](./bitburner.export.md) | Export order for a material |
| [FactionWorkTask](./bitburner.factionworktask.md) | Faction Work |
| [FileRequirement](./bitburner.filerequirement.md) | Player must have a specific Literature or Message file on their home computer. |
| [Format](./bitburner.format.md) | Format API |
| [Formulas](./bitburner.formulas.md) | Formulas API |
| [Fragment](./bitburner.fragment.md) | |
| [GameInfo](./bitburner.gameinfo.md) | Game Information |
Expand Down
18 changes: 18 additions & 0 deletions markdown/bitburner.ns.format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [format](./bitburner.ns.format.md)

## NS.format property

Namespace for formatting functions.

**Signature:**

```typescript
readonly format: Format;
```

## Remarks

RAM cost: 0 GB

6 changes: 1 addition & 5 deletions markdown/bitburner.ns.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function main(ns) {
| [codingcontract](./bitburner.ns.codingcontract.md) | <code>readonly</code> | [CodingContract](./bitburner.codingcontract.md) | Namespace for codingcontract functions. |
| [corporation](./bitburner.ns.corporation.md) | <code>readonly</code> | [Corporation](./bitburner.corporation.md) | Namespace for corporation functions. Contains spoilers. |
| [enums](./bitburner.ns.enums.md) | | [NSEnums](./bitburner.nsenums.md) | |
| [format](./bitburner.ns.format.md) | <code>readonly</code> | [Format](./bitburner.format.md) | Namespace for formatting functions. |
| [formulas](./bitburner.ns.formulas.md) | <code>readonly</code> | [Formulas](./bitburner.formulas.md) | Namespace for formulas functions. |
| [gang](./bitburner.ns.gang.md) | <code>readonly</code> | [Gang](./bitburner.gang.md) | Namespace for gang functions. Contains spoilers. |
| [go](./bitburner.ns.go.md) | <code>readonly</code> | [Go](./bitburner.go.md) | Namespace for Go functions. |
Expand Down Expand Up @@ -69,9 +70,6 @@ export async function main(ns) {
| [exit()](./bitburner.ns.exit.md) | Terminates the current script immediately. |
| [fileExists(filename, host)](./bitburner.ns.fileexists.md) | Check if a file exists. |
| [flags(schema)](./bitburner.ns.flags.md) | Parse command line flags. |
| [formatNumber(n, fractionalDigits, suffixStart, isInteger)](./bitburner.ns.formatnumber.md) | Format a number. |
| [formatPercent(n, fractionalDigits, suffixStart)](./bitburner.ns.formatpercent.md) | Format a number as a percentage. |
| [formatRam(n, fractionalDigits)](./bitburner.ns.formatram.md) | Format a number as an amount of ram. |
| [ftpcrack(host)](./bitburner.ns.ftpcrack.md) | Runs FTPCrack.exe on a server. |
| [getBitNodeMultipliers(n, lvl)](./bitburner.ns.getbitnodemultipliers.md) | Get the current BitNode multipliers. |
| [getFavorToDonate()](./bitburner.ns.getfavortodonate.md) | Returns the amount of Faction favor required to be able to donate to a faction. |
Expand Down Expand Up @@ -134,7 +132,6 @@ export async function main(ns) {
| [moveTail(x, y, pid)](./bitburner.ns.movetail.md) | Move a tail window. |
| [mv(host, source, destination)](./bitburner.ns.mv.md) | Move a file on the target server. |
| [nextPortWrite(port)](./bitburner.ns.nextportwrite.md) | Listen for a port write. |
| [nFormat(n, format)](./bitburner.ns.nformat.md) | Format a number using the numeral library. This function is deprecated and will be removed in 2.4. |
| [nuke(host)](./bitburner.ns.nuke.md) | Runs NUKE.exe on a server. |
| [peek(portNumber)](./bitburner.ns.peek.md) | Get a copy of the data from a port without popping it. |
| [print(args)](./bitburner.ns.print.md) | Prints one or more values or variables to the script’s logs. |
Expand Down Expand Up @@ -163,7 +160,6 @@ export async function main(ns) {
| [sprintf(format, args)](./bitburner.ns.sprintf.md) | Format a string. |
| [sqlinject(host)](./bitburner.ns.sqlinject.md) | Runs SQLInject.exe on a server. |
| [tail(fn, host, args)](./bitburner.ns.tail.md) | Open the tail window of a script. |
| [tFormat(milliseconds, milliPrecision)](./bitburner.ns.tformat.md) | Format time to a readable string. |
| [toast(msg, variant, duration)](./bitburner.ns.toast.md) | Queue a toast (bottom-right notification). |
| [tprint(args)](./bitburner.ns.tprint.md) | Prints one or more values or variables to the Terminal. |
| [tprintf(format, values)](./bitburner.ns.tprintf.md) | Prints a raw value or a variable to the Terminal. |
Expand Down
40 changes: 0 additions & 40 deletions markdown/bitburner.ns.nformat.md

This file was deleted.

9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"material-ui-popup-state": "^1.9.3",
"monaco-vim": "^0.3.5",
"notistack": "^2.0.8",
"numeral": "^2.0.6",
"prop-types": "^15.8.1",
"react": "^17.0.2",
"react-beautiful-dnd": "^13.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
export const CONSTANTS = {
VersionString: "2.6.3dev",
isDevBranch: true,
VersionNumber: 40,
VersionNumber: 41,

/** Max level for any skill, assuming no multipliers. Determined by max numerical value in javascript for experience
* and the skill level formula in Player.js. Note that all this means it that when experience hits MAX_INT, then
Expand Down
13 changes: 8 additions & 5 deletions src/Netscript/RamCostGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ const singularity = {
getCurrentWork: SF4Cost(0.5),
} as const;

const format = {
number: 0,
ram: 0,
percent: 0,
time: 0,
} as const;

// Gang API
const gang = {
createGang: RamCostConstants.GangApiBase / 4,
Expand Down Expand Up @@ -473,6 +480,7 @@ export const RamCosts: RamCostTree<NSFull> = {
hacknet,
stock,
singularity,
format,
gang,
go,
bladeburner,
Expand Down Expand Up @@ -579,11 +587,6 @@ export const RamCosts: RamCostTree<NSFull> = {
getScriptExpGain: RamCostConstants.GetScript,
getRunningScript: RamCostConstants.GetRunningScript,
ramOverride: 0,
formatNumber: 0,
formatRam: 0,
formatPercent: 0,
nFormat: 0,
tFormat: 0,
getTimeSinceLastAug: RamCostConstants.GetHackTime,
prompt: 0,
wget: 0,
Expand Down
42 changes: 2 additions & 40 deletions src/NetscriptFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import {
formatRam,
formatSecurity,
formatThreads,
formatNumber,
} from "./ui/formatNumber";
import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions";
import { roundToTwo } from "./utils/helpers/roundToTwo";
Expand Down Expand Up @@ -97,7 +96,6 @@ import { ScriptDeath } from "./Netscript/ScriptDeath";
import { getBitNodeMultipliers } from "./BitNode/BitNode";
import { assert, arrayAssert, stringAssert, objectAssert } from "./utils/helpers/typeAssertion";
import { escapeRegExp } from "lodash";
import numeral from "numeral";
import { clearPort, peekPort, portHandle, readPort, tryWritePort, writePort, nextPortWrite } from "./NetscriptPort";
import { FilePath, resolveFilePath } from "./Paths/FilePath";
import { hasScriptExtension } from "./Paths/ScriptFilePath";
Expand All @@ -112,6 +110,7 @@ import { assertFunction } from "./Netscript/TypeAssertion";
import { Router } from "./ui/GameRoot";
import { Page } from "./ui/Router";
import { canAccessBitNodeFeature, validBitNodes } from "./BitNode/BitNodeUtils";
import { NetscriptFormat } from "./NetscriptFunctions/Format";

export const enums: NSEnums = {
CityName,
Expand All @@ -132,6 +131,7 @@ export type NSFull = Readonly<Omit<NS & INetscriptExtra, "pid" | "args" | "enums

export const ns: InternalAPI<NSFull> = {
singularity: NetscriptSingularity(),
format: NetscriptFormat(),
gang: NetscriptGang(),
go: NetscriptGo(),
bladeburner: NetscriptBladeburner(),
Expand Down Expand Up @@ -1594,44 +1594,6 @@ 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, _multStart = 1e6) => {
const n = helpers.number(ctx, "n", _n);
const fractionalDigits = helpers.number(ctx, "fractionalDigits", _fractionalDigits);
const multStart = helpers.number(ctx, "multStart", _multStart);
return formatPercent(n, fractionalDigits, multStart);
},
// Todo: Remove function for real though in 2.4. Until then it just directly wraps numeral.
nFormat: (ctx) => (_n, _format) => {
deprecationWarning(
"ns.nFormat",
"Use ns.formatNumber, formatRam, formatPercent, or js builtins like Intl.NumberFormat instead.",
);
const n = helpers.number(ctx, "n", _n);
const format = helpers.string(ctx, "format", _format);
return numeral(n).format(format);
},
tFormat: (ctx) => (_milliseconds, _milliPrecision) => {
const milliseconds = helpers.number(ctx, "milliseconds", _milliseconds);
const milliPrecision = !!_milliPrecision;
return convertTimeMsToTimeElapsedString(milliseconds, milliPrecision);
},
getTimeSinceLastAug: () => () => {
deprecationWarning(
"ns.getTimeSinceLastAug()",
Expand Down
Loading
Loading