Skip to content

Commit

Permalink
Extended scan.js with the ability to display additional server info
Browse files Browse the repository at this point in the history
  • Loading branch information
alainbryden committed Oct 21, 2024
1 parent 7919137 commit b903489
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions scan.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { getConfiguration, formatRam, formatMoney, formatNumber } from './helpers.js'

const argsSchema = [
['hide-stats', false], // Set to false to hide detailed server statistics (RAM, max money, etc...)
];

export function autocomplete(data, _) {
data.flags(argsSchema);
return [];
}

/**
* @param {NS} ns
* @returns interactive server map
*/
export function main(ns) {
const options = getConfiguration(ns, argsSchema);
const showStats = !options['hide-stats'];
const factionServers = ["CSEC", "avmnite-02h", "I.I.I.I", "run4theh111z", "w0r1d_d43m0n", "fulcrumassets"],
css = ` <style id="scanCSS">
.serverscan {white-space:pre; color:#ccc; font:14px monospace; line-height: 16px; }
Expand All @@ -17,6 +30,7 @@ export function main(ns) {
.serverscan .backdoor {color:#6f3; font:12px monospace}
.serverscan .backdoor > a {cursor:pointer; text-decoration:underline;}
.serverscan .cct {color:#0ff;}
.serverscan .serverStats {color:#8AA;}
</style>`,
doc = eval("document"),
terminalInsert = html => doc.getElementById("terminal").insertAdjacentHTML('beforeend', `<li>${html}</li>`),
Expand All @@ -37,26 +51,37 @@ export function main(ns) {
hasAdminRights: ns.hasRootAccess(serverName),
purchasedByPlayer: serverName.includes('daemon') || serverName.includes('hacknet'),
backdoorInstalled: true // No way of knowing without ns.getServer
// TODO: Other things needed if showStats is true
} */
},
createServerEntry = serverName => {
let server = serverInfo(serverName),
requiredHackLevel = server.requiredHackingSkill,
rooted = server.hasAdminRights,
canHack = requiredHackLevel <= myHackLevel,
shouldBackdoor = !server?.backdoorInstalled && canHack && serverName != 'home' && rooted && !server.purchasedByPlayer,
shouldBackdoor = !server.backdoorInstalled && canHack && serverName != 'home' && rooted && !server.purchasedByPlayer,
contracts = ns.ls(serverName, ".cct")

return `<span id="${serverName}">`
+ `<a class="server${factionServers.includes(serverName) ? " faction" : ""}`
+ `${rooted ? " rooted" : ""}">${serverName}</a>`
+ (server.purchasedByPlayer ? '' : ` <span class="hack ${(canHack ? 'green' : 'red')}">(${requiredHackLevel})</span>`)
+ `${(shouldBackdoor ? ' <span class="backdoor">[<a>backdoor</a>]</span>' : '')}`
+ `${(shouldBackdoor ? ` <span class="backdoor${factionServers.includes(serverName) ? " faction" : ""}">[<a>backdoor</a>]</span>` : '')}`
+ ` ${contracts.map(c => `<span class="cct" title="${c}">@</span>`)}`
+ (showStats ? ` <span class="serverStats">... ` +
`Money: ` + ((server.moneyMax > 0 ? `${formatMoney(server.moneyAvailable, 4, 1).padStart(7)} / ` : '') + `${formatMoney(server.moneyMax, 4, 1).padStart(7)} `).padEnd(18) +
`Sec: ${formatNumber(server.hackDifficulty, 0, 0).padStart(2)}/${formatNumber(server.minDifficulty, 0, 0)} `.padEnd(11) +
`RAM: ${formatRam(server.maxRam).replace(' ', '').padStart(5)}` + (server.maxRam > 0 ? ` (${formatNumber(server.ramUsed * 100.0 / server.maxRam, 0, 1)}% used)` : '') +
`</span>` : '')
+ "</span>"
},
buildOutput = (parent = servers[0], prefix = ["\n"]) => {
let output = prefix.join("") + createServerEntry(parent)
if (showStats) { // Roughly right-align server stats if enabled
const expectedLength = parent.length + (2 * prefix.length) + (output.includes('backdoor') ? 10 : 0) +
(output.match(/@/g) || []).length + (((output.match(/\(\d+\)/g) || [{ length: 0 }])[0].length) + 1);
output = output.replace('...', '.'.repeat(Math.max(1, 60 - expectedLength)));
}
for (let i = 0; i < servers.length; i++) {
if (parentByIndex[i] != parent) continue
let newPrefix = prefix.slice()
Expand Down

0 comments on commit b903489

Please sign in to comment.