mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-17 23:08:36 +02:00
DOCUMENTATION: Update tutorial script for buying cloud servers (#2653)
This commit is contained in:
@@ -367,12 +367,11 @@ Paste the following code into the [Script](../basic/scripts.md) editor:
|
|||||||
|
|
||||||
/** @param {NS} ns */
|
/** @param {NS} ns */
|
||||||
export async function main(ns) {
|
export async function main(ns) {
|
||||||
// How much RAM each cloud server will have. In this case, it'll
|
// How much RAM each cloud server will have. In this case, it'll be 8GB.
|
||||||
// be 8GB.
|
|
||||||
const ram = 8;
|
const ram = 8;
|
||||||
|
|
||||||
// Iterator we'll use for our loop
|
// Iterator we'll use for our loop
|
||||||
let i = 0;
|
let i = ns.cloud.getServerNames().length;
|
||||||
|
|
||||||
// Continuously try to purchase cloud servers until we've reached the maximum
|
// Continuously try to purchase cloud servers until we've reached the maximum
|
||||||
// amount of servers
|
// amount of servers
|
||||||
@@ -381,16 +380,16 @@ Paste the following code into the [Script](../basic/scripts.md) editor:
|
|||||||
if (ns.getServerMoneyAvailable("home") > ns.cloud.getRamLimit(ram)) {
|
if (ns.getServerMoneyAvailable("home") > ns.cloud.getRamLimit(ram)) {
|
||||||
// If we have enough money, then:
|
// If we have enough money, then:
|
||||||
// 1. Purchase the server
|
// 1. Purchase the server
|
||||||
// 2. Copy our hacking script onto the newly-purchased cloud server
|
// 2. Copy our hacking script onto the newly purchased cloud server
|
||||||
// 3. Run our hacking script on the newly-purchased cloud server with 3 threads
|
// 3. Run our hacking script on the newly purchased cloud server with 3 threads
|
||||||
// 4. Increment our iterator to indicate that we've bought a new server
|
// 4. Increment our iterator to indicate that we've bought a new server
|
||||||
let hostname = ns.cloud.purchaseServer("cloud-server-" + i, ram);
|
const hostname = ns.cloud.purchaseServer("cloud-server-" + i, ram);
|
||||||
ns.scp("early-hack-template.js", hostname);
|
ns.scp("early-hack-template.js", hostname);
|
||||||
ns.exec("early-hack-template.js", hostname, 3);
|
ns.exec("early-hack-template.js", hostname, 3);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
//Make the script wait for a second before looping again.
|
// Make the script wait for a second before looping again.
|
||||||
//Removing this line will cause an infinite loop and crash the game.
|
// Removing this line will cause an infinite loop and crash the game.
|
||||||
await ns.sleep(1000);
|
await ns.sleep(1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,12 +33,12 @@ Adding a sleep like in the first example, or changing the code so that the `awai
|
|||||||
|
|
||||||
Common infinite loop when translating the server purchasing script in starting guide to scripts is to have a while loop, where the condition's change is conditional:
|
Common infinite loop when translating the server purchasing script in starting guide to scripts is to have a while loop, where the condition's change is conditional:
|
||||||
|
|
||||||
var ram = 8;
|
const ram = 8;
|
||||||
var i = 0;
|
let i = ns.cloud.getServerNames().length;
|
||||||
|
|
||||||
while (i < ns.cloud.getServerLimit()) {
|
while (i < ns.cloud.getServerLimit()) {
|
||||||
if (ns.getServerMoneyAvailable("home") > ns.cloud.getRamLimit(ram)) {
|
if (ns.getServerMoneyAvailable("home") > ns.cloud.getRamLimit(ram)) {
|
||||||
var hostname = ns.cloud.purchaseServer("cloud-server-" + i, ram);
|
const hostname = ns.cloud.purchaseServer("cloud-server-" + i, ram);
|
||||||
ns.scp("early-hack-template.js", hostname);
|
ns.scp("early-hack-template.js", hostname);
|
||||||
ns.exec("early-hack-template.js", hostname, 3);
|
ns.exec("early-hack-template.js", hostname, 3);
|
||||||
++i;
|
++i;
|
||||||
|
|||||||
Reference in New Issue
Block a user