From 52297268f6b8bcbcc45734d9249dd0e1d0daa2cc Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Thu, 13 Apr 2017 12:33:34 -0500 Subject: [PATCH] Addded Perk class, Traveling functionality, fixed bugs with script logging and terminal commands --- README.md | 8 ++++- index.html | 14 +++++++++ src/Constants.js | 10 +++---- src/CreateProgram.js | 18 ++++++------ src/Location.js | 50 +++++++++++++++++++++++++++++--- src/Netscript/NetscriptWorker.js | 5 +--- Perk.js => src/Perk.js | 0 src/Player.js | 8 ++--- src/PurchaseServers.js | 1 - src/Script.js | 12 ++++---- src/Terminal.js | 11 +++---- src/engine.js | 14 ++++----- utils/TravelBox.js | 46 +++++++++++++++++++++++++++++ 13 files changed, 151 insertions(+), 46 deletions(-) rename Perk.js => src/Perk.js (100%) create mode 100644 utils/TravelBox.js diff --git a/README.md b/README.md index 14a5a1574..64d7f8d5b 100644 --- a/README.md +++ b/README.md @@ -56,11 +56,17 @@ TESTING TODO: Augmentations Make it so that a script cannot be edited if it is running + + + Traveling Script logging functionality? Logs to internal "log file" (property of script itself) Can see log with tail. Should add something where if you click it in the "Active Scripts" GUI you can see the logs too + + Seems to work fine Tasks TODO: + New server hostname in Purchase Server Pop-up Box needs limits..don't think the ones set in HTML work + Tutorial and help - INTERACTIVE TUTORIAL Secret Servers @@ -68,7 +74,7 @@ Tasks TODO: Create new menu page for purchased servers - Gyms + Traveling + Gyms - Later..don't need for MVP Update CONSTANTS.HelpText Account for Max possible int when gaining exp (it will overflow) diff --git a/index.html b/index.html index bc7c6b536..533f944ce 100644 --- a/index.html +++ b/index.html @@ -22,6 +22,7 @@ + @@ -45,6 +46,7 @@ + @@ -466,6 +468,9 @@ Purchase 1TB Server - $600,000,000 +

+ From here, you can travel to any other city! A ticket costs $1,000,000. +

Travel to Aevum Travel to Chongqing Travel to Sector-12 @@ -495,6 +500,15 @@ Cancel + + +
+

