2017-08-30 19:44:29 +02:00
|
|
|
import {BitNodeMultipliers} from "./BitNode.js";
|
|
|
|
import {CONSTANTS} from "./Constants.js";
|
|
|
|
import {Engine} from "./engine.js";
|
|
|
|
import {iTutorialSteps, iTutorialNextStep,
|
|
|
|
iTutorialIsRunning, currITutorialStep} from "./InteractiveTutorial.js";
|
|
|
|
import {Player} from "./Player.js";
|
|
|
|
import {dialogBoxCreate} from "../utils/DialogBox.js";
|
2018-06-05 19:19:33 +02:00
|
|
|
import {clearEventListeners, createElement,
|
|
|
|
getElementById} from "../utils/HelperFunctions.js";
|
2017-08-30 19:44:29 +02:00
|
|
|
import {Reviver, Generic_toJSON,
|
|
|
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
2018-06-22 23:30:24 +02:00
|
|
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-05 19:19:33 +02:00
|
|
|
/**
|
|
|
|
* Overwrites the inner text of the specified HTML element if it is different from what currently exists.
|
|
|
|
* @param {string} elementId The HTML ID to find the first instance of.
|
|
|
|
* @param {string} text The inner text that should be set.
|
|
|
|
*/
|
|
|
|
function updateText(elementId, text) {
|
|
|
|
var el = getElementById(elementId);
|
|
|
|
if (el.innerText != text) {
|
|
|
|
el.innerText = text;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-26 23:51:00 +02:00
|
|
|
/* HacknetNode.js */
|
2017-05-17 05:50:32 +02:00
|
|
|
function hacknetNodesInit() {
|
2018-06-05 19:19:33 +02:00
|
|
|
var performMapping = function(x) {
|
|
|
|
getElementById("hacknet-nodes-" + x.id + "-multiplier")
|
|
|
|
.addEventListener("click", function() {
|
|
|
|
hacknetNodePurchaseMultiplier = x.multiplier;
|
|
|
|
updateHacknetNodesMultiplierButtons();
|
|
|
|
updateHacknetNodesContent();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var mappings = [
|
|
|
|
{ id: "1x", multiplier: 1 },
|
|
|
|
{ id: "5x", multiplier: 5 },
|
|
|
|
{ id: "10x", multiplier: 10 },
|
|
|
|
{ id: "max", multiplier: 0 }
|
|
|
|
];
|
|
|
|
for (var elem of mappings) {
|
|
|
|
// Encapsulate in a function so that the appropriate scope is kept in the click handler.
|
|
|
|
performMapping(elem);
|
|
|
|
}
|
2017-05-17 05:50:32 +02:00
|
|
|
}
|
2018-06-05 19:19:33 +02:00
|
|
|
|
2017-05-17 05:50:32 +02:00
|
|
|
document.addEventListener("DOMContentLoaded", hacknetNodesInit, false);
|
|
|
|
|
2017-04-26 23:51:00 +02:00
|
|
|
function HacknetNode(name) {
|
|
|
|
this.level = 1;
|
2017-04-27 22:02:58 +02:00
|
|
|
this.ram = 1; //GB
|
2017-07-25 04:51:03 +02:00
|
|
|
this.cores = 1;
|
|
|
|
|
2017-04-26 23:51:00 +02:00
|
|
|
this.name = name;
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-26 23:51:00 +02:00
|
|
|
this.totalMoneyGenerated = 0;
|
|
|
|
this.onlineTimeSeconds = 0;
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-26 23:51:00 +02:00
|
|
|
this.moneyGainRatePerSecond = 0;
|
|
|
|
}
|
|
|
|
|
2018-06-03 03:37:56 +02:00
|
|
|
|
2017-04-26 23:51:00 +02:00
|
|
|
HacknetNode.prototype.updateMoneyGainRate = function() {
|
|
|
|
//How much extra $/s is gained per level
|
2017-04-27 22:02:58 +02:00
|
|
|
var gainPerLevel = CONSTANTS.HacknetNodeMoneyGainPerLevel;
|
2017-07-25 04:51:03 +02:00
|
|
|
|
|
|
|
this.moneyGainRatePerSecond = (this.level * gainPerLevel) *
|
|
|
|
Math.pow(1.035, this.ram-1) *
|
2017-08-30 19:44:29 +02:00
|
|
|
((this.cores + 5) / 6) *
|
|
|
|
Player.hacknet_node_money_mult *
|
|
|
|
BitNodeMultipliers.HacknetNodeMoney;
|
2017-04-27 22:02:58 +02:00
|
|
|
if (isNaN(this.moneyGainRatePerSecond)) {
|
2017-05-15 17:13:21 +02:00
|
|
|
this.moneyGainRatePerSecond = 0;
|
|
|
|
dialogBoxCreate("Error in calculating Hacknet Node production. Please report to game developer");
|
2017-04-27 22:02:58 +02:00
|
|
|
}
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-05-10 19:42:46 +02:00
|
|
|
updateTotalHacknetProduction();
|
2017-04-26 23:51:00 +02:00
|
|
|
}
|
|
|
|
|
2017-05-16 23:22:54 +02:00
|
|
|
HacknetNode.prototype.calculateLevelUpgradeCost = function(levels=1) {
|
2018-01-09 21:48:06 +01:00
|
|
|
levels = Math.round(levels);
|
2018-06-05 19:19:33 +02:00
|
|
|
if (isNaN(levels) || levels < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
var mult = CONSTANTS.HacknetNodeUpgradeLevelMult;
|
2017-07-25 04:51:03 +02:00
|
|
|
var totalMultiplier = 0; //Summed
|
2017-05-16 23:22:54 +02:00
|
|
|
var currLevel = this.level;
|
|
|
|
for (var i = 0; i < levels; ++i) {
|
2017-07-25 04:51:03 +02:00
|
|
|
totalMultiplier += Math.pow(mult, currLevel);
|
2017-05-16 23:22:54 +02:00
|
|
|
++currLevel;
|
|
|
|
}
|
2018-06-05 19:19:33 +02:00
|
|
|
|
2017-05-16 23:22:54 +02:00
|
|
|
return CONSTANTS.BaseCostForHacknetNode / 2 * totalMultiplier * Player.hacknet_node_level_cost_mult;
|
2017-04-26 23:51:00 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 04:51:03 +02:00
|
|
|
//Wrapper function for Netscript
|
|
|
|
HacknetNode.prototype.getLevelUpgradeCost = function(levels=1) {
|
|
|
|
return this.calculateLevelUpgradeCost(levels);
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:22:54 +02:00
|
|
|
HacknetNode.prototype.purchaseLevelUpgrade = function(levels=1) {
|
2017-12-03 05:19:49 +01:00
|
|
|
levels = Math.round(levels);
|
2017-05-16 23:22:54 +02:00
|
|
|
var cost = this.calculateLevelUpgradeCost(levels);
|
2018-06-05 19:19:33 +02:00
|
|
|
if (isNaN(cost) || levels < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-01 20:55:23 +02:00
|
|
|
if (this.level + levels > CONSTANTS.HacknetNodeMaxLevel) {
|
2017-05-31 02:00:24 +02:00
|
|
|
var diff = Math.max(0, CONSTANTS.HacknetNodeMaxLevel - this.level);
|
|
|
|
return this.purchaseLevelUpgrade(diff);
|
2017-05-16 23:22:54 +02:00
|
|
|
}
|
2018-06-05 19:19:33 +02:00
|
|
|
|
|
|
|
if (Player.money.lt(cost)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-11 00:44:33 +02:00
|
|
|
Player.loseMoney(cost);
|
2017-05-16 23:22:54 +02:00
|
|
|
this.level += levels;
|
2017-04-27 22:02:58 +02:00
|
|
|
this.updateMoneyGainRate();
|
2017-05-25 14:18:34 +02:00
|
|
|
return true;
|
2017-04-27 07:01:21 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 18:54:29 +02:00
|
|
|
//Wrapper function for Netscript
|
|
|
|
HacknetNode.prototype.upgradeLevel = function(levels=1) {
|
2018-06-06 18:02:21 +02:00
|
|
|
let res = this.purchaseLevelUpgrade(levels);
|
|
|
|
createPlayerHacknetNodeWrappers();
|
|
|
|
return res;
|
2017-07-13 18:54:29 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 07:01:21 +02:00
|
|
|
HacknetNode.prototype.calculateRamUpgradeCost = function() {
|
|
|
|
var numUpgrades = Math.log2(this.ram);
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 07:01:21 +02:00
|
|
|
//Calculate cost
|
2017-05-16 23:22:54 +02:00
|
|
|
//Base cost of RAM is 50k per 1GB, increased by some multiplier for each time RAM is upgraded
|
|
|
|
var baseCost = this.ram * CONSTANTS.BaseCostFor1GBOfRamHacknetNode;
|
2017-04-27 22:02:58 +02:00
|
|
|
var mult = Math.pow(CONSTANTS.HacknetNodeUpgradeRamMult, numUpgrades);
|
2017-05-16 23:22:54 +02:00
|
|
|
return baseCost * mult * Player.hacknet_node_ram_cost_mult;
|
2017-04-27 07:01:21 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 04:51:03 +02:00
|
|
|
//Wrapper function for Netscript
|
|
|
|
HacknetNode.prototype.getRamUpgradeCost = function() {
|
|
|
|
return this.calculateRamUpgradeCost();
|
|
|
|
}
|
|
|
|
|
2017-04-27 07:01:21 +02:00
|
|
|
HacknetNode.prototype.purchaseRamUpgrade = function() {
|
|
|
|
var cost = this.calculateRamUpgradeCost();
|
2018-06-05 19:19:33 +02:00
|
|
|
if (isNaN(cost)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Player.money.lt(cost)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.ram >= CONSTANTS.HacknetNodeMaxRam) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-30 22:34:29 +02:00
|
|
|
Player.loseMoney(cost);
|
2017-04-27 07:01:21 +02:00
|
|
|
this.ram *= 2; //Ram is always doubled
|
2017-04-27 22:02:58 +02:00
|
|
|
this.updateMoneyGainRate();
|
2017-05-25 14:18:34 +02:00
|
|
|
return true;
|
2017-04-27 07:01:21 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 18:54:29 +02:00
|
|
|
//Wrapper function for Netscript
|
|
|
|
HacknetNode.prototype.upgradeRam = function() {
|
2018-06-06 18:02:21 +02:00
|
|
|
let res = this.purchaseRamUpgrade();
|
|
|
|
createPlayerHacknetNodeWrappers();
|
|
|
|
return res;
|
2017-07-13 18:54:29 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 07:01:21 +02:00
|
|
|
HacknetNode.prototype.calculateCoreUpgradeCost = function() {
|
2017-04-27 22:02:58 +02:00
|
|
|
var coreBaseCost = CONSTANTS.BaseCostForHacknetNodeCore;
|
|
|
|
var mult = CONSTANTS.HacknetNodeUpgradeCoreMult;
|
2018-06-05 19:19:33 +02:00
|
|
|
return coreBaseCost * Math.pow(mult, this.cores - 1) * Player.hacknet_node_core_cost_mult;
|
2017-04-27 07:01:21 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 04:51:03 +02:00
|
|
|
//Wrapper function for Netscript
|
|
|
|
HacknetNode.prototype.getCoreUpgradeCost = function() {
|
2018-06-06 18:02:21 +02:00
|
|
|
let res = this.calculateCoreUpgradeCost();
|
|
|
|
createPlayerHacknetNodeWrappers();
|
|
|
|
return res;
|
2017-07-25 04:51:03 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 07:01:21 +02:00
|
|
|
HacknetNode.prototype.purchaseCoreUpgrade = function() {
|
|
|
|
var cost = this.calculateCoreUpgradeCost();
|
2018-06-05 19:19:33 +02:00
|
|
|
if (isNaN(cost)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Player.money.lt(cost)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.cores >= CONSTANTS.HacknetNodeMaxCores) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-30 22:34:29 +02:00
|
|
|
Player.loseMoney(cost);
|
2017-07-13 18:54:29 +02:00
|
|
|
++this.cores;
|
2017-04-27 22:02:58 +02:00
|
|
|
this.updateMoneyGainRate();
|
2017-05-25 14:18:34 +02:00
|
|
|
return true;
|
2017-04-27 07:01:21 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 18:54:29 +02:00
|
|
|
//Wrapper function for Netscript
|
|
|
|
HacknetNode.prototype.upgradeCore = function() {
|
|
|
|
return this.purchaseCoreUpgrade();
|
|
|
|
}
|
|
|
|
|
2017-04-26 23:51:00 +02:00
|
|
|
/* Saving and loading HackNets */
|
|
|
|
HacknetNode.prototype.toJSON = function() {
|
|
|
|
return Generic_toJSON("HacknetNode", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
HacknetNode.fromJSON = function(value) {
|
|
|
|
return Generic_fromJSON(HacknetNode, value.data);
|
|
|
|
}
|
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
Reviver.constructors.HacknetNode = HacknetNode;
|
|
|
|
|
2018-06-03 03:37:56 +02:00
|
|
|
var HacknetNodeWrapper = function(hacknetNodeObj) {
|
|
|
|
var _node = hacknetNodeObj;
|
|
|
|
return {
|
|
|
|
name : _node.name,
|
|
|
|
level : _node.level,
|
|
|
|
ram : _node.ram,
|
|
|
|
cores : _node.cores,
|
|
|
|
totalMoneyGenerated : _node.totalMoneyGenerated,
|
|
|
|
onlineTimeSeconds : _node.onlineTimeSeconds,
|
|
|
|
moneyGainRatePerSecond : _node.moneyGainRatePerSecond,
|
|
|
|
upgradeLevel : function(n) {
|
|
|
|
return _node.upgradeLevel(n);
|
|
|
|
},
|
|
|
|
upgradeRam : function() {
|
|
|
|
return _node.upgradeRam();
|
|
|
|
},
|
|
|
|
upgradeCore : function() {
|
|
|
|
return _node.upgradeCore();
|
|
|
|
},
|
|
|
|
getLevelUpgradeCost : function(n) {
|
|
|
|
return _node.getLevelUpgradeCost(n);
|
|
|
|
},
|
|
|
|
getRamUpgradeCost : function() {
|
|
|
|
return _node.getRamUpgradeCost();
|
|
|
|
},
|
|
|
|
getCoreUpgradeCost : function() {
|
|
|
|
return _node.getCoreUpgradeCost();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 22:02:58 +02:00
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function purchaseHacknet() {
|
2017-05-06 08:24:01 +02:00
|
|
|
/* INTERACTIVE TUTORIAL */
|
|
|
|
if (iTutorialIsRunning) {
|
|
|
|
if (currITutorialStep == iTutorialSteps.HacknetNodesIntroduction) {
|
|
|
|
iTutorialNextStep();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-05-06 08:24:01 +02:00
|
|
|
/* END INTERACTIVE TUTORIAL */
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
var cost = getCostOfNextHacknetNode();
|
2018-06-05 19:19:33 +02:00
|
|
|
if (isNaN(cost)) {
|
|
|
|
throw new Error("Cost is NaN");
|
|
|
|
}
|
|
|
|
|
2017-07-29 18:25:40 +02:00
|
|
|
if (Player.money.lt(cost)) {
|
2017-07-13 18:54:29 +02:00
|
|
|
//dialogBoxCreate("You cannot afford to purchase a Hacknet Node!");
|
|
|
|
return false;
|
2017-04-27 22:02:58 +02:00
|
|
|
}
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
//Auto generate a name for the node for now...TODO
|
|
|
|
var numOwned = Player.hacknetNodes.length;
|
|
|
|
var name = "hacknet-node-" + numOwned;
|
|
|
|
var node = new HacknetNode(name);
|
|
|
|
node.updateMoneyGainRate();
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
Player.loseMoney(cost);
|
|
|
|
Player.hacknetNodes.push(node);
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2018-06-06 18:02:21 +02:00
|
|
|
if (Engine.currentPage === Engine.Page.HacknetNodes) {
|
|
|
|
displayHacknetNodesContent();
|
|
|
|
}
|
|
|
|
createPlayerHacknetNodeWrappers();
|
2017-05-10 19:42:46 +02:00
|
|
|
updateTotalHacknetProduction();
|
2017-07-13 18:54:29 +02:00
|
|
|
return numOwned;
|
2017-05-10 19:42:46 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 04:51:03 +02:00
|
|
|
//Calculates the total production from all HacknetNodes
|
2017-08-30 19:44:29 +02:00
|
|
|
function updateTotalHacknetProduction() {
|
2017-05-10 19:42:46 +02:00
|
|
|
var total = 0;
|
|
|
|
for (var i = 0; i < Player.hacknetNodes.length; ++i) {
|
|
|
|
total += Player.hacknetNodes[i].moneyGainRatePerSecond;
|
|
|
|
}
|
|
|
|
Player.totalHacknetNodeProduction = total;
|
2017-04-27 22:02:58 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function getCostOfNextHacknetNode() {
|
2017-04-27 22:02:58 +02:00
|
|
|
//Cost increases exponentially based on how many you own
|
|
|
|
var numOwned = Player.hacknetNodes.length;
|
|
|
|
var mult = CONSTANTS.HacknetNodePurchaseNextMult;
|
2017-05-03 06:38:58 +02:00
|
|
|
return CONSTANTS.BaseCostForHacknetNode * Math.pow(mult, numOwned) * Player.hacknet_node_purchase_cost_mult;
|
2017-04-27 22:02:58 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 05:50:32 +02:00
|
|
|
var hacknetNodePurchaseMultiplier = 1;
|
2017-08-30 19:44:29 +02:00
|
|
|
function updateHacknetNodesMultiplierButtons() {
|
2017-05-17 05:50:32 +02:00
|
|
|
var mult1x = document.getElementById("hacknet-nodes-1x-multiplier");
|
|
|
|
var mult5x = document.getElementById("hacknet-nodes-5x-multiplier");
|
|
|
|
var mult10x = document.getElementById("hacknet-nodes-10x-multiplier");
|
|
|
|
var multMax = document.getElementById("hacknet-nodes-max-multiplier");
|
|
|
|
mult1x.setAttribute("class", "a-link-button");
|
|
|
|
mult5x.setAttribute("class", "a-link-button");
|
|
|
|
mult10x.setAttribute("class", "a-link-button");
|
|
|
|
multMax.setAttribute("class", "a-link-button");
|
|
|
|
if (Player.hacknetNodes.length == 0) {
|
|
|
|
mult1x.setAttribute("class", "a-link-button-inactive");
|
|
|
|
mult5x.setAttribute("class", "a-link-button-inactive");
|
|
|
|
mult10x.setAttribute("class", "a-link-button-inactive");
|
|
|
|
multMax.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else if (hacknetNodePurchaseMultiplier == 1) {
|
|
|
|
mult1x.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else if (hacknetNodePurchaseMultiplier == 5) {
|
|
|
|
mult5x.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else if (hacknetNodePurchaseMultiplier == 10) {
|
|
|
|
mult10x.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else {
|
|
|
|
multMax.setAttribute("class", "a-link-button-inactive");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Calculate the maximum number of times the Player can afford to upgrade
|
|
|
|
//a Hacknet Node's level"
|
2017-08-30 19:44:29 +02:00
|
|
|
function getMaxNumberLevelUpgrades(nodeObj) {
|
2018-06-05 19:19:33 +02:00
|
|
|
if (Player.money.lt(nodeObj.calculateLevelUpgradeCost(1))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-17 05:50:32 +02:00
|
|
|
var min = 1;
|
2018-06-05 19:19:33 +02:00
|
|
|
var max = CONSTANTS.HacknetNodeMaxLevel - 1;
|
2017-05-30 22:54:26 +02:00
|
|
|
var levelsToMax = CONSTANTS.HacknetNodeMaxLevel - nodeObj.level;
|
2017-07-29 18:25:40 +02:00
|
|
|
if (Player.money.gt(nodeObj.calculateLevelUpgradeCost(levelsToMax))) {
|
2017-07-05 18:49:01 +02:00
|
|
|
return levelsToMax;
|
|
|
|
}
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-05-17 05:50:32 +02:00
|
|
|
while (min <= max) {
|
|
|
|
var curr = (min + max) / 2 | 0;
|
2017-07-25 04:51:03 +02:00
|
|
|
if (curr != CONSTANTS.HacknetNodeMaxLevel &&
|
2017-07-29 18:25:40 +02:00
|
|
|
Player.money.gt(nodeObj.calculateLevelUpgradeCost(curr)) &&
|
2018-06-05 19:19:33 +02:00
|
|
|
Player.money.lt(nodeObj.calculateLevelUpgradeCost(curr + 1))) {
|
2017-05-30 22:54:26 +02:00
|
|
|
return Math.min(levelsToMax, curr);
|
2017-07-29 18:25:40 +02:00
|
|
|
} else if (Player.money.lt(nodeObj.calculateLevelUpgradeCost(curr))) {
|
2017-05-17 05:50:32 +02:00
|
|
|
max = curr - 1;
|
2017-07-29 18:25:40 +02:00
|
|
|
} else if (Player.money.gt(nodeObj.calculateLevelUpgradeCost(curr))) {
|
2017-05-17 05:50:32 +02:00
|
|
|
min = curr + 1;
|
|
|
|
} else {
|
2017-05-30 22:54:26 +02:00
|
|
|
return Math.min(levelsToMax, curr);
|
2017-05-17 05:50:32 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-16 23:22:54 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
//Creates Hacknet Node DOM elements when the page is opened
|
2017-08-30 19:44:29 +02:00
|
|
|
function displayHacknetNodesContent() {
|
2017-04-27 22:02:58 +02:00
|
|
|
//Update Hacknet Nodes button
|
2017-05-06 08:24:01 +02:00
|
|
|
var newPurchaseButton = clearEventListeners("hacknet-nodes-purchase-button");
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
newPurchaseButton.addEventListener("click", function() {
|
|
|
|
purchaseHacknet();
|
|
|
|
return false;
|
|
|
|
});
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-05-16 23:22:54 +02:00
|
|
|
//Handle Purchase multiplier buttons
|
2017-05-17 05:50:32 +02:00
|
|
|
updateHacknetNodesMultiplierButtons();
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
//Remove all old hacknet Node DOM elements
|
|
|
|
var hacknetNodesList = document.getElementById("hacknet-nodes-list");
|
2018-06-05 19:19:33 +02:00
|
|
|
while (hacknetNodesList.firstChild) {
|
2017-04-27 22:02:58 +02:00
|
|
|
hacknetNodesList.removeChild(hacknetNodesList.firstChild);
|
|
|
|
}
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
//Then re-create them
|
|
|
|
for (var i = 0; i < Player.hacknetNodes.length; ++i) {
|
|
|
|
createHacknetNodeDomElement(Player.hacknetNodes[i]);
|
|
|
|
}
|
2018-06-05 19:19:33 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
updateHacknetNodesContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Update information on all Hacknet Node DOM elements
|
2017-08-30 19:44:29 +02:00
|
|
|
function updateHacknetNodesContent() {
|
2017-04-27 22:02:58 +02:00
|
|
|
//Set purchase button to inactive if not enough money, and update its price display
|
|
|
|
var cost = getCostOfNextHacknetNode();
|
2018-06-05 19:19:33 +02:00
|
|
|
var purchaseButton = getElementById("hacknet-nodes-purchase-button");
|
|
|
|
var formattedCost = formatNumber(cost, 2);
|
|
|
|
|
|
|
|
updateText("hacknet-nodes-purchase-button", "Purchase Hacknet Node - $" + formattedCost);
|
|
|
|
|
2017-07-29 18:25:40 +02:00
|
|
|
if (Player.money.lt(cost)) {
|
2017-04-27 22:02:58 +02:00
|
|
|
purchaseButton.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else {
|
|
|
|
purchaseButton.setAttribute("class", "a-link-button");
|
|
|
|
}
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
//Update player's money
|
2018-06-05 19:19:33 +02:00
|
|
|
updateText("hacknet-nodes-player-money", "$" + formatNumber(Player.money.toNumber(), 2));
|
|
|
|
updateText("hacknet-nodes-total-production", "$" + formatNumber(Player.totalHacknetNodeProduction, 2) + " / second");
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
//Update information in each owned hacknet node
|
|
|
|
for (var i = 0; i < Player.hacknetNodes.length; ++i) {
|
|
|
|
updateHacknetNodeDomElement(Player.hacknetNodes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Creates a single Hacknet Node DOM element
|
2017-08-30 19:44:29 +02:00
|
|
|
function createHacknetNodeDomElement(nodeObj) {
|
2017-04-27 22:02:58 +02:00
|
|
|
var nodeName = nodeObj.name;
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2018-06-05 19:19:33 +02:00
|
|
|
var nodeLevelContainer = createElement("div", {
|
|
|
|
class: "hacknet-node-level-container row",
|
|
|
|
innerHTML: "<p>Level:</p><span class=\"text upgradable-info\" id=\"hacknet-node-level-" + nodeName + "\"></span>"
|
|
|
|
});
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2018-06-05 19:19:33 +02:00
|
|
|
var nodeRamContainer = createElement("div", {
|
|
|
|
class: "hacknet-node-ram-container row",
|
|
|
|
innerHTML: "<p>RAM:</p><span class=\"text upgradable-info\" id=\"hacknet-node-ram-" + nodeName + "\"></span>"
|
|
|
|
});
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2018-06-05 19:19:33 +02:00
|
|
|
var nodeCoresContainer = createElement("div", {
|
|
|
|
class: "hacknet-node-cores-container row",
|
|
|
|
innerHTML: "<p>Cores:</p><span class=\"text upgradable-info\" id=\"hacknet-node-cores-" + nodeName + "\"><span>"
|
|
|
|
})
|
|
|
|
var containingDiv = createElement("div", {
|
|
|
|
class: "hacknet-node-container",
|
|
|
|
innerHTML: "<div class=\"hacknet-node-name-container row\">" +
|
|
|
|
"<p>Node name:</p>" +
|
|
|
|
"<span class=\"text\" id=\"hacknet-node-name-" + nodeName + "\"></span>" +
|
|
|
|
"</div>" +
|
|
|
|
"<div class=\"hacknet-node-production-container row\">" +
|
|
|
|
"<p>Production:</p>" +
|
|
|
|
"<span class=\"text\" id=\"hacknet-node-total-production-" + nodeName + "\"></span>" +
|
|
|
|
"<span class=\"text\" id=\"hacknet-node-production-rate-" + nodeName + "\"></span>" +
|
|
|
|
"</div>"
|
|
|
|
});
|
|
|
|
containingDiv.appendChild(nodeLevelContainer);
|
|
|
|
containingDiv.appendChild(nodeRamContainer);
|
|
|
|
containingDiv.appendChild(nodeCoresContainer);
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2018-06-05 19:19:33 +02:00
|
|
|
var listItem = createElement("li", {
|
|
|
|
class: "hacknet-node"
|
|
|
|
});
|
|
|
|
listItem.appendChild(containingDiv);
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 07:01:21 +02:00
|
|
|
//Upgrade buttons
|
2018-06-05 19:19:33 +02:00
|
|
|
nodeLevelContainer.appendChild(createElement("a", {
|
|
|
|
id: "hacknet-node-upgrade-level-" + nodeName,
|
|
|
|
class: "a-link-button-inactive",
|
|
|
|
clickListener: function() {
|
|
|
|
var numUpgrades = hacknetNodePurchaseMultiplier;
|
|
|
|
if (hacknetNodePurchaseMultiplier == 0) {
|
|
|
|
numUpgrades = getMaxNumberLevelUpgrades(nodeObj);
|
|
|
|
}
|
|
|
|
nodeObj.purchaseLevelUpgrade(numUpgrades);
|
|
|
|
updateHacknetNodesContent();
|
|
|
|
return false;
|
2017-05-17 05:50:32 +02:00
|
|
|
}
|
2018-06-05 19:19:33 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
nodeRamContainer.appendChild(createElement("a", {
|
|
|
|
id: "hacknet-node-upgrade-ram-" + nodeName,
|
|
|
|
class: "a-link-button-inactive",
|
|
|
|
clickListener: function() {
|
|
|
|
nodeObj.purchaseRamUpgrade();
|
|
|
|
updateHacknetNodesContent();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
nodeCoresContainer.appendChild(createElement("a", {
|
|
|
|
id: "hacknet-node-upgrade-core-" + nodeName,
|
|
|
|
class: "a-link-button-inactive",
|
|
|
|
clickListener: function() {
|
|
|
|
nodeObj.purchaseCoreUpgrade();
|
|
|
|
updateHacknetNodesContent();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}));
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
document.getElementById("hacknet-nodes-list").appendChild(listItem);
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
//Set the text and stuff inside the DOM element
|
|
|
|
updateHacknetNodeDomElement(nodeObj);
|
2017-04-27 07:01:21 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
//Updates information on a single hacknet node DOM element
|
2017-08-30 19:44:29 +02:00
|
|
|
function updateHacknetNodeDomElement(nodeObj) {
|
2017-04-27 07:01:21 +02:00
|
|
|
var nodeName = nodeObj.name;
|
2018-06-05 19:19:33 +02:00
|
|
|
|
|
|
|
updateText("hacknet-node-name-" + nodeName, nodeName);
|
|
|
|
updateText("hacknet-node-total-production-" + nodeName, "$" + formatNumber(nodeObj.totalMoneyGenerated, 2));
|
|
|
|
updateText("hacknet-node-production-rate-" + nodeName, "($" + formatNumber(nodeObj.moneyGainRatePerSecond, 2) + " / second)");
|
|
|
|
updateText("hacknet-node-level-" + nodeName, nodeObj.level);
|
|
|
|
updateText("hacknet-node-ram-" + nodeName, nodeObj.ram + "GB");
|
|
|
|
updateText("hacknet-node-cores-" + nodeName, nodeObj.cores);
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-05-16 00:22:17 +02:00
|
|
|
//Upgrade level
|
2018-06-05 19:19:33 +02:00
|
|
|
var upgradeLevelButton = getElementById("hacknet-node-upgrade-level-" + nodeName);
|
|
|
|
|
2017-05-16 00:22:17 +02:00
|
|
|
if (nodeObj.level >= CONSTANTS.HacknetNodeMaxLevel) {
|
2018-06-05 19:19:33 +02:00
|
|
|
updateText("hacknet-node-upgrade-level-" + nodeName, "MAX LEVEL");
|
2017-04-27 22:02:58 +02:00
|
|
|
upgradeLevelButton.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else {
|
2017-05-17 05:50:32 +02:00
|
|
|
var multiplier = 0;
|
|
|
|
if (hacknetNodePurchaseMultiplier == 0) {
|
|
|
|
//Max
|
|
|
|
multiplier = getMaxNumberLevelUpgrades(nodeObj);
|
|
|
|
} else {
|
2017-05-31 02:00:24 +02:00
|
|
|
var levelsToMax = CONSTANTS.HacknetNodeMaxLevel - nodeObj.level;
|
|
|
|
multiplier = Math.min(levelsToMax, hacknetNodePurchaseMultiplier);
|
2017-05-17 05:50:32 +02:00
|
|
|
}
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-05-17 05:50:32 +02:00
|
|
|
var upgradeLevelCost = nodeObj.calculateLevelUpgradeCost(multiplier);
|
2018-06-05 19:19:33 +02:00
|
|
|
updateText("hacknet-node-upgrade-level-" + nodeName, "Upgrade x" + multiplier + " - $" + formatNumber(upgradeLevelCost, 2))
|
2017-07-29 18:25:40 +02:00
|
|
|
if (Player.money.lt(upgradeLevelCost)) {
|
2017-05-16 00:22:17 +02:00
|
|
|
upgradeLevelButton.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else {
|
|
|
|
upgradeLevelButton.setAttribute("class", "a-link-button");
|
|
|
|
}
|
2017-04-27 22:02:58 +02:00
|
|
|
}
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-05-16 00:22:17 +02:00
|
|
|
//Upgrade RAM
|
2018-06-05 19:19:33 +02:00
|
|
|
var upgradeRamButton = getElementById("hacknet-node-upgrade-ram-" + nodeName);
|
|
|
|
|
2017-05-16 00:22:17 +02:00
|
|
|
if (nodeObj.ram >= CONSTANTS.HacknetNodeMaxRam) {
|
2018-06-05 19:19:33 +02:00
|
|
|
updateText("hacknet-node-upgrade-ram-" + nodeName, "MAX RAM");
|
2017-04-27 22:02:58 +02:00
|
|
|
upgradeRamButton.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else {
|
2017-05-16 00:22:17 +02:00
|
|
|
var upgradeRamCost = nodeObj.calculateRamUpgradeCost();
|
2018-06-05 19:19:33 +02:00
|
|
|
updateText("hacknet-node-upgrade-ram-" + nodeName, "Upgrade - $" + formatNumber(upgradeRamCost, 2));
|
2017-07-29 18:25:40 +02:00
|
|
|
if (Player.money.lt(upgradeRamCost)) {
|
2017-05-16 00:22:17 +02:00
|
|
|
upgradeRamButton.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else {
|
|
|
|
upgradeRamButton.setAttribute("class", "a-link-button");
|
|
|
|
}
|
2017-04-27 22:02:58 +02:00
|
|
|
}
|
2017-07-25 04:51:03 +02:00
|
|
|
|
2017-05-16 00:22:17 +02:00
|
|
|
//Upgrade Cores
|
2018-06-05 19:19:33 +02:00
|
|
|
var upgradeCoreButton = getElementById("hacknet-node-upgrade-core-" + nodeName);
|
|
|
|
|
2017-07-13 18:54:29 +02:00
|
|
|
if (nodeObj.cores >= CONSTANTS.HacknetNodeMaxCores) {
|
2018-06-05 19:19:33 +02:00
|
|
|
updateText("hacknet-node-upgrade-core-" + nodeName, "MAX CORES");
|
2017-04-27 22:02:58 +02:00
|
|
|
upgradeCoreButton.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else {
|
2017-05-16 00:22:17 +02:00
|
|
|
var upgradeCoreCost = nodeObj.calculateCoreUpgradeCost();
|
2018-06-05 19:19:33 +02:00
|
|
|
updateText("hacknet-node-upgrade-core-" + nodeName, "Upgrade - $" + formatNumber(upgradeCoreCost, 2));
|
2017-07-29 18:25:40 +02:00
|
|
|
if (Player.money.lt(upgradeCoreCost)) {
|
2017-05-16 00:22:17 +02:00
|
|
|
upgradeCoreButton.setAttribute("class", "a-link-button-inactive");
|
|
|
|
} else {
|
|
|
|
upgradeCoreButton.setAttribute("class", "a-link-button");
|
|
|
|
}
|
2017-04-27 22:02:58 +02:00
|
|
|
}
|
2017-04-27 07:01:21 +02:00
|
|
|
}
|
2017-04-26 23:51:00 +02:00
|
|
|
|
2018-06-03 03:37:56 +02:00
|
|
|
function createPlayerHacknetNodeWrappers() {
|
|
|
|
Player.hacknetNodeWrappers.length = Player.hacknetNodes.length;
|
|
|
|
for (var i = 0; i < Player.hacknetNodes.length; ++i) {
|
|
|
|
Player.hacknetNodeWrappers[i] = new HacknetNodeWrapper(Player.hacknetNodes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updatePlayerHacknetNodeWrappers() {
|
|
|
|
if (Player.hacknetNodeWrappers.length !== Player.hacknetNodes.length) {
|
|
|
|
return createPlayerHacknetNodeWrappers();
|
|
|
|
}
|
2018-06-05 19:19:33 +02:00
|
|
|
|
2018-06-03 03:37:56 +02:00
|
|
|
for (var i = 0; i < Player.hacknetNodeWrappers.length; ++i) {
|
2018-06-05 19:19:33 +02:00
|
|
|
if (!(Player.hacknetNodeWrappers[i] instanceof HacknetNodeWrapper)) {
|
|
|
|
return createPlayerHacknetNodeWrappers();
|
|
|
|
}
|
|
|
|
|
2018-06-03 03:37:56 +02:00
|
|
|
Player.hacknetNodeWrappers[i].level = Player.hacknetNodes[i].level;
|
|
|
|
Player.hacknetNodeWrappers[i].ram = Player.hacknetNodes[i].ram;
|
|
|
|
Player.hacknetNodeWrappers[i].cores = Player.hacknetNodes[i].cores;
|
|
|
|
Player.hacknetNodeWrappers[i].totalMoneyGenerated = Player.hacknetNodes[i].totalMoneyGenerated;
|
|
|
|
Player.hacknetNodeWrappers[i].onlineTimeSeconds = Player.hacknetNodes[i].onlineTimeSeconds;
|
|
|
|
Player.hacknetNodeWrappers[i].moneyGainRatePerSecond = Player.hacknetNodes[i].moneyGainRatePerSecond;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function processAllHacknetNodeEarnings(numCycles) {
|
2017-05-20 11:27:42 +02:00
|
|
|
var total = 0;
|
2018-06-03 03:37:56 +02:00
|
|
|
updatePlayerHacknetNodeWrappers();
|
2017-04-27 22:02:58 +02:00
|
|
|
for (var i = 0; i < Player.hacknetNodes.length; ++i) {
|
2017-05-20 11:27:42 +02:00
|
|
|
total += processSingleHacknetNodeEarnings(numCycles, Player.hacknetNodes[i]);
|
2017-04-27 22:02:58 +02:00
|
|
|
}
|
2018-06-05 19:19:33 +02:00
|
|
|
|
2017-05-20 11:27:42 +02:00
|
|
|
return total;
|
2017-04-27 07:01:21 +02:00
|
|
|
}
|
2017-04-27 22:02:58 +02:00
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function processSingleHacknetNodeEarnings(numCycles, nodeObj) {
|
2017-04-27 22:02:58 +02:00
|
|
|
var cyclesPerSecond = 1000 / Engine._idleSpeed;
|
|
|
|
var earningPerCycle = nodeObj.moneyGainRatePerSecond / cyclesPerSecond;
|
2018-01-10 16:50:31 +01:00
|
|
|
if (isNaN(earningPerCycle)) {
|
2018-06-05 19:19:33 +02:00
|
|
|
console.error("Hacknet Node '" + nodeObj.name + "' Calculated earnings is NaN");
|
2018-01-10 16:50:31 +01:00
|
|
|
earningPerCycle = 0;
|
|
|
|
}
|
2018-06-05 19:19:33 +02:00
|
|
|
|
2017-04-27 22:02:58 +02:00
|
|
|
var totalEarnings = numCycles * earningPerCycle;
|
|
|
|
nodeObj.totalMoneyGenerated += totalEarnings;
|
|
|
|
nodeObj.onlineTimeSeconds += (numCycles * (Engine._idleSpeed / 1000));
|
|
|
|
Player.gainMoney(totalEarnings);
|
2017-05-20 11:27:42 +02:00
|
|
|
return totalEarnings;
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function getHacknetNode(name) {
|
2017-05-20 11:27:42 +02:00
|
|
|
for (var i = 0; i < Player.hacknetNodes.length; ++i) {
|
|
|
|
if (Player.hacknetNodes[i].name == name) {
|
2017-05-23 19:49:26 +02:00
|
|
|
return Player.hacknetNodes[i];
|
2017-05-20 11:27:42 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-05 19:19:33 +02:00
|
|
|
|
2017-05-20 11:27:42 +02:00
|
|
|
return null;
|
2017-07-25 04:51:03 +02:00
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-05 19:19:33 +02:00
|
|
|
export {
|
|
|
|
HacknetNode,
|
|
|
|
createPlayerHacknetNodeWrappers,
|
|
|
|
displayHacknetNodesContent,
|
|
|
|
getCostOfNextHacknetNode,
|
|
|
|
getHacknetNode,
|
|
|
|
getMaxNumberLevelUpgrades,
|
|
|
|
hacknetNodesInit,
|
|
|
|
processAllHacknetNodeEarnings,
|
|
|
|
purchaseHacknet,
|
|
|
|
updateHacknetNodesContent,
|
|
|
|
updateHacknetNodesMultiplierButtons,
|
|
|
|
updateTotalHacknetProduction
|
|
|
|
};
|