From e6708152c072b42f7c3f6a1cd83d556b6527658c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=82ngelo=20Tadeucci?= Date: Sun, 4 Jun 2023 18:24:05 -0300 Subject: [PATCH] fix typescript errors --- src/controllers/api/findSessionsController.ts | 39 ++++++++++--------- src/controllers/api/hostSessionController.ts | 5 +-- src/controllers/api/joinSessionController.ts | 5 +-- .../api/rerollRandomModController.ts | 2 +- .../api/updateSessionController.ts | 6 +-- .../custom/createAccountController.ts | 1 + src/index.ts | 8 +--- src/managers/sessionManager.ts | 16 +++++--- src/types/session.ts | 14 ++++++- 9 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/controllers/api/findSessionsController.ts b/src/controllers/api/findSessionsController.ts index 9e4da0f5..558aed25 100644 --- a/src/controllers/api/findSessionsController.ts +++ b/src/controllers/api/findSessionsController.ts @@ -1,28 +1,29 @@ import { RequestHandler } from "express"; import { getSession } from "@/src/managers/sessionManager"; +import { FindSessionRequest } from "@/src/types/session"; const findSessionsController: RequestHandler = (_req, res) => { - console.log("FindSession Request:", JSON.parse(_req.body)); - let req = JSON.parse(_req.body); - if (req.id != undefined) { - console.log("Found ID"); - let session = getSession(req.id); + const sessionRequest = JSON.parse(String(_req.body)) as FindSessionRequest; + if (sessionRequest.id != undefined) { + const session = getSession(sessionRequest.id); - if (session) res.json({ queryId: req.queryId, Sessions: session }); - else res.json({}); - } else if (req.originalSessionId != undefined) { - console.log("Found OriginalSessionID"); - - let session = getSession(req.originalSessionId); - if (session) res.json({ queryId: req.queryId, Sessions: session }); - else res.json({}); - } else { - console.log("Found SessionRequest"); - - let session = getSession(_req.body); - if (session) res.json({ queryId: req.queryId, Sessions: session }); - else res.json({}); + if (session) { + return res.json({ queryId: sessionRequest.queryId, Sessions: session }); + } + return res.json({}); + } else if (sessionRequest.originalSessionId != undefined) { + const session = getSession(sessionRequest.originalSessionId); + if (session) { + return res.json({ queryId: sessionRequest.queryId, Sessions: session }); + } + return res.json({}); } + + const session = getSession(sessionRequest); + if (session) { + return res.json({ queryId: sessionRequest.queryId, Sessions: session }); + } + return res.json({}); }; export { findSessionsController }; diff --git a/src/controllers/api/hostSessionController.ts b/src/controllers/api/hostSessionController.ts index c7ac7cb9..58e30154 100644 --- a/src/controllers/api/hostSessionController.ts +++ b/src/controllers/api/hostSessionController.ts @@ -1,10 +1,9 @@ import { RequestHandler } from "express"; import { createNewSession } from "@/src/managers/sessionManager"; +import { Session } from "@/src/types/session"; const hostSessionController: RequestHandler = (_req, res) => { - console.log("HostSession Request:", JSON.parse(_req.body)); - let session = createNewSession(JSON.parse(_req.body), _req.query.accountId as string); - console.log("New Session Created: ", session); + const session = createNewSession(JSON.parse(String(_req.body)) as Session, _req.query.accountId as string); res.json({ sessionId: { $oid: session.sessionId }, rewardSeed: 99999999 }); }; diff --git a/src/controllers/api/joinSessionController.ts b/src/controllers/api/joinSessionController.ts index 3cd6a7e0..5ce71775 100644 --- a/src/controllers/api/joinSessionController.ts +++ b/src/controllers/api/joinSessionController.ts @@ -2,9 +2,8 @@ import { RequestHandler } from "express"; import { getSessionByID } from "@/src/managers/sessionManager"; const joinSessionController: RequestHandler = (_req, res) => { - console.log("JoinSession Request:", JSON.parse(_req.body)); - let req = JSON.parse(_req.body); - let session = getSessionByID(req.sessionIds[0]); + const req = JSON.parse(String(_req.body)) as { sessionIds: string[] }; + const session = getSessionByID(req.sessionIds[0]); res.json({ rewardSeed: session?.rewardSeed, sessionId: { $oid: session?.sessionId } }); }; diff --git a/src/controllers/api/rerollRandomModController.ts b/src/controllers/api/rerollRandomModController.ts index eabffb2e..b4d03365 100644 --- a/src/controllers/api/rerollRandomModController.ts +++ b/src/controllers/api/rerollRandomModController.ts @@ -1,7 +1,7 @@ import { RequestHandler } from "express"; const rerollRandomModController: RequestHandler = (_req, res) => { - console.log("RerollRandomMod Request:", _req.body.toString("hex").replace(/(.)(.)/g, "$1$2 ")); + // console.log("RerollRandomMod Request:", _req.body.toString("hex").replace(/(.)(.)/g, "$1$2 ")); res.json({}); }; diff --git a/src/controllers/api/updateSessionController.ts b/src/controllers/api/updateSessionController.ts index e949d83e..a80380a5 100644 --- a/src/controllers/api/updateSessionController.ts +++ b/src/controllers/api/updateSessionController.ts @@ -5,9 +5,9 @@ const updateSessionGetController: RequestHandler = (_req, res) => { res.json({}); }; const updateSessionPostController: RequestHandler = (_req, res) => { - console.log("UpdateSessions POST Request:", JSON.parse(_req.body)); - console.log("ReqID:", _req.query.sessionId as string); - updateSession(_req.query.sessionId as string, _req.body); + // console.log("UpdateSessions POST Request:", JSON.parse(_req.body)); + // console.log("ReqID:", _req.query.sessionId as string); + updateSession(_req.query.sessionId as string, String(_req.body)); res.json({}); }; export { updateSessionGetController, updateSessionPostController }; diff --git a/src/controllers/custom/createAccountController.ts b/src/controllers/custom/createAccountController.ts index 0d0d567d..2fad8eb3 100644 --- a/src/controllers/custom/createAccountController.ts +++ b/src/controllers/custom/createAccountController.ts @@ -2,6 +2,7 @@ import { toCreateAccount, toDatabaseAccount } from "@/src/helpers/customHelpers" import { createAccount } from "@/src/services/loginService"; import { RequestHandler } from "express"; +// eslint-disable-next-line @typescript-eslint/no-misused-promises const createAccountController: RequestHandler = async (req, res) => { const createAccountData = toCreateAccount(req.body); const databaseAccount = toDatabaseAccount(createAccountData); diff --git a/src/index.ts b/src/index.ts index 689fa854..3586a038 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,6 @@ import http from "http"; import https from "https"; import fs from "node:fs"; import { app } from "./app"; -//const morgan = require("morgan"); -//const bodyParser = require("body-parser"); const options = { key: fs.readFileSync("static/certs/key.pem"), @@ -11,9 +9,5 @@ const options = { passphrase: "123456" }; -// const server = http.createServer(app).listen(80); http.createServer(app).listen(80, () => console.log("server started on port 80")); -const server = https.createServer(options, app).listen(443, () => console.log("server started on port 443")); - -// server.keepAliveTimeout = 60 * 1000 + 1000; -// server.headersTimeout = 60 * 1000 + 2000; +https.createServer(options, app).listen(443, () => console.log("server started on port 443")); diff --git a/src/managers/sessionManager.ts b/src/managers/sessionManager.ts index de990f9e..fd87b68c 100644 --- a/src/managers/sessionManager.ts +++ b/src/managers/sessionManager.ts @@ -24,7 +24,7 @@ function createNewSession(sessionData: Session, Creator: string): Session { customSettings: sessionData.customSettings || "", rewardSeed: sessionData.rewardSeed || -1, guildId: sessionData.guildId || "", - buildId: sessionData.buildId || 4920386201513015989, + buildId: sessionData.buildId || 4920386201513015989n, platform: sessionData.platform || 0, xplatform: sessionData.xplatform || true, freePublic: sessionData.freePublic || 3, @@ -43,7 +43,10 @@ function getSessionByID(sessionId: string): Session | undefined { return sessions.find(session => session.sessionId === sessionId); } -function getSession(sessionIdOrRequest: string | FindSessionRequest): any[] { +function getSession(sessionIdOrRequest: string | FindSessionRequest): { + createdBy: string; + id: string; +}[] { if (typeof sessionIdOrRequest === "string") { const session = sessions.find(session => session.sessionId === sessionIdOrRequest); if (session) { @@ -58,7 +61,7 @@ function getSession(sessionIdOrRequest: string | FindSessionRequest): any[] { return []; } - const request = sessionIdOrRequest as FindSessionRequest; + const request = sessionIdOrRequest; const matchingSessions = sessions.filter(session => { for (const key in request) { if (key !== "eloRating" && key !== "queryId" && request[key] !== session[key as keyof Session]) { @@ -100,9 +103,12 @@ function getNewSessionID(): string { function updateSession(sessionId: string, sessionData: string): boolean { const session = sessions.find(session => session.sessionId === sessionId); - if (!session) return false; + if (!session) { + return false; + } + try { - const updatedData = JSON.parse(sessionData); + const updatedData: unknown = JSON.parse(sessionData); Object.assign(session, updatedData); return true; } catch (error) { diff --git a/src/types/session.ts b/src/types/session.ts index 10ed78ea..5e6fce97 100644 --- a/src/types/session.ts +++ b/src/types/session.ts @@ -18,7 +18,7 @@ export interface Session { customSettings: string; rewardSeed: number; guildId: string; - buildId: number; + buildId: number | bigint; platform: number; xplatform: boolean; freePublic: number; @@ -27,5 +27,15 @@ export interface Session { } export interface FindSessionRequest { - [key: string]: any; + id?: string; + originalSessionId?: string; + buildId: number; + gameModeId: number; + regionId: number; + maxEloDifference: number; + eloRating: number; + enforceElo: boolean; + platform: number; + xplatform: boolean; + queryId: number; }