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
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:
ctrl+C functionality for all running command like hack(), analyze(), and tail
Scroll all the way down when something is post()ed
Script logging functionality? Logs to internal "log file" (property of script itself)
Tutorial and help

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

@ -28,6 +28,12 @@ CONSTANTS = {
"run [script/program] Execute a program or a script\n" +
"scan See 'netstat' 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
*/
//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) {
if (Engine.currentPage == Engine.Page.ScriptEditor) {
//Ctrl + x

@ -19,10 +19,11 @@ var postNetburnerText = function() {
post("Netburner v0.1");
}
//Defines what happens when enter is pressed (keycode 13)
//Defines key commands in terminal
$(document).keyup(function(event) {
//Terminal
if (Engine.currentPage == Engine.Page.Terminal) {
//Enter
if (event.keyCode == 13) {
var command = $('input[class=terminal-input]').val();
if (command.length > 0) {
@ -33,6 +34,13 @@ $(document).keyup(function(event) {
$('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,16 +76,17 @@ var Terminal = {
hackFlag: false,
analyzeFlag: false,
finishAction: function() {
finishAction: function(cancelled = false) {
if (Terminal.hackFlag) {
Terminal.finishHack();
Terminal.finishHack(cancelled);
} else if (Terminal.analyzeFlag) {
Terminal.finishAnalyze();
Terminal.finishAnalyze(cancelled);
}
},
//Complete the hack/analyze command
finishHack: function() {
finishHack: function(cancelled = false) {
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
@ -104,6 +113,7 @@ var Terminal = {
Player.hacking_exp += expGainedOnFailure;
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
$("#hack-progress-bar").attr('id', "old-hack-progress-bar");
@ -114,7 +124,8 @@ var Terminal = {
Terminal.hackFlag = false;
},
finishAnalyze: function() {
finishAnalyze: function(cancelled = false) {
if (cancelled == false) {
post(Player.getCurrentServer().hostname + ": ");
post("Required hacking skill: " + Player.getCurrentServer().requiredHackingSkill);
//TODO Make these actual estimates by adding a random offset to result?
@ -152,6 +163,7 @@ var Terminal = {
} else {
post("SQL port: Closed")
}
}
Terminal.analyzeFlag = false;
//Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal
@ -411,10 +423,6 @@ var Terminal = {
break;
case "top":
//TODO List each's script RAM usage
break;
case "test":
post("test \n this post");
break;
default:
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 = {
//Clickable objects
@ -283,7 +278,9 @@ var Engine = {
Engine.checkCounters();
//Manual hacks
if (Engine._actionInProgress == true) {
Engine.updateHackProgress(numCycles);
}
//Update the running time of all active scripts
updateOnlineScriptTimes(numCycles);
@ -344,8 +341,6 @@ var Engine = {
_actionProgressBarCount: 1,
_actionInProgress: false,
updateHackProgress: function(numCycles = 1) {
if (Engine._actionInProgress == true) {
//TODO Do this calculation based on numCycles rather than idle speed
var timeElapsedMilli = numCycles * Engine._idleSpeed;
Engine._actionTimeLeft -= (timeElapsedMilli/ 1000); //Substract idle speed (ms)
@ -370,8 +365,6 @@ var Engine = {
Engine._actionInProgress = false;
Terminal.finishAction();
}
}
},
/* Initialization */

@ -18,3 +18,8 @@ function getIndicesOf(searchStr, str, caseSensitive) {
}
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);
}