Merge pull request #2736 from burak28/feat/add-game-server-support
Feat/add game server support
This commit is contained in:
34 files changed
+755
-104
No files matched your search
@@ -4,7 +4,7 @@ const SERVICE_NAME = "MonitorService";
|
||||
class MonitorService {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
|
||||
constructor({ db, settingsService, jobQueue, stringService, emailService, papaparse, logger, errorService }) {
|
||||
constructor({ db, settingsService, jobQueue, stringService, emailService, papaparse, logger, errorService, games }) {
|
||||
this.db = db;
|
||||
this.settingsService = settingsService;
|
||||
this.jobQueue = jobQueue;
|
||||
@@ -13,6 +13,7 @@ class MonitorService {
|
||||
this.papaparse = papaparse;
|
||||
this.logger = logger;
|
||||
this.errorService = errorService;
|
||||
this.games = games;
|
||||
}
|
||||
|
||||
get serviceName() {
|
||||
@@ -183,7 +184,8 @@ class MonitorService {
|
||||
};
|
||||
|
||||
addDemoMonitors = async ({ userId, teamId }) => {
|
||||
const demoMonitors = await this.db.monitorModuleaddDemoMonitors(userId, teamId);
|
||||
const demoMonitors = await this.db.monitorModule.addDemoMonitors(userId, teamId);
|
||||
|
||||
await Promise.all(demoMonitors.map((monitor) => this.jobQueue.addJob(monitor._id, monitor)));
|
||||
return demoMonitors;
|
||||
};
|
||||
@@ -261,6 +263,10 @@ class MonitorService {
|
||||
const csv = this.papaparse.unparse(csvData);
|
||||
return csv;
|
||||
};
|
||||
|
||||
getAllGames = () => {
|
||||
return this.games;
|
||||
};
|
||||
}
|
||||
|
||||
export default MonitorService;
|
||||
@@ -6,6 +6,7 @@ const TYPE_MAP = {
|
||||
docker: "checks",
|
||||
pagespeed: "pagespeedChecks",
|
||||
hardware: "hardwareChecks",
|
||||
game: "checks",
|
||||
};
|
||||
|
||||
class BufferService {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import jmespath from "jmespath";
|
||||
import https from "https";
|
||||
import { GameDig } from "gamedig";
|
||||
|
||||
const SERVICE_NAME = "NetworkService";
|
||||
const UPROCK_ENDPOINT = "https://api.uprock.com/checkmate/push";
|
||||
@@ -23,6 +24,7 @@ class NetworkService {
|
||||
this.TYPE_HARDWARE = "hardware";
|
||||
this.TYPE_DOCKER = "docker";
|
||||
this.TYPE_PORT = "port";
|
||||
this.TYPE_GAME = "game";
|
||||
this.SERVICE_NAME = SERVICE_NAME;
|
||||
this.NETWORK_ERROR = 5000;
|
||||
this.PING_ERROR = 5001;
|
||||
@@ -470,6 +472,62 @@ class NetworkService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the status of a game monitor.
|
||||
*
|
||||
* @param {Object} monitor - The monitor object to request the status for.
|
||||
* @returns {Promise<Object>} The response from the game status request.
|
||||
* @throws {Error} Throws an error if the request fails or if the monitor is not configured correctly.
|
||||
* @property {string} monitorId - The ID of the monitor.
|
||||
* @property {string} type - The type of the monitor (should be "game").
|
||||
* @property {number} responseTime - The time taken for the request.
|
||||
* @property {Object|null} payload - The game state response or null if the request failed.
|
||||
* @property {boolean} status - Indicates if the request was successful (true) or not (false).
|
||||
* @property {number} code - The status code of the request (200 for success, NETWORK_ERROR for failure).
|
||||
* @property {string} message - A message indicating the result of the request.
|
||||
*/
|
||||
async requestGame(monitor) {
|
||||
try {
|
||||
const { url, port, gameId } = monitor;
|
||||
|
||||
const gameResponse = {
|
||||
code: 200,
|
||||
status: true,
|
||||
message: "Success",
|
||||
monitorId: monitor._id,
|
||||
type: "game",
|
||||
};
|
||||
|
||||
const state = await GameDig.query({
|
||||
type: gameId,
|
||||
host: url,
|
||||
port: port,
|
||||
}).catch((error) => {
|
||||
this.logger.warn({
|
||||
message: error.message,
|
||||
service: this.SERVICE_NAME,
|
||||
method: "requestGame",
|
||||
details: { url, port, gameId },
|
||||
});
|
||||
});
|
||||
|
||||
if (!state) {
|
||||
gameResponse.status = false;
|
||||
gameResponse.code = this.NETWORK_ERROR;
|
||||
gameResponse.message = "No response";
|
||||
return gameResponse;
|
||||
}
|
||||
|
||||
gameResponse.responseTime = state.ping;
|
||||
gameResponse.payload = state;
|
||||
return gameResponse;
|
||||
} catch (error) {
|
||||
error.service = this.SERVICE_NAME;
|
||||
error.method = "requestPing";
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the status of a job based on its type and returns the appropriate response.
|
||||
*
|
||||
@@ -494,6 +552,8 @@ class NetworkService {
|
||||
return await this.requestDocker(monitor);
|
||||
case this.TYPE_PORT:
|
||||
return await this.requestPort(monitor);
|
||||
case this.TYPE_GAME:
|
||||
return await this.requestGame(monitor);
|
||||
default:
|
||||
return this.handleUnsupportedType(type);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user