-
Notifications
You must be signed in to change notification settings - Fork 0
/
purchaser.js
151 lines (129 loc) · 4.2 KB
/
purchaser.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/** @param {NS} ns */
export async function main(ns) {
//for(let server_name of server_names) await inject(ns, server_name)
while (true) {
let server_names = await get_server_names(ns)
let player_money = ns.getServerMoneyAvailable("home")
let timeout = 500;
await server_buyer(ns, player_money, server_names)
//Hacknet Stuff
await hacknet_buyer(ns)
await ns.sleep(timeout)
}
}
async function server_buyer(ns, player_money, server_names) {
let num = Math.floor(server_names.length / 2)
if (num >= 12) num += 4
let ram = 2 ** (4 + num);
let cost = await ns.getPurchasedServerCost(ram);
//ns.tprint(cost)
if (player_money > cost) {
//ns.tprint('Enough to buy ram')
server_names = await get_server_names(ns)
let upgraded = false
for (let i in server_names) {
let up_cost = await ns.getPurchasedServerUpgradeCost(server_names[i], ram)
if ((up_cost < cost && up_cost > 0) ||
(server_names.length == 25 && player_money > up_cost)) {
await ns.upgradePurchasedServer(server_names[i], ram)
await inject(ns, server_names[i])
upgraded = true
}
}
if (!upgraded && server_names.length < 25) {
let server_name = 'gloat-' + server_names.length
await ns.purchaseServer(server_name, ram)
await inject(ns, server_name)
}
}
}
async function inject(ns, hostname) {
ns.print('Injecting & Executing...' + hostname)
let script_name = 'worm.js'
await ns.scp(script_name, hostname);
await execute(ns, hostname, script_name)
}
async function execute(ns, hostname, script_name) {
await ns.killall(hostname, true)
let max_ram = await ns.getServerMaxRam(hostname)
let used_ram = await ns.getServerUsedRam(hostname);
let free_ram = max_ram - used_ram;
let script_cost = await ns.getScriptRam(script_name)
let threads = Math.floor(free_ram / script_cost)
if (threads > 0) await ns.exec(script_name, hostname, threads, hostname)
}
async function get_server_names(ns) {
let hosts = await ns.scan()
let return_array = []
for (let host of hosts) {
if (host.includes('gloat-')) {
return_array.push(host)
}
}
return return_array
}
//Start of hacknet_buyer
async function hacknet_buyer(ns) {
let buy_max = 60000000;
let cheapestHost = []
let targetHost = {}
for (let i = 0; i < ns.hacknet.numNodes(); i++) {
let node = await get_costs(ns, i)
cheapestHost.push(node)
}
let result = await find_least(cheapestHost)
for (let host of cheapestHost) {
// hostComparison.push(host.cost)
if (host.cost === result && host.cost < buy_max) {
targetHost.name = host.name
targetHost.upgrade = host.upgrade
}
}
// let result = Math.min(...hostComparison)
await upgrade(ns, targetHost.upgrade, targetHost.name)
}
async function find_least(cheapestHost) {
let hostComparison = []
for (let host of cheapestHost) {
hostComparison.push(host.cost)
}
return Math.min(...hostComparison)
}
async function upgrade(ns, upgrade, hostname) {
if (hostname == undefined) {
ns.hacknet.purchaseNode(0)
return
}
// u_log(ns, upgrade, hostname)
let upgrade_obj = {
'ramCost': ns.hacknet.upgradeRam(hostname),
'levelCost': ns.hacknet.upgradeLevel(hostname),
'coreCost': ns.hacknet.upgradeCore(hostname),
'nodeCost': ns.hacknet.purchaseNode(hostname)
}
upgrade_obj[upgrade]
}
async function u_log(ns, upgrade, hostname) {
ns.print('Upgrading the following: ')
ns.print('Upgrade: ' + upgrade)
ns.print('Hostname: ' + hostname)
}
async function get_costs(ns, nodeNum) {
let nodeCosts = {}
let nodeData = ns.hacknet.getNodeStats(nodeNum)
let cheapestUpgrade = {}
nodeCosts.ramCost = await ns.hacknet.getRamUpgradeCost(nodeNum)
nodeCosts.levelCost = await ns.hacknet.getLevelUpgradeCost(nodeNum)
nodeCosts.coreCost = await ns.hacknet.getCoreUpgradeCost(nodeNum)
nodeCosts.nodeCost = await ns.hacknet.getPurchaseNodeCost(nodeNum)
var values = Object.values(nodeCosts);
cheapestUpgrade.cost = Math.min(...values);
cheapestUpgrade.name = nodeData.name.split("-")[2];
for (let upgrade in nodeCosts) {
if (nodeCosts[upgrade] === cheapestUpgrade.cost) {
cheapestUpgrade.upgrade = upgrade
break;
}
}
return cheapestUpgrade
}