Refactored Server/Script/Files code to TypeScript

This commit is contained in:
danielyxie
2019-03-04 17:40:28 -08:00
parent e1b8a23f1e
commit 473f0f1447
34 changed files with 763 additions and 695 deletions
+35 -14
View File
@@ -1,40 +1,61 @@
import { ipExists } from "../../utils/IPAddress";
import { Server } from "./Server";
import { SpecialServerIps } from "./SpecialServerIps";
import { serverMetadata } from "./data/servers";
import { IMap } from "../types";
import { createRandomIp,
ipExists } from "../../utils/IPAddress";
import { getRandomInt } from "../../utils/helpers/getRandomInt";
import { Reviver } from "../../utils/JSONReviver";
// Map of all Servers that exist in the game
// Key (string) = IP
// Value = Server object
let AllServers = {};
export let AllServers: IMap<Server> = {};
// Saftely add a Server to the AllServers map
export function AddToAllServers(server) {
export function AddToAllServers(server: Server): void {
var serverIp = server.ip;
if (ipExists(serverIp)) {
console.log("IP of server that's being added: " + serverIp);
console.log("Hostname of the server thats being added: " + server.hostname);
console.log("The server that already has this IP is: " + AllServers[serverIp].hostname);
throw new Error("Error: Trying to add a server with an existing IP");
return;
}
AllServers[serverIp] = server;
}
export function initForeignServers() {
interface IServerParams {
hackDifficulty?: number;
hostname: string;
ip: string;
maxRam?: number;
moneyAvailable?: number;
numOpenPortsRequired: number;
organizationName: string;
requiredHackingSkill?: number;
serverGrowth?: number;
[key: string]: any;
}
export function initForeignServers(homeComputer: Server) {
/* Create a randomized network for all the foreign servers */
//Groupings for creating a randomized network
const networkLayers = [];
const networkLayers: Server[][] = [];
for (let i = 0; i < 15; i++) {
networkLayers.push([]);
}
// Essentially any property that is of type 'number | IMinMaxRange'
const propertiesToPatternMatch = [
const propertiesToPatternMatch: string[] = [
"hackDifficulty",
"moneyAvailable",
"requiredHackingSkill",
"serverGrowth"
];
const toNumber = (value) => {
const toNumber = (value: any) => {
switch (typeof value) {
case 'number':
return value;
@@ -46,7 +67,7 @@ export function initForeignServers() {
}
for (const metadata of serverMetadata) {
const serverParams = {
const serverParams: IServerParams = {
hostname: metadata.hostname,
ip: createRandomIp(),
numOpenPortsRequired: metadata.numOpenPortsRequired,
@@ -79,21 +100,21 @@ export function initForeignServers() {
}
/* Create a randomized network for all the foreign servers */
const linkComputers = (server1, server2) => {
const linkComputers = (server1: Server, server2: Server) => {
server1.serversOnNetwork.push(server2.ip);
server2.serversOnNetwork.push(server1.ip);
};
const getRandomArrayItem = (arr) => arr[Math.floor(Math.random() * arr.length)];
const getRandomArrayItem = (arr: any[]) => arr[Math.floor(Math.random() * arr.length)];
const linkNetworkLayers = (network1, selectServer) => {
const linkNetworkLayers = (network1: Server[], selectServer: () => Server) => {
for (const server of network1) {
linkComputers(server, selectServer());
}
};
// Connect the first tier of servers to the player's home computer
linkNetworkLayers(networkLayers[0], () => Player.getHomeComputer());
linkNetworkLayers(networkLayers[0], () => homeComputer);
for (let i = 1; i < networkLayers.length; i++) {
linkNetworkLayers(networkLayers[i], () => getRandomArrayItem(networkLayers[i - 1]));
}
@@ -106,6 +127,6 @@ export function prestigeAllServers() {
AllServers = {};
}
export function loadAllServers(saveString) {
export function loadAllServers(saveString: string) {
AllServers = JSON.parse(saveString, Reviver);
}
+36 -36
View File
@@ -1,9 +1,14 @@
// Class representing a single generic Server
// TODO This import is a circular import. Try to fix it in the future
import { GetServerByHostname } from "./ServerHelpers";
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
import { CodingContract } from "../CodingContracts";
import { Message } from "../Message/Message";
import { RunningScript } from "../Script/RunningScript";
import { Script } from "../Script/Script";
import { isScriptFilename } from "../Script/ScriptHelpersTS";
import { TextFile } from "../TextFile";
import { createRandomIp } from "../../utils/IPAddress";
@@ -27,6 +32,11 @@ interface IConstructorParams {
}
export class Server {
// Initializes a Server Object from a JSON save state
static fromJSON(value: any): Server {
return Generic_fromJSON(Server, value.data);
}
// Initial server security level
// (i.e. security level when the server was created)
baseDifficulty: number = 1;
@@ -172,49 +182,43 @@ export class Server {
this.maxRam = ram;
}
//The serverOnNetwork array holds the IP of all the servers. This function
//returns the actual Server objects
Server.prototype.getServerOnNetwork = function(i) {
if (i > this.serversOnNetwork.length) {
console.log("Tried to get server on network that was out of range");
return;
}
return AllServers[this.serversOnNetwork[i]];
}
//Given the name of the script, returns the corresponding
//script object on the server (if it exists)
Server.prototype.getScript = function(scriptName) {
for (var i = 0; i < this.scripts.length; i++) {
if (this.scripts[i].filename == scriptName) {
// Given the name of the script, returns the corresponding
// script object on the server (if it exists)
getScript(scriptName: string): Script | null {
for (let i = 0; i < this.scripts.length; i++) {
if (this.scripts[i].filename === scriptName) {
return this.scripts[i];
}
}
return null;
}
Server.prototype.capDifficulty = function() {
// Ensures that the server's difficulty (server security) doesn't get too high
capDifficulty(): void {
if (this.hackDifficulty < this.minDifficulty) {this.hackDifficulty = this.minDifficulty;}
if (this.hackDifficulty < 1) {this.hackDifficulty = 1;}
//Place some arbitrarily limit that realistically should never happen unless someone is
//screwing around with the game
// Place some arbitrarily limit that realistically should never happen unless someone is
// screwing around with the game
if (this.hackDifficulty > 1000000) {this.hackDifficulty = 1000000;}
}
//Strengthens a server's security level (difficulty) by the specified amount
Server.prototype.fortify = function(amt) {
// Strengthens a server's security level (difficulty) by the specified amount
fortify(amt: number): void {
this.hackDifficulty += amt;
this.capDifficulty();
}
Server.prototype.weaken = function(amt) {
// Lowers the server's security level (difficulty) by the specified amount)
weaken(amt: number): void {
this.hackDifficulty -= (amt * BitNodeMultipliers.ServerWeakenRate);
this.capDifficulty();
}
// Write to a script file
// Overwrites existing files. Creates new files if the script does not eixst
Server.prototype.writeToScriptFile = function(fn, code) {
writeToScriptFile(fn: string, code: string) {
var ret = {success: false, overwritten: false};
if (!isScriptFilename(fn)) { return ret; }
@@ -232,7 +236,7 @@ export class Server {
}
//Otherwise, create a new script
var newScript = new Script();
const newScript = new Script();
newScript.filename = fn;
newScript.code = code;
newScript.updateRamUsage();
@@ -244,8 +248,8 @@ export class Server {
// Write to a text file
// Overwrites existing files. Creates new files if the text file does not exist
Server.prototype.writeToTextFile = function(fn, txt) {
var ret = {success: false, overwritten: false};
writeToTextFile(fn: string, txt: string) {
var ret = { success: false, overwritten: false };
if (!fn.endsWith("txt")) { return ret; }
//Check if the text file already exists, and overwrite if it does
@@ -265,11 +269,11 @@ export class Server {
return ret;
}
Server.prototype.addContract = function(contract) {
addContract(contract: CodingContract) {
this.contracts.push(contract);
}
Server.prototype.removeContract = function(contract) {
removeContract(contract: CodingContract) {
if (contract instanceof CodingContract) {
this.contracts = this.contracts.filter((c) => {
return c.fn !== contract.fn;
@@ -281,7 +285,7 @@ export class Server {
}
}
Server.prototype.getContract = function(contractName) {
getContract(contractName: string) {
for (const contract of this.contracts) {
if (contract.fn === contractName) {
return contract;
@@ -289,15 +293,11 @@ export class Server {
}
return null;
}
}
//Functions for loading and saving a Server
Server.prototype.toJSON = function() {
return Generic_toJSON("Server", this);
}
Server.fromJSON = function(value) {
return Generic_fromJSON(Server, value.data);
// Serialize the current object to a JSON save state
toJSON(): any {
return Generic_toJSON("Server", this);
}
}
Reviver.constructors.Server = Server;
@@ -1,23 +1,16 @@
import { BitNodeMultipliers } from "./BitNode/BitNodeMultipliers";
import { CodingContract,
ContractTypes } from "./CodingContracts";
import { CONSTANTS } from "./Constants";
import { Script,
isScriptFilename } from "./Script";
import { Player } from "./Player";
import { Programs } from "./Programs/Programs";
import { SpecialServerIps } from "./SpecialServerIps";
import { TextFile } from "./TextFile";
import { getRandomInt } from "../utils/helpers/getRandomInt";
import { serverMetadata } from "./data/servers";
import { Reviver,
Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import {isValidIPAddress} from "../utils/helpers/isValidIPAddress";
import { AllServers } from "./AllServers";
import { Server } from "./Server";
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
import { CONSTANTS } from "../Constants";
import { IPlayer } from "../PersonObjects/IPlayer";
import { Programs } from "../Programs/Programs";
import {isValidIPAddress} from "../../utils/helpers/isValidIPAddress";
// Returns the number of cycles needed to grow the specified server by the
// specified amount. 'growth' parameter is in decimal form, not percentage
export function numCycleForGrowth(server, growth) {
export function numCycleForGrowth(server: Server, growth: number, p: IPlayer) {
let ajdGrowthRate = 1 + (CONSTANTS.ServerBaseGrowthRate - 1) / server.hackDifficulty;
if(ajdGrowthRate > CONSTANTS.ServerMaxGrowthRate) {
ajdGrowthRate = CONSTANTS.ServerMaxGrowthRate;
@@ -25,12 +18,12 @@ export function numCycleForGrowth(server, growth) {
const serverGrowthPercentage = server.serverGrowth / 100;
const cycles = Math.log(growth)/(Math.log(ajdGrowthRate)*Player.hacking_grow_mult*serverGrowthPercentage);
const cycles = Math.log(growth)/(Math.log(ajdGrowthRate) * p.hacking_grow_mult * serverGrowthPercentage);
return cycles;
}
//Applied server growth for a single server. Returns the percentage growth
export function processSingleServerGrowth(server, numCycles) {
export function processSingleServerGrowth(server: Server, numCycles: number, p: IPlayer) {
//Server growth processed once every 450 game cycles
const numServerGrowthCycles = Math.max(Math.floor(numCycles / 450), 0);
@@ -44,7 +37,7 @@ export function processSingleServerGrowth(server, numCycles) {
const numServerGrowthCyclesAdjusted = numServerGrowthCycles * serverGrowthPercentage * BitNodeMultipliers.ServerGrowthRate;
//Apply serverGrowth for the calculated number of growth cycles
var serverGrowth = Math.pow(adjGrowthRate, numServerGrowthCyclesAdjusted * Player.hacking_grow_mult);
let serverGrowth = Math.pow(adjGrowthRate, numServerGrowthCyclesAdjusted * p.hacking_grow_mult);
if (serverGrowth < 1) {
console.log("WARN: serverGrowth calculated to be less than 1");
serverGrowth = 1;
@@ -66,14 +59,14 @@ export function processSingleServerGrowth(server, numCycles) {
// if there was any growth at all, increase security
if (oldMoneyAvailable !== server.moneyAvailable) {
//Growing increases server security twice as much as hacking
let usedCycles = numCycleForGrowth(server, server.moneyAvailable / oldMoneyAvailable);
let usedCycles = numCycleForGrowth(server, server.moneyAvailable / oldMoneyAvailable, p);
usedCycles = Math.max(0, usedCycles);
server.fortify(2 * CONSTANTS.ServerFortifyAmount * Math.ceil(usedCycles));
}
return server.moneyAvailable / oldMoneyAvailable;
}
export function prestigeHomeComputer(homeComp) {
export function prestigeHomeComputer(homeComp: Server) {
const hasBitflume = homeComp.programs.includes(Programs.BitFlume.name);
homeComp.programs.length = 0; //Remove programs
@@ -93,17 +86,9 @@ export function prestigeHomeComputer(homeComp) {
homeComp.messages.push("hackers-starting-handbook.lit");
}
function SizeOfAllServers() {
var size = 0, key;
for (key in AllServers) {
if (AllServers.hasOwnProperty(key)) size++;
}
return size;
}
//Returns server object with corresponding hostname
// Relatively slow, would rather not use this a lot
export function GetServerByHostname(hostname) {
export function GetServerByHostname(hostname: string): Server | null {
for (var ip in AllServers) {
if (AllServers.hasOwnProperty(ip)) {
if (AllServers[ip].hostname == hostname) {
@@ -111,16 +96,30 @@ export function GetServerByHostname(hostname) {
}
}
}
return null;
}
//Get server by IP or hostname. Returns null if invalid
export function getServer(s) {
export function getServer(s: string): Server | null {
if (!isValidIPAddress(s)) {
return GetServerByHostname(s);
}
if(AllServers[s] !== undefined) {
if (AllServers[s] !== undefined) {
return AllServers[s];
}
return null;
}
// Returns the i-th server on the specified server's network
// A Server's serverOnNetwork property holds only the IPs. This function returns
// the actual Server object
export function getServerOnNetwork(server: Server, i: number) {
if (i > server.serversOnNetwork.length) {
console.error("Tried to get server on network that was out of range");
return;
}
return AllServers[server.serversOnNetwork[i]];
}
+10 -10
View File
@@ -2,16 +2,16 @@
* Implements functions for purchasing servers or purchasing more RAM for
* the home computer
*/
import { BitNodeMultipliers } from "./BitNode/BitNodeMultipliers";
import { CONSTANTS } from "./Constants";
import { Player } from "./Player";
import { Server,
AllServers,
AddToAllServers} from "./Server";
import { dialogBoxCreate } from "../utils/DialogBox";
import { createRandomIp } from "../utils/IPAddress";
import { yesNoTxtInpBoxGetInput } from "../utils/YesNoBox";
import { isPowerOfTwo } from "../utils/helpers/isPowerOfTwo";
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
import { CONSTANTS } from "../Constants";
import { Player } from "../Player";
import { AllServers } from "../Server/AllServers";
import { Server } from "../Server/Server";
import { AddToAllServers } from "../Server/ServerHelpers";
import { dialogBoxCreate } from "../../utils/DialogBox";
import { createRandomIp } from "../../utils/IPAddress";
import { yesNoTxtInpBoxGetInput } from "../../utils/YesNoBox";
import { isPowerOfTwo } from "../../utils/helpers/isPowerOfTwo";
// Returns the cost of purchasing a server with the given RAM
// Returns Infinity for invalid 'ram' arguments
-50
View File
@@ -1,50 +0,0 @@
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
/* Holds IP of Special Servers */
let SpecialServerNames = {
FulcrumSecretTechnologies: "Fulcrum Secret Technologies Server",
CyberSecServer: "CyberSec Server",
NiteSecServer: "NiteSec Server",
TheBlackHandServer: "The Black Hand Server",
BitRunnersServer: "BitRunners Server",
TheDarkArmyServer: "The Dark Army Server",
DaedalusServer: "Daedalus Server",
WorldDaemon: "w0r1d_d43m0n",
}
function SpecialServerIpsMap() {}
SpecialServerIpsMap.prototype.addIp = function(name, ip) {
this[name] = ip;
}
SpecialServerIpsMap.prototype.toJSON = function() {
return Generic_toJSON("SpecialServerIpsMap", this);
}
SpecialServerIpsMap.fromJSON = function(value) {
return Generic_fromJSON(SpecialServerIpsMap, value.data);
}
Reviver.constructors.SpecialServerIpsMap = SpecialServerIpsMap;
let SpecialServerIps = new SpecialServerIpsMap();
function prestigeSpecialServerIps() {
for (var member in SpecialServerIps) {
delete SpecialServerIps[member];
}
SpecialServerIps = null;
SpecialServerIps = new SpecialServerIpsMap();
}
function loadSpecialServerIps(saveString) {
SpecialServerIps = JSON.parse(saveString, Reviver);
}
function initSpecialServerIps() {
SpecialServerIps = new SpecialServerIpsMap();
}
export {SpecialServerNames, SpecialServerIps, SpecialServerIpsMap, loadSpecialServerIps,
prestigeSpecialServerIps, initSpecialServerIps};
+56
View File
@@ -0,0 +1,56 @@
import { IMap } from "../types";
import { Reviver,
Generic_toJSON,
Generic_fromJSON } from "../../utils/JSONReviver";
/* Holds IP of Special Servers */
export let SpecialServerNames: IMap<string> = {
FulcrumSecretTechnologies: "Fulcrum Secret Technologies Server",
CyberSecServer: "CyberSec Server",
NiteSecServer: "NiteSec Server",
TheBlackHandServer: "The Black Hand Server",
BitRunnersServer: "BitRunners Server",
TheDarkArmyServer: "The Dark Army Server",
DaedalusServer: "Daedalus Server",
WorldDaemon: "w0r1d_d43m0n",
}
export class SpecialServerIpsMap {
// Initializes a SpecialServerIpsMap Object from a JSON save state
static fromJSON(value: any): SpecialServerIpsMap {
return Generic_fromJSON(SpecialServerIpsMap, value.data);
}
[key: string]: Function | string;
constructor() {}
addIp(name:string, ip: string) {
this[name] = ip;
}
// Serialize the current object to a JSON save state
toJSON(): any {
return Generic_toJSON("SpecialServerIpsMap", this);
}
}
Reviver.constructors.SpecialServerIpsMap = SpecialServerIpsMap;
export let SpecialServerIps: SpecialServerIpsMap = new SpecialServerIpsMap();
export function prestigeSpecialServerIps() {
for (var member in SpecialServerIps) {
delete SpecialServerIps[member];
}
SpecialServerIps = new SpecialServerIpsMap();
}
export function loadSpecialServerIps(saveString: string) {
SpecialServerIps = JSON.parse(saveString, Reviver);
}
export function initSpecialServerIps() {
SpecialServerIps = new SpecialServerIpsMap();
}
File diff suppressed because it is too large Load Diff