From 83e0401dd8a7bbc32f6e60d8f13aab45ca387b28 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Tue, 24 Jan 2023 23:03:01 +0800 Subject: [PATCH] Show game list for GameDig monitor --- server/model/monitor.js | 2 +- .../socket-handlers/general-socket-handler.js | 31 ++++++++++++++ src/pages/EditMonitor.vue | 42 +++++++++++++++++-- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 62d788a93..2fcb08758 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -502,7 +502,7 @@ class Monitor extends BeanModel { bean.status = UP; bean.ping = state.ping; } catch (e) { - throw new Error("Server is offline"); + throw new Error(e.message); } } else if (this.type === "docker") { log.debug(`[${this.name}] Prepare Options for Axios`); diff --git a/server/socket-handlers/general-socket-handler.js b/server/socket-handlers/general-socket-handler.js index 700b4a384..11b47a5bf 100644 --- a/server/socket-handlers/general-socket-handler.js +++ b/server/socket-handlers/general-socket-handler.js @@ -2,6 +2,30 @@ const { log } = require("../../src/util"); const { Settings } = require("../settings"); const { sendInfo } = require("../client"); const { checkLogin } = require("../util-server"); +const GameResolver = require("gamedig/lib/GameResolver"); + +let gameResolver = new GameResolver(); +let gameList = null; + +/** + * Get a game list via GameDig + * @returns {any[]} + */ +function getGameList() { + if (!gameList) { + gameList = gameResolver._readGames().games.sort((a, b) => { + if ( a.pretty < b.pretty ) { + return -1; + } + if ( a.pretty > b.pretty ) { + return 1; + } + return 0; + }); + } else { + return gameList; + } +} module.exports.generalSocketHandler = (socket, server) => { @@ -17,4 +41,11 @@ module.exports.generalSocketHandler = (socket, server) => { } }); + socket.on("getGameList", async (callback) => { + callback({ + ok: true, + gameList: getGameList(), + }); + }); + }; diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 3003f3a93..16ecb887b 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -114,7 +114,11 @@
- +
@@ -636,7 +640,8 @@ export default { acceptedStatusCodeOptions: [], dnsresolvetypeOptions: [], ipOrHostnameRegexPattern: hostNameRegexPattern(), - mqttIpOrHostnameRegexPattern: hostNameRegexPattern(true) + mqttIpOrHostnameRegexPattern: hostNameRegexPattern(true), + gameList: null, }; }, @@ -713,7 +718,18 @@ message HealthCheckResponse { { "HeaderName": "HeaderValue" }` ]); - } + }, + + currentGameObject() { + if (this.gameList) { + for (let game of this.gameList) { + if (game.keys[0] === this.monitor.game) { + return game; + } + } + } + return null; + }, }, watch: { @@ -757,6 +773,24 @@ message HealthCheckResponse { this.monitor.port = undefined; } } + + // Get the game list from server + if (this.monitor.type === "gamedig") { + this.$root.getSocket().emit("getGameList", (res) => { + if (res.ok) { + this.gameList = res.gameList; + } else { + toast.error(res.msg); + } + }); + } + }, + + currentGameObject(newGameObject, previousGameObject) { + if (!this.monitor.port || (previousGameObject && previousGameObject.options.port === this.monitor.port)) { + this.monitor.port = newGameObject.options.port; + } + this.monitor.game = newGameObject.keys[0]; } }, @@ -947,7 +981,7 @@ message HealthCheckResponse { // Enable it if the Docker Host is added in EditMonitor.vue addedDockerHost(id) { this.monitor.docker_host = id; - } + }, }, };