Show game list for GameDig monitor

This commit is contained in:
Louis Lam 2023-01-24 23:03:01 +08:00
parent aab04f6644
commit 83e0401dd8
3 changed files with 70 additions and 5 deletions

View File

@ -502,7 +502,7 @@ class Monitor extends BeanModel {
bean.status = UP; bean.status = UP;
bean.ping = state.ping; bean.ping = state.ping;
} catch (e) { } catch (e) {
throw new Error("Server is offline"); throw new Error(e.message);
} }
} else if (this.type === "docker") { } else if (this.type === "docker") {
log.debug(`[${this.name}] Prepare Options for Axios`); log.debug(`[${this.name}] Prepare Options for Axios`);

View File

@ -2,6 +2,30 @@ const { log } = require("../../src/util");
const { Settings } = require("../settings"); const { Settings } = require("../settings");
const { sendInfo } = require("../client"); const { sendInfo } = require("../client");
const { checkLogin } = require("../util-server"); 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) => { module.exports.generalSocketHandler = (socket, server) => {
@ -17,4 +41,11 @@ module.exports.generalSocketHandler = (socket, server) => {
} }
}); });
socket.on("getGameList", async (callback) => {
callback({
ok: true,
gameList: getGameList(),
});
});
}; };

View File

@ -114,7 +114,11 @@
<!-- GameDig only --> <!-- GameDig only -->
<div v-if="monitor.type === 'gamedig'" class="my-3"> <div v-if="monitor.type === 'gamedig'" class="my-3">
<label for="game" class="form-label"> {{ $t("Game") }} </label> <label for="game" class="form-label"> {{ $t("Game") }} </label>
<input id="game" v-model="monitor.game" type="text" class="form-control" required> <select id="game" v-model="monitor.game" class="form-select" required>
<option v-for="game in gameList" :key="game.keys[0]" :value="game.keys[0]">
{{ game.pretty }}
</option>
</select>
</div> </div>
<!-- Hostname --> <!-- Hostname -->
@ -636,7 +640,8 @@ export default {
acceptedStatusCodeOptions: [], acceptedStatusCodeOptions: [],
dnsresolvetypeOptions: [], dnsresolvetypeOptions: [],
ipOrHostnameRegexPattern: hostNameRegexPattern(), ipOrHostnameRegexPattern: hostNameRegexPattern(),
mqttIpOrHostnameRegexPattern: hostNameRegexPattern(true) mqttIpOrHostnameRegexPattern: hostNameRegexPattern(true),
gameList: null,
}; };
}, },
@ -713,7 +718,18 @@ message HealthCheckResponse {
{ {
"HeaderName": "HeaderValue" "HeaderName": "HeaderValue"
}` ]); }` ]);
} },
currentGameObject() {
if (this.gameList) {
for (let game of this.gameList) {
if (game.keys[0] === this.monitor.game) {
return game;
}
}
}
return null;
},
}, },
watch: { watch: {
@ -757,6 +773,24 @@ message HealthCheckResponse {
this.monitor.port = undefined; 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 // Enable it if the Docker Host is added in EditMonitor.vue
addedDockerHost(id) { addedDockerHost(id) {
this.monitor.docker_host = id; this.monitor.docker_host = id;
} },
}, },
}; };
</script> </script>