XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFESpaceNinjaServer

chore: improve IFindSessionRequest (#652)

8a4f2f4d
Sainan <sainan@calamity.inc>
提交于

代码差异

3 个文件 +22 -14
Modified src/controllers/api/findSessionsController.ts +7 -10
@@ -1,31 +1,28 @@
1 1 import { RequestHandler } from "express";
2 2 import { getSession } from "@/src/managers/sessionManager";
3 3 import { logger } from "@/src/utils/logger";
4 import { IFindSessionRequest } from "@/src/types/session";
4 5
5 //TODO: cleanup
6 const findSessionsController: RequestHandler = (_req, res) => {
7 const reqBody = JSON.parse(String(_req.body));
8 logger.debug("FindSession Request ", { reqBody });
9 const req = JSON.parse(String(_req.body));
6 export const findSessionsController: RequestHandler = (_req, res) => {
7 const req = JSON.parse(String(_req.body)) as IFindSessionRequest;
8 logger.debug("FindSession Request ", req);
10 9 if (req.id != undefined) {
11 10 logger.debug("Found ID");
12 const session = getSession(req.id as string);
11 const session = getSession(req.id);
13 12
14 13 if (session) res.json({ queryId: req.queryId, Sessions: session });
15 14 else res.json({});
16 15 } else if (req.originalSessionId != undefined) {
17 16 logger.debug("Found OriginalSessionID");
18 17
19 const session = getSession(req.originalSessionId as string);
18 const session = getSession(req.originalSessionId);
20 19 if (session) res.json({ queryId: req.queryId, Sessions: session });
21 20 else res.json({});
22 21 } else {
23 22 logger.debug("Found SessionRequest");
24 23
25 const session = getSession(String(_req.body));
24 const session = getSession(req);
26 25 if (session) res.json({ queryId: req.queryId, Sessions: session });
27 26 else res.json({});
28 27 }
29 28 };
30
31 export { findSessionsController };
Modified src/managers/sessionManager.ts +5 -2
@@ -44,7 +44,6 @@ function getSessionByID(sessionId: string): ISession | undefined {
44 44 return sessions.find(session => session.sessionId === sessionId);
45 45 }
46 46
47 //TODO: proper typings
48 47 function getSession(sessionIdOrRequest: string | IFindSessionRequest): any[] {
49 48 if (typeof sessionIdOrRequest === "string") {
50 49 const session = sessions.find(session => session.sessionId === sessionIdOrRequest);
@@ -63,7 +62,11 @@ function getSession(sessionIdOrRequest: string | IFindSessionRequest): any[] {
63 62 const request = sessionIdOrRequest;
64 63 const matchingSessions = sessions.filter(session => {
65 64 for (const key in request) {
66 if (key !== "eloRating" && key !== "queryId" && request[key] !== session[key as keyof ISession]) {
65 if (
66 key !== "eloRating" &&
67 key !== "queryId" &&
68 request[key as keyof IFindSessionRequest] !== session[key as keyof ISession]
69 ) {
67 70 return false;
68 71 }
69 72 }
Modified src/types/session.ts +10 -2
@@ -1,4 +1,3 @@
1 /* eslint-disable @typescript-eslint/no-explicit-any */
2 1 export interface ISession {
3 2 sessionId: string;
4 3 creatorId: string;
@@ -28,5 +27,14 @@ export interface ISession {
28 27 }
29 28
30 29 export interface IFindSessionRequest {
31 [key: string]: any;
30 id?: string;
31 originalSessionId?: string;
32 buildId?: number;
33 gameModeId?: number;
34 regionId?: number;
35 maxEloDifference?: number;
36 eloRating?: number;
37 enforceElo?: boolean;
38 xplatform?: boolean;
39 queryId?: number;
32 40 }