Skip to content

Instantly share code, notes, and snippets.

@JasonMadeSomething
Created April 4, 2022 20:36
Show Gist options
  • Save JasonMadeSomething/d940843b9767a01bfa0e35516d64b3f2 to your computer and use it in GitHub Desktop.
Save JasonMadeSomething/d940843b9767a01bfa0e35516d64b3f2 to your computer and use it in GitHub Desktop.
/** @param {NS} ns**/
export async function main(ns) {
ns.tail()
ns.disableLog("ALL")
//Need to set up a dynamic target selection based on top money potential server
const target = "foodnstuff"
if (ns.getServerMinSecurityLevel(target) < ns.getServerSecurityLevel(target)) {
await prepTarget(target, ns)
}
}
/** @param {NS} ns**/
function freeRam(ns) {
const currentServer = ns.getHostname()
const usedRam = ns.getServerUsedRam(currentServer)
return ns.getServerMaxRam(currentServer) - usedRam
}
/** @param {NS} ns**/
async function prepTarget(target, ns) {
//Calculate the threads I can dedicate to attacking the server
//Analyze the impact of the attack
//Divide the total by the amount taken away to get the number of runs.
await prepWeak(target, ns)
await prepGrow(target, ns)
await prepWeak(target, ns)
}
function selectTarget(servers){
}
/** @param {NS} ns**/
async function prepWeak(target, ns){
const availableRAM = freeRam(ns)
const attackThreads = availableRAM / 1.75
const attacks = (ns.getServerSecurityLevel(target) - ns.getServerMinSecurityLevel(target))/ ns.weakenAnalyze(attackThreads)
ns.print(`Security Starting at: ${ns.getServerSecurityLevel(target)}. Minimum Sec: ${ns.getServerMinSecurityLevel(target)}`)
for (let i = 0; i < attacks; i++) {
ns.print(`Running ${attackThreads} attack threads of weaken`)
ns.exec("weaken.js", ns.getHostname(), attackThreads, target)
let sleepTime = ns.getWeakenTime(target) + 20
ns.print(`Sleeping for ${sleepTime}`)
await ns.sleep(sleepTime);
ns.print(`i: ${i} -> Weakened by ${ns.weakenAnalyze(attackThreads)} to ${ns.getServerSecurityLevel(target)}`)
}
}
/** @param {NS} ns**/
async function prepGrow(target, ns){
const availableRAM = freeRam(ns)
const maxMoney = ns.getServerMaxMoney(target)
const snapshotMoney = ns.getServerMoneyAvailable(target)
const gMulti = maxMoney/snapshotMoney
const growThreadsNeeded = Math.ceil(ns.growthAnalyze(target, gMulti))
const maxCap = availableRAM / 1.75
const totalRuns = growThreadsNeeded / maxCap
ns.print(`Running ${totalRuns} runs of grow for a total of ${growThreadsNeeded} threads`)
for(let i = 0; i < totalRuns; i++){
ns.exec("grow.js", ns.getHostname(), maxCap, target)
let sleepTime = ns.getGrowTime(target) + 20
await ns.sleep(sleepTime);
ns.print(`i: ${i} -> Grown by ${ns.growthAnalyzeSecurity(maxCap)} to ${ns.getServerSecurityLevel(target)}`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment