mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-20 21:25:47 +01:00
Finished wget() Netscript fn implementation. Updated documentation
This commit is contained in:
parent
44ce4bc674
commit
a7592250f3
@ -1041,7 +1041,18 @@ wget
|
||||
|
||||
wget("https://raw.githubusercontent.com/danielyxie/bitburner/master/README.md", "game_readme.txt");
|
||||
|
||||
Returns a boolean indicating whether or not the data was successfully downloaded.
|
||||
**IMPORTANT:** This is an asynchronous function that returns a Promise. The Promise's resolved
|
||||
value will be a boolean indicating whether or not the data was successfully
|
||||
retrieved from the URL. Because the function is async and returns a Promise,
|
||||
it is recommended you use :code:`wget` in :ref:`netscriptjs`.
|
||||
|
||||
In NetscriptJS, you must preface any call to
|
||||
:code:`wget` with the :code:`await` keyword (like you would :code:`hack` or :code:`sleep`).
|
||||
|
||||
:code:`wget` will still work in :ref:`netscript1`, but the functions execution will not
|
||||
be synchronous (i.e. it may not execute when you expect/want it to). Furthermore, since Promises are not
|
||||
supported in ES5, you will not be able to process the returned value of :code:`wget` in
|
||||
Netscript 1.0.
|
||||
|
||||
getFavorToDonate
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
@ -58,6 +58,7 @@ Here is a summary of all rules you need to follow when writing Netscript JS code
|
||||
* run
|
||||
* exec
|
||||
* prompt
|
||||
* wget
|
||||
|
||||
* Any function that contains :code:`await` must be declared as :code:`async`
|
||||
|
||||
|
@ -85,7 +85,6 @@ let CONSTANTS = {
|
||||
ScriptGetScriptRamCost: 0.1,
|
||||
ScriptGetHackTimeRamCost: 0.05,
|
||||
ScriptGetFavorToDonate: 0.10,
|
||||
ScriptWgetRamCost: 5,
|
||||
ScriptCodingContractBaseRamCost:10,
|
||||
|
||||
ScriptSingularityFn1RamCost: 1,
|
||||
|
@ -2262,35 +2262,34 @@ function NetscriptFunctions(workerScript) {
|
||||
});
|
||||
},
|
||||
wget : async function(url, target, ip=workerScript.serverIp) {
|
||||
if (workerScript.checkingRam) {
|
||||
return updateStaticRam("wget", CONSTANTS.ScriptWgetRamCost);
|
||||
}
|
||||
updateDynamicRam("wget", CONSTANTS.ScriptWgetRamCost);
|
||||
if (workerScript.checkingRam) { return 0; }
|
||||
if (!isScriptFilename(target) && !target.endsWith(".txt")) {
|
||||
workerSript.log(`ERROR: wget() failed because of an invalid target file: ${target}. Target file must be a script or text file`);
|
||||
return false;
|
||||
}
|
||||
var s = safeGetServer(ip, "wget");
|
||||
$.get(url, function(data) {
|
||||
let res;
|
||||
if (isScriptFilename(target)) {
|
||||
res = s.writeToScriptFile(target, data);
|
||||
} else {
|
||||
res = s.writeToTextFile(target, data);
|
||||
}
|
||||
if (!res.success) {
|
||||
workerScript.log("ERROR: wget() failed");
|
||||
return false;
|
||||
}
|
||||
if (res.overwritten) {
|
||||
workerScript.log(`wget() successfully retrieved content and overwrote ${target} on ${ip}`);
|
||||
return true;
|
||||
}
|
||||
workerScript.log(`wget successfully retrieved content to new file ${target} on ${ip}`);
|
||||
return true;
|
||||
}, 'text').fail(function(e) {
|
||||
workerScript.log("ERROR: wget() failed: " + JSON.stringify(e));
|
||||
return false;
|
||||
return new Promise(function(resolve, reject) {
|
||||
$.get(url, function(data) {
|
||||
let res;
|
||||
if (isScriptFilename(target)) {
|
||||
res = s.writeToScriptFile(target, data);
|
||||
} else {
|
||||
res = s.writeToTextFile(target, data);
|
||||
}
|
||||
if (!res.success) {
|
||||
workerScript.log("ERROR: wget() failed");
|
||||
return resolve(false);
|
||||
}
|
||||
if (res.overwritten) {
|
||||
workerScript.log(`wget() successfully retrieved content and overwrote ${target} on ${ip}`);
|
||||
return resolve(true);
|
||||
}
|
||||
workerScript.log(`wget successfully retrieved content to new file ${target} on ${ip}`);
|
||||
return resolve(true);
|
||||
}, 'text').fail(function(e) {
|
||||
workerScript.log(`ERROR: wget() failed: ${JSON.stringify(e)}`);
|
||||
return resolve(false)
|
||||
});
|
||||
});
|
||||
},
|
||||
getFavorToDonate: function() {
|
||||
|
@ -518,12 +518,11 @@ function parseOnlyRamCalculate(server, code, workerScript) {
|
||||
if (typeof func === "function") {
|
||||
try {
|
||||
let res;
|
||||
if (func instance of (async () => {}).constructor) {
|
||||
|
||||
if (func.constructor.name === "AsyncFunction") {
|
||||
res = 0; // Async functions will always be 0 RAM
|
||||
} else {
|
||||
|
||||
res = func.apply(null, []);
|
||||
}
|
||||
let res = func.apply(null, []);
|
||||
if (typeof res === "number") {
|
||||
return res;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user