r/Bitburner • • 7d ago

why does this ns.cloud.ugradeServer fail

Even though everything seems correct, it always fails and does not buy any upgrades. I have even printed out the values to make sure everything is what I expect it to be and it is. I always keep all servers at the same amount of ram, thus I only check one since they will all be the some. I had a verison of this before where I told it how much ram to buy, but I wanted to change it to tell it how much I wanted to spend which just seemed to make more sense.

/** u/param {NS} ns */
export async function main(ns) {
  const maxSpend = ns.args[0];
  const server = "cloud-server-";
  const costPerGig = 55000;
  const numServers = 25;


  // calculate how much to buy for each server
  const budgetPerServer = maxSpend / numServers;
  let gigsPerServer = Math.floor(budgetPerServer / costPerGig)


  if(gigsPerServer % 2){ gigsPerServer -= 1}


  const currentRamAmount = ns.getServerMaxRam("cloud-server-0");
  if(gigsPerServer > currentRamAmount){
    for(let i = 0; i < 25; i++){
        ns.tprint(gigsPerServer);
        if(!ns.cloud.upgradeServer(server + i,gigsPerServer)){ns.tprint("something went wrong");}
        else{ns.tprint("purchased ", gigsPerServer, " for " + i);}
    }
  }
  else{
    ns.tprint("If you're broke, just say so.");
  }
}

/** u/param {NS} ns */
export async function main(ns) {
  const maxSpend = ns.args[0];
  const server = "cloud-server-";
  const costPerGig = 55000;
  const numServers = 25;


  // calculate how much to buy for each server
  const budgetPerServer = maxSpend / numServers;
  let gigsPerServer = Math.floor(budgetPerServer / costPerGig)


  if(gigsPerServer % 2){ gigsPerServer -= 1}


  const currentRamAmount = ns.getServerMaxRam("cloud-server-0")
  if(gigsPerServer > currentRamAmount){
    for(let i = 0; i < 25; i++){
        ns.tprint(gigsPerServer)
        if(!ns.cloud.upgradeServer(server + i,gigsPerServer)){ns.tprint("something went wrong");}
        ns.tprint("purchased ", gigsPerServer, " for " + i);
    }
  }
  else{
    ns.tprint("If you're broke, just say so.");
  }
}
5 Upvotes

7 comments sorted by

4

u/kaoshavoc 7d ago

I think I see now. It has to be in powers of 2, not incements of 2. That would make a lot of sense as to why I am getting the error. Also, seems the price does start scaling so I need to rerwite to check actual values. I thought it was always 55,000 per gig.

2

u/goodwill82 Slum Lord 4d ago edited 4d ago

It looks like you found it. In my script, I give ram as an agument, then I calc the next power of 2 beyond the typed amount:

const ramTarget = Number(ns.args[0]); // Assuming the parsed number is valid, you might add a check
let pow2ram = 1;
while (pow2ram < ramTarget) { pow2ram *= 2; }

Then I can just type "8000" instead of thinking about powers of two. You could also provide powers of 2 as autocorrect hints, then those options show up as hints when you press <TAB> after typing run scriptname.js in the terminal.

2

u/kaoshavoc 3d ago

I was unaware that you could put in auto Hints. I will now have to check this out so I can do that for other stuff as well. Thank you.

1

u/goodwill82 Slum Lord 3d ago edited 3d ago

autocomplete is so nice! Add the autocomplete function to your script, but make sure it is not inside the main function.

/**
 * This function is called from the terminal after the user types "run <scriptName.js> ", where <scriptName.js> is this script's filename, and then presses the "TAB" key for autocomplete results. The returned strings are the available autocompletions. Make sure it is above or below the main function in the script, and not inside of it. 
 *
 *  {AutocompleteData} data - holds some relavant game information, kind of like the 'ns' Namespace, but more limited. See https://github.com/bitburner-official/bitburner-src/blob/stable/markdown/bitburner.autocompletedata.md
 *  {ScriptArg[]} args - Arguments that have been added already. Like ns.args
 * u/returns {string[]} A string list of available hints. 
 */
export function autocomplete(data, args) {
  return [...data.servers, "help"]; // this return provides all of the available servers as hints, as well as the word "help", which you can use to print a usage statement if (ns.args.includes("help")) in the main function.
}

Using data or args is optional - you can define your own list of hints. Or filter the data.servers array for servers starting with "cloud-server-" so you only have your cloud servers show up. If you use it for number hints, they need to be strings, e.g.:

return ["1", "2", "4"]; // and not return [1, 2, 4];

Enjoy!

4

u/wannabe_pixie 7d ago

I don’t know what stage of the game you’re at but at some point you can’t assume that Ram cost is fixed

3

u/kaoshavoc 7d ago

I did not know that. I am pretty sure that I am still in the beginning stages of the game. I have not played this before so i don't know how to judge how far in I am. i am pretty sure I saw some functions to return cost now that you mention it. I think I should give those a try and see if maybe it is costing more than I thought it was.

3

u/kaoshavoc 7d ago

addding

        const ramCost = ns.cloud.getServerUpgradeCost(server + i, gigsPerServer);

gives back -1, so something is wrong for sure. the fun of learning :D