Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. //should be moved into server.script
  2. // when import depth bug is fixed
  3. function miningRatio(servername) {
  4. disableLog("ALL");
  5.  
  6. //calculation of optimal settings
  7.  
  8. /*
  9. * Optimal settings are the combination of g grow calls and h hacking calls
  10. * where the amount replenished from growth is more or equal to the amount of money hacked.
  11. *
  12. * for this we calculate the amount of growth calls from the amount of money hacked by a single hack * the chance of success
  13. * this allows us to get a grow/hack ratio we then round up to be sure of having enough growth to provide enough money to hack.
  14. *
  15. */
  16.  
  17. h = 1;
  18. h_percent = (hackAnalyzePercent(servername) * hackChance(servername)) / 100;
  19. g = growthAnalyze(servername, 1 + h_percent);
  20. //if we have an infinite value of growth needed (likely to be a server with 0 max money)
  21. if (!isFinite(g)) {
  22. h = 0;
  23. g = 0;
  24. w = 0;
  25. c = 0;
  26. hacked_per_hack = 0.0;
  27. hacked_per_second_per_hack = 0.0;
  28. } else {
  29. ////g is not necessarily > 1 yet
  30. // if g < 1 then we scale up h to keep up with a single grow call => h = 1/g; g = 1;
  31. if (g < 1) {
  32. h = Math.floor(1 / g);
  33. g = 1;
  34. }
  35. //else if g > 1, then we round up g without changing h to replenish what the hacking depletes.
  36. else if (g > 1) {
  37. h = 1;
  38. g = Math.ceil(g);
  39. }
  40. //cycles calculation
  41. /*
  42. * We take into account the server security, and want to achieve a specific optimum of
  43. * running as much grow-hack cycles as possible while keeping the security gradient below the weaken threshold (0.05)
  44. *
  45. * the calculus is simple ;
  46. * c*(0.004*g + 0.002*h) <= 0.05 -> c*0.002(2*g + h) <= 0.05
  47. * hence c <= 0.05/0.002 * 1/(2g + h) -> c <= 25/(2*g + h)
  48. */
  49.  
  50. c = Math.max(1, Math.floor(25 / (2 * g + h)));
  51.  
  52. // the amount of weaken needed to keep the security gain below or equal to zero can be calculated as such:
  53. // (0.004g+0.002h)c-0.05w<= 0
  54. // -> w >= (0.004g+0.002h)c/0.05
  55. w = Math.ceil((0.004 * g + 0.002 * h) * c / 0.05);
  56.  
  57. hacked_per_hack = (getServerMaxMoney(servername) * h_percent);
  58. hacked_per_second_per_hack = (hacked_per_hack / getHackTime(servername));
  59. }
  60. data = {
  61. g: g,
  62. h: h,
  63. c: c,
  64. w: w,
  65. //PRODUCTIVITY
  66. hPerHack: hacked_per_hack,
  67. hPerSecPerHack: hacked_per_second_per_hack,
  68. hPerSec: (hacked_per_hack * h)/(c*(h*getHackTime(servername)+g*getGrowTime(servername))+w*getWeakenTime(servername))
  69. };
  70. //tprint("SERVER " + servername);
  71. //Object.keys(data).forEach(function(key) { tprint(key + " -> " + data[key]); });
  72. return data;
  73. }
  74.  
  75. function analyze(servername, parentname) {
  76. disableLog("ALL");
  77.  
  78. parentname = parentname || "";
  79.  
  80. server = {
  81. //server related
  82. name: servername,
  83. RAM: getServerRam(servername)[0],
  84. hackLevel: getServerRequiredHackingLevel(servername),
  85. portsNeeded: getServerNumPortsRequired(servername),
  86. rootAccess: hasRootAccess(servername),
  87. parent: parentname,
  88. //Mining related
  89. //GROWTH-HACK-WEAKEN RATIOS as amounts of calls, always considered stable, calculated to be.
  90. growthPerCycle: 0,
  91. hackPerCycle: 0,
  92. growHackCycles: 0,
  93. weakenPostCycles: 0,
  94. //PRODUCTIVITY
  95. growthPerSec: 0,
  96. hackedPerSec: 0,
  97. //RAM USAGE considering each as a separate script thread (1.75GB for grow and weaken, 1.7GB for hack)
  98. growthRAM: 0,
  99. hackRAM: 0,
  100. weakenRAM: 0,
  101. totalRAM: 0,
  102. HackedPerSecondPerRAM: 0
  103. };
  104. return update(server);
  105. }
  106. function update(server) {
  107. if (!server.rootAccess)
  108. server.rootAccess = hasRootAccess(server.name);
  109. if (server.rootAccess) {
  110. mining_data = miningRatio(server.name);
  111. server.growthPerCycle = mining_data.g;
  112. server.hackPerCycle = mining_data.h;
  113. server.growHackCycles = mining_data.c;
  114. server.weakenPostCycles = mining_data.w;
  115. //PRODUCTIVITY
  116. server.growthPerSec = mining_data.gPerSec;
  117. server.hackedPerSec = mining_data.hPerSec;
  118. //RAM USAGE considering each as a separate script thread (1.75GB for grow and weaken, 1.7GB for hack)
  119. server.growthRAM = 1.75 * mining_data.g;
  120. server.hackRAM = 1.7 * mining_data.h;
  121. server.weakenRAM = 1.75 * mining_data.w;
  122. server.totalRAM = 1.75 * mining_data.g + 1.75 * mining_data.w + 1.7 * mining_data.h;
  123. server.HackedPerSecondPerRAM = (mining_data.hPerHack == 0) ? 0 : mining_data.hPerSec / (1.75 * mining_data.g + 1.75 * mining_data.w + 1.7 * mining_data.h);
  124. }
  125. return server;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement