Added dynamic array functioanlity. Refactored tail so that it displays a dynamic popup with log contents

This commit is contained in:
Daniel Xie 2017-06-14 20:19:52 -05:00
parent 4aa7edb576
commit 6fe0ec1ea5
13 changed files with 236 additions and 138 deletions

@ -10,10 +10,11 @@
overflow: auto; /* Enable scroll if needed */
/*background-color: black; /* Fallback color */
/*background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
background-color: var(--my-background-color);
background-color: rbga(var(--my-background-color), 0.4);
}
.dialog-box-container {
.dialog-box-container,
#log-box-container {
display: block;
position: absolute;
z-index: 2;
@ -28,14 +29,15 @@
border: 5px solid var(--my-highlight-color);
}
.dialog-box-content {
.dialog-box-content,
#log-box-content {
z-index: 2;
background-color: var(--my-background-color);
padding: 10px;
border: 5px solid var(--my-highlight-color);;
}
.dialog-box-close-button {
.dialog-box-close-button,
#log-box-close {
color: #aaa;
float: right;
font-size: 20px;
@ -49,7 +51,9 @@
}
.dialog-box-close-button:hover,
.dialog-box-close-button:focus {
.dialog-box-close-button:focus,
#log-box-close:hover,
#log-box-close:focus {
color: white;
text-decoration: none;
cursor: pointer;
@ -61,7 +65,7 @@
}
#purchase-server-box-content {
background-color: background-color: var(--my-background-color);
background-color: var(--my-background-color);
margin: 15% auto; /* 15% from the top and centered */
padding: 12px;
border: 5px solid var(--my-highlight-color);;

@ -25,6 +25,7 @@
<script src="utils/TravelBox.js"></script>
<script src="utils/PurchaseRamForHomeBox.js"></script>
<script src="utils/GameOptions.js"></script>
<script src="utils/LogBox.js"></script>
<!-- Netscript -->
<script src="src/NetscriptWorker.js"></script>
@ -645,6 +646,14 @@
<a id="location-slums-heist" class="a-link-button tooltip"> Heist </a>
</div>
<!-- Log Box -->
<div id="log-box-container">
<div id="log-box-content">
<span id="log-box-close"> &times; </span>
<p id="log-box-text"> </p>
</div>
</div>
<!-- Purchase Server Pop-up Box -->
<div id="purchase-server-box-container" class="popup-box-container">
<div id="purchase-server-box-content">

