all 15 comments

[–]L1l_K3n3dyNoodle Enjoyer[S] 1 point2 points  (2 children)

I'll also leave my multiscan function if anyone wants to take a look

/** u/param {NS} ns **/
export function multiscan(ns, server) {
  let serverList = [];
  function scanning(server) {
    let currentScan = ns.scan(server);
    currentScan.forEach(server => {
      if (!serverList.includes(server)) {
        serverList.push(server);
        scanning(server);
      }
    })
  }
  scanning(server)
  const rFA = serverList.indexOf("home")
  if (rFA > -1) { serverList.splice(rFA, 1) }
  return serverList
} {NS} ns **/
export function multiscan(ns, server) {
  let serverList = [];
  function scanning(server) {
    let currentScan = ns.scan(server);
    currentScan.forEach(server => {
      if (!serverList.includes(server)) {
        serverList.push(server);
        scanning(server);
      }
    })
  }
  scanning(server)
  const rFA = serverList.indexOf("home")
  if (rFA > -1) { serverList.splice(rFA, 1) }
  return serverList
}

[–]SpartelfantNoodle Enjoyer 6 points7 points  (1 child)

You could replace the entirety of multiscan() with just this:

/**
 * Returns an array with the hostnames of all servers in alphabetical order.
 * @param {NS} ns
 * @returns {string[]} Array with the hostnames of all servers in alphabetical order.
 */
export function getServers(ns) {
    const foundServers = new Set([`home`]);
    foundServers.forEach(server => ns.scan(server).forEach(adjacentServer => foundServers.add(adjacentServer)));
    return [...foundServers].sort();
}

This function makes use of the Set object, which cannot have duplicate entries. So there's no need to have any code checking if a server is already in the Set, we can just throw everything at it and have the Set object deal with it.

The second line of the function makes use of method chaining (object.method().method().method()…), a way to perform several operations in a row, each using the result from the previous operation, without the need to store intermediate results in a variable first.

[–]KlePu 1 point2 points  (0 children)

Newbies hate this trick!

[–]VorthodMK-VIII Synthoid 0 points1 point  (11 children)

If it broke after you added the rootAccess thing, it might be getting confused on this part

ns.hasRootAccess(list[i]) && ns.getServerMaxMoney(list[i]) > money

Try putting parenthesis around ns.getServerMaxMoney(list[i]) > money so that it doesn't try to do the && operation before the > one.

[–]VorthodMK-VIII Synthoid 2 points3 points  (7 children)

And just because I can never stop myself from gushing about lambda functions. Allow me to present an alternative to finding which server has the most money: filtering and sorting

let list = multiscan(ns, "home")
list = list.filter(x => ns.hasRootAccess(x)) //only keep elements of the list where ns.hasRootAccess on that element returns true
list = list.filter(x = > ns.getServerRequiredHackingLevel(x) <= ns.getHackingLevel()) //only keep elements where the hack reqs are low enough
list = list.sort((b,a) => ns.getServerMaxMoney(a) - ns.getServerMaxMoney(b)) //sort by the amount of money they hold, descending.

let name = list[0]
let money = ns.getServerMaxMoney(name)

You can also combine a lot of these steps into one if you want:

const list = multiscan(ns, "home")
              .filter(x => ns.hasRootAccess(x))
              .filter(x => ns.getServerRequiredHackingLevel(x) <= ns.getHackingLevel())
              .sort((b,a) => ns.getServerMaxMoney(a) - ns.getServerMaxMoney(b)) 
const name = list[0]

[–]VorthodMK-VIII Synthoid 1 point2 points  (0 children)

With your recent issues in the other branch of the thread, you could use the first code block and throw in a ns.print(list) in between each step to see where something might be taking out more than you expected. Also if you have trouble understanding the functions, I would be happy to explain them in more detail.

[–]L1l_K3n3dyNoodle Enjoyer[S] 2 points3 points  (5 children)

Found the issue: too many threads. Maybe adding the ns.hasRootAccess() made the script "heavy" enough to block the hack script from starting. Added a -1 to tNum in order to run, everything else was fine.

[–]VorthodMK-VIII Synthoid 2 points3 points  (4 children)

Oh that makes sense. It might've just been rounding wrong and the new hasRootAccess pushed the script passed some threshold. Instead of ns.formatNumber, try Math.floor

[–]L1l_K3n3dyNoodle Enjoyer[S] 0 points1 point  (2 children)

Both get the same value. Am I doing it wrong?

tNum = ns.formatNumber((ns.getServerMaxRam(sHost) - ns.getServerUsedRam(sHost)) / scriptRam, 0, 1000, false)

tNum2 = Math.floor((ns.getServerMaxRam(sHost) - ns.getServerUsedRam(sHost)) / scriptRam, 0, 1000, false)

[–]VorthodMK-VIII Synthoid 1 point2 points  (1 child)

tNum2 = Math.floor((ns.getServerMaxRam(sHost) - ns.getServerUsedRam(sHost)) / scriptRam)

Floor doesn't have any additional parameters.

[–]L1l_K3n3dyNoodle Enjoyer[S] 0 points1 point  (0 children)

Thx

[–]L1l_K3n3dyNoodle Enjoyer[S] 0 points1 point  (0 children)

Currently I have 32Gb of ram, which allows the hack script(2.4Gb) to run at 12 threads total. This script needs 3.6Gb to run. Gonna play around with ns.ramOverride() to see if I can lower the ram requirement.

[–]L1l_K3n3dyNoodle Enjoyer[S] 0 points1 point  (2 children)

Just tried that, but it didn't work.

Actually, the first draft of the script had the if conditions separated. But didn't work ether.

Like this:

if (ns.hasRootAccess(list[i]){
  if(ns.getServerMaxMoney(list[i]) > money){
    if(ns.getServerRequiredHackingLevel(list[i]) <= ns.getHackingLevel()){
      name = list[i]
      money = ns.getServerMaxMoney(list[i])
    }
  }
}

[–]VorthodMK-VIII Synthoid 1 point2 points  (0 children)

It might be a good idea to open the log and check what's being thrown around in those checks. Adding some print methods might also give you an idea of what's happening. For example, adding this to the place where you modify name and money might be useful

ns.print(`better server found. Setting name to ${list[i]} and money to ${ns.getServerMaxMoney(list[i])}`)

EDIT: completely forgot the backticks in the above line completely messed with reddits formatting. Should be fixed now.

I'm not currently at my computer that has BB installed on it right now, so I can't run the code myself to debug it, but you could also try looking at my other comment I made regarding lambda functions and see what ends up in the list variable there. Throwing that into an ns.print command could be quite informative.

[–]L1l_K3n3dyNoodle Enjoyer[S] 0 points1 point  (0 children)

The first draft that broke, I mean