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.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`);

View file

@ -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(),
});
});
};

View file

@ -114,7 +114,11 @@
<!-- GameDig only -->
<div v-if="monitor.type === 'gamedig'" class="my-3">
<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>
<!-- Hostname -->
@ -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;
}
},
},
};
</script>