NETSCRIPT: add ns.getResetInfo(#490)

* Move lastAugReset and lastNodeReset back to main API under getResetInfo, also included currentNode.
* The associated properties are deprecated on getPlayer()
This commit is contained in:
Snarling 2023-04-25 05:22:36 -04:00 committed by GitHub
parent 62adaf3006
commit 6024c81113
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 183 additions and 143 deletions

@ -86,6 +86,7 @@
| [Product](./bitburner.product.md) | Product in a warehouse |
| [RecentScript](./bitburner.recentscript.md) | |
| [ReputationFormulas](./bitburner.reputationformulas.md) | Reputation formulas |
| [ResetInfo](./bitburner.resetinfo.md) | Various info about resets |
| [RunningScript](./bitburner.runningscript.md) | |
| [RunOptions](./bitburner.runoptions.md) | |
| [Server](./bitburner.server.md) | A server. Not all servers have all of these properties - optional properties are missing on certain servers. |

@ -0,0 +1,31 @@
<!-- 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; [getResetInfo](./bitburner.ns.getresetinfo.md)
## NS.getResetInfo() method
Get information about resets.
**Signature:**
```typescript
getResetInfo(): ResetInfo;
```
**Returns:**
[ResetInfo](./bitburner.resetinfo.md)
## Remarks
RAM cost: 1 GB
## Example
```js
const resetInfo = ns.getResetInfo();
const lastAugReset = resetInfo.lastAugReset;
ns.tprint(`The last augmentation reset was: ${new Date(lastAugReset)}`);
ns.tprint(`It has been ${Date.now() - lastAugReset}ms since the last augmentation reset.`);
```

@ -97,6 +97,7 @@ export async function main(ns) {
| [getPurchasedServers()](./bitburner.ns.getpurchasedservers.md) | Returns an array with the hostnames of all of the servers you have purchased. |
| [getPurchasedServerUpgradeCost(hostname, ram)](./bitburner.ns.getpurchasedserverupgradecost.md) | Get cost of upgrading a purchased server to the given ram. |
| [getRecentScripts()](./bitburner.ns.getrecentscripts.md) | Get an array of recently killed scripts across all servers. |
| [getResetInfo()](./bitburner.ns.getresetinfo.md) | Get information about resets. |
| [getRunningScript(filename, hostname, args)](./bitburner.ns.getrunningscript.md) | Get general info about a running script. |
| [getScriptExpGain(script, host, args)](./bitburner.ns.getscriptexpgain.md) | Get the exp gain of a script. |
| [getScriptIncome(script, host, args)](./bitburner.ns.getscriptincome.md) | Get the income of a script. |
@ -138,7 +139,7 @@ export async function main(ns) {
| [ls(host, substring)](./bitburner.ns.ls.md) | List files on a server. |
| [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. |
| [nFormat(n, format)](./bitburner.ns.nformat.md) | Format a number using the numeral library. This function is deprecated and will be removed in 2.3. |
| [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 scripts logs. |

@ -28,8 +28,6 @@ void
RAM cost: 0 GB
NS2 exclusive.
Move the source file to the specified destination on the target server.
This command only works for scripts and text files (.txt). It cannot, however, be used to convert from script to text file, or vice versa.

@ -6,10 +6,10 @@
> Warning: This API is now obsolete.
>
> Use ns.formatNumber, formatRam, or formatPercent instead. Will be removed in 2.3.
> Use ns.formatNumber, formatRam, or formatPercent instead. Will be removed in 2.4.
>
Format a number using the numeral library. This function is deprecated and will be removed in 2.3.
Format a number using the numeral library. This function is deprecated and will be removed in 2.4.
**Signature:**

@ -1,11 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Player](./bitburner.player.md) &gt; [bitNodeN](./bitburner.player.bitnoden.md)
## Player.bitNodeN property
**Signature:**
```typescript
bitNodeN: number;
```

@ -16,7 +16,6 @@ interface Player extends Person
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [bitNodeN](./bitburner.player.bitnoden.md) | | number | |
| [entropy](./bitburner.player.entropy.md) | | number | |
| [factions](./bitburner.player.factions.md) | | string\[\] | |
| [jobs](./bitburner.player.jobs.md) | | Record&lt;string, string&gt; | |

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [ResetInfo](./bitburner.resetinfo.md) &gt; [currentNode](./bitburner.resetinfo.currentnode.md)
## ResetInfo.currentNode property
The current bitnode
**Signature:**
```typescript
currentNode: number;
```

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [ResetInfo](./bitburner.resetinfo.md) &gt; [lastAugReset](./bitburner.resetinfo.lastaugreset.md)
## ResetInfo.lastAugReset property
Numeric timestamp (from Date.now()) of last augmentation reset
**Signature:**
```typescript
lastAugReset: number;
```

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [ResetInfo](./bitburner.resetinfo.md) &gt; [lastNodeReset](./bitburner.resetinfo.lastnodereset.md)
## ResetInfo.lastNodeReset property
Numeric timestamp (from Date.now()) of last bitnode reset
**Signature:**
```typescript
lastNodeReset: number;
```

@ -0,0 +1,22 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [ResetInfo](./bitburner.resetinfo.md)
## ResetInfo interface
Various info about resets
**Signature:**
```typescript
interface ResetInfo
```
## Properties
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [currentNode](./bitburner.resetinfo.currentnode.md) | | number | The current bitnode |
| [lastAugReset](./bitburner.resetinfo.lastaugreset.md) | | number | Numeric timestamp (from Date.now()) of last augmentation reset |
| [lastNodeReset](./bitburner.resetinfo.lastnodereset.md) | | number | Numeric timestamp (from Date.now()) of last bitnode reset |

@ -1,23 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Singularity](./bitburner.singularity.md) &gt; [getLastAugReset](./bitburner.singularity.getlastaugreset.md)
## Singularity.getLastAugReset() method
Get the last timestamp from when you installed augmentations.
**Signature:**
```typescript
getLastAugReset(): number;
```
**Returns:**
number
The timestamp in milliseconds from your last augmentation install.
## Remarks
RAM cost: 0.5 GB \* 16/4/1

@ -1,23 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [Singularity](./bitburner.singularity.md) &gt; [getLastNodeReset](./bitburner.singularity.getlastnodereset.md)
## Singularity.getLastNodeReset() method
Get the last timestamp from when you finished a bitnode.
**Signature:**
```typescript
getLastNodeReset(): number;
```
**Returns:**
number
The timestamp in milliseconds from your last bitnode finish.
## Remarks
RAM cost: 0.5 GB \* 16/4/1

@ -50,8 +50,6 @@ This API requires Source-File 4 to use. The RAM cost of all these functions is m
| [getFactionFavor(faction)](./bitburner.singularity.getfactionfavor.md) | Get faction favor. |
| [getFactionFavorGain(faction)](./bitburner.singularity.getfactionfavorgain.md) | Get faction favor gain. |
| [getFactionRep(faction)](./bitburner.singularity.getfactionrep.md) | Get faction reputation. |
| [getLastAugReset()](./bitburner.singularity.getlastaugreset.md) | Get the last timestamp from when you installed augmentations. |
| [getLastNodeReset()](./bitburner.singularity.getlastnodereset.md) | Get the last timestamp from when you finished a bitnode. |
| [getOwnedAugmentations(purchased)](./bitburner.singularity.getownedaugmentations.md) | Get a list of owned augmentation. |
| [getOwnedSourceFiles()](./bitburner.singularity.getownedsourcefiles.md) | Get a list of acquired Source-Files. |
| [getUpgradeHomeCoresCost()](./bitburner.singularity.getupgradehomecorescost.md) | Get the price of upgrading home cores. |

@ -204,8 +204,6 @@ const singularity = {
b1tflum3: SF4Cost(16),
destroyW0r1dD43m0n: SF4Cost(32),
getCurrentWork: SF4Cost(0.5),
getLastAugReset: SF4Cost(0.5),
getLastNodeReset: SF4Cost(0.5),
} as const;
// Gang API
@ -535,6 +533,7 @@ export const RamCosts: RamCostTree<NSFull> = {
getPlayer: RamCostConstants.SingularityFn1 / 4,
getMoneySources: RamCostConstants.GetMoneySourcesCost,
mv: 0,
getResetInfo: 1,
tail: 0,
toast: 0,
moveTail: 0,

@ -76,7 +76,7 @@ import { Flags } from "./NetscriptFunctions/Flags";
import { calculateIntelligenceBonus } from "./PersonObjects/formulas/intelligence";
import { CalculateShareMult, StartSharing } from "./NetworkShare/Share";
import { recentScripts } from "./Netscript/RecentScripts";
import { InternalAPI, removedFunction, NSProxy, NetscriptContext } from "./Netscript/APIWrapper";
import { InternalAPI, removedFunction, NSProxy } from "./Netscript/APIWrapper";
import { INetscriptExtra } from "./NetscriptFunctions/Extra";
import { ScriptDeath } from "./Netscript/ScriptDeath";
import { getBitNodeMultipliers } from "./BitNode/BitNode";
@ -1012,7 +1012,6 @@ export const ns: InternalAPI<NSFull> = {
},
getServerBaseSecurityLevel: (ctx) => (_hostname) => {
const hostname = helpers.string(ctx, "hostname", _hostname);
helpers.log(ctx, () => `getServerBaseSecurityLevel is deprecated because it's not useful.`);
const server = helpers.getServer(ctx, hostname);
if (!(server instanceof Server)) {
helpers.log(ctx, () => "Cannot be executed on this server.");
@ -1568,8 +1567,12 @@ export const ns: InternalAPI<NSFull> = {
const multStart = helpers.number(ctx, "multStart", _multStart);
return formatPercent(n, fractionalDigits, multStart);
},
// Todo: Remove function in 2.3. Until then it just directly wraps numeral.
// 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);
@ -1580,6 +1583,7 @@ export const ns: InternalAPI<NSFull> = {
return convertTimeMsToTimeElapsedString(milliseconds, milliPrecision);
},
getTimeSinceLastAug: () => () => {
deprecationWarning("ns.getTimeSinceLastAug()", "Use ns.getResetInfo().lastAugReset instead.");
return Player.playtimeSinceLastAug;
},
alert: (ctx) => (_message) => {
@ -1670,38 +1674,40 @@ export const ns: InternalAPI<NSFull> = {
getFavorToDonate: () => () => {
return Math.floor(CONSTANTS.BaseFavorToDonate * BitNodeMultipliers.RepToDonateToFaction);
},
getPlayer: (ctx) => () => {
getPlayer: () => () => {
const data = {
// Person
hp: cloneDeep(Player.hp),
skills: cloneDeep(Player.skills),
exp: cloneDeep(Player.exp),
mults: cloneDeep(Player.mults),
city: Player.city,
// Player-specific
numPeopleKilled: Player.numPeopleKilled,
money: Player.money,
city: Player.city,
location: Player.location,
bitNodeN: Player.bitNodeN,
totalPlaytime: Player.totalPlaytime,
jobs: cloneDeep(Player.jobs),
factions: Player.factions.slice(),
entropy: Player.entropy,
};
createDeprecatedProperty(
ctx,
data,
"playtimeSinceLastAug",
Player.playtimeSinceLastAug,
"getPlayer.playtimeSinceLastAug",
"Use ns.getPlaytimeSinceLastAug or ns.singularity.getLastAugReset instead.",
);
createDeprecatedProperty(
ctx,
data,
"playtimeSinceLastBitnode",
Player.playtimeSinceLastBitnode,
"getPlayer.playtimeSinceLastBitnode",
"Use ns.singularity.getLastNodeReset instead.",
);
setDeprecatedProperties(data, {
playtimeSinceLastAug: {
identifier: "ns.getPlayer().playtimeSinceLastAug",
message: "Use ns.getResetInfo().lastAugReset instead. This is a static timestamp instead of an elapsed time.",
value: Player.playtimeSinceLastAug,
},
playtimeSinceLastBitnode: {
identifier: "ns.getPlayer().playtimeSinceLastBitnode",
message: "Use ns.getResetInfo().lastNodeReset instead. This is a static timestamp instead of an elapsed time.",
value: Player.playtimeSinceLastBitnode,
},
bitNodeN: {
identifier: "ns.getPlayer().bitNodeN",
message: "Use ns.getResetInto().currentNode instead",
value: Player.bitNodeN,
},
});
return data;
},
getMoneySources: () => () => ({
@ -1747,6 +1753,11 @@ export const ns: InternalAPI<NSFull> = {
}
helpers.log(ctx, () => `ERROR: Failed. Was unable to remove file ${sourcePath} from its original location.`);
},
getResetInfo: () => () => ({
lastAugReset: Player.lastAugReset,
lastNodeReset: Player.lastNodeReset,
currentNode: Player.bitNodeN,
}),
flags: Flags,
...NetscriptExtra(),
};
@ -1776,27 +1787,28 @@ function getFunctionNames(obj: object, prefix: string): string[] {
}
const deprecatedWarningsGiven = new Set();
function createDeprecatedProperty(
ctx: NetscriptContext,
function setDeprecatedProperties(
obj: object,
propName: string,
value: any,
identifier: string,
message: string,
properties: Record<string, { identifier: string; message: string; value: any }>,
) {
Object.defineProperty(obj, propName, {
set: (newVal: any) => (value = newVal),
for (const [name, info] of Object.entries(properties)) {
Object.defineProperty(obj, name, {
get: () => {
if (!deprecatedWarningsGiven.has(identifier)) {
deprecatedWarningsGiven.add(identifier);
Terminal.warn(`Deprecated property ${propName} accessed from ns.${ctx.functionPath} return value.`);
Terminal.warn(`This is no longer supported usage and will be removed in a later version.`);
Terminal.warn(message);
Terminal.info(`Note that this message can also appear if you iterate through the object's values.`);
Terminal.info(`This message will only be shown once per game session for each deprecated property accessed.`);
}
return value;
deprecationWarning(info.identifier, info.message);
return info.value;
},
set: (value: any) => (info.value = value),
enumerable: true,
});
}
}
function deprecationWarning(identifier: string, message: string) {
if (!deprecatedWarningsGiven.has(identifier)) {
deprecatedWarningsGiven.add(identifier);
Terminal.warn(`Accessed deprecated function or property: ${identifier}`);
Terminal.warn(`This is no longer supported usage and will be removed in a later version.`);
Terminal.warn(message);
Terminal.info(`This message can also appear for object properties when the object's values are iterated.`);
Terminal.info(`This message will only be shown once per game session for each deprecated item accessed.`);
}
}

@ -87,15 +87,16 @@ export function NetscriptFormulas(): InternalAPI<IFormulas> {
serverGrowth: 0,
}),
mockPlayer: () => (): IPlayer => ({
// Person
hp: { current: 0, max: 0 },
skills: { hacking: 0, strength: 0, defense: 0, dexterity: 0, agility: 0, charisma: 0, intelligence: 0 },
exp: { hacking: 0, strength: 0, defense: 0, dexterity: 0, agility: 0, charisma: 0, intelligence: 0 },
mults: defaultMultipliers(),
city: CityName.Sector12,
// Player-specific
numPeopleKilled: 0,
money: 0,
city: CityName.Sector12,
location: LocationName.TravelAgency,
bitNodeN: 0,
totalPlaytime: 0,
jobs: {},
factions: [],

@ -1254,14 +1254,6 @@ export function NetscriptSingularity(): InternalAPI<ISingularity> {
helpers.checkSingularityAccess(ctx);
return canGetBonus();
},
getLastAugReset: (ctx) => () => {
helpers.checkSingularityAccess(ctx);
return Player.lastAugReset;
},
getLastNodeReset: (ctx) => () => {
helpers.checkSingularityAccess(ctx);
return Player.lastNodeReset;
},
};
Object.assign(singularityAPI, {
getAugmentationCost: removedFunction(

@ -50,7 +50,6 @@ interface Player extends Person {
entropy: number;
jobs: Record<string, string>;
factions: string[];
bitNodeN: number;
totalPlaytime: number;
location: string;
}
@ -67,6 +66,17 @@ interface SleevePerson extends Person {
storedCycles: number;
}
/** Various info about resets
* @public */
interface ResetInfo {
/** Numeric timestamp (from Date.now()) of last augmentation reset */
lastAugReset: number;
/** Numeric timestamp (from Date.now()) of last bitnode reset */
lastNodeReset: number;
/** The current bitnode */
currentNode: number;
}
/** @public */
interface MoneySource {
bladeburner: number;
@ -2361,24 +2371,6 @@ export interface Singularity {
* @returns - An object representing the current work. Fields depend on the kind of work.
*/
getCurrentWork(): any | null;
/**
* Get the last timestamp from when you installed augmentations.
* @remarks
* RAM cost: 0.5 GB * 16/4/1
*
* @returns The timestamp in milliseconds from your last augmentation install.
*/
getLastAugReset(): number;
/**
* Get the last timestamp from when you finished a bitnode.
* @remarks
* RAM cost: 0.5 GB * 16/4/1
*
* @returns The timestamp in milliseconds from your last bitnode finish.
*/
getLastNodeReset(): number;
}
/**
@ -6573,8 +6565,8 @@ export interface NS {
formatPercent(n: number, fractionalDigits?: number, multStart?: number): string;
/**
* Format a number using the numeral library. This function is deprecated and will be removed in 2.3.
* @deprecated Use ns.formatNumber, formatRam, or formatPercent instead. Will be removed in 2.3.
* Format a number using the numeral library. This function is deprecated and will be removed in 2.4.
* @deprecated Use ns.formatNumber, formatRam, or formatPercent instead. Will be removed in 2.4.
* @remarks
* RAM cost: 0 GB
*
@ -6815,8 +6807,6 @@ export interface NS {
* @remarks
* RAM cost: 0 GB
*
* NS2 exclusive.
*
* Move the source file to the specified destination on the target server.
*
* This command only works for scripts and text files (.txt). It cannot, however, be used
@ -6830,6 +6820,20 @@ export interface NS {
*/
mv(host: string, source: string, destination: string): void;
/** Get information about resets.
* @remarks
* RAM cost: 1 GB
*
* @example
* ```js
* const resetInfo = ns.getResetInfo();
* const lastAugReset = resetInfo.lastAugReset;
* ns.tprint(`The last augmentation reset was: ${new Date(lastAugReset)}`);
* ns.tprint(`It has been ${Date.now() - lastAugReset}ms since the last augmentation reset.`);
* ```
* */
getResetInfo(): ResetInfo;
/**
* Parse command line flags.
* @remarks