Implemented control + c functionality to stop running cmds such as hack() and analyze()

This commit is contained in:
Daniel Xie 2016-12-21 10:33:00 -06:00
parent fc3d52b832
commit d656ee3114
7 changed files with 125 additions and 107 deletions

@ -34,9 +34,11 @@ TESTING TODO:
Server growth Server growth
Implemented but it might need to be balance/formula readjusted Implemented but it might need to be balance/formula readjusted
ctrl+C functionality for all running command like hack(), analyze(), and tail
Implemented for hack() and analyze(). Seems to work
Tasks TODO: Tasks TODO:
ctrl+C functionality for all running command like hack(), analyze(), and tail
Scroll all the way down when something is post()ed Scroll all the way down when something is post()ed
Script logging functionality? Logs to internal "log file" (property of script itself) Script logging functionality? Logs to internal "log file" (property of script itself)
Tutorial and help Tutorial and help

@ -70,6 +70,10 @@
<a href="#" id="faction-menu-link"> Factions </a> <a href="#" id="faction-menu-link"> Factions </a>
</li> </li>
<li class="tutorial-tab">
<a href="#" id="tutorial-menu-link"> Tutorial </a>
</li>
<li class="save-game-tab"> <li class="save-game-tab">
<a href="#" id="save-game-link"> Save Game </a> <a href="#" id="save-game-link"> Save Game </a>
</li> </li>

@ -28,6 +28,12 @@ CONSTANTS = {
"run [script/program] Execute a program or a script\n" + "run [script/program] Execute a program or a script\n" +
"scan See 'netstat' command\n" + "scan See 'netstat' command\n" +
"telnet [ip/hostname] See 'connect' command\n" + "telnet [ip/hostname] See 'connect' command\n" +
"top Display all running scripts and their RAM usage\n" "top Display all running scripts and their RAM usage\n",
//TutorialGettingStartedText:
//TutorialServersText:
//TutorialScriptText:
} }

