NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/** @param {NS} ns **/
export async function main(ns) {
ns.disableLog("ALL");

const { hacknetNodes } = ns.formulas;
const { hacknet } = ns;

const {
MaxCores,
MaxLevel,
MaxRam
} = hacknetNodes.constants();

const maxValues = {
cores: MaxCores,
level: MaxLevel,
ram: MaxRam
};

const upgradeCostFunc = {
cores: hacknet.getCoreUpgradeCost,
level: hacknet.getLevelUpgradeCost,
ram: hacknet.getRamUpgradeCost
};

const maxNodes = hacknet.maxNumNodes();

function getUpgradeInfo(index, stats, field) {
if (stats[field] === maxValues[field]) {
return undefined; // Already maxed out
}
const getCost = (node) => upgradeCostFunc[field](node, 1);
const cost = getCost(index);
const newStats = { ...stats };
newStats[field] += 1;
const gain = hacknetNodes.moneyGainRate(newStats.level, newStats.ram, newStats.cores);
const roi = gain / cost;
return {
cost,
roi,
gain
}
}

function getPurchaseInfo() {
if (hacknet.numNodes() === maxNodes) {
return undefined; // Can't purchase anymore
}
const cost = hacknet.getPurchaseNodeCost();
const gain = hacknetNodes.moneyGainRate(1, 1, 1);
const roi = gain / cost;
return {
cost,
roi,
gain
};
}

function getOwnedHacknetNodes() {
const nodes = [];
for (let i = 0; i < hacknet.numNodes(); i++) {
const stats = hacknet.getNodeStats(i);
const nodeInfo = { ...stats, id: i };
for (const field of Object.keys(upgradeCostFunc)) {
nodeInfo[`${field}Upgrade`] = getUpgradeInfo(i, stats, field);
}
nodes.push(nodeInfo);
}
return nodes;
}

function getBestNodesToUpgrade(nodes) {
const nodesToUpgrade = {
cores: undefined,
ram: undefined,
level: undefined
};
for (const node of nodes) {
for (const field of Object.keys(nodesToUpgrade)) {
const upgradeInfo = node[`${field}Upgrade`];
if (!upgradeInfo) {
continue;
}

if (nodesToUpgrade[field]) {
// Ensure that we're taking the one with largest
if (nodesToUpgrade[field].roi < upgradeInfo.roi) {
nodesToUpgrade[field] = {
node: node.id,
roi: upgradeInfo.roi,
cost: upgradeInfo.cost
}
}
} else {
nodesToUpgrade[field] = {
node: node.id,
roi: upgradeInfo.roi,
cost: upgradeInfo.cost
}
}
}
}
return nodesToUpgrade;
}

function toReadableMoney(cost) {
return cost.toLocaleString("en-US", { style: "currency", currency: "USD" });
}

async function waitForMoney(cost) {
while (ns.getServerMoneyAvailable("home") < cost) {
await ns.sleep(10000); // wait 10s
}
}

async function purchaseNewNode(cost) {
await waitForMoney(cost)
const dollars = toReadableMoney(cost);
ns.print("ERRORtnode-new : PURCHASE_NODEt@ " + dollars);
hacknet.purchaseNode();
}

async function upgradeNode(field, node, cost) {
await waitForMoney(cost);
const dollars = toReadableMoney(cost);
switch (field) {
case "cores":
ns.print("INFOtnode-" + node + " : UPGRADE_CORESt@ " + dollars);
hacknet.upgradeCore(node, 1);
break;
case "level":
ns.print("SUCCESStnode-" + node + " : UPGRADE_LEVELt@ " + dollars);
hacknet.upgradeLevel(node, 1);
break;
case "ram":
ns.print("WARNtnode-" + node + " : UPGRADE_RAMt@ " + dollars);
hacknet.upgradeRam(node, 1);
break;
default:
return;
}
}

async function doAction(action, nodesToUpgrade, purchaseInfo) {
const { cores, level, ram } = nodesToUpgrade;
switch (action) {
case "purchase":
await purchaseNewNode(purchaseInfo.cost);
break;
case "upgradeCore":
await upgradeNode("cores", cores.node, cores.cost);
break;
case "upgradeLevel":
await upgradeNode("level", level.node, level.cost);
break;
case "upgradeRam":
await upgradeNode("ram", ram.node, ram.cost);
break;
default:
return; // do nothing
}
}

async function doNextAction(purchaseInfo, nodesToUpgrade) {
const actionROI = {
purchase: purchaseInfo?.roi,
upgradeCore: nodesToUpgrade?.cores?.roi,
upgradeLevel: nodesToUpgrade?.level?.roi,
upgradeRam: nodesToUpgrade?.ram?.roi
};
const allROI = Object.values(actionROI).filter(roi => roi);
if (allROI.length < 1) {
ns.exit(); // all maxed out
}
const maxROI = Math.max(...allROI);
for (const action of Object.keys(actionROI)) {
const value = actionROI[action];
if (value === maxROI) {
await doAction(action, nodesToUpgrade, purchaseInfo);
break;
}
}
}

while (true) {
const nodes = getOwnedHacknetNodes();
const nodesToUpgrade = getBestNodesToUpgrade(nodes);
const purchaseInfo = getPurchaseInfo();
await doNextAction(purchaseInfo, nodesToUpgrade);
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.