Skip to content

Commit

Permalink
working games list
Browse files Browse the repository at this point in the history
  • Loading branch information
a-sync committed Jan 6, 2024
1 parent 57b5965 commit 5cdfd32
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
7 changes: 4 additions & 3 deletions public/game-server-config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
}
},
"type": {
"title": "Gamedig type",
"description": "Look for the <i>GameDig Type ID</i> in the <a href=\"https://github.com/gamedig/node-gamedig/blob/master/GAMES_LIST.md\">games list</a>.",
"title": "GameDig type",
"description": "For more info check the <a href=\"https://github.com/gamedig/node-gamedig/blob/master/GAMES_LIST.md\">GameDig games list</a>.",
"type": "string",
"minLength": 1,
"options": {
"grid_columns": 3
}
},
"$ref": "gamedig-games"
},
"host": {
"title": "Host name or IP",
Expand Down
4 changes: 2 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
<link href="https://use.fontawesome.com/releases/v5.15.4/css/all.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/animate.css@4/animate.min.css" rel="stylesheet">
<!--
<link href="https://cdn.jsdelivr.net/npm/select2@4/dist/css/select2.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/flatpickr@4/dist/flatpickr.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/simplemde@1/dist/simplemde.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/select2@4/dist/css/select2.min.css" rel="stylesheet">
-->

<script src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.bundle.min.js"></script>
<!--
<script src="https://cdn.jsdelivr.net/npm/select2@4/dist/js/select2.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr@4/dist/flatpickr.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/simplemde@1/dist/simplemde.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4/dist/js/select2.min.js"></script>
-->
<script src="https://cdn.jsdelivr.net/npm/node-forge@1/dist/forge.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify@3/dist/purify.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 36 additions & 13 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string, string> = {
Expand Down Expand Up @@ -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<string,{pretty: string}> = 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 = {};
Expand All @@ -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),
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 5cdfd32

Please sign in to comment.