This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathif.server.js
87 lines (77 loc) · 2.97 KB
/
if.server.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
/** @param {NS} ns **/
import { handleDB } from "./lib.db";
import { Cacheable } from "./lib.utils";
import { reservedHomeRam } from "./var.constants";
export default class BaseServer extends Cacheable {
constructor(ns, hostname) {
super();
this.ns = ns;
this._id = hostname;
}
listGetters(instance, properties=new Set()) {
let getters = Object.entries(
Object.getOwnPropertyDescriptors(
Reflect.getPrototypeOf(instance)
)).filter(e => typeof e[1]["get"] === 'function' && e[0] !== '__proto__').map(e => e[0])
getters.forEach(g => {
properties.add(g);
return this.listGetters(Object.getPrototypeOf(instance), properties)
})
return properties
}
get id() { return this._id }
get data() { return this.ns.getServer(this.id); }
get updated_at() { return new Date().valueOf(); }
get hostname() { return this.data.hostname; }
get admin() { return this.data.hasAdminRights; }
get level() { return this.data.requiredHackingSkill; }
get purchased() { return (this.data.purchasedByPlayer && this.data.hostname !== "home"); }
get connected() { return this.data.isConnectedTo; }
get backdoored() { return this.data.backdoorInstalled; }
get cores() { return this.data.cpuCores; }
get ram() { return {
used: this.data.ramUsed,
max: this.data.maxRam - (this.data.hostname === "home" ? reservedHomeRam : 0),
free: Math.max(0, this.data.maxRam - this.data.ramUsed - (this.data.hostname === "home" ? reservedHomeRam : 0)),
trueMax: this.data.maxRam
}}
get power() { return Math.max(0, Math.log2(this.data.maxRam)); }
get organization() { return this.data.organizationName; }
get isHome() { return (this.data.hostname === "home"); }
get ports() { return {
required: this.data.numOpenPortsRequired,
open: this.data.openPortCount,
ftp: this.data.ftpPortOpen,
http: this.data.httpPortOpen,
smtp: this.data.smtpPortOpen,
sql: this.data.sqlPortOpen,
ssh: this.data.sshPortOpen
}}
get security() { return {
level: this.data.hackDifficulty,
min: this.data.minDifficulty
}}
get money() { return {
available: this.data.moneyAvailable,
max: this.data.moneyMax,
growth: this.data.serverGrowth
}}
threadCount(scriptRam) {
let threads = 0;
threads = this.ram.free / scriptRam
return Math.floor(threads)
}
async updateCache(repeat=true, kv=new Map()) {
do {
const db = await handleDB();
let old = await db["get"]("servers", this.id) || {}
let getters = this.listGetters(this)
getters.forEach(g => {
old[g] = this[g];
})
kv.forEach((v,k) => old[k] = v)
await db["put"]("servers", old)
if (repeat) { await this.ns.asleep(Math.random()*10000) + 55000}
} while (repeat)
}
}