cleanup, download lit, log function

This commit is contained in:
2022-09-12 01:33:24 +02:00
parent 3529597fb0
commit 2963efd97b
13 changed files with 123 additions and 83 deletions
+20 -4
View File
@@ -32,10 +32,6 @@
"", "",
"export async function main(ns) {", "export async function main(ns) {",
" // ns.disableLog('ALL');", " // ns.disableLog('ALL');",
"",
" async function writeLog(obj) {",
" await ns.tryWritePort($1, obj + '\\n');",
" }",
" ", " ",
" $2", " $2",
"}" "}"
@@ -60,6 +56,26 @@
], ],
"description": "NS terminal print" "description": "NS terminal print"
}, },
"writelog": {
"scope": "",
"prefix": "writelog",
"body": [
"async function writeLog(type, obj) {",
" let timeNow = new Date();",
" let year = timeNow.getFullYear().toString().substring(2);",
" let month = (timeNow.getMonth() < 9 ? '0' : '') + (timeNow.getMonth() + 1);",
" let day = (timeNow.getDate() < 10 ? '0' : '') + timeNow.getDate();",
" let hour = (timeNow.getHours() < 10 ? '0' : '') + timeNow.getHours();",
" let minute = (timeNow.getMinutes() < 10 ? '0' : '') + timeNow.getMinutes();",
" let second = (timeNow.getSeconds() < 10 ? '0' : '') + timeNow.getSeconds();",
" let timestamp = `\\${year}\\${month}\\${day}_\\${hour}\\${minute}\\${second}`;",
" await ns.tryWritePort($1, `t\\${timestamp}_\\${type} = \\${obj};\\n`);",
" ",
" if (['ERROR', 'TERMINAL'].indexOf(type) != -1) ns.tprint(`\\${timestamp} \\${type} = \\${obj}`);",
"}"
],
"description": "writelog function definition"
},
"log": { "log": {
"scope": "", "scope": "",
"prefix": "log", "prefix": "log",
+30 -29
View File
@@ -3,7 +3,14 @@ export async function main(ns) {
ns.disableLog('ALL'); ns.disableLog('ALL');
async function writeLog(type, obj) { async function writeLog(type, obj) {
let timestamp = ''; let timeNow = new Date();
let year = timeNow.getFullYear().toString().substring(2);
let month = (timeNow.getMonth() < 9 ? '0' : '') + (timeNow.getMonth() + 1);
let day = (timeNow.getDate() < 10 ? '0' : '') + timeNow.getDate();
let hour = (timeNow.getHours() < 10 ? '0' : '') + timeNow.getHours();
let minute = (timeNow.getMinutes() < 10 ? '0' : '') + timeNow.getMinutes();
let second = (timeNow.getSeconds() < 10 ? '0' : '') + timeNow.getSeconds();
let timestamp = `${year}${month}${day}_${hour}${minute}${second}`;
await ns.tryWritePort(2, `t${timestamp}_${type} = ${obj};\n`); await ns.tryWritePort(2, `t${timestamp}_${type} = ${obj};\n`);
if (['ERROR', 'TERMINAL'].indexOf(type) != -1) ns.tprint(`${timestamp} ${type} = ${obj}`); if (['ERROR', 'TERMINAL'].indexOf(type) != -1) ns.tprint(`${timestamp} ${type} = ${obj}`);
@@ -180,49 +187,43 @@ export async function main(ns) {
* @async * @async
* @param {string} currentServer server to scan * @param {string} currentServer server to scan
*/ */
async function scanServer(currentServer, maxDepth = Number.MAX_SAFE_INTEGER, depth = 1, previousServer = currentServer) { async function scanServer(currentServer, previousServer = currentServer) {
if (depth <= maxDepth) { let availableServers = ns.scan(currentServer);
let availableServers = ns.scan(currentServer);
for (let i = 0; i < availableServers.length; ++i) { for (let i = 0; i < availableServers.length; ++i) {
let nextServer = availableServers[i]; let nextServer = availableServers[i];
if (nextServer != currentServer && nextServer != previousServer) { if (nextServer != currentServer && nextServer != previousServer) {
if (excludeServers.indexOf(nextServer) == -1) { if (excludeServers.indexOf(nextServer) == -1) {
let hackExitCode = await runHack(nextServer); let hackExitCode = await runHack(nextServer);
if (hackExitCode == 0) { if (hackExitCode == 0) {
await writeLog('HACK', `"Hack already running on ${nextServer}"`); await writeLog('HACK', `"Hack already running on ${nextServer}"`);
} else if (hackExitCode == -6) { } else if (hackExitCode == -6) {
await writeLog('HACK', `"No root access to ${nextServer}!"`); await writeLog('HACK', `"No root access to ${nextServer}!"`);
} else if (hackExitCode == -4) { } else if (hackExitCode == -4) {
await writeLog('HACK', `"Not enough RAM to run script on ${nextServer}!"`); await writeLog('HACK', `"Not enough RAM to run script on ${nextServer}!"`);
} else {
if (hackExitCode < 0) {
await writeLog('ERROR', `"Hack exec ERROR on '${nextServer}': ${hackExitCode}!"`);
} else { } else {
if (hackExitCode < 0) { await writeLog('HACK', `"Hack started on '${nextServer}'"`);
await writeLog('ERROR', `"Hack exec ERROR on '${nextServer}': ${hackExitCode}!"`);
} else {
await writeLog('HACK', `"Hack started on '${nextServer}'"`);
}
} }
} }
await scanServer(nextServer, maxDepth, depth + 1, currentServer);
} }
await scanServer(nextServer, currentServer);
} }
} }
await ns.sleep(10); await ns.sleep(10);
} }
let runDepth = ns.args.length ? ns.args[0] : 1;
ns.write('nuke-log.js', '', 'w'); ns.write('nuke-log.js', '', 'w');
if (!ns.scriptRunning('simple-hack.js', 'n00dles')) ns.write('hack-log.js', '', 'w'); if (!ns.scriptRunning('simple-hack.js', 'n00dles')) ns.write('hack-log.js', '', 'w');
ns.tprint(`TERMINAL = "Running scan depth: ${runDepth}"`); ns.tprint(`TERMINAL = "Running scan"`);
while (true) { while (true) {
await writeLog('INFO', `"Running scan depth: ${runDepth}"`); await writeLog('INFO', `"Running scan"`);
await scanServer('home',); await scanServer('home');
await ns.sleep(600000); await ns.sleep(60000);
} }
} }
-2
View File
@@ -15,6 +15,4 @@ export async function main(ns) {
ns.tprint(`Exec 'grow-helper.js' exit code: ${execExitCode}`); ns.tprint(`Exec 'grow-helper.js' exit code: ${execExitCode}`);
} }
} }
await ns.sleep(10);
} }
+26
View File
@@ -0,0 +1,26 @@
/** @param {import(".").NS } ns */
export async function main(ns) {
// ns.disableLog('ALL');
function scanServer(currentServer, previousServer = currentServer) {
// ns.tprint(currentServer);
let availableServers = ns.scan(currentServer);
for (let i = 0; i < availableServers.length; ++i) {
let nextServer = availableServers[i];
if (nextServer != currentServer && nextServer != previousServer) {
let litFiles = ns.ls(nextServer, 'lit');
ns.tprint(`Download from '${nextServer}': ${litFiles}`);
if (ns.args[0] && litFiles.length) {
ns.scp(litFiles, 'home', nextServer);
}
scanServer(nextServer, currentServer);
}
}
}
scanServer('home');
}
+2
View File
@@ -0,0 +1,2 @@
CyberSec
Netburners
+14 -19
View File
@@ -1,7 +1,7 @@
/** @param {import(".").NS } ns */ /** @param {import(".").NS } ns */
export async function main(ns) { export async function main(ns) {
ns.disableLog('ALL'); // ns.disableLog('ALL');
let excludeServers = ['home']; let excludeServers = ['home'];
for (let index = 0; index < 25; index++) { for (let index = 0; index < 25; index++) {
@@ -10,33 +10,28 @@ export async function main(ns) {
excludeServers.push('CSEC'); excludeServers.push('CSEC');
excludeServers.push('avmnite-02h'); excludeServers.push('avmnite-02h');
async function scanServer(currentServer, maxDepth = Number.MAX_SAFE_INTEGER, depth = 1, previousServer = currentServer) { async function scanServer(currentServer, previousServer = currentServer) {
if (depth <= maxDepth) { let availableServers = ns.scan(currentServer);
let availableServers = ns.scan(currentServer);
for (let i = 0; i < availableServers.length; ++i) { for (let i = 0; i < availableServers.length; ++i) {
let nextServer = availableServers[i]; let nextServer = availableServers[i];
if (nextServer != currentServer && nextServer != previousServer) { if (nextServer != currentServer && nextServer != previousServer) {
if (excludeServers.indexOf(nextServer) == -1 && ns.scriptRunning('simple-hack.js', nextServer)) { if (excludeServers.indexOf(nextServer) == -1 && ns.scriptRunning('simple-hack.js', nextServer)) {
let maxMoney = ns.getServerMaxMoney(nextServer); let maxMoney = ns.getServerMaxMoney(nextServer);
let currentMoney = ns.getServerMoneyAvailable(nextServer); let currentMoney = ns.getServerMoneyAvailable(nextServer);
if (currentMoney < 0.2 * maxMoney) { if (currentMoney < 0.2 * maxMoney) {
await ns.grow(nextServer); await ns.grow(nextServer);
}
} }
await scanServer(nextServer, maxDepth, depth + 1, currentServer);
} }
await scanServer(nextServer, maxDepth, depth + 1, currentServer);
} }
} }
await ns.sleep(10);
} }
let runDepth = ns.args.length ? ns.args[0] : 1;
while (true) { while (true) {
ns.print(`Running scan`);
ns.print(`Running scan depth: ${runDepth}`); await scanServer('home');
await scanServer('home', runDepth);
} }
} }
+2 -2
View File
@@ -9,7 +9,7 @@ export async function main(ns) {
let levelCap = 150; let levelCap = 150;
let ramCap = 64; let ramCap = 64;
let coreCap = 4; let coreCap = 4;
let nodeCap = 15; let nodeCap = 18;
ns.tprint(`Bot started {nodeCostThreshold: ${nodeCostThreshold}, upgradeCostThreshold: ${upgradeCostThreshold}, upgradeCount: ${upgradeCount}}`); ns.tprint(`Bot started {nodeCostThreshold: ${nodeCostThreshold}, upgradeCostThreshold: ${upgradeCostThreshold}, upgradeCount: ${upgradeCount}}`);
while (true) { while (true) {
@@ -62,6 +62,6 @@ export async function main(ns) {
if (portData != 'NULL PORT DATA') ns.write(logFile, portData, 'a'); if (portData != 'NULL PORT DATA') ns.write(logFile, portData, 'a');
} }
await ns.sleep(10); await ns.sleep(50);
} }
} }
+9 -19
View File
@@ -15,29 +15,19 @@ export async function main(ns) {
* @async * @async
* @param {string} currentServer server to scan * @param {string} currentServer server to scan
*/ */
async function scanServer(currentServer, maxDepth = Number.MAX_SAFE_INTEGER, depth = 1, previousServer = currentServer) { function scanServer(currentServer, previousServer = currentServer) {
if (depth <= maxDepth) { let availableServers = ns.scan(currentServer);
let availableServers = ns.scan(currentServer);
for (let i = 0; i < availableServers.length; ++i) { for (let i = 0; i < availableServers.length; ++i) {
let nextServer = availableServers[i]; let nextServer = availableServers[i];
if (nextServer != currentServer && nextServer != previousServer) { if (nextServer != currentServer && nextServer != previousServer) {
if (excludeServers.indexOf(nextServer) == -1) ns.scriptKill('simple-hack.js', nextServer); if (excludeServers.indexOf(nextServer) == -1) ns.scriptKill('simple-hack.js', nextServer);
await scanServer(nextServer, maxDepth, depth + 1, currentServer); scanServer(nextServer, currentServer);
}
} }
} }
await ns.sleep(10);
} }
ns.tprint(`Running scan`);
let runDepth = ns.args.length ? ns.args[0] : 1; scanServer('home');
ns.tprint(`Running scan depth: ${runDepth}`);
await scanServer('home', runDepth);
await ns.sleep(10);
} }
-1
View File
@@ -4,5 +4,4 @@ export async function main(ns) {
ns.disableLog('ALL'); ns.disableLog('ALL');
ns.scriptKill('hacknet-bot.js', 'home'); ns.scriptKill('hacknet-bot.js', 'home');
await ns.sleep(10);
} }
-1
View File
@@ -4,5 +4,4 @@ export async function main(ns) {
ns.disableLog('ALL'); ns.disableLog('ALL');
ns.scriptKill('auto-nuke.js', 'home'); ns.scriptKill('auto-nuke.js', 'home');
await ns.sleep(10);
} }
File diff suppressed because one or more lines are too long
+14 -5
View File
@@ -3,8 +3,18 @@
export async function main(ns) { export async function main(ns) {
ns.disableLog('ALL'); ns.disableLog('ALL');
async function writeLog(obj) { async function writeLog(type, obj) {
await ns.tryWritePort(1, obj + '\n'); let timeNow = new Date();
let year = timeNow.getFullYear().toString().substring(2);
let month = (timeNow.getMonth() < 9 ? '0' : '') + (timeNow.getMonth() + 1);
let day = (timeNow.getDate() < 10 ? '0' : '') + timeNow.getDate();
let hour = (timeNow.getHours() < 10 ? '0' : '') + timeNow.getHours();
let minute = (timeNow.getMinutes() < 10 ? '0' : '') + timeNow.getMinutes();
let second = (timeNow.getSeconds() < 10 ? '0' : '') + timeNow.getSeconds();
let timestamp = `${year}${month}${day}_${hour}${minute}${second}`;
await ns.tryWritePort(1, `t${timestamp}_${type} = ${obj};\n`);
if (['ERROR', 'TERMINAL'].indexOf(type) != -1) ns.tprint(`${timestamp} ${type} = ${obj}`);
} }
let server = ns.args[0]; let server = ns.args[0];
@@ -38,8 +48,7 @@ export async function main(ns) {
currentSecurity = ns.getServerSecurityLevel(server); currentSecurity = ns.getServerSecurityLevel(server);
} }
await writeLog(`${server.replaceAll('-', '_')} = {loop: ${++loop}, hack: ${Math.floor(hackedMoney)}, money: ${Math.floor(currentMoney)} / ${Math.floor(maxMoney)} == ${((currentMoney / maxMoney) * 100).toFixed(2)}, security: ${currentSecurity.toFixed(2)} / ${Math.floor(minSecurity)} == ${((currentSecurity / minSecurity) * 100).toFixed(4)}};`); await writeLog('LOG', `${server.replaceAll('-', '_')} = {loop: ${++loop}, hack: ${Math.floor(hackedMoney)}, money: ${Math.floor(currentMoney)} / ${Math.floor(maxMoney)} == ${((currentMoney / maxMoney) * 100).toFixed(2)}, security: ${currentSecurity.toFixed(2)} / ${Math.floor(minSecurity)} == ${((currentSecurity / minSecurity) * 100).toFixed(4)}};`);
await ns.sleep(10); await ns.sleep(50);
} }
await ns.sleep(10);
} }
+5 -1
View File
@@ -7,4 +7,8 @@ export async function main(ns) {
ns.scp('grow-helper.js', `home-${server}`, 'home'); ns.scp('grow-helper.js', `home-${server}`, 'home');
ns.exec('grow-helper.js', `home-${server}`, 5, 20); ns.exec('grow-helper.js', `home-${server}`, 5, 20);
} }
} }