forked from alainbryden/bitburner-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspend-hacknet-hashes.js
More file actions
58 lines (53 loc) · 3.08 KB
/
spend-hacknet-hashes.js
File metadata and controls
58 lines (53 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { disableLogs, formatDuration } from './helpers.js'
/** @typedef {import('./index.js').NS} NS*/
const argsSchema = [
['v', false], // Verbose
['verbose', false],
['l', false], // Turn all hashes into money
['liquidate', false],
['interval', 1000], // Rate at which the program runs and spends hashes
['spend-on', 'Sell for Money'],
['spend-on-server', undefined],
];
const purchaseOptions = ['Sell for Money', 'Sell for Corporation Funds', 'Exchange for Corporation Research', 'Generate Coding Contract', 'Improve Studying', 'Improve Gym Training'];
export function autocomplete(data, args) {
data.flags(argsSchema);
const lastFlag = args.length > 1 ? args[args.length - 2] : null;
if (lastFlag == "--spend-on") // Provide a couple auto-complete options to facilitate these arguments with spaces in them
return purchaseOptions.map(f => f.replaceAll(" ", "_")).sort().concat(purchaseOptions.map(f => `'${f}'`).sort());
return [];
}
/** @param {NS} ns **/
export async function main(ns) {
const options = ns.flags(argsSchema);
const verbose = options.v || options.verbose;
const liquidate = options.l || options.liquidate;
const interval = options.interval;
const toBuy = options['spend-on'].replaceAll("_", " ");
const spendOnServer = options['spend-on-server']?.replaceAll("_", " ") ?? undefined;
disableLogs(ns, ['sleep']);
ns.print(`Starting spend-hacknet-hashes.js to ensure no hashes go unspent. Will check in every ${formatDuration(interval)}`);
ns.print(liquidate ? `-l --liquidate mode active! Will spend all hashes on money as soon as possible.` :
`Only spending hashes every when near capacity to avoid wasting them.`);
while (true) {
let capacity = ns.hacknet.hashCapacity() || 0;
let startingHashes = ns.hacknet.numHashes() || 0;
let nodes = ns.hacknet.numNodes();
if (capacity == 0 && nodes > 0)
return ns.print('We have hacknet nodes, not hacknet servers, so spending hashes is not applicable.');
let globalProduction = Array.from({ length: nodes }, (_, i) => ns.hacknet.getNodeStats(i))
.reduce((total, node) => total + node.production, 0);
//ns.print(`Current hacknet production: ${globalProduction.toPrecision(3)}...`);
// Spend hashes before we lose them
let reserve = 10 + globalProduction * interval / 1000; // If we are this far from our capacity, start spending
let success = true;
while (success && ns.hacknet.numHashes() > (liquidate ? 4 : capacity - reserve))
success = ns.hacknet.spendHashes(toBuy, spendOnServer);
if (!success)
ns.print(`Weird, failed to spend hashes. (Have: ${ns.hacknet.numHashes()} Capacity: ${ns.hacknet.hashCapacity()}`);
if (verbose && ns.hacknet.numHashes() < startingHashes)
ns.print(`Spent ${(startingHashes - ns.hacknet.numHashes()).toFixed(0)} hashes` +
(liquidate ? '' : ` to avoid reaching capacity (${capacity})`) + ` at ${globalProduction.toPrecision(3)} hashes per second`);
await ns.sleep(interval);
}
}