Bitburner – Hacknet Auto Upgrade Script

Auto upgrade script for hacknet.

Script of Hacknet Auto Upgrade

Note: Credit goes to undefined

The Code

Hello!

I just want to share my auto upgrade script that I wrote today.

Usage: run scriptName.js numberOfDesiredFullNodes

/** @param {NS} ns **/
export async function main(ns) {

	ns.disableLog("ALL");

	const debug = ns.args[1] || false;
	const poolSize = ns.args[0] || 100;

	let fullNodes = 0;

	while (fullNodes != poolSize) {

		let balance = getBalance();

		if (canBuyNode(balance)) {

			ns.hacknet.purchaseNode();
		}

		let nodes = getHackNetNodes();

		nodes.forEach((node) => {

			upgradeNode(node, balance);

		});

		displayStats();

		fullNodes = countFullNodes()

		await ns.sleep(1000);

	}

	function getBalance() {

		return ns.getServerMoneyAvailable("home");
	}


	function canBuyNode(balance) {

		let price = ns.hacknet.getPurchaseNodeCost();

		return balance > price;

	}

	function log(str) {

		if (debug) {
			ns.tprint(str);
		} else {
			ns.print(str);
		}

	}

	function getHackNetNodes() {

		const nodeCount = ns.hacknet.numNodes();
		const hNodes = [];

		for (let i = 0; i < nodeCount; i++) {

			let hNode = ns.hacknet.getNodeStats(i);

			hNodes.push({
				index: i,
				node: hNode,
				maxLevel: false,
				maxRam: false,
				maxCore: false,
			});

		}

		return hNodes;

	}

	function displayStats() {

		ns.clearLog();

		log("--- UPGRADING HACKNET NODES ---");
		log("BALANCE: " + getBalance());
		log("NUMBER OF NODES: " + ns.hacknet.numNodes() + " OF: " + poolSize);
		log("NUMBER OF FULL NODES: " + fullNodes + " OF: " + poolSize);

	}

	function upgradeNode(node, balance) {

		const stats = [
			"Level",
			"Ram",
			"Core"
		];

		stats.forEach((stat) => {

			if (canUpgrade(stat, node, balance)) {

				log("UPGRADING " + stat + " ON " + node.node.name); 

				ns.hacknet["upgrade" + stat](node.index);

			}

		});

	}

	function canUpgrade(stat, node, balance) {

		let cost = ns.hacknet["get" + stat + "UpgradeCost"](node.index);

		if (cost == Infinity) {

			node["max" + stat] = true;

			return false;
		}

		return balance > cost;

	}

	function countFullNodes() {

		let nodes = getHackNetNodes();
		let count = 0;

		nodes.forEach((node) => {

			if (node.maxCore && node.maxLevel && node.maxRam) {

				count += 1;
			}

		});

		return count;

	}
}
Jan Bonkoski
About Jan Bonkoski 954 Articles
Jan Bakowski aka Lazy Dice, was always interested in gaming and writing. His love of gaming began with The Legend of Zelda: Ocarina of Time (Nintendo 64) back in 1998. He’s been making game guides since 2012. Sharing his gaming experience with other players has become not only his hobby but also his job. In his free time, he plays on Steam Deck.

Be the first to comment

Leave a Reply

Your email address will not be published.


*