Finished wget() Netscript fn implementation. Updated documentation

This commit is contained in:
danielyxie 2018-09-27 23:19:52 -05:00
parent 44ce4bc674
commit a7592250f3
5 changed files with 39 additions and 30 deletions

@ -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,15 +2262,13 @@ 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");
return new Promise(function(resolve, reject) {
$.get(url, function(data) {
let res;
if (isScriptFilename(target)) {
@ -2280,17 +2278,18 @@ function NetscriptFunctions(workerScript) {
}
if (!res.success) {
workerScript.log("ERROR: wget() failed");
return false;
return resolve(false);
}
if (res.overwritten) {
workerScript.log(`wget() successfully retrieved content and overwrote ${target} on ${ip}`);
return true;
return resolve(true);
}
workerScript.log(`wget successfully retrieved content to new file ${target} on ${ip}`);
return true;
return resolve(true);
}, 'text').fail(function(e) {
workerScript.log("ERROR: wget() failed: " + JSON.stringify(e));
return false;
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;
}