From 5cdfd32942923c65db0db0164bb8412087d25906 Mon Sep 17 00:00:00 2001 From: Smith Date: Sat, 6 Jan 2024 16:34:27 +0100 Subject: [PATCH] working games list --- public/game-server-config.schema.json | 7 ++-- public/index.html | 4 +-- public/main.js | 2 +- src/server.ts | 49 ++++++++++++++++++++------- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/public/game-server-config.schema.json b/public/game-server-config.schema.json index 6d37652..537ec23 100644 --- a/public/game-server-config.schema.json +++ b/public/game-server-config.schema.json @@ -17,13 +17,14 @@ } }, "type": { - "title": "Gamedig type", - "description": "Look for the GameDig Type ID in the games list.", + "title": "GameDig type", + "description": "For more info check the GameDig games list.", "type": "string", "minLength": 1, "options": { "grid_columns": 3 - } + }, + "$ref": "gamedig-games" }, "host": { "title": "Host name or IP", diff --git a/public/index.html b/public/index.html index 5a90070..3159022 100644 --- a/public/index.html +++ b/public/index.html @@ -16,18 +16,18 @@ diff --git a/public/main.js b/public/main.js index c16f1bb..43bbd93 100644 --- a/public/main.js +++ b/public/main.js @@ -36,7 +36,7 @@ $(async () => { "compact": 0, "show_errors": "always", "required_by_default": 0, - "no_additional_properties": 1, + "no_additional_properties": true, "display_required_only": 1, "show_opt_in": 0, "remove_empty_properties": 0, diff --git a/src/server.ts b/src/server.ts index 6a4e726..b5cc431 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4,8 +4,8 @@ import crypto from 'node:crypto'; import { createServer } from 'node:http'; import { URL } from 'node:url'; import { getInstance } from 'gamedig'; -//import gamedigPjson from '../node_modules/node-gamedig/package.json' assert {type: 'json'}; -const gamedigPjson = fs.readFileSync('../node_modules/node-gamedig/package.json', 'utf-8'); +//import gamedigPjson from '../node_modules/gamedig/package.json' assert {type: 'json'}; +const gamedigPjson = fs.readFileSync(path.resolve(__dirname, '../node_modules/gamedig/package.json'), 'utf-8'); const gamedigVersion = JSON.parse(gamedigPjson).version || 0; import 'dotenv/config'; @@ -20,18 +20,30 @@ const DBG = Boolean(Number(process.env.DBG)); let loop: NodeJS.Timeout | undefined; -interface ApiResponse { +interface ApiResponse extends FeaturesResponse, ConfigResponse { message?: string; error?: string; - config?: GameServerConfig[]; +} + +interface FeaturesResponse{ features?: { + gamedig: string; steam: boolean; discord: boolean; telegram: boolean; slack: boolean; }; - version?: string; - games?: any[];//debug +} + +interface ConfigResponse{ + config?: GameServerConfig[]; +} + +interface SelectOptionsResponse { + options: { + enum_titles: string[]; + }; + enum: string[]; } const EXT_MIME: Record = { @@ -61,6 +73,23 @@ createServer(async (req, res) => { } else if (p === 'ping') { if (DBG) console.log('ping'); res.end('pong'); + } else if (p === 'gamedig-games') { + //re.version = gamedigPjson.version; + const gd = getInstance(); + // @ts-ignore + const games: Map = gd.queryRunner.gameResolver.gamesByKey || new Map(); + let status = 200; + let re: SelectOptionsResponse = { + enum: Array.from(games.keys()), + options: { + enum_titles: Array.from(games.values()).map(g=>g.pretty) + } + }; + res.writeHead(status, { + 'Content-Type': 'application/json', + 'Cache-Control': 'max-age=0' + }); + res.end(JSON.stringify(re, null, DBG ? 2 : 0)); } else if (SECRET !== '' && req.headers['x-btoken']) { let status = 200; let re: ApiResponse = {}; @@ -70,6 +99,7 @@ createServer(async (req, res) => { try { if (reqPath[0] === 'features') { re.features = { + gamedig: String(gamedigVersion), steam: Boolean(process.env.STEAM_WEB_API_KEY), discord: Boolean(process.env.DISCORD_BOT_TOKEN), telegram: Boolean(process.env.TELEGRAM_BOT_TOKEN), @@ -101,13 +131,6 @@ createServer(async (req, res) => { } else if (reqPath[0] === 'flush' && ['servers', 'discord', 'telegram', 'slack'].includes(reqPath[1])) { await restart(reqPath[1]); re.message = '🗑️ ' + reqPath[1].slice(0, 1).toUpperCase() + reqPath[1].slice(1) + ' data flushed.'; - } else if (reqPath[0] === 'gamedig-games') { - //re.version = gamedigPjson.version; - const gd = getInstance(); - // @ts-ignore - const games = gd.queryRunner.gameResolver.games || []; - re.version = gamedigVersion; - re.games = games; } else { status = 400; re.error = 'Invalid Request';