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

@ -33,10 +33,12 @@ 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,42 +76,44 @@ 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() {
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
var hackChance = Player.calculateHackingChance();
var rand = Math.random();
console.log("Hack success chance: " + hackChance + ", rand: " + rand);
var expGainedOnSuccess = Player.calculateExpGain();
var expGainedOnFailure = Math.round(expGainedOnSuccess / 4);
if (rand < hackChance) { //Success!
var moneyGained = Player.calculatePercentMoneyHacked();
moneyGained = Math.floor(Player.getCurrentServer().moneyAvailable * moneyGained);
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");
//Safety check
if (moneyGained <= 0) {moneyGained = 0;}
Player.getCurrentServer().moneyAvailable -= moneyGained;
Player.gainMoney(moneyGained);
Player.hacking_exp += expGainedOnSuccess;
post("Hack successful! Gained $" + moneyGained + " and " + expGainedOnSuccess + " hacking EXP");
} else { //Failure
//Player only gains 25% exp for failure? TODO Can change this later to balance
Player.hacking_exp += expGainedOnFailure;
post("Failed to hack " + Player.getCurrentServer().hostname + ". Gained " + expGainedOnFailure + " hacking EXP");
}
//Calculate whether hack was successful
var hackChance = Player.calculateHackingChance();
var rand = Math.random();
console.log("Hack success chance: " + hackChance + ", rand: " + rand);
var expGainedOnSuccess = Player.calculateExpGain();
var expGainedOnFailure = Math.round(expGainedOnSuccess / 4);
if (rand < hackChance) { //Success!
var moneyGained = Player.calculatePercentMoneyHacked();
moneyGained = Math.floor(Player.getCurrentServer().moneyAvailable * moneyGained);
//Safety check
if (moneyGained <= 0) {moneyGained = 0;}
Player.getCurrentServer().moneyAvailable -= moneyGained;
Player.gainMoney(moneyGained);
Player.hacking_exp += expGainedOnSuccess;
post("Hack successful! Gained $" + moneyGained + " and " + expGainedOnSuccess + " hacking EXP");
} else { //Failure
//Player only gains 25% exp for failure? TODO Can change this later to balance
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,44 +124,46 @@ var Terminal = {
Terminal.hackFlag = false;
},
finishAnalyze: function() {
post(Player.getCurrentServer().hostname + ": ");
post("Required hacking skill: " + Player.getCurrentServer().requiredHackingSkill);
//TODO Make these actual estimates by adding a random offset to result?
//TODO Change the text to sound better
post("Estimated chance to hack: " + Math.round(Player.calculateHackingChance() * 100) + "%");
post("Estimated time to hack: " + Math.round(Player.calculateHackingTime()) + " seconds");
post("Estimed total money available on server: $" + Player.getCurrentServer().moneyAvailable);
post("Required number of open ports for PortHack: " +Player.getCurrentServer().numOpenPortsRequired);
if (Player.getCurrentServer().sshPortOpen) {
post("SSH port: Open")
} else {
post("SSH port: Closed")
}
if (Player.getCurrentServer().ftpPortOpen) {
post("FTP port: Open")
} else {
post("FTP port: Closed")
}
if (Player.getCurrentServer().smtpPortOpen) {
post("SMTP port: Open")
} else {
post("SMTP port: Closed")
}
if (Player.getCurrentServer().httpPortOpen) {
post("HTTP port: Open")
} else {
post("HTTP port: Closed")
}
if (Player.getCurrentServer().sqlPortOpen) {
post("SQL port: Open")
} else {
post("SQL port: Closed")
}
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?
//TODO Change the text to sound better
post("Estimated chance to hack: " + Math.round(Player.calculateHackingChance() * 100) + "%");
post("Estimated time to hack: " + Math.round(Player.calculateHackingTime()) + " seconds");
post("Estimed total money available on server: $" + Player.getCurrentServer().moneyAvailable);
post("Required number of open ports for PortHack: " +Player.getCurrentServer().numOpenPortsRequired);
if (Player.getCurrentServer().sshPortOpen) {
post("SSH port: Open")
} else {
post("SSH port: Closed")
}
if (Player.getCurrentServer().ftpPortOpen) {
post("FTP port: Open")
} else {
post("FTP port: Closed")
}
if (Player.getCurrentServer().smtpPortOpen) {
post("SMTP port: Open")
} else {
post("SMTP port: Closed")
}
if (Player.getCurrentServer().httpPortOpen) {
post("HTTP port: Open")
} else {
post("HTTP port: Closed")
}
if (Player.getCurrentServer().sqlPortOpen) {
post("SQL port: Open")
} 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
Engine.updateHackProgress(numCycles);
if (Engine._actionInProgress == true) {
Engine.updateHackProgress(numCycles);
}
//Update the running time of all active scripts
updateOnlineScriptTimes(numCycles);
@ -344,33 +341,29 @@ 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)
var timeElapsedMilli = numCycles * Engine._idleSpeed;
Engine._actionTimeLeft -= (timeElapsedMilli/ 1000); //Substract idle speed (ms)
//Calculate percent filled
var percent = Math.round((1 - Engine._actionTimeLeft / Engine._totalActionTime) * 100);
//Calculate percent filled
var percent = Math.round((1 - Engine._actionTimeLeft / Engine._totalActionTime) * 100);
//Update progress bar
while (Engine._actionProgressBarCount * 2 <= percent) {
Engine._actionProgressStr = Engine._actionProgressStr.replaceAt(Engine._actionProgressBarCount, "|");
Engine._actionProgressBarCount += 1;
}
//Update hack time remaining
Engine._actionTimeStr = "Time left: " + Math.max(0, Math.round(Engine._actionTimeLeft)).toString() + "s";
document.getElementById("hack-progress").innerHTML = Engine._actionTimeStr;
//Dynamically update progress bar
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();
}
//Update progress bar
while (Engine._actionProgressBarCount * 2 <= percent) {
Engine._actionProgressStr = Engine._actionProgressStr.replaceAt(Engine._actionProgressBarCount, "|");
Engine._actionProgressBarCount += 1;
}
//Update hack time remaining
Engine._actionTimeStr = "Time left: " + Math.max(0, Math.round(Engine._actionTimeLeft)).toString() + "s";
document.getElementById("hack-progress").innerHTML = Engine._actionTimeStr;
//Dynamically update progress bar
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();
}
},

@ -17,4 +17,9 @@ function getIndicesOf(searchStr, str, caseSensitive) {
startIndex = index + searchStrLen;
}
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);
}