mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-02 05:47:14 +02:00
Refactored Server/Script/Files code to TypeScript
This commit is contained in:
+35
-14
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user