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"); 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 getFavorToDonate
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^

@ -58,6 +58,7 @@ Here is a summary of all rules you need to follow when writing Netscript JS code
* run * run
* exec * exec
* prompt * prompt
* wget
* Any function that contains :code:`await` must be declared as :code:`async` * Any function that contains :code:`await` must be declared as :code:`async`

@ -85,7 +85,6 @@ let CONSTANTS = {
ScriptGetScriptRamCost: 0.1, ScriptGetScriptRamCost: 0.1,
ScriptGetHackTimeRamCost: 0.05, ScriptGetHackTimeRamCost: 0.05,
ScriptGetFavorToDonate: 0.10, ScriptGetFavorToDonate: 0.10,
ScriptWgetRamCost: 5,
ScriptCodingContractBaseRamCost:10, ScriptCodingContractBaseRamCost:10,
ScriptSingularityFn1RamCost: 1, ScriptSingularityFn1RamCost: 1,

@ -2262,35 +2262,34 @@ function NetscriptFunctions(workerScript) {
}); });
}, },
wget : async function(url, target, ip=workerScript.serverIp) { wget : async function(url, target, ip=workerScript.serverIp) {
if (workerScript.checkingRam) { if (workerScript.checkingRam) { return 0; }
return updateStaticRam("wget", CONSTANTS.ScriptWgetRamCost);
}
updateDynamicRam("wget", CONSTANTS.ScriptWgetRamCost);
if (!isScriptFilename(target) && !target.endsWith(".txt")) { 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`); workerSript.log(`ERROR: wget() failed because of an invalid target file: ${target}. Target file must be a script or text file`);
return false; return false;
} }
var s = safeGetServer(ip, "wget"); var s = safeGetServer(ip, "wget");
$.get(url, function(data) { return new Promise(function(resolve, reject) {
let res; $.get(url, function(data) {
if (isScriptFilename(target)) { let res;
res = s.writeToScriptFile(target, data); if (isScriptFilename(target)) {
} else { res = s.writeToScriptFile(target, data);
res = s.writeToTextFile(target, data); } else {
} res = s.writeToTextFile(target, data);
if (!res.success) { }
workerScript.log("ERROR: wget() failed"); if (!res.success) {
return false; workerScript.log("ERROR: wget() failed");
} return resolve(false);
if (res.overwritten) { }
workerScript.log(`wget() successfully retrieved content and overwrote ${target} on ${ip}`); if (res.overwritten) {
return true; 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 true; workerScript.log(`wget successfully retrieved content to new file ${target} on ${ip}`);
}, 'text').fail(function(e) { return resolve(true);
workerScript.log("ERROR: wget() failed: " + JSON.stringify(e)); }, 'text').fail(function(e) {
return false; workerScript.log(`ERROR: wget() failed: ${JSON.stringify(e)}`);
return resolve(false)
});
}); });
}, },
getFavorToDonate: function() { getFavorToDonate: function() {

@ -518,12 +518,11 @@ function parseOnlyRamCalculate(server, code, workerScript) {
if (typeof func === "function") { if (typeof func === "function") {
try { try {
let res; let res;
if (func instance of (async () => {}).constructor) { if (func.constructor.name === "AsyncFunction") {
res = 0; // Async functions will always be 0 RAM
} else { } else {
res = func.apply(null, []);
} }
let res = func.apply(null, []);
if (typeof res === "number") { if (typeof res === "number") {
return res; return res;
} }