chore: improve IFindSessionRequest #652

Merged
Sainan merged 2 commits from improve-IFindSessionRequest into main 2024-12-29 12:40:25 -08:00
3 changed files with 22 additions and 14 deletions

View File

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

View File

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

View File

@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface ISession { export interface ISession {
sessionId: string; sessionId: string;
creatorId: string; creatorId: string;
@ -28,5 +27,14 @@ export interface ISession {
} }
export interface IFindSessionRequest { 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;
} }