Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.9 kB
108
Indexable
Never
/** @param {NS} ns **/
export async function main(ns) {
    var ram = ns.args[0];

	// Continuously try to purchase servers until we've reached the maximum
	// amount of servers
	while(true){
		var i = 0;
		ns.tprint('25x ' + ram + 'GB ' + 'RAM cost: ' + (ns.getPurchasedServerCost(ram)*25)+'$');
		
		var totalMoney = ns.getServerMoneyAvailable('home');
		var cost = ns.getPurchasedServerCost(ram) * 25;
		var costGB = ns.getPurchasedServerCost(ram) / ram;
		var maxRam = ns.getPurchasedServerMaxRam();
		var budget = totalMoney - cost;
		ns.tprint("money left: "+ budget+'$');


		// calculate max RAM for the budget
		if (budget > 0) {
			ram *= 2;

			while (budget > 0) {
				ns.tprint(ram + ' to ' + (ram*2));
				ns.tprint(budget)
				ram *= 2;
				budget = ns.getServerMoneyAvailable('home') - (ns.getPurchasedServerCost(ram) * 25);
			}
			ram /= 2;
		}

		if (ram > maxRam) {
			ram = maxRam;
		}
		budget = ns.getServerMoneyAvailable('home') - (ns.getPurchasedServerCost(ram) * 25);


		while (i < ns.getPurchasedServerLimit()) {
			ns.tprint('entering purchased server limit')
			// Check if we have enough money to purchase a server
			if (budget > 0) {
				ns.tprint('entered budget server phase')
				ns.killall("pserv-" + i);
				ns.tprint('killed')
				ns.deleteServer("pserv-" + i);
				ns.tprint('deleted')
				var hostname = ns.purchaseServer("pserv-" + i, ram);
				await ns.scp("hack-joesguns.script", hostname);
				ns.tprint('copied')

				var serverRam = ns.getServerMaxRam("pserv-" + i)
				var scriptRam = ns.getScriptRam('hack-joesguns.script');
				var threads = Math.round(serverRam/scriptRam);
				ns.tprint('rounded')
				ns.tprint(budget)
				ns.exec('hack-joesguns.script', hostname, threads);
				ns.tprint(ram + 'GB ' + 'pserv-' + i +' installed');
			}
			++i;
			ns.tprint('i++')
		}
		i = 0;
		ns.tprint('server loop ended')
	}
}