2021-10-05 04:25:21 +02:00
|
|
|
import { INetscriptHelper } from "./INetscriptHelper";
|
|
|
|
import { IPlayer } from "../PersonObjects/IPlayer";
|
|
|
|
import { WorkerScript } from "../Netscript/WorkerScript";
|
|
|
|
import { HacknetServerConstants } from "../Hacknet/data/Constants";
|
|
|
|
import {
|
|
|
|
getCostOfNextHacknetNode,
|
|
|
|
getCostOfNextHacknetServer,
|
|
|
|
hasHacknetServers,
|
|
|
|
purchaseHacknet,
|
|
|
|
purchaseLevelUpgrade,
|
|
|
|
purchaseRamUpgrade,
|
|
|
|
purchaseCoreUpgrade,
|
|
|
|
purchaseCacheUpgrade,
|
|
|
|
purchaseHashUpgrade,
|
|
|
|
updateHashManagerCapacity,
|
|
|
|
} from "../Hacknet/HacknetHelpers";
|
|
|
|
import { HacknetServer } from "../Hacknet/HacknetServer";
|
|
|
|
import { HacknetNode } from "../Hacknet/HacknetNode";
|
2021-10-07 22:56:01 +02:00
|
|
|
import { GetServer } from "../Server/AllServers";
|
2021-10-05 04:25:21 +02:00
|
|
|
|
2021-11-04 01:19:52 +01:00
|
|
|
import { Hacknet as IHacknet, NodeStats } from "../ScriptEditor/NetscriptDefinitions";
|
2021-10-05 04:25:21 +02:00
|
|
|
|
2021-11-03 23:16:10 +01:00
|
|
|
export function NetscriptHacknet(player: IPlayer, workerScript: WorkerScript, helper: INetscriptHelper): IHacknet {
|
2021-10-05 04:25:21 +02:00
|
|
|
// Utility function to get Hacknet Node object
|
|
|
|
const getHacknetNode = function (i: any, callingFn = ""): HacknetNode | HacknetServer {
|
|
|
|
if (isNaN(i)) {
|
|
|
|
throw helper.makeRuntimeErrorMsg(callingFn, "Invalid index specified for Hacknet Node: " + i);
|
|
|
|
}
|
|
|
|
if (i < 0 || i >= player.hacknetNodes.length) {
|
|
|
|
throw helper.makeRuntimeErrorMsg(callingFn, "Index specified for Hacknet Node is out-of-bounds: " + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasHacknetServers(player)) {
|
|
|
|
const hi = player.hacknetNodes[i];
|
|
|
|
if (typeof hi !== "string") throw new Error("hacknet node was not a string");
|
2021-10-07 22:56:01 +02:00
|
|
|
const hserver = GetServer(hi);
|
2021-10-05 04:25:21 +02:00
|
|
|
if (!(hserver instanceof HacknetServer)) throw new Error("hacknet server was not actually hacknet server");
|
|
|
|
if (hserver == null) {
|
|
|
|
throw helper.makeRuntimeErrorMsg(
|
|
|
|
callingFn,
|
|
|
|
`Could not get Hacknet Server for index ${i}. This is probably a bug, please report to game dev`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hserver;
|
|
|
|
} else {
|
|
|
|
const node = player.hacknetNodes[i];
|
|
|
|
if (!(node instanceof HacknetNode)) throw new Error("hacknet node was not node.");
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2021-11-04 01:19:52 +01:00
|
|
|
numNodes: function (): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
return player.hacknetNodes.length;
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
maxNumNodes: function (): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (hasHacknetServers(player)) {
|
|
|
|
return HacknetServerConstants.MaxServers;
|
|
|
|
}
|
|
|
|
return Infinity;
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
purchaseNode: function (): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
return purchaseHacknet(player);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
getPurchaseNodeCost: function (): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (hasHacknetServers(player)) {
|
|
|
|
return getCostOfNextHacknetServer(player);
|
|
|
|
} else {
|
|
|
|
return getCostOfNextHacknetNode(player);
|
|
|
|
}
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
getNodeStats: function (i: any): NodeStats {
|
2021-10-05 04:25:21 +02:00
|
|
|
const node = getHacknetNode(i, "getNodeStats");
|
|
|
|
const hasUpgraded = hasHacknetServers(player);
|
|
|
|
const res: any = {
|
|
|
|
name: node instanceof HacknetServer ? node.hostname : node.name,
|
|
|
|
level: node.level,
|
|
|
|
ram: node instanceof HacknetServer ? node.maxRam : node.ram,
|
2021-11-12 16:10:33 +01:00
|
|
|
ramUsed: node instanceof HacknetServer ? node.ramUsed : undefined,
|
2021-10-05 04:25:21 +02:00
|
|
|
cores: node.cores,
|
|
|
|
production: node instanceof HacknetServer ? node.hashRate : node.moneyGainRatePerSecond,
|
|
|
|
timeOnline: node.onlineTimeSeconds,
|
|
|
|
totalProduction: node instanceof HacknetServer ? node.totalHashesGenerated : node.totalMoneyGenerated,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (hasUpgraded && node instanceof HacknetServer) {
|
|
|
|
res.cache = node.cache;
|
|
|
|
res.hashCapacity = node.hashCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
upgradeLevel: function (i: any, n: any): boolean {
|
2021-10-05 04:25:21 +02:00
|
|
|
const node = getHacknetNode(i, "upgradeLevel");
|
|
|
|
return purchaseLevelUpgrade(player, node, n);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
upgradeRam: function (i: any, n: any): boolean {
|
2021-10-05 04:25:21 +02:00
|
|
|
const node = getHacknetNode(i, "upgradeRam");
|
|
|
|
return purchaseRamUpgrade(player, node, n);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
upgradeCore: function (i: any, n: any): boolean {
|
2021-10-05 04:25:21 +02:00
|
|
|
const node = getHacknetNode(i, "upgradeCore");
|
|
|
|
return purchaseCoreUpgrade(player, node, n);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
upgradeCache: function (i: any, n: any): boolean {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (!hasHacknetServers(player)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const node = getHacknetNode(i, "upgradeCache");
|
|
|
|
if (!(node instanceof HacknetServer)) {
|
|
|
|
workerScript.log("upgradeCache", "Can only be called on hacknet servers");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const res = purchaseCacheUpgrade(player, node, n);
|
|
|
|
if (res) {
|
|
|
|
updateHashManagerCapacity(player);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
getLevelUpgradeCost: function (i: any, n: any): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
const node = getHacknetNode(i, "upgradeLevel");
|
|
|
|
return node.calculateLevelUpgradeCost(n, player.hacknet_node_level_cost_mult);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
getRamUpgradeCost: function (i: any, n: any): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
const node = getHacknetNode(i, "upgradeRam");
|
|
|
|
return node.calculateRamUpgradeCost(n, player.hacknet_node_ram_cost_mult);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
getCoreUpgradeCost: function (i: any, n: any): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
const node = getHacknetNode(i, "upgradeCore");
|
|
|
|
return node.calculateCoreUpgradeCost(n, player.hacknet_node_core_cost_mult);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
getCacheUpgradeCost: function (i: any, n: any): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (!hasHacknetServers(player)) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
|
|
|
const node = getHacknetNode(i, "upgradeCache");
|
|
|
|
if (!(node instanceof HacknetServer)) {
|
|
|
|
workerScript.log("getCacheUpgradeCost", "Can only be called on hacknet servers");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return node.calculateCacheUpgradeCost(n);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
numHashes: function (): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (!hasHacknetServers(player)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return player.hashManager.hashes;
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
hashCapacity: function (): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (!hasHacknetServers(player)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return player.hashManager.capacity;
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
hashCost: function (upgName: any): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (!hasHacknetServers(player)) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return player.hashManager.getUpgradeCost(upgName);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
spendHashes: function (upgName: any, upgTarget: any): boolean {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (!hasHacknetServers(player)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return purchaseHashUpgrade(player, upgName, upgTarget);
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
getHashUpgradeLevel: function (upgName: any): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
const level = player.hashManager.upgrades[upgName];
|
|
|
|
if (level === undefined) {
|
|
|
|
throw helper.makeRuntimeErrorMsg("hacknet.hashUpgradeLevel", `Invalid Hash Upgrade: ${upgName}`);
|
|
|
|
}
|
|
|
|
return level;
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
getStudyMult: function (): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (!hasHacknetServers(player)) {
|
2021-11-04 01:19:52 +01:00
|
|
|
return 1;
|
2021-10-05 04:25:21 +02:00
|
|
|
}
|
|
|
|
return player.hashManager.getStudyMult();
|
|
|
|
},
|
2021-11-04 01:19:52 +01:00
|
|
|
getTrainingMult: function (): number {
|
2021-10-05 04:25:21 +02:00
|
|
|
if (!hasHacknetServers(player)) {
|
2021-11-04 01:19:52 +01:00
|
|
|
return 1;
|
2021-10-05 04:25:21 +02:00
|
|
|
}
|
|
|
|
return player.hashManager.getTrainingMult();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|