2019-03-05 02:40:28 +01:00
|
|
|
import { Server } from "./Server";
|
|
|
|
import { SpecialServerIps } from "./SpecialServerIps";
|
|
|
|
import { serverMetadata } from "./data/servers";
|
|
|
|
|
2019-03-27 09:36:14 +01:00
|
|
|
import { HacknetServer } from "../Hacknet/HacknetServer";
|
|
|
|
|
2019-03-05 02:40:28 +01:00
|
|
|
import { IMap } from "../types";
|
|
|
|
import { createRandomIp,
|
|
|
|
ipExists } from "../../utils/IPAddress";
|
|
|
|
import { getRandomInt } from "../../utils/helpers/getRandomInt";
|
|
|
|
import { Reviver } from "../../utils/JSONReviver";
|
2019-03-03 04:15:10 +01:00
|
|
|
|
|
|
|
// Map of all Servers that exist in the game
|
|
|
|
// Key (string) = IP
|
|
|
|
// Value = Server object
|
2019-03-27 09:36:14 +01:00
|
|
|
export let AllServers: IMap<Server | HacknetServer> = {};
|
2019-03-03 04:15:10 +01:00
|
|
|
|
|
|
|
// Saftely add a Server to the AllServers map
|
2019-04-05 11:08:41 +02:00
|
|
|
export function AddToAllServers(server: Server | HacknetServer): void {
|
2019-03-03 04:15:10 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
AllServers[serverIp] = server;
|
|
|
|
}
|
|
|
|
|
2019-03-05 02:40:28 +01:00
|
|
|
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) {
|
2019-03-03 04:15:10 +01:00
|
|
|
/* Create a randomized network for all the foreign servers */
|
|
|
|
//Groupings for creating a randomized network
|
2019-03-05 02:40:28 +01:00
|
|
|
const networkLayers: Server[][] = [];
|
2019-03-03 04:15:10 +01:00
|
|
|
for (let i = 0; i < 15; i++) {
|
|
|
|
networkLayers.push([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Essentially any property that is of type 'number | IMinMaxRange'
|
2019-03-05 02:40:28 +01:00
|
|
|
const propertiesToPatternMatch: string[] = [
|
2019-03-03 04:15:10 +01:00
|
|
|
"hackDifficulty",
|
|
|
|
"moneyAvailable",
|
|
|
|
"requiredHackingSkill",
|
|
|
|
"serverGrowth"
|
|
|
|
];
|
|
|
|
|
2019-03-05 02:40:28 +01:00
|
|
|
const toNumber = (value: any) => {
|
2019-03-03 04:15:10 +01:00
|
|
|
switch (typeof value) {
|
|
|
|
case 'number':
|
|
|
|
return value;
|
|
|
|
case 'object':
|
|
|
|
return getRandomInt(value.min, value.max);
|
|
|
|
default:
|
|
|
|
throw Error(`Do not know how to convert the type '${typeof value}' to a number`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const metadata of serverMetadata) {
|
2019-03-05 02:40:28 +01:00
|
|
|
const serverParams: IServerParams = {
|
2019-03-03 04:15:10 +01:00
|
|
|
hostname: metadata.hostname,
|
|
|
|
ip: createRandomIp(),
|
|
|
|
numOpenPortsRequired: metadata.numOpenPortsRequired,
|
|
|
|
organizationName: metadata.organizationName
|
|
|
|
};
|
|
|
|
|
|
|
|
if (metadata.maxRamExponent !== undefined) {
|
|
|
|
serverParams.maxRam = Math.pow(2, toNumber(metadata.maxRamExponent));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const prop of propertiesToPatternMatch) {
|
|
|
|
if (metadata[prop] !== undefined) {
|
|
|
|
serverParams[prop] = toNumber(metadata[prop]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = new Server(serverParams);
|
|
|
|
for (const filename of (metadata.literature || [])) {
|
|
|
|
server.messages.push(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (metadata.specialName !== undefined) {
|
|
|
|
SpecialServerIps.addIp(metadata.specialName, server.ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
AddToAllServers(server);
|
|
|
|
if (metadata.networkLayer !== undefined) {
|
|
|
|
networkLayers[toNumber(metadata.networkLayer) - 1].push(server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a randomized network for all the foreign servers */
|
2019-03-05 02:40:28 +01:00
|
|
|
const linkComputers = (server1: Server, server2: Server) => {
|
2019-03-03 04:15:10 +01:00
|
|
|
server1.serversOnNetwork.push(server2.ip);
|
|
|
|
server2.serversOnNetwork.push(server1.ip);
|
|
|
|
};
|
|
|
|
|
2019-03-05 02:40:28 +01:00
|
|
|
const getRandomArrayItem = (arr: any[]) => arr[Math.floor(Math.random() * arr.length)];
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2019-03-05 02:40:28 +01:00
|
|
|
const linkNetworkLayers = (network1: Server[], selectServer: () => Server) => {
|
2019-03-03 04:15:10 +01:00
|
|
|
for (const server of network1) {
|
|
|
|
linkComputers(server, selectServer());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Connect the first tier of servers to the player's home computer
|
2019-03-05 02:40:28 +01:00
|
|
|
linkNetworkLayers(networkLayers[0], () => homeComputer);
|
2019-03-03 04:15:10 +01:00
|
|
|
for (let i = 1; i < networkLayers.length; i++) {
|
|
|
|
linkNetworkLayers(networkLayers[i], () => getRandomArrayItem(networkLayers[i - 1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function prestigeAllServers() {
|
|
|
|
for (var member in AllServers) {
|
|
|
|
delete AllServers[member];
|
|
|
|
}
|
|
|
|
AllServers = {};
|
|
|
|
}
|
|
|
|
|
2019-03-05 02:40:28 +01:00
|
|
|
export function loadAllServers(saveString: string) {
|
2019-03-03 04:15:10 +01:00
|
|
|
AllServers = JSON.parse(saveString, Reviver);
|
|
|
|
}
|