Advertisement
Kesseleth

server_list(2).ns

Oct 6th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function serverAppend(serverList, toAppend, ns) {
  2.     //for each server in toAppend
  3.     toAppend.forEach(function(item, index, array) {
  4.         //if serverList does not contain the new server and server isn't already being hacked
  5.         if ( (!(serverList.includes(item))) && (!(ns.isRunning("basic_hack.ns", item)))) {
  6.         //then add the new server to serverList
  7.             serverList.push(item);
  8.        
  9.         }
  10.     });
  11.  
  12.  
  13. }
  14.  
  15. async function checkList(ns, serverList) {
  16.                 //for each server in the list
  17.     await serverList.forEach(async function(item, index, array) {
  18.         //if the number of ports required to open is less than or equal to the number of port-busting programs owned and the server is hackable currently
  19.         if (item == "home") {
  20.             serverList.splice(serverList.indexOf("home"));
  21.         } else {
  22.             await ns.run("required_ports_open.ns", 1, item);
  23.             if ((ns.read(2) === true) && (ns.getHackingLevel() >= ns.getServerRequiredHackingLevel(item))) {
  24.                 //run infect.ns
  25.                 await ns.run("infect.ns", 1, item);
  26.                 serverList.splice(serverList.indexOf(item), 1);
  27.             }
  28.         }
  29.     });
  30. }
  31.  
  32. export async function main(ns) {
  33.     await ns.run ("first_write.ns");
  34.     let serverList = [];
  35.     //test: serverList will be empty and a few things will be written into port 1
  36.     //While true
  37.     while (true) {
  38.         //Check port 1 for more servers
  39.         if (ns.peek(1) != "NULL PORT DATA") {
  40.         let serversToRead = ns.read(1);
  41.         serverAppend(serverList, serversToRead, ns);
  42.         }
  43.         await checkList(ns, serverList);
  44.         //await ns.print(serverList);
  45.         //wait 5 seconds
  46.         await ns.sleep(5000);
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement