From 6d79561859aea8a975eacb2f941f0ed4101655b5 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 19 Dec 2021 04:03:14 +0000 Subject: [PATCH] fix(servers): fix issue of space in server name Replace the space in a server name with a hyphen (-), also noticed some interesting behaviour with the 'count up naming', so tweaked that. Resolves danielyxie/bitburner#1999 --- src/Server/ServerHelpers.ts | 11 +++++++---- src/Server/ServerPurchases.ts | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Server/ServerHelpers.ts b/src/Server/ServerHelpers.ts index 4f49f2338..affb42a00 100644 --- a/src/Server/ServerHelpers.ts +++ b/src/Server/ServerHelpers.ts @@ -16,22 +16,25 @@ import { isValidNumber } from "../utils/helpers/isValidNumber"; * does not have a duplicate hostname/ip. */ export function safetlyCreateUniqueServer(params: IConstructorParams): Server { + let hostname: string = params.hostname.replace(/ /g, `-`); + if (params.ip != null && ipExists(params.ip)) { params.ip = createUniqueRandomIp(); } - if (GetServer(params.hostname) != null) { + if (GetServer(hostname) != null) { + hostname = `${hostname}-0`; + // Use a for loop to ensure that we don't get suck in an infinite loop somehow - let hostname: string = params.hostname; for (let i = 0; i < 200; ++i) { - hostname = `${params.hostname}-${i}`; + hostname = hostname.replace(/-[0-9]+$/, `-${i}`); if (GetServer(hostname) == null) { break; } } - params.hostname = hostname; } + params.hostname = hostname; return new Server(params); } diff --git a/src/Server/ServerPurchases.ts b/src/Server/ServerPurchases.ts index 7d2b754e8..1c7f6d08d 100644 --- a/src/Server/ServerPurchases.ts +++ b/src/Server/ServerPurchases.ts @@ -96,7 +96,7 @@ export function purchaseServer(hostname: string, ram: number, cost: number, p: I p.loseMoney(cost, "servers"); - dialogBoxCreate("Server successfully purchased with hostname " + hostname); + dialogBoxCreate("Server successfully purchased with hostname " + newServ.hostname); } // Manually upgrade RAM on home computer (NOT through Netscript)