Skip to content

Commit

Permalink
feat(server): server arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Veradictus committed Sep 15, 2023
1 parent 4dff323 commit e504a6c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SSL=false

# API Configuration
API_ENABLED=false
API_PORT=9003
API_PORT=9002

# Server ID (increment with each server hosted)
SERVER_ID=1
Expand Down
60 changes: 60 additions & 0 deletions packages/server/src/args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import config from '@kaetram/common/config';

/**
* Class responsible for overriding the default arguments.
*/

export default class Args {
public constructor(private params: string[] = process.argv) {
if (params.length < 2) return;

// Iterate through the additional parameters and override the default values.
for (let i = 2; i < this.params.length; i += 2) {
let paramIdentifier = this.params[i],
paramValue = this.params[i + 1];

this.override(paramIdentifier, paramValue);
}
}

/**
* Takes a parameter key and overrides the environment variables in the
* configuration based on that.
* @param key The key of the parameter.
* @param value The value of the parameter.
*/

private override(key: string, value: string): void {
switch (key) {
case '--host': {
config.host = value;
break;
}

case '--port': {
config.port = parseInt(value);
config.apiPort = parseInt(value) + 1;
break;
}

case 'remoteServerHost': {
config.remoteServerHost = value;
break;
}

case 'serverId': {
config.serverId = parseInt(value);
break;
}

case 'updateTime': {
config.updateTime = parseInt(value);
break;
}

case 'maxPlayers': {
config.maxPlayers = parseInt(value);
}
}
}
}
6 changes: 5 additions & 1 deletion packages/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Console from './console';
import World from './game/world';
import Loader from './info/loader';
import SocketHandler from './network/sockethandler';
import Args from './args';

import log from '@kaetram/common/util/log';
import config from '@kaetram/common/config';
Expand All @@ -19,7 +20,7 @@ class Main {

private ready = false;

public constructor() {
public constructor(private params: string[] = process.argv) {
if (!this.handleLicensing()) return;

log.info(`Initializing ${config.name} game engine...`);
Expand Down Expand Up @@ -139,4 +140,7 @@ class Main {
}
}

// Parse the override arguments.
new Args();

export default new Main();

0 comments on commit e504a6c

Please sign in to comment.