chore: improve IFindSessionRequest

This commit is contained in:
Sainan 2024-12-29 05:56:57 +01:00
parent 27af54d039
commit 5237871efd
3 changed files with 22 additions and 13 deletions

View File

@ -1,31 +1,28 @@
import { RequestHandler } from "express";
import { getSession } from "@/src/managers/sessionManager";
import { logger } from "@/src/utils/logger";
import { IFindSessionRequest } from "@/src/types/session";
//TODO: cleanup
const findSessionsController: RequestHandler = (_req, res) => {
const reqBody = JSON.parse(String(_req.body));
logger.debug("FindSession Request ", { reqBody });
const req = JSON.parse(String(_req.body));
export const findSessionsController: RequestHandler = (_req, res) => {
const req = JSON.parse(String(_req.body)) as IFindSessionRequest;
logger.debug("FindSession Request ", req);
if (req.id != undefined) {
logger.debug("Found ID");
const session = getSession(req.id as string);
const session = getSession(req.id);
if (session) res.json({ queryId: req.queryId, Sessions: session });
else res.json({});
} else if (req.originalSessionId != undefined) {
logger.debug("Found OriginalSessionID");
const session = getSession(req.originalSessionId as string);
const session = getSession(req.originalSessionId);
if (session) res.json({ queryId: req.queryId, Sessions: session });
else res.json({});
} else {
logger.debug("Found SessionRequest");
const session = getSession(String(_req.body));
const session = getSession(req);
if (session) res.json({ queryId: req.queryId, Sessions: session });
else res.json({});
}
};
export { findSessionsController };

View File

@ -44,7 +44,6 @@ function getSessionByID(sessionId: string): ISession | undefined {
return sessions.find(session => session.sessionId === sessionId);
}
//TODO: proper typings
function getSession(sessionIdOrRequest: string | IFindSessionRequest): any[] {
if (typeof sessionIdOrRequest === "string") {
const session = sessions.find(session => session.sessionId === sessionIdOrRequest);
@ -63,7 +62,11 @@ function getSession(sessionIdOrRequest: string | IFindSessionRequest): any[] {
const request = sessionIdOrRequest;
const matchingSessions = sessions.filter(session => {
for (const key in request) {
if (key !== "eloRating" && key !== "queryId" && request[key] !== session[key as keyof ISession]) {
if (
key !== "eloRating" &&
key !== "queryId" &&
request[key as keyof IFindSessionRequest] !== session[key as keyof ISession]
) {
return false;
}
}

View File

@ -28,5 +28,14 @@ export interface ISession {
}
export interface IFindSessionRequest {
[key: string]: any;
id?: string;
originalSessionId?: string;
buildId?: number;
gameModeId?: number;
regionId?: number;
maxEloDifference?: number;
eloRating?: number;
enforceElo?: boolean;
xplatform?: boolean;
queryId?: number;
}