gameProvider
This commit is contained in:
1 parent
49672db563
commit
190d9dd2b1
3 files changed
+82
-1
No files matched your search
@@ -0,0 +1,78 @@
|
|||||||
|
import { IStatusProvider } from "@/service/infrastructure/network/IStatusProvider.js";
|
||||||
|
import { GameStatusPayload, MonitorStatusResponse } from "@/types/network.js";
|
||||||
|
import { Monitor, MonitorType } from "@/types/monitor.js";
|
||||||
|
import { GameDig } from "gamedig";
|
||||||
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { NETWORK_ERROR } from "@/service/infrastructure/network/utils.js";
|
||||||
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
|
||||||
|
type GameDigType = typeof GameDig;
|
||||||
|
|
||||||
|
const SERVICE_NAME = "GameProvider";
|
||||||
|
export class GameProvider implements IStatusProvider<GameStatusPayload> {
|
||||||
|
readonly type = "game";
|
||||||
|
constructor(
|
||||||
|
private logger: ILogger,
|
||||||
|
private gameDig: GameDigType
|
||||||
|
) {}
|
||||||
|
|
||||||
|
supports(type: MonitorType): boolean {
|
||||||
|
return type === "game";
|
||||||
|
}
|
||||||
|
|
||||||
|
async handle(monitor: Monitor): Promise<MonitorStatusResponse<GameStatusPayload>> {
|
||||||
|
const { url, port, gameId, id, teamId, type } = monitor;
|
||||||
|
|
||||||
|
// Clean the host in case a full URL was passed
|
||||||
|
const host = url?.replace(/^https?:\/\//, "").split(/[/?#:]/)[0] || "";
|
||||||
|
try {
|
||||||
|
const state = await this.gameDig
|
||||||
|
.query({
|
||||||
|
type: gameId ?? "unknown",
|
||||||
|
host: host,
|
||||||
|
port: port ?? 0,
|
||||||
|
})
|
||||||
|
.catch((error: unknown) => {
|
||||||
|
this.logger.warn({
|
||||||
|
message: error instanceof Error ? error.message : String(error),
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "handle",
|
||||||
|
details: { host, port, gameId },
|
||||||
|
});
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!state) {
|
||||||
|
return {
|
||||||
|
monitorId: id,
|
||||||
|
teamId: teamId,
|
||||||
|
type: type,
|
||||||
|
status: false,
|
||||||
|
code: NETWORK_ERROR,
|
||||||
|
message: "No response from game server",
|
||||||
|
responseTime: 0,
|
||||||
|
payload: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
monitorId: id,
|
||||||
|
teamId: teamId,
|
||||||
|
type: type,
|
||||||
|
status: true,
|
||||||
|
code: 200,
|
||||||
|
message: "Success",
|
||||||
|
responseTime: state.ping ?? 0,
|
||||||
|
payload: state,
|
||||||
|
};
|
||||||
|
} catch (err: unknown) {
|
||||||
|
const originalMessage = err instanceof Error ? err.message : String(err);
|
||||||
|
throw new AppError({
|
||||||
|
message: originalMessage || "Error performing game server check",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "handle",
|
||||||
|
details: { url: monitor.url, port: monitor.port, gameId: monitor.gameId },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -51,6 +51,7 @@ export class PortProvider implements IStatusProvider<PortStatusPayload> {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
if (error) {
|
if (error) {
|
||||||
|
console.log(error);
|
||||||
const errorMessage = error instanceof Error ? error.message : "Port check failed";
|
const errorMessage = error instanceof Error ? error.message : "Port check failed";
|
||||||
return {
|
return {
|
||||||
monitorId: monitor.id,
|
monitorId: monitor.id,
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import type {
|
|||||||
MonitorStatus,
|
MonitorStatus,
|
||||||
} from "@/types/index.js";
|
} from "@/types/index.js";
|
||||||
|
|
||||||
|
import type { QueryResult } from "gamedig";
|
||||||
|
|
||||||
export interface MonitorStatusResponse<
|
export interface MonitorStatusResponse<
|
||||||
T =
|
T =
|
||||||
| HttpStatusPayload
|
| HttpStatusPayload
|
||||||
@@ -100,7 +102,7 @@ export interface PortStatusPayload {
|
|||||||
success: boolean;
|
success: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GameStatusPayload = Record<string, unknown>;
|
export type GameStatusPayload = QueryResult;
|
||||||
|
|
||||||
export interface GrpcStatusPayload {
|
export interface GrpcStatusPayload {
|
||||||
grpcStatusCode: number;
|
grpcStatusCode: number;
|
||||||
|
|||||||
Reference in new issue
Block a user