@ -2,7 +2,7 @@
* Script object * Script object
*/ */
//Define commands in script editor (ctrl x to close, etc.) //Define key commands in script editor (ctrl x to close, etc.)
$(document).keydown(function(e) { $(document).keydown(function(e) {
if (Engine.currentPage == Engine.Page.ScriptEditor) { if (Engine.currentPage == Engine.Page.ScriptEditor) {
//Ctrl + x //Ctrl + x

@ -19,10 +19,11 @@ var postNetburnerText = function() {
post("Netburner v0.1"); post("Netburner v0.1");
} }
//Defines what happens when enter is pressed (keycode 13) //Defines key commands in terminal
$(document).keyup(function(event) { $(document).keyup(function(event) {
//Terminal //Terminal
if (Engine.currentPage == Engine.Page.Terminal) { if (Engine.currentPage == Engine.Page.Terminal) {
//Enter
if (event.keyCode == 13) { if (event.keyCode == 13) {
var command = $('input[class=terminal-input]').val(); var command = $('input[class=terminal-input]').val();
if (command.length > 0) { if (command.length > 0) {
@ -33,6 +34,13 @@ $(document).keyup(function(event) {
$('input[class=terminal-input]').val(""); $('input[class=terminal-input]').val("");
} }
} }
//Ctrl + c when an "Action" is in progress
if (event.keyCode == 67 && event.ctrlKey && Engine._actionInProgress) {
post("Cancelling...");
Engine._actionInProgress = false;
Terminal.finishAction(true);
}
} }
}); });
@ -68,42 +76,44 @@ var Terminal = {
hackFlag: false, hackFlag: false,
analyzeFlag: false, analyzeFlag: false,
finishAction: function() { finishAction: function(cancelled = false) {
if (Terminal.hackFlag) { if (Terminal.hackFlag) {
Terminal.finishHack(); Terminal.finishHack(cancelled);
} else if (Terminal.analyzeFlag) { } else if (Terminal.analyzeFlag) {
Terminal.finishAnalyze(); Terminal.finishAnalyze(cancelled);
} }
}, },
//Complete the hack/analyze command //Complete the hack/analyze command
finishHack: function() { finishHack: function(cancelled = false) {
console.log("Hack done. Determining success/failure of hack. Re-enabling terminal and changing the id of the hack progress bar"); if (cancelled == false) {
console.log("Hack done. Determining success/failure of hack. Re-enabling terminal and changing the id of the hack progress bar");
//Calculate whether hack was successful //Calculate whether hack was successful
var hackChance = Player.calculateHackingChance(); var hackChance = Player.calculateHackingChance();
var rand = Math.random(); var rand = Math.random();
console.log("Hack success chance: " + hackChance + ", rand: " + rand); console.log("Hack success chance: " + hackChance + ", rand: " + rand);
var expGainedOnSuccess = Player.calculateExpGain(); var expGainedOnSuccess = Player.calculateExpGain();
var expGainedOnFailure = Math.round(expGainedOnSuccess / 4); var expGainedOnFailure = Math.round(expGainedOnSuccess / 4);
if (rand < hackChance) { //Success! if (rand < hackChance) { //Success!
var moneyGained = Player.calculatePercentMoneyHacked(); var moneyGained = Player.calculatePercentMoneyHacked();
moneyGained = Math.floor(Player.getCurrentServer().moneyAvailable * moneyGained); moneyGained = Math.floor(Player.getCurrentServer().moneyAvailable * moneyGained);
//Safety check //Safety check
if (moneyGained <= 0) {moneyGained = 0;} if (moneyGained <= 0) {moneyGained = 0;}
Player.getCurrentServer().moneyAvailable -= moneyGained; Player.getCurrentServer().moneyAvailable -= moneyGained;
Player.gainMoney(moneyGained); Player.gainMoney(moneyGained);
Player.hacking_exp += expGainedOnSuccess; Player.hacking_exp += expGainedOnSuccess;
post("Hack successful! Gained $" + moneyGained + " and " + expGainedOnSuccess + " hacking EXP"); post("Hack successful! Gained $" + moneyGained + " and " + expGainedOnSuccess + " hacking EXP");
} else { //Failure } else { //Failure
//Player only gains 25% exp for failure? TODO Can change this later to balance //Player only gains 25% exp for failure? TODO Can change this later to balance
Player.hacking_exp += expGainedOnFailure; Player.hacking_exp += expGainedOnFailure;
post("Failed to hack " + Player.getCurrentServer().hostname + ". Gained " + expGainedOnFailure + " hacking EXP"); post("Failed to hack " + Player.getCurrentServer().hostname + ". Gained " + expGainedOnFailure + " hacking EXP");
} }
}
//Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal //Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal
$("#hack-progress-bar").attr('id', "old-hack-progress-bar"); $("#hack-progress-bar").attr('id', "old-hack-progress-bar");
@ -114,44 +124,46 @@ var Terminal = {
Terminal.hackFlag = false; Terminal.hackFlag = false;
}, },
finishAnalyze: function() { finishAnalyze: function(cancelled = false) {
post(Player.getCurrentServer().hostname + ": "); if (cancelled == false) {
post("Required hacking skill: " + Player.getCurrentServer().requiredHackingSkill); post(Player.getCurrentServer().hostname + ": ");
//TODO Make these actual estimates by adding a random offset to result? post("Required hacking skill: " + Player.getCurrentServer().requiredHackingSkill);
//TODO Change the text to sound better //TODO Make these actual estimates by adding a random offset to result?
post("Estimated chance to hack: " + Math.round(Player.calculateHackingChance() * 100) + "%"); //TODO Change the text to sound better
post("Estimated time to hack: " + Math.round(Player.calculateHackingTime()) + " seconds"); post("Estimated chance to hack: " + Math.round(Player.calculateHackingChance() * 100) + "%");
post("Estimed total money available on server: $" + Player.getCurrentServer().moneyAvailable); post("Estimated time to hack: " + Math.round(Player.calculateHackingTime()) + " seconds");
post("Required number of open ports for PortHack: " +Player.getCurrentServer().numOpenPortsRequired); post("Estimed total money available on server: $" + Player.getCurrentServer().moneyAvailable);
if (Player.getCurrentServer().sshPortOpen) { post("Required number of open ports for PortHack: " +Player.getCurrentServer().numOpenPortsRequired);
post("SSH port: Open") if (Player.getCurrentServer().sshPortOpen) {
} else { post("SSH port: Open")
post("SSH port: Closed") } else {
} post("SSH port: Closed")
}
if (Player.getCurrentServer().ftpPortOpen) { if (Player.getCurrentServer().ftpPortOpen) {
post("FTP port: Open") post("FTP port: Open")
} else { } else {
post("FTP port: Closed") post("FTP port: Closed")
} }
if (Player.getCurrentServer().smtpPortOpen) { if (Player.getCurrentServer().smtpPortOpen) {
post("SMTP port: Open") post("SMTP port: Open")
} else { } else {
post("SMTP port: Closed") post("SMTP port: Closed")
} }
if (Player.getCurrentServer().httpPortOpen) { if (Player.getCurrentServer().httpPortOpen) {
post("HTTP port: Open") post("HTTP port: Open")
} else { } else {
post("HTTP port: Closed") post("HTTP port: Closed")
} }
if (Player.getCurrentServer().sqlPortOpen) { if (Player.getCurrentServer().sqlPortOpen) {
post("SQL port: Open") post("SQL port: Open")
} else { } else {
post("SQL port: Closed") post("SQL port: Closed")
} }
}
Terminal.analyzeFlag = false; Terminal.analyzeFlag = false;
//Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal //Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal
@ -411,10 +423,6 @@ var Terminal = {
break; break;
case "top": case "top":
//TODO List each's script RAM usage //TODO List each's script RAM usage
break;
case "test":
post("test \n this post");
break; break;
default: default:
post("Command not found"); post("Command not found");

@ -1,8 +1,3 @@
//Replaces the character at an index with a new character
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
}
var Engine = { var Engine = {
//Clickable objects //Clickable objects
@ -283,7 +278,9 @@ var Engine = {
Engine.checkCounters(); Engine.checkCounters();
//Manual hacks //Manual hacks
Engine.updateHackProgress(numCycles); if (Engine._actionInProgress == true) {
Engine.updateHackProgress(numCycles);
}
//Update the running time of all active scripts //Update the running time of all active scripts
updateOnlineScriptTimes(numCycles); updateOnlineScriptTimes(numCycles);
@ -344,33 +341,29 @@ var Engine = {
_actionProgressBarCount: 1, _actionProgressBarCount: 1,
_actionInProgress: false, _actionInProgress: false,
updateHackProgress: function(numCycles = 1) { updateHackProgress: function(numCycles = 1) {
if (Engine._actionInProgress == true) { var timeElapsedMilli = numCycles * Engine._idleSpeed;
//TODO Do this calculation based on numCycles rather than idle speed Engine._actionTimeLeft -= (timeElapsedMilli/ 1000); //Substract idle speed (ms)
var timeElapsedMilli = numCycles * Engine._idleSpeed;
Engine._actionTimeLeft -= (timeElapsedMilli/ 1000); //Substract idle speed (ms)
//Calculate percent filled //Calculate percent filled
var percent = Math.round((1 - Engine._actionTimeLeft / Engine._totalActionTime) * 100); var percent = Math.round((1 - Engine._actionTimeLeft / Engine._totalActionTime) * 100);
//Update progress bar //Update progress bar
while (Engine._actionProgressBarCount * 2 <= percent) { while (Engine._actionProgressBarCount * 2 <= percent) {
Engine._actionProgressStr = Engine._actionProgressStr.replaceAt(Engine._actionProgressBarCount, "|"); Engine._actionProgressStr = Engine._actionProgressStr.replaceAt(Engine._actionProgressBarCount, "|");
Engine._actionProgressBarCount += 1; Engine._actionProgressBarCount += 1;
} }
//Update hack time remaining //Update hack time remaining
Engine._actionTimeStr = "Time left: " + Math.max(0, Math.round(Engine._actionTimeLeft)).toString() + "s"; Engine._actionTimeStr = "Time left: " + Math.max(0, Math.round(Engine._actionTimeLeft)).toString() + "s";
document.getElementById("hack-progress").innerHTML = Engine._actionTimeStr; document.getElementById("hack-progress").innerHTML = Engine._actionTimeStr;
//Dynamically update progress bar //Dynamically update progress bar
document.getElementById("hack-progress-bar").innerHTML = Engine._actionProgressStr.replace( / /g, "&nbsp;" ); document.getElementById("hack-progress-bar").innerHTML = Engine._actionProgressStr.replace( / /g, "&nbsp;" );
//Once percent is 100, the hack is completed
if (percent >= 100) {
Engine._actionInProgress = false;
Terminal.finishAction();
}
//Once percent is 100, the hack is completed
if (percent >= 100) {
Engine._actionInProgress = false;
Terminal.finishAction();
} }
}, },

@ -18,3 +18,8 @@ function getIndicesOf(searchStr, str, caseSensitive) {
} }
return indices; return indices;
} }
//Replaces the character at an index with a new character
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
}