From f6aaef455da575a1c6e38b2ad7481641e0c4835e Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Thu, 27 Oct 2016 13:26:00 -0500 Subject: [PATCH] Added full hack functionality with animated progress bar and everthing. All thats left is balancing exp/money gains and also taking care of porthack stuff. Also fixed the terminal not being scrollable --- css/terminal.css | 5 +++ index.html | 20 ++++++++--- src/Player.js | 41 +++++++++++++++------ src/Server.js | 4 +-- src/Terminal.js | 93 ++++++++++++++++++++++++++++++++++++++++++------ src/engine.js | 37 +++++++++++-------- 6 files changed, 157 insertions(+), 43 deletions(-) diff --git a/css/terminal.css b/css/terminal.css index 423fb1469..cfe21cef8 100644 --- a/css/terminal.css +++ b/css/terminal.css @@ -1,7 +1,10 @@ #terminal-container { position: fixed; margin-left: 10%; + height: 100%; width: 99%; + overflow: auto; + overflow-y: scroll; } #terminal { @@ -10,6 +13,8 @@ height: auto; width: 100%; font-size: 16px; + overflow: auto; + overflow-y: scroll; } #terminal-input { diff --git a/index.html b/index.html index 7d6f7bfcb..fc45fd60a 100644 --- a/index.html +++ b/index.html @@ -18,9 +18,21 @@
  • Character
  • - -
  • - Gym + +
  • + + + + + + @@ -29,7 +41,7 @@ - +
    $ $
    diff --git a/src/Player.js b/src/Player.js index f261f0e18..3eacee7ff 100644 --- a/src/Player.js +++ b/src/Player.js @@ -10,6 +10,7 @@ var Player = { hacking_chance_multiplier: 2, //Increase through ascensions/augmentations //hacking_speed_multiplier: 5, //Decrease through ascensions/augmentations hacking_speed_multiplier: 1, //Make it faster for debugging + hacking_money_multiplier: .01, //Increase through ascensions/augmentations. Can't go above 1 //Note: "Lifetime" refers to current ascension, "total" refers to the entire game history //Accumulative stats and skills @@ -55,7 +56,6 @@ var Player = { //Flag to let the engine know the player is starting a hack startHack: false, - finishHack : false, hackingTime: 0, @@ -63,6 +63,9 @@ var Player = { /* Initialize properties of Player's home computer */ Player.homeComputer.init("19.42.93.219", "home", "Home PC", true, true, true, true, 1); Player.currentServer = Player.homeComputer; + + //FOR TESTING ONLY + Player.homeComputer.programs.push("PortHack.exe"); var NetworkGroup1 = [ForeignServers.IronGym, ForeignServers.FoodNStuff, ForeignServers.SigmaCosmetics, ForeignServers.JoesGuns, ForeignServers.HongFangTeaHouse, ForeignServers.HaraKiriSushiBar]; for (var i = 0; i < NetworkGroup1.length; i++) { @@ -82,7 +85,7 @@ var Player = { //The formula is: // (hacking_chance_multiplier * hacking_skill - requiredLevel) 100 - difficulty // ----------------------------------------------------------- * ----------------- - // hacking_chance_multiplier * hacking_skill) 100 + // (hacking_chance_multiplier * hacking_skill) 100 calculateHackingChance: function() { var difficultyMult = (100 - Player.currentServer.hackDifficulty) / 100; var skillMult = (Player.hacking_chance_multiplier * Player.hacking_skill); @@ -98,22 +101,40 @@ var Player = { calculateHackingTime: function() { var difficultyMult = Player.currentServer.requiredHackingSkill * Player.currentServer.hackDifficulty; var skillFactor = difficultyMult / Player.hacking_skill; - console.log("Player.hacking_speed_multiplier: " + Player.hacking_speed_multiplier); return skillFactor * Player.hacking_speed_multiplier; }, - //Hack a server. Return the amount of money hacked. - //This assumes that the server being hacked is not purchased by the palyer, that the player's hacking skill is greater than the + //Calculates the PERCENTAGE of a server's money that the player will hack from the server if successful + //The formula is: + // (hacking_skill - (requiredLevel-1)) 100 - difficulty + // --------------------------------------* ----------------------- * hacking_money_multiplier + // hacking_skill 100 + calculatePercentMoneyHacked: function() { + var difficultyMult = (100 - Player.currentServer.hackDifficulty) / 100; + var skillMult = (Player.hacking_skill - (Player.currentServer.requiredHackingSkill - 1)) / Player.hacking_skill; + var percentMoneyHacked = difficultyMult * skillMult * Player.hacking_money_multiplier; + console.log("Percent money hacked calculated to be: " + percentMoneyHacked); + return percentMoneyHacked; + }, + + //Returns how much EXP the player gains on a successful hack + //The formula is: + // difficulty * requiredLevel * hacking_multiplier + // + // Note: Keep it at an integer for now, + calculateExpGain: function() { + return Math.round(Player.currentServer.hackDifficulty * Player.currentServer.requiredHackingSkill * Player.hacking_exp_mult); + }, + + //Hack a server. Return the amount of time the hack will take. This lets the Terminal object know how long to disable itself for + //This assumes that the server being hacked is not purchased by the player, that the player's hacking skill is greater than the //required hacking skill and that the player has admin rights. - hack: function(hackingSkill) { + hack: function() { Player.hackingTime = Player.calculateHackingTime(); console.log("Hacking time: " + Player.hackingTime); //Set the startHack flag so the engine starts the hacking process Player.startHack = true; - //DEBUG - return 5; + return Player.hackingTime; } - - }; \ No newline at end of file diff --git a/src/Server.js b/src/Server.js index f004f0405..0cf5e9de6 100644 --- a/src/Server.js +++ b/src/Server.js @@ -422,9 +422,7 @@ ForeignServers = { /* Create a randomized network of all foreign servers */ - createNetwork: function() { - console.log("createNetwork() called"); - + createNetwork: function() { //Groupings for creating a randomized network var NetworkGroup1 = [ForeignServers.IronGym, ForeignServers.FoodNStuff, ForeignServers.SigmaCosmetics, ForeignServers.JoesGuns, ForeignServers.HongFangTeaHouse, ForeignServers.HaraKiriSushiBar]; var NetworkGroup2 = [ForeignServers.MaxHardware, ForeignServers.NectarNightclub, ForeignServers.Zer0Nightclub]; diff --git a/src/Terminal.js b/src/Terminal.js index 544282432..918354104 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -34,6 +34,37 @@ $(document).keyup(function(event) { }); var Terminal = { + 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.currentServer.moneyAvailable * moneyGained); + + Player.currentServer.moneyAvailable -= moneyGained; + Player.money += 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.currentServer.hostname + ". Gained " + expGainedOnFailure + " hacking EXP"); + } + + $("#hack-progress-bar").attr('id', "old-hack-progress-bar"); + $("#hack-progress").attr('id', "old-hack-progress"); + document.getElementById("terminal-input-td").innerHTML = '$ '; + $('input[class=terminal-input]').prop('disabled', false); + }, + executeCommand: function(command) { var commandArray = command.split(" "); @@ -53,7 +84,7 @@ var Terminal = { break; case "connect": case "telnet": - //Disconnect from current server in terminal and connect to new one..maybe rename this to telnet? + //Disconnect from current server in terminal and connect to new one if (commandArray.length != 2) { post("Incorrect usage of connect/telnet command. Usage: connect/telnet [ip/hostname]"); return; @@ -79,7 +110,7 @@ var Terminal = { post("Available: " + (Player.currentServer.maxRam - Player.currentServer.ramUsed).toString() + " GB"); break; case "hack": - //TODO Hack the current PC (usually for money) + //Hack the current PC (usually for money) //You can't hack your home pc or servers you purchased if (Player.currentServer.purchasedByPlayer) { post("Cannot hack your own machines! You are currently connected to your home PC or one of your purchased servers"); @@ -90,7 +121,12 @@ var Terminal = { } else { hackProgressPost("Time left:"); hackProgressBarPost("["); - var hackResult = Player.hack(); + var timeToHack = Player.hack(); + + //Disable terminal + console.log("Disabling terminal"); + document.getElementById("terminal-input-td").innerHTML = ''; + $('input[class=terminal-input]').prop('disabled', true); } break; case "help": @@ -108,7 +144,23 @@ var Terminal = { //TODO break; case "ls": - //TODO + //Display all programs and scripts + var allFiles = []; + + //Get all of the programs and scripts on the machine into one temporary array + for (var i = 0; i < Player.currentServer.programs.length; i++) { + allFiles.push(Player.currentServer.programs[i]); + } + for (var i = 0; i < Player.currentServer.scripts.length; i++) { + allFiles.push(Player.currentServer.scripts[i]); + } + + //Sort the files alphabetically then print each + allFiles.sort(); + + for (var i = 0; i < allFiles.length; i++) { + post(allFiles[i]); + } break; case "netstat": case "scan": @@ -154,16 +206,35 @@ var Terminal = { } }, + //First called when the "run [program]" command is called. Checks to see if you + //have the executable and, if you do, calls the executeProgram() function runProgram: function(programName) { + //Check if you have the program on your computer. If you do, execute it, otherwise + //display an error message + for (var i = 0; i < Player.homeComputer.programs.length; i++) { + if (Player.homeComputer.programs[i] == programName) { + Terminal.executeProgram(programName); + return; + } + } + post("ERROR: No such executable"); + }, + + //Contains the implementations of all possible programs + executeProgram: function(programName) { switch (programName) { - case "PortHack": - console.log("Running PortHack executable"); - if (Player.currentServer.openPortCount >= Player.currentServer.numOpenPortsRequired) { - Player.currentServer.hasAdminRights = true; - post("PortHack successful! Gained root access to " + Player.currentServer.hostname); - //TODO Make this take time rather than be instant + case "PortHack.exe": + if (Player.currentServer.hasAdminRights) { + post("You already have root access to this computer. There is no reason to run PortHack.exe"); } else { - post("PortHack unsuccessful. Not enough ports have been opened"); + console.log("Running PortHack executable"); + if (Player.currentServer.openPortCount >= Player.currentServer.numOpenPortsRequired) { + Player.currentServer.hasAdminRights = true; + post("PortHack successful! Gained root access to " + Player.currentServer.hostname); + //TODO Make this take time rather than be instant + } else { + post("PortHack unsuccessful. Not enough ports have been opened"); + } } break; default: diff --git a/src/engine.js b/src/engine.js index c5f588fe5..fb4e0b746 100644 --- a/src/engine.js +++ b/src/engine.js @@ -1,3 +1,7 @@ +//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 = { @@ -132,14 +136,15 @@ var Engine = { //Manual hack if (Player.startHack == true) { - console.log("Player.startHack flag was set to true"); Engine._totalHackTime = Player.hackingTime; Engine._hackTimeLeft = Player.hackingTime; Engine._manualHackInProgress = true; - Engine._hackProgressBarCount = 0; - Engine._hackProgressStr = "["; + Engine._hackProgressBarCount = 1; + Engine._hackProgressStr = "[ ]"; Engine._hackTimeStr = "Time left: "; Player.startHack = false; + + document.getElementById("hack-progress-bar").style.whiteSpace = "pre"; } Engine.updateHackProgress(); @@ -155,33 +160,35 @@ var Engine = { _totalHackTime: 0, _hackTimeLeft: 0, _hackTimeStr: "Time left: ", - _hackProgressStr: "[", - _hackProgressBarCount: 0, + _hackProgressStr: "[ ]", + _hackProgressBarCount: 1, _manualHackInProgress: false, updateHackProgress: function() { if (Engine._manualHackInProgress == true) { - console.log("Manual Hack in Progress"); Engine._hackTimeLeft -= (Engine._idleSpeed/ 1000); //Substract idle speed (ms) //Calculate percent filled - var percent = Math.floor((1 - Engine._hackTimeLeft / Engine.totalhackTime) * 100); - console.log("Hack progress percent: " + percent); + var percent = Math.round((1 - Engine._hackTimeLeft / Engine._totalHackTime) * 100); //Update progress bar - if (Engine._hackProgressBarCount * 2 < percent) { - Engine._hackProgressStr += '|'; - Engine._ProgressBarCount += 1; - document.getElementbyId("hack-progress-bar").innerHTML = Engine._hackProgressStr; + while (Engine._hackProgressBarCount * 2 <= percent) { + Engine._hackProgressStr = Engine._hackProgressStr.replaceAt(Engine._hackProgressBarCount, "|"); + Engine._hackProgressBarCount += 1; } //Update hack time remaining - //Engine._hackTimeStr = "Time left: " + Engine._hackTimeLeft.asString(); + Engine._hackTimeStr = "Time left: " + Math.max(0, Math.round(Engine._hackTimeLeft)).toString() + "s"; + document.getElementById("hack-progress").innerHTML = Engine._hackTimeStr; + + //Dynamically update progress bar + document.getElementById("hack-progress-bar").innerHTML = Engine._hackProgressStr.replace( / /g, " " ); //Once percent is 100, the hack is completed if (percent >= 100) { - Engine.manualHackInProgress = false; - Player.finishHack = true; + Engine._manualHackInProgress = false; + Terminal.finishHack(); } + } },