From bfba6a3d5bca860537f60e20f76f3a9c330d7563 Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Thu, 25 May 2017 09:50:58 -0500 Subject: [PATCH] Updated Netscript documentation for new Hacknet Node 'API'. Adde game options --- css/popupboxes.css | 50 ++++++++++++++++++++++++++++++++++- css/styles.css | 21 ++++++++++++++- index.html | 27 +++++++++++-------- src/Constants.js | 55 ++++++++++++++++++++++++++++++--------- src/NetscriptEvaluator.js | 6 ++--- src/NetscriptWorker.js | 2 +- src/engine.js | 6 +++++ utils/GameOptions.js | 43 ++++++++++++++++++++++++++++++ 8 files changed, 180 insertions(+), 30 deletions(-) create mode 100644 utils/GameOptions.js diff --git a/css/popupboxes.css b/css/popupboxes.css index 75a8514d7..dfb9d85bb 100644 --- a/css/popupboxes.css +++ b/css/popupboxes.css @@ -286,7 +286,7 @@ color: #66ff33; } -#trave-l-box-text { +#travel-box-text { margin: 8px; } @@ -313,4 +313,52 @@ color: white; text-decoration: none; cursor: pointer; +} + +/* Game Options */ +#game-options-container { + display: none; /* Hidden by default */ + position: fixed; /* Stay in place */ + z-index: 1; /* Sit on top */ + left: 0; + right: 0; + bottom: 0; + top: 0; + width: 100%; /* Full width */ + height: 100%; /* Full height */ + overflow: auto; /* Enable scroll if needed */ + background-color: black; /* Fallback color */ + background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ + transition: opacity 400ms ease-in; +} + +#game-options-content { + background-color: black; + margin: 15% auto; /* 15% from the top and centered */ + padding: 10px; + border: 5px solid #FFFFFF; + width: 50%; /* Could be more or less, depending on screen size */ + color: #66ff33; +} + +#game-options-close-button { + color: #aaa; + float: right; + margin: 4px; + padding: 4px; + font-size: 20px; + font-weight: bold; + -webkit-border-radius: 12px; + -moz-border-radius: 12px; + border-radius: 12px white; + -moz-box-shadow: 1px 1px 3px #000; + -webkit-box-shadow: 1px 1px 3px #000; + box-shadow: 1px 1px 3px #000; +} + +#game-options-close-button:hover, +#game-options-close-button:focus { + color: white; + text-decoration: none; + cursor: pointer; } \ No newline at end of file diff --git a/css/styles.css b/css/styles.css index 7cbf5a14c..c273084e4 100644 --- a/css/styles.css +++ b/css/styles.css @@ -241,7 +241,7 @@ tr:focus { position: absolute; /* Stay in place */ right: 0; top: 0; - height: 150px; /* Full height */ + height: 175px; /* Full height */ /*margin: 50% auto;*/ padding: 5px; border: 2px solid #66ff33; @@ -257,3 +257,22 @@ tr:focus { color: white; background-color: #444; } + +#character-overview-save-button { + color: #aaa; + font-size: 16px; + font-weight: bold; + -webkit-border-radius: 12px; + -moz-border-radius: 12px; + border-radius: 12px; + -moz-box-shadow: 1px 1px 3px #000; + -webkit-box-shadow: 1px 1px 3px #000; + box-shadow: 1px 1px 3px #000; +} + +#character-overview-save-button:hover, +#character-overview-save-button:focus { + color: white; + text-decoration: none; + cursor: pointer; +} diff --git a/index.html b/index.html index 6100a8e21..a3a570432 100644 --- a/index.html +++ b/index.html @@ -24,6 +24,7 @@ + @@ -105,19 +106,10 @@
  • Tutorial
  • - -
  • - Save Game -
  • - -
  • - Delete Game -
  • -
  • - (DEBUG) Delete Active Scripts +
  • + Options
  • - @@ -739,6 +731,7 @@

    + Save Game
    @@ -747,5 +740,17 @@

    Test

    + +
    + +
    + \ No newline at end of file diff --git a/src/Constants.js b/src/Constants.js index cb13fb0db..cc6f7d2c8 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -286,20 +286,49 @@ CONSTANTS = { "getHackingLevel()
    Returns the Player's current hacking level. Does NOT work while offline

    " + "getServerMoneyAvailable(hostname/ip)
    Returns the amount of money available on a server. The argument passed in must be a string with either the " + "hostname or IP of the target server. Does NOT work while offline
    Example: getServerMoneyAvailable('foodnstuff');

    " + - "purchaseHacknetNode()
    Purchases a new Hacknet Node. Returns a string with the name of the new Hacknet Node. If the player cannot afford to purchase " + - "a new hacknet node then the function will return an empty string. Does NOT work offline

    " + - "upgradeHacknetNode(name)
    Upgrades the level of a Hacknet Node. The argument passed in must be a string with the name of the Hacknet Node to upgrade. " + - "If the Hacknet Node is successfully upgraded the function will return true. It will return false otherwise. Does NOT work offline. Example:
    " + - "node = purchaseHacknetNode();
    " + - "if (node != '') {
    " + - "    i = 0;
    " + - "    while(i < 10) {
    " + - "         if (upgradeHacknetNode(node)) {i = i + 1;}
    " + - "    };
    " + + "purchaseHacknetNode()
    Purchases a new Hacknet Node. Returns a number with the index of the Hacknet Node. This index is equivalent to the number " + + "at the end of the Hacknet Node's name (e.g The Hacknet Node named 'hacknet-node-4' will have an index of 4). If the player cannot afford to purchase " + + "a new Hacknet Node then the function will return false. Does NOT work offline

    " + + "

    Hacknet Nodes API


    " + + "Netscript provides the following API for accessing and upgrading your Hacknet Nodes through scripts. This API does NOT work offline.

    " + + "hacknetnodes
    A special variable. This is an array that maps to the Player's Hacknet Nodes. The Hacknet Nodes are accessed through " + + "indexes. These indexes correspond to the number at the end of the name of the Hacknet Node. For example, the first Hacknet Node you purchase " + + "will have the same 'hacknet-node-0' and can be accessed with hacknetnodes[0]. The fourth Hacknet Node you purchase will have the name " + + "'hacknet-node-3' and can be accessed with hacknetnodes[3].

    " + + "hacknetnodes.length
    Returns the number of Hacknet Nodes that the player owns

    " + + "hacknetnodes[i].level
    Returns the level of the corresponding Hacknet Node

    " + + "hacknetnodes[i].ram
    Returns the amount of RAM on the corresponding Hacknet Node

    " + + "hacknetnodes[i].cores
    Returns the number of cores on the corresponding Hacknet Node

    " + + "hacknetnodes[i].upgradeLevel(n)
    Tries to upgrade the level of the corresponding Hacknet Node n times. The argument n must be a " + + "positive integer. Returns true if the Hacknet Node's level is successfully upgraded n times, and false otherwise.

    " + + "hacknetnodes[i].upgradeRam()
    Tries to upgrade the amount of RAM on the corresponding Hacknet Node. Returns true if the " + + "RAM is successfully upgraded, and false otherwise.

    " + + "hacknetnodes[i].upgradeCore()
    Attempts to purchase an additional core for the corresponding Hacknet Node. Returns true if the " + + "additional core is successfully purchase, and false otherwise.

    " + + "Example: The following is an example of one way a script can be used to automate the purchasing and upgrading of Hacknet Nodes. " + + "This script purchases new Hacknet Nodes until the player has four. Then, it iteratively upgrades each of those four Hacknet Nodes " + + "to a level of at least 75, RAM to at least 8GB, and number of cores to at least 2.

    " + + "while(hacknetnodes.length < 4) {
    " + + "    purchaseHacknetNode();
    " + + "};
    " + + "for (i = 0; i < 4; i = i+1) {
    " + + "    while (hacknetnodes[i].level <= 75) {
    " + + "        hacknetnodes[i].upgradeLevel(5);
    " + + "        sleep(10000);
    " + + "    };
    " + + "};
    " + + "for (i = 0; i < 4; i = i+1) {
    " + + "    while (hacknetnodes[i].ram < 8) {
    " + + "        hacknetnodes[i].upgradeRam();
    " + + "        sleep(10000);
    " + + "    };
    " + + "};
    " + + "for (i = 0; i < 4; i = i+1) {
    " + + "    while (hacknetnodes[i].cores < 2) {
    " + + "        hacknetnodes[i].upgradeCore();
    " + + "        sleep(10000);
    " + + "    };
    " + "};

    " + - "The example code above will attempt to purchase a new Hacknet Node. If the Hacknet Node is purchased, then it will " + - "continuously try to level it up until it is leveled up 10 times.

    " + - "getNumHacknetNodes()
    returns the number of Hacknet Nodes that the Player owns. Does NOT work while offline

    " + "

    While loops


    " + "A while loop is a control flow statement that repeatedly executes code as long as a condition is met.

    " + "while ([cond]) {
        [code]
    }


    " + diff --git a/src/NetscriptEvaluator.js b/src/NetscriptEvaluator.js index b0ed43598..c3298cea1 100644 --- a/src/NetscriptEvaluator.js +++ b/src/NetscriptEvaluator.js @@ -995,13 +995,13 @@ function evaluateHacknetNode(exp, workerScript) { if (exp.op.args.length == 1) { var argPromise = evaluate(exp.op.args[0], workerScript); argPromise.then(function(arg) { - if (isNaN(arg)) { - reject(makeRuntimeRejectMsg(workerScript, "Argument passed into upgradeLevel() is not numeric")); + if (isNaN(arg) || arg < 0) { + reject(makeRuntimeRejectMsg(workerScript, "Invalid argument passed into upgradeLevel()")); return; } var res = nodeObj.purchaseLevelUpgrade(arg); if (res) { - workerScript.scriptRef.log("Upgraded " + nodeObj.name + arg + " times to level " + nodeObj.level); + workerScript.scriptRef.log("Upgraded " + nodeObj.name + " " + arg + " times to level " + nodeObj.level); } resolve(res); }, function(e) { diff --git a/src/NetscriptWorker.js b/src/NetscriptWorker.js index 4f57bb201..1076ced86 100644 --- a/src/NetscriptWorker.js +++ b/src/NetscriptWorker.js @@ -31,7 +31,7 @@ function runScriptsLoop() { if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == false) { try { var ast = Parser(Tokenizer(InputStream(workerScripts[i].code))); - console.log(ast); + //console.log(ast); } catch (e) { dialogBoxCreate("Syntax ERROR in " + workerScripts[i].name + ":", e, "", ""); workerScripts[i].env.stopFlag = true; diff --git a/src/engine.js b/src/engine.js index 8733b7f97..03fb348a7 100644 --- a/src/engine.js +++ b/src/engine.js @@ -988,6 +988,12 @@ var Engine = { return false; }); + //Character Overview Save button + var charOverviewSaveButton = document.getElementById("character-overview-save-button"); + charOverviewSaveButton.addEventListener("click", function() { + saveObject.saveGame(); + return false; + }); //Script Editor Netscript documentation button var netscriptDocButton = document.getElementById("script-editor-netscript-doc-button"); diff --git a/utils/GameOptions.js b/utils/GameOptions.js new file mode 100644 index 000000000..019e9e0fc --- /dev/null +++ b/utils/GameOptions.js @@ -0,0 +1,43 @@ +/* GameOptions.js */ + +//Close box when clicking outside +$(document).click(function(event) { + if (gameOptionsOpened) { + if ( $(event.target).closest(".game-options-box").get(0) == null ) { + gameOptionsBoxClose(); + } + } +}); + +var gameOptionsOpened = false; +function gameOptionsBoxInit() { + //Menu link button + document.getElementById("options-menu-link").addEventListener("click", function() { + gameOptionsBoxOpen(); + return false; + }); + + //Close button + var closeButton = document.getElementById("game-options-close-button"); + closeButton.addEventListener("click", function() { + gameOptionsBoxClose(); + return false; + }); +}; + +document.addEventListener("DOMContentLoaded", gameOptionsBoxInit, false); + +gameOptionsBoxClose = function() { + gameOptionsOpened = false; + var box = document.getElementById("game-options-container"); + box.style.display = "none"; +} + +gameOptionsBoxOpen = function() { + var box = document.getElementById("game-options-container"); + box.style.display = "block"; + setTimeout(function() { + gameOptionsOpened = true; + }, 500); + +} \ No newline at end of file