@ -78,7 +78,7 @@ CONSTANTS = {
AugmentationRepMultiplier: 1.5, //Used for balancing rep cost without having to readjust every value
//Maximum number of log entries for a script
MaxLogCapacity: 40,
MaxLogCapacity: 50,
//How much a TOR router costs
TorRouterCost: 200000,
@ -153,6 +153,7 @@ CONSTANTS = {
HelpText: 'alias [name="value"] Create aliases for Terminal commands, or list existing aliases<br>' +
"analyze Get statistics and information about current machine <br>" +
"cat [message] Display a .msg file<br>" +
"check [script] Print script logs to Terminal<br>" +
"clear Clear all text on the terminal <br>" +
"cls See 'clear' command <br>" +
"connect [ip/hostname] Connects to the machine given by its IP or hostname <br>" +
@ -576,7 +577,11 @@ CONSTANTS = {
"v0.21.0<br>" +
"-Added basic theme functionality - All credit goes to /u/0x726564646974 who implemented the awesome feature<br>" +
"-Optimized Script objects, which were causing save errors when the player had too many scripts<br>" +
"-Fixed bug where you could purchase Darkweb items without TOR router"
"-Fixed bug where you could purchase Darkweb items without TOR router<br>" +
"-Slightly increased cost multiplier for Home Computer RAM<br>" +
"-Changed tail command so that it brings up a display box with dynamic log contents. To get " +
"old functionality where the logs are printed to the Terminal, use the new 'check' command<br>" +
"-A script's logs now get cleared when the script is run<br>"+
"v0.20.2<br>" +
"-Fixed several small bugs<br>" +
"-Added basic array functionality to Netscript<br>" +

@ -40,25 +40,13 @@ function evaluate(exp, workerScript) {
}
try {
var res = env.get(exp.value);
if (exp.index) {
//If theres an index field, then this variable is supposed to be an array
//and the user needs to be indexing it
if (res.constructor === Array || res instanceof Array) {
var iPromise = evaluate(exp.index.value, workerScript);
iPromise.then(function(i) {
if (i >= res.length || i < 0) {
return reject(makeRuntimeRejectMsg(workerScript, "Out of bounds: Invalid index in [] operator"));
} else {
return evaluate(res[i], workerScript);
}
}).then(function(res) {
resolve(res);
}).catch(function(e) {
reject(e);
});
} else {
reject(makeRuntimeRejectMsg(workerScript, "Trying to access a non-array variable using the [] operator"));
}
if (res.constructor === Array || res instanceof Array) {
var evalArrayPromise = netscriptArray(exp, workerScript);
evalArrayPromise.then(function(res) {
resolve(res);
}, function(e) {
reject(e);
});
} else {
resolve(res);
}
@ -763,7 +751,7 @@ function evaluateHacknetNode(exp, workerScript) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (exp.index == null) {
if ((exp.op.type == "call" && exp.op.value == "length") ||
if ((exp.op.type == "call" && exp.op.func.value == "length") ||
(exp.op.type == "var" && exp.op.value == "length")) {
resolve(Player.hacknetNodes.length);
workerScript.scriptRef.log("hacknetnodes.length returned " + Player.hacknetNodes.length);
@ -978,6 +966,7 @@ function runScriptFromScript(server, scriptname, workerScript, threads=1) {
script.numTimesHackMap = new AllServersMap();
script.numTimesGrowMap = new AllServersMap();
script.numTimesWeakenMap = new AllServersMap();
script.logs = [];
addWorkerScript(script, server);
resolve(true);
return;
@ -1022,7 +1011,10 @@ function scriptCalculateHackingTime(server) {
//The same as Player's calculateExpGain() function but takes in the server as an argument
function scriptCalculateExpGain(server) {
return (server.hackDifficulty * Player.hacking_exp_mult * 0.9);
if (server.baseDifficulty == null) {
server.baseDifficulty = server.hackDifficulty;
}
return (server.baseDifficulty * Player.hacking_exp_mult * 0.5 + 4);
}
//The same as Player's calculatePercentMoneyHacked() function but takes in the server as an argument

@ -1,72 +1,76 @@
/* Netscript Functions
* Implementation for Netscript features */
/*
function netscriptAssign(exp, workerScript) {
function netscriptArray(exp, workerScript) {
var env = workerScript.env;
var arr = env.get(exp.value);
return new Promise(function(resolve, reject) {
if (env.stopFlag) {return reject(workerScript);}
if (exp.left.type != "var") {
return reject(makeRuntimeRejectMsg(workerScript, "Cannot assign to " + JSON.stringify(exp.left)));
}
//Assigning an element in an array
if (exp.left.index) {
try {
var res = env.get(exp.left.value);
if (res.constructor === Array || res instanceof Array) {
var i = 0;
var iPromise = evaluate(exp.left.index.value, workerScript);
iPromise.then(function(idx) {
if (idx >= res.length || idx < 0) {
return reject(makeRuntimeRejectMsg(workerScript, "Out of bounds: Invalid index in [] operator"));
} else {
//TODO evaluate exp.right here....and then determine its type and
//set the type and value below accordingly
i = idx;
return evaluate(exp.right, workerScript);
}
}).then(function(right) {
console.log("evaluate right with result: " + right);
if (right === true || right === false) {
res[i].type = "bool";
res[i].value = right;
} else if (!isNaN(right) || typeof right == 'number') {
res[i].type = "num";
res[i].value = right;
} else { //String
res[i].type = "str";
res[i].value = right.toString();
}
console.log(res);
return resolve(true);
}).then(function(finalRes) {
resolve(finalRes);
}).catch(function(e) {
return reject(e);
});
if (exp.index == null || exp.index == undefined) {
if ((exp.op.type == "call" && exp.op.func.value == "length") ||
(exp.op.type == "var" && exp.op.value == "length")) {
return resolve(arr.length);
} else if ((exp.op.type == "call" && exp.op.func.value == "clear") ||
(exp.op.type == "var" && exp.op.value == "clear")) {
arr.length = 0;
return resolve(true);
} else if (exp.op.type == "call" && exp.op.func.value == "push") {
if (exp.op.args.length == 1) {
var entry = Object.assign({}, exp.op.args[0]);
arr.push(entry);
return resolve(true);
} else {
return reject(makeRuntimeRejectMsg(workerScript, "Trying to access a non-array variable using the [] operator"));
return reject(makeRuntimeRejectMsg(workerScript, "Invalid number of arguments passed into array.push() command. Takes 1 argument"));
}
} catch(e) {
return reject(makeRuntimeRejectMsg(workerScript, e.toString()));
} else {
return reject(makeRuntimeRejectMsg(workerScript, "Invalid operation on an array"));
}
} else {
var expRightPromise = evaluate(exp.right, workerScript);
expRightPromise.then(function(expRight) {
try {
env.set(exp.left.value, expRight);
} catch (e) {
return reject(makeRuntimeRejectMsg(workerScript, "Failed to set environment variable: " + e.toString()));
}
resolve(false); //Return false so this doesnt cause conditionals to evaluate
}, function(e) {
reject(e);
});
}
//The array is being indexed
var indexPromise = evaluate(exp.index.value, workerScript);
indexPromise.then(function(i) {
if (isNaN(i)) {
return reject(makeRuntimeRejectMsg(workerScript, "Invalid access to array. Index is not a number: " + idx));
} else if (i >= arr.length || i < 0) {
return reject(makeRuntimeRejectMsg(workerScript, "Out of bounds: Invalid index in [] operator"));
} else {
if (exp.op && exp.op.type == "call") {
switch(exp.op.func.value) {
case "insert":
if (exp.op.args.length == 1) {
var entry = Object.assign({}, exp.op.args[0]);
arr.splice(i, 0, entry);
return resolve(arr.length);
} else {
return reject(makeRuntimeRejectMsg(workerScript, "Invalid number of arguments passed into array insert() call. Takes 1 argument"));
}
break;
case "remove":
if (exp.op.args.length == 0) {
return resolve(arr.splice(i, 1));
} else {
return reject(makeRuntimeRejectMsg(workerScript, "Invalid number of arguments passed into array remove() call. Takes 1 argument"));
}
break;
default:
return reject(makeRuntimeRejectMsg(workerScript, "Invalid call on array element: " + exp.op.func.value));
break;
}
} else {
//Return the indexed element
var resPromise = evaluate(arr[i], workerScript);
resPromise.then(function(res) {
resolve(res);
}, function(e) {
reject(e);
});
}
}
}, function(e) {
reject(e);
});
});
}
*/
function netscriptAssign(exp, workerScript) {
var env = workerScript.env;
return new Promise(function(resolve, reject) {
@ -84,7 +88,9 @@ function netscriptAssign(exp, workerScript) {
var i = 0;
var iPromise = evaluate(exp.left.index.value, workerScript);
iPromise.then(function(idx) {
if (idx >= res.length || idx < 0) {
if (isNaN(idx)) {
return reject(makeRuntimeRejectMsg(workerScript, "Invalid access to array. Index is not a number: " + idx));
} else if (idx >= res.length || idx < 0) {
return reject(makeRuntimeRejectMsg(workerScript, "Out of bounds: Invalid index in [] operator"));
} else {
//Clone res to be exp.right

@ -216,8 +216,8 @@ function Parser(input) {
unexpected();
}
function parse_array() {
//Declaring a new array with Array[1,2,3]
//Declaring a new array with Array[1,2,3]
function parse_arraydecl() {
var array = delimited("[", "]", ",", parse_expression);
return {type: "var",
value: "array",
@ -225,14 +225,38 @@ function Parser(input) {
};
}
//Parsing an operation on an array, such as accessing, push(), etc.
//tok is a reference to a token of type var
function parse_arrayop(tok) {
//Returns a variable node except with an extra "index" field so
//we can identify it as an index
if (is_punc("[")) {
var index = parse_arrayindex();
if (index.type != "index") {
unexpected();
}
}
var op = null;
if (is_punc(".")) {
checkPuncAndSkip(".");
op = maybe_call(function() {
var callTok = input.next();
return callTok;
});
}
tok.index = index;
tok.op = op; //Will be null if no operation
return tok;
}
function parse_arrayindex() {
var index = delimited("[", "]", ";", parse_expression);
var val = 0;
if (index.length == 1 && (index[0].type == "num" || index[0].type == "var")) {
if (index.length == 1) {
val = index[0];
} else {
val = 0;
console.log("WARNING: Extra indices passed in")
unexpected();
}
return { type: "index", value: val };
@ -267,16 +291,9 @@ function Parser(input) {
var tok = input.next();
if (tok.type == "var" && tok.value == "hacknetnodes") return parse_hacknetnodes();
if (tok.type == "var" && tok.value == "Array") return parse_array();
if (tok.type == "var" && is_punc("[")) {
//Returns a variable node except with an extra "index" field so
//we can identify it as an index
var index = parse_arrayindex();
if (index.type != "index") {
unexpected();
}
tok.index = index;
return tok;
if (tok.type == "var" && tok.value == "Array") return parse_arraydecl();
if (tok.type == "var" && (is_punc("[") || is_punc("."))) {
return parse_arrayop(tok);
}
if (tok.type == "var" || tok.type == "num" || tok.type == "str")
return tok;

@ -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) {
console.log("Error parsing script: " + workerScripts[i].name);
dialogBoxCreate("Syntax ERROR in " + workerScripts[i].name + ":<br>" + e);

@ -254,7 +254,11 @@ PlayerObject.prototype.calculatePercentMoneyHacked = function() {
//The formula is:
// difficulty * requiredLevel * hacking_multiplier
PlayerObject.prototype.calculateExpGain = function() {
return (this.getCurrentServer().hackDifficulty * this.hacking_exp_mult * 0.9);
var s = this.getCurrentServer();
if (s.baseDifficulty == null) {
s.baseDifficulty = s.hackDifficulty;
}
return (s.baseDifficulty * this.hacking_exp_mult * 0.5 + 4);
}
//Hack/Analyze a server. Return the amount of time the hack will take. This lets the Terminal object know how long to disable itself for

@ -616,6 +616,29 @@ var Terminal = {
}
}
post("Error: No such file " + filename);
break;
case "check":
if (commandArray.length != 2) {
post("Incorrect number of arguments. Usage: check [script]");
} else {
var scriptName = commandArray[1];
//Can only check script files
if (scriptName.endsWith(".script") == false) {
post("Error: check can only be called on .script files (filename must end with .script)"); return;
}
//Check that the script exists on this machine
var currScripts = Player.getCurrentServer().scripts;
for (var i = 0; i < currScripts.length; ++i) {
if (scriptName == currScripts[i].filename) {
currScripts[i].displayLog();
return;
}
}
post("Error: No such script exists");
}
break;
case "clear":
case "cls":
@ -947,7 +970,7 @@ var Terminal = {
var currScripts = Player.getCurrentServer().scripts;
for (var i = 0; i < currScripts.length; ++i) {
if (scriptName == currScripts[i].filename) {
currScripts[i].displayLog();
logBoxCreate(currScripts[i]);
return;
}
}
@ -959,7 +982,8 @@ var Terminal = {
//todo support theme saving
var args = commandArray[1] ? commandArray[1].split(" ") : [];
if(args.length != 1 && args.length != 3) {
post("Incorrect number of arguments. Usage: theme [default|muted|solarized] | [background color hex] [text color hex] [highlight color hex]");
post("Incorrect number of arguments.");
post("Usage: theme [default|muted|solarized] | [background color hex] [text color hex] [highlight color hex]");
}else if(args.length == 1){
var themeName = args[0];
if(themeName == "default"){
@ -1244,6 +1268,9 @@ var Terminal = {
script.numTimesGrowMap = new AllServersMap();
script.numTimesWeakenMap = new AllServersMap();
//Clear logs
script.logs = [];
addWorkerScript(script, server);
return;
}

@ -545,6 +545,10 @@ var Engine = {
displayCreateProgramContent();
}
if (logBoxOpened) {
logBoxUpdateText();
}
Engine.Counters.updateDisplays = 3;
}

@ -2,22 +2,6 @@
dialogBoxes = [];
//Close dialog box when clicking outside
/*
$(document).click(function(event) {
if (dialogBoxOpened) {
if (!$(event.target).closest('.dialog-box-container').length){
--dialogBoxCount;
dialogBoxes.splice(0, 1);
$(".dialog-box-container").remove();
if (dialogBoxes.length == 0) {
dialogBoxOpened = false;
} else {
dialogBoxes[0].style.display +
}
}
}
});*/
$(document).click(function(event) {
if (dialogBoxOpened && dialogBoxes.length >= 1) {
if (!$(event.target).closest(dialogBoxes[0]).length){
@ -34,18 +18,6 @@ $(document).click(function(event) {
//Dialog box close buttons
/*
$(document).on('click', '.dialog-box-close-button', function( event ) {
console.log("clicked close button");
if (dialogBoxOpened) {
$(this).closest('.dialog-box-container').remove();
--dialogBoxCount;
if (dialogBoxes.length == 0) {
dialogBoxOpened = false;
}
}
});
*/
$(document).on('click', '.dialog-box-close-button', function( event ) {
if (dialogBoxOpened && dialogBoxes.length >= 1) {
dialogBoxes[0].remove();

58
utils/LogBox.js Normal file

@ -0,0 +1,58 @@
/* Log Box */
//Close box when clicking outside
$(document).click(function(event) {
if (logBoxOpened) {
if ( $(event.target).closest("#log-box-container").get(0) == null ) {
logBoxClose();
}
}
});
function logBoxInit() {
var closeButton = document.getElementById("log-box-close");
logBoxClose();
//Close Dialog box
closeButton.addEventListener("click", function() {
logBoxClose();
return false;
});
};
document.addEventListener("DOMContentLoaded", logBoxInit, false);
logBoxClose = function() {
logBoxOpened = false;
var logBox = document.getElementById("log-box-container");
logBox.style.display = "none";
}
logBoxOpen = function() {
logBoxOpened = true;
var logBox = document.getElementById("log-box-container");
logBox.style.display = "block";
}
var logBoxOpened = false;
var logBoxCurrentScript = null;
//ram argument is in GB
logBoxCreate = function(script) {
logBoxCurrentScript = script;
logBoxOpen();
logBoxUpdateText();
}
logBoxUpdateText = function() {
var txt = document.getElementById("log-box-text");
if (logBoxCurrentScript && logBoxOpened && txt) {
txt.innerHTML = logBoxCurrentScript.filename + ":<br>";
for (var i = 0; i < logBoxCurrentScript.logs.length; ++i) {
txt.innerHTML += logBoxCurrentScript.logs[i];
txt.innerHTML += "<br>";
}
}
}

@ -36,7 +36,7 @@ purchaseRamForHomeBoxCreate = function() {
//Calculate cost
//Have cost increase by some percentage each time RAM has been upgraded
var cost = currentRam * CONSTANTS.BaseCostFor1GBOfRamHome;
var mult = Math.pow(1.40, numUpgrades);
var mult = Math.pow(1.43, numUpgrades);
cost = cost * mult;
purchaseRamForHomeBoxSetText("Would you like to purchase additional RAM for your home computer? <br><br>" +