+ Yes + No +
+
diff --git a/src/Constants.js b/src/Constants.js index 81f14e430..6723298b7 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -34,7 +34,7 @@ CONSTANTS = { "rm Delete a script/program from the machine. (WARNING: Permanent)
" + "run [script/program] Execute a program or a script
" + "scan See 'netstat' command
" + - "tail [script] Display script logs (logs contain details about active scripts)" + "tail [script] Display script logs (logs contain details about active scripts)" + "telnet [ip/hostname] See 'connect' command
" + "top Display all running scripts and their RAM usage
", @@ -64,7 +64,7 @@ CONSTANTS = { "will have very high security and will need many ports opened. In order to open ports on another " + "server, you will need to run programs that attack the server to open specific ports. These programs " + "can be coded once your hacking skill gets high enough, or they can be purchased if you can find " + - "a seller.

" + "a seller.

" + "In order to determine how many ports need to be opened to successfully NUKE a server, connect to " + "that server and run the 'analyze' command. This will also show you which ports have already been " + "opened.
" + @@ -95,7 +95,7 @@ CONSTANTS = { "ps - Displays all scripts that are actively running on the current server
" + "run [script] - Run a script
" + "tail [script] - Displays a script's logs
" + - "top - Displays all active scripts and their RAM usage

" + "top - Displays all active scripts and their RAM usage

", TutorialTravelingText:"There are six major cities in the world that you are able to travel to:
" + " Aevum
" + " Chongqing
" + @@ -105,7 +105,7 @@ CONSTANTS = { " Volhaven
" + "To travel between cities, visit your current city's travel agency through the 'World' page. " + "From the travel agency you can travel to any other city. Doing so costs money.
" + - "Each city has its own set of companies and unique locations. " + "Each city has its own set of companies and unique locations. ", TutorialJobsText: "Hacking is not the only way to gain money and experience! Located around the world are many " + "different companies which you can work for. By working for a company you can earn money, " + "train your various labor skills, and unlock powerful passive perks.
" + @@ -122,7 +122,7 @@ CONSTANTS = { "other actions such as using your terminal or visiting other locations (However, note that any scripts you have " + "running on servers will continue to run as you work!). It is possible to cancel your work shift before the " + "8 hours is up, but doing so will result in you gaining only half of all of the money, experience, and reputation " + - "that you had earned up to that point.
" + "that you had earned up to that point.
" + "As you continue to work at a company, you will gain more and more reputation at that company. When your stats " + "and reputation are high enough, you can get a promotion. You can apply for a promotion on the company page, just like " + "you applied for the job originally. Higher positions at a company provide better salaries and stat gains", diff --git a/src/CreateProgram.js b/src/CreateProgram.js index caf59826e..6d28ab69c 100644 --- a/src/CreateProgram.js +++ b/src/CreateProgram.js @@ -30,7 +30,7 @@ function displayCreateProgramContent() { portHackALink.style.display = "block"; portHackALink.addEventListener("click", function() { createProgram(Programs.PortHackProgram); - } + }); } //BruteSSH @@ -39,7 +39,7 @@ function displayCreateProgramContent() { bruteSshALink.style.display = "block"; bruteSshALink.addEventListener("click", function() { createProgram(Programs.BruteSSHProgram); - } + }); } //FTPCrack @@ -48,34 +48,34 @@ function displayCreateProgramContent() { ftpCrackALink.style.display = "block"; ftpCrackALink.addEventListener("click", function() { createProgram(Programs.FTPCrackProgram); - } + }); } //relaySMTP - if (Player.getHomeComputer().programs.indexOf(Programs.RelaySMTPProgram == -1 && + if (Player.getHomeComputer().programs.indexOf(Programs.RelaySMTPProgram) == -1 && Player.hacking_skill >= 300) { relaySmtpALink.style.display = "block"; relaySmtpAlink.addEventListener("click", function() { createProgram(Programs.RelaySMTPProgram); - } + }); } //HTTPWorm - if (Player.getHomeComputer().programs.indexOf(Programs.HTTPWormProgram == -1 && + if (Player.getHomeComputer().programs.indexOf(Programs.HTTPWormProgram) == -1 && Player.hacking_skill >= 500) { httpWormALink.style.display = "block"; httpWormALink.addEventListener("click", function() { createProgram(Programs.HTTPWormProgram); - } + }); } //SQLInject - if (Player.getHomeComputer().programs.indexOf(Programs.SQLInjectProgram == -1 && + if (Player.getHomeComputer().programs.indexOf(Programs.SQLInjectProgram) == -1 && Player.hacking_skill >= 750) { sqlInjectALink.style.display = "block"; sqlInjectALink.addEventListener("click", function() { createProgram(Programs.SQLInjectProgram); - } + }); } } diff --git a/src/Location.js b/src/Location.js index 5aa9e3bca..9e66886eb 100644 --- a/src/Location.js +++ b/src/Location.js @@ -116,6 +116,7 @@ displayLocationContent = function() { var purchase512gb = document.getElementById("location-purchase-512gb"); var purchase1tb = document.getElementById("location-purchase-1tb"); + var travelAgencyText = document.getElementById("location-travel-agency-text"); var travelToAevum = document.getElementById("location-travel-to-aevum"); var travelToChongqing = document.getElementById("location-travel-to-chongqing"); var travelToSector12 = document.getElementById("location-travel-to-sector12"); @@ -173,6 +174,7 @@ displayLocationContent = function() { purchase512gb.style.display = "none"; purchase1tb.style.display = "none"; + travelAgencyText.style.display = "none"; travelToAevum.style.display = "none"; travelToChongqing.style.display = "none"; travelToSector12.style.display = "none"; @@ -221,6 +223,7 @@ displayLocationContent = function() { switch (loc) { case Locations.AevumTravelAgency: + travelAgencyText.style.display = "block"; travelToChongqing.style.display = "block"; travelToSector12.style.display = "block"; travelToNewTokyo.style.display = "block"; @@ -356,6 +359,7 @@ displayLocationContent = function() { break; case Locations.ChongqingTravelAgency: + travelAgencyText.style.display = "block"; travelToAevum.style.display = "block"; travelToSector12.style.display = "block"; travelToNewTokyo.style.display = "block"; @@ -387,6 +391,7 @@ displayLocationContent = function() { case Locations.Sector12TravelAgency: + travelAgencyText.style.display = "block"; travelToAevum.style.display = "block"; travelToChongqing.style.display = "block"; travelToNewTokyo.style.display = "block"; @@ -528,6 +533,7 @@ displayLocationContent = function() { break; case Locations.NewTokyoTravelAgency: + travelAgencyText.style.display = "block"; travelToAevum.style.display = "block"; travelToChongqing.style.display = "block"; travelToSector12.style.display = "block"; @@ -573,7 +579,8 @@ displayLocationContent = function() { break; - case Locations.IshimaTravelAgency: + case Locations.IshimaTravelAgency: + travelAgencyText.style.display = "block"; travelToAevum.style.display = "block"; travelToChongqing.style.display = "block"; travelToSector12.style.display = "block"; @@ -624,8 +631,7 @@ displayLocationContent = function() { break; case Locations.VolhavenTravelAgency: - locationInfo.innerHTML = Companies[loc].info; - + travelAgencyText.style.display = "block"; travelToAevum.style.display = "block"; travelToChongqing.style.display = "block"; travelToSector12.style.display = "block"; @@ -1225,4 +1231,40 @@ initLocationButtons = function() { return false; }); -} \ No newline at end of file + travelToAevum.addEventListener("click", function() { + travelBoxCreate(Locations.Aevum, 1000000); + }); + + travelToChongqing.addEventListener("click", function() { + travelBoxCreate(Locations.Chongqing, 1000000); + }); + + travelToSector12.addEventListener("click", function() { + travelBoxCreate(Locations.Sector12, 1000000); + }); + + travelToNewTokyo.addEventListener("click", function() { + travelBoxCreate(Locations.NewTokyo, 1000000); + }); + + travelToIshima.addEventListener("click", function() { + travelBoxCreate(Locations.Ishima, 1000000); + }); + + travelToVolhaven.addEventListener("click", function() { + travelBoxCreate(Locations.Volhaven, 1000000); + }); + +} + +travelToCity = function(destCityName, cost) { + if (cost > Player.money) { + dialogBoxCreate("You cannot afford to travel to " + destCityName); + return; + } + Player.money -= cost; + + Player.city = destCityName; + dialogBoxCreate("You are now in " + destCityName + "!"); + Engine.loadWorldContent(); +} \ No newline at end of file diff --git a/src/Netscript/NetscriptWorker.js b/src/Netscript/NetscriptWorker.js index e605d29ca..bd2c17c10 100644 --- a/src/Netscript/NetscriptWorker.js +++ b/src/Netscript/NetscriptWorker.js @@ -1,7 +1,4 @@ -/* Worker code, contains Netscript scripts that are actually running */ -//TODO Need some way to stop scripts. Idea: Put a flag in the environment, we can setActive -//this flag from outside. If the evaluate() function sees that flag it rejects the current -// Promise. We can catch that rejection and stop the script. +/* Worker code, contains Netscript scripts that are actually running */ //TODO Tested For and while and generic call statements. Have not tested if statements diff --git a/Perk.js b/src/Perk.js similarity index 100% rename from Perk.js rename to src/Perk.js diff --git a/src/Player.js b/src/Player.js index e40af3565..bb069f45d 100644 --- a/src/Player.js +++ b/src/Player.js @@ -61,8 +61,8 @@ function PlayerObject() { //Money this.money = 0; - this.total_money = 0; - this.lifetime_money = 0; + this.total_money = 0; //Total money ever earned + this.lifetime_money = 0; //Total money ever earned //IP Address of Starting (home) computer this.homeComputer = ""; @@ -140,7 +140,7 @@ PlayerObject.prototype.init = function() { this.currentServer = t_homeComp.ip; AddToAllServers(t_homeComp); - this.getHomeComputer().programs.push(CONSTANTS.PortHackProgram); + this.getHomeComputer().programs.push(Programs.NukeProgram); } PlayerObject.prototype.getCurrentServer = function() { @@ -648,7 +648,7 @@ PlayerObject.prototype.createProgramWork = function(numCycles) { var txt = document.getElementById("work-in-progress-text"); txt.innerHTML = "You are currently working on coding " + programName + ".

" + "You have been working for " + convertTimeMsToTimeElapsedString(this.timeWorked) + "

" + - "The program is " + (this.timeWorked / timeToComplete).toFixed(2) "% complete.
" + + "The program is " + (this.timeWorked / timeToComplete).toFixed(2) + "% complete.
" + "If you cancel, you will lose all of your progress."; } diff --git a/src/PurchaseServers.js b/src/PurchaseServers.js index 32cedf0a1..67caa66ec 100644 --- a/src/PurchaseServers.js +++ b/src/PurchaseServers.js @@ -6,7 +6,6 @@ purchaseServer = function(ram, cost) { return; } - var newServ = new Server(); var hostname = document.getElementById("purchase-server-box-input").value; if (hostname == "") { diff --git a/src/Script.js b/src/Script.js index 2bb503014..1e2fbdcee 100644 --- a/src/Script.js +++ b/src/Script.js @@ -68,7 +68,7 @@ function Script() { this.code = ""; this.ramUsage = 0; this.server = ""; //IP of server this script is on - this.log = []; //Script logging. Array of strings, with each element being a log entry + this.logs = []; //Script logging. Array of strings, with each element being a log entry /* Properties to calculate offline progress. Only applies for infinitely looping scripts */ @@ -127,18 +127,18 @@ Script.prototype.updateRamUsage = function() { } Script.prototype.log = function(txt) { - if (this.log.length > CONSTANTS.MaxLogCapacity) { + if (this.logs.length > CONSTANTS.MaxLogCapacity) { //Delete first element and add new log entry to the end. //TODO Eventually it might be better to replace this with circular array //to improve performance - this.log.shift(); + this.logs.shift(); } - this.log.push(txt); + this.logs.push(txt); } Script.prototype.displayLog = function() { - for (var i = 0; i < this.log.length; ++i) { - post(this.log[i]); + for (var i = 0; i < this.logs.length; ++i) { + post(this.logs[i]); } } diff --git a/src/Terminal.js b/src/Terminal.js index 21854a9d0..b023af2d0 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -140,8 +140,8 @@ var Terminal = { 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 NUKE: " +Player.getCurrentServer().numOpenPortsRequired); - if (Player.getCurrentServer().sshPortOpen) { + post("Required number of open ports for NUKE: " + Player.getCurrentServer().numOpenPortsRequired); + if (Player.getCurrentServer().sshPortOpen) { post("SSH port: Open") } else { post("SSH port: Closed") @@ -425,10 +425,10 @@ var Terminal = { //TODO break; case "tail": - if (commandArray.length != 1) { + if (commandArray.length != 2) { post("Incorrect number of arguments. Usage: tail [script]"); } else { - var scriptName == commandArray[1]; + var scriptName = commandArray[1]; //Can only tail script files if (scriptName.endsWith(".script") == false) { @@ -438,8 +438,9 @@ var Terminal = { //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]) { + if (scriptName == currScripts[i].filename) { currScripts[i].displayLog(); + return; } } diff --git a/src/engine.js b/src/engine.js index 553f0e031..c73c5d0ed 100644 --- a/src/engine.js +++ b/src/engine.js @@ -424,7 +424,7 @@ var Engine = { //Online var onlineTotalMoneyMade = "Total online production: $" + workerscript.scriptRef.onlineMoneyMade.toFixed(2); - var onlineTotalExpEarned = (Array(26).join(" ") + workerScript.scriptRef.onlineExpGained.toFixed(2) + " exp").replace( / /g, " "); + var onlineTotalExpEarned = (Array(26).join(" ") + workerscript.scriptRef.onlineExpGained.toFixed(2) + " exp").replace( / /g, " "); var onlineMps = workerscript.scriptRef.onlineMoneyMade / workerscript.scriptRef.onlineRunningTime; var onlineMpsText = "Online production rate: $" + onlineMps.toFixed(2) + "/second"; @@ -432,8 +432,8 @@ var Engine = { var onlineEpsText = (Array(25).join(" ") + onlineEps.toFixed(4) + " exp/second").replace( / /g, " "); //Offline - var offlineTotalMoneyMade = "Total offline production: $" + workerScript.scriptRef.offlineMoneyMade.toFixed(2); - var offlineTotalExpEarned = (Array(27).join(" ") + workerScript.scriptRef.offlineExpGained.toFixed(2) + " exp").replace( / /g, " "); + var offlineTotalMoneyMade = "Total offline production: $" + workerscript.scriptRef.offlineMoneyMade.toFixed(2); + var offlineTotalExpEarned = (Array(27).join(" ") + workerscript.scriptRef.offlineExpGained.toFixed(2) + " exp").replace( / /g, " "); var offlineMps = workerscript.scriptRef.offlineMoneyMade / workerscript.scriptRef.offlineRunningTime; var offlineMpsText = "Offline production rate: $" + offlineMps.toFixed(2) + "/second"; @@ -503,7 +503,7 @@ var Engine = { Engine.Clickables.tutorialFactionsButton.style.display = "block"; Engine.Clickables.tutorialAugmentationsButton.style.display = "block"; - Engine.CLickables.tutorialBackButton.style.display = "none"; + Engine.Clickables.tutorialBackButton.style.display = "none"; document.getElementById("tutorial-text").style.display = "none"; }, @@ -518,7 +518,7 @@ var Engine = { Engine.Clickables.tutorialFactionsButton.style.display = "none"; Engine.Clickables.tutorialAugmentationsButton.style.display = "none"; - Engine.Clickables.tutorialBackButton.style.display = "block"; + Engine.Clickables.tutorialBackButton.style.display = "inline-block"; document.getElementById("tutorial-text").style.display = "block"; document.getElementById("tutorial-text").innerHTML = text; }, @@ -577,7 +577,7 @@ var Engine = { Counters: { autoSaveCounter: 300, //Autosave every minute updateSkillLevelsCounter: 10, //Only update skill levels every 2 seconds. Might improve performance - updateDisplays: 10, //Update displays such as Active Scripts display and character display + updateDisplays: 5, //Update displays such as Active Scripts display and character display serverGrowth: 450, //Process server growth every minute and a half checkFactionInvitations: 1500, //Check whether you qualify for any faction invitations every 5 minutes }, @@ -610,7 +610,7 @@ var Engine = { Engine.displayCharacterInfo(); } - Engine.Counters.updateDisplays = 10; + Engine.Counters.updateDisplays = 5; } if (Engine.Counters.serverGrowth <= 0) { diff --git a/utils/TravelBox.js b/utils/TravelBox.js new file mode 100644 index 000000000..db977bb70 --- /dev/null +++ b/utils/TravelBox.js @@ -0,0 +1,46 @@ +/* Pop up Purchase Server Box */ +function travelBoxInit() { + if (Engine.Debug) { + console.log("Travel box Initialized"); + } + var cancelButton = document.getElementById("travel-box-cancel"); + + //Close Dialog box + cancelButton.addEventListener("click", function() { + travelBoxClose(); + return false; + }); +}; + +document.addEventListener("DOMContentLoaded", travelBoxInit, false); + +travelBoxClose = function() { + var travelBox = document.getElementById("travel-box-container"); + travelBox.style.display = "none"; +} + +travelBoxOpen = function() { + var travelBox = document.getElementById("travel-box-container"); + travelBox.style.display = "block"; +} + +travelBoxSetText = function(txt) { + var travelBoxText = document.getElementById("travel-box-text"); + travelBoxText.innerHTML = txt; +} + +travelBoxCreate = function(destCityName, cost) { + travelBoxSetText("Would you like to travel to " + destCityName + "? The trip will cost $" + cost + "."); + + //Clear old event listeners from Confirm button + var confirmButton = document.getElementById("travel-box-confirm"); + var newConfirmButton = confirmButton.cloneNode(true); + confirmButton.parentNode.replaceChild(newConfirmButton, confirmButton); + newConfirmButton.addEventListener("click", function() { + travelBoxClose(); + travelToCity(destCityName, cost); + return false; + }); + + travelBoxOpen(); +} \ No newline at end of file