mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-24 18:22:58 +02:00
Started server code refactor
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
import {AllServers} from "../src/Server";
|
||||
import {getRandomByte} from "./helpers/getRandomByte";
|
||||
|
||||
/* Functions to deal with manipulating IP addresses*/
|
||||
|
||||
//Generate a random IP address
|
||||
//Will not return an IP address that already exists in the AllServers array
|
||||
function createRandomIp() {
|
||||
var ip = getRandomByte(99) + '.' +
|
||||
getRandomByte(9) + '.' +
|
||||
getRandomByte(9) + '.' +
|
||||
getRandomByte(9);
|
||||
|
||||
//If the Ip already exists, recurse to create a new one
|
||||
if (ipExists(ip)) {
|
||||
return createRandomIp();
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
//Returns true if the IP already exists in one of the game's servers
|
||||
function ipExists(ip) {
|
||||
for (var property in AllServers) {
|
||||
if (AllServers.hasOwnProperty(property)) {
|
||||
if (property == ip) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export {createRandomIp, ipExists};
|
||||
@@ -0,0 +1,25 @@
|
||||
import {AllServers} from "../src/Server";
|
||||
import {getRandomByte} from "./helpers/getRandomByte";
|
||||
|
||||
/* Functions to deal with manipulating IP addresses*/
|
||||
|
||||
//Generate a random IP address
|
||||
//Will not return an IP address that already exists in the AllServers array
|
||||
export function createRandomIp(): string {
|
||||
const ip: string = getRandomByte(99) + '.' +
|
||||
getRandomByte(9) + '.' +
|
||||
getRandomByte(9) + '.' +
|
||||
getRandomByte(9);
|
||||
|
||||
// If the Ip already exists, recurse to create a new one
|
||||
if (ipExists(ip)) {
|
||||
return createRandomIp();
|
||||
}
|
||||
|
||||
return ip;
|
||||
}
|
||||
|
||||
// Returns true if the IP already exists in one of the game's servers
|
||||
export function ipExists(ip: string) {
|
||||
return (AllServers[ip] != null);
|
||||
}
|
||||
Reference in New Issue
Block a user