basic session management completed (#11)
This commit is contained in:
parent
b3a5479e75
commit
52a8ad6bd0
@ -1,6 +1,8 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { deleteSession } from "@/src/managers/sessionManager";
|
||||
|
||||
const deleteSessionController: RequestHandler = (_req, res) => {
|
||||
deleteSession(_req.query.sessionId as string);
|
||||
res.sendStatus(200);
|
||||
};
|
||||
|
||||
|
@ -1,9 +1,28 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { getSession } from "@/src/managers/sessionManager";
|
||||
|
||||
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);
|
||||
|
||||
res.json({ sessionId: { $oid: "64768f104722f795300c9fc0" }, rewardSeed: 5867309943877621023 });
|
||||
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({});
|
||||
}
|
||||
};
|
||||
|
||||
export { findSessionsController };
|
||||
|
@ -1,7 +1,13 @@
|
||||
import { RequestHandler } from "express";
|
||||
|
||||
const getNewRewardSeedController: RequestHandler = (_req, res) => {
|
||||
res.json({ rewardSeed: 5867309943877621023 });
|
||||
res.json({ rewardSeed: generateRewardSeed() });
|
||||
};
|
||||
|
||||
function generateRewardSeed(): number {
|
||||
const min = -Number.MAX_SAFE_INTEGER;
|
||||
const max = Number.MAX_SAFE_INTEGER;
|
||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
}
|
||||
|
||||
export { getNewRewardSeedController };
|
||||
|
@ -1,9 +1,12 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { createNewSession } from "@/src/managers/sessionManager";
|
||||
|
||||
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);
|
||||
|
||||
res.json({ sessionId: { $oid: "64768f104722f795300c9fc0" }, rewardSeed: 5867309943877621023 });
|
||||
res.json({ sessionId: { $oid: session.sessionId }, rewardSeed: 99999999 });
|
||||
};
|
||||
|
||||
export { hostSessionController };
|
||||
|
11
src/controllers/api/joinSessionController.ts
Normal file
11
src/controllers/api/joinSessionController.ts
Normal file
@ -0,0 +1,11 @@
|
||||
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]);
|
||||
res.json({ rewardSeed: session?.rewardSeed, sessionId: { $oid: session?.sessionId } });
|
||||
};
|
||||
|
||||
export { joinSessionController };
|
@ -1,12 +1,11 @@
|
||||
import { RequestHandler } from "express";
|
||||
|
||||
const logoutController: RequestHandler = (_req, res) => {
|
||||
const data = Buffer.from([0x31]);
|
||||
res.writeHead(200, {
|
||||
"Content-Type": "text/html",
|
||||
"Content-Length": data.length
|
||||
"Content-Length": 1
|
||||
});
|
||||
res.end(data);
|
||||
res.end("1");
|
||||
};
|
||||
|
||||
export { logoutController };
|
||||
|
@ -1,11 +1,13 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { updateSession } from "@/src/managers/sessionManager";
|
||||
|
||||
const updateSessionGetController: RequestHandler = (_req, res) => {
|
||||
res.json({});
|
||||
};
|
||||
const updateSessionPostController: RequestHandler = (_req, res) => {
|
||||
console.log("UpdateSessions POST Request:", JSON.parse(_req.body));
|
||||
|
||||
res.json({ hasStarted: true });
|
||||
console.log("ReqID:", _req.query.sessionId as string);
|
||||
updateSession(_req.query.sessionId as string, _req.body);
|
||||
res.json({});
|
||||
};
|
||||
export { updateSessionGetController, updateSessionPostController };
|
||||
|
132
src/managers/sessionManager.ts
Normal file
132
src/managers/sessionManager.ts
Normal file
@ -0,0 +1,132 @@
|
||||
import { Session, FindSessionRequest } from "@/src/types/session";
|
||||
|
||||
const sessions: Session[] = [];
|
||||
|
||||
function createNewSession(sessionData: Session, Creator: string): Session {
|
||||
const sessionId = getNewSessionID();
|
||||
const newSession: Session = {
|
||||
sessionId,
|
||||
creatorId: Creator,
|
||||
maxPlayers: sessionData.maxPlayers || 4,
|
||||
minPlayers: sessionData.minPlayers || 1,
|
||||
privateSlots: sessionData.privateSlots || 0,
|
||||
scoreLimit: sessionData.scoreLimit || 15,
|
||||
timeLimit: sessionData.timeLimit || 900,
|
||||
gameModeId: sessionData.gameModeId || 0,
|
||||
eloRating: sessionData.eloRating || 3,
|
||||
regionId: sessionData.regionId || 3,
|
||||
difficulty: sessionData.difficulty || 0,
|
||||
hasStarted: sessionData.hasStarted || false,
|
||||
enableVoice: sessionData.enableVoice || true,
|
||||
matchType: sessionData.matchType || "NORMAL",
|
||||
maps: sessionData.maps || [],
|
||||
originalSessionId: sessionData.originalSessionId || "",
|
||||
customSettings: sessionData.customSettings || "",
|
||||
rewardSeed: sessionData.rewardSeed || -1,
|
||||
guildId: sessionData.guildId || "",
|
||||
buildId: sessionData.buildId || 4920386201513015989,
|
||||
platform: sessionData.platform || 0,
|
||||
xplatform: sessionData.xplatform || true,
|
||||
freePublic: sessionData.freePublic || 3,
|
||||
freePrivate: sessionData.freePrivate || 0,
|
||||
fullReset: 0
|
||||
};
|
||||
sessions.push(newSession);
|
||||
return newSession;
|
||||
}
|
||||
|
||||
function getAllSessions(): Session[] {
|
||||
return sessions;
|
||||
}
|
||||
|
||||
function getSessionByID(sessionId: string): Session | undefined {
|
||||
return sessions.find(session => session.sessionId === sessionId);
|
||||
}
|
||||
|
||||
function getSession(sessionIdOrRequest: string | FindSessionRequest): any[] {
|
||||
if (typeof sessionIdOrRequest === "string") {
|
||||
const session = sessions.find(session => session.sessionId === sessionIdOrRequest);
|
||||
if (session) {
|
||||
console.log("Found Sessions:", session);
|
||||
return [
|
||||
{
|
||||
createdBy: session.creatorId,
|
||||
id: session.sessionId
|
||||
}
|
||||
];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
const request = sessionIdOrRequest as FindSessionRequest;
|
||||
const matchingSessions = sessions.filter(session => {
|
||||
for (const key in request) {
|
||||
if (key !== "eloRating" && key !== "queryId" && request[key] !== session[key as keyof Session]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
console.log("Found Matching Sessions:", matchingSessions);
|
||||
return true;
|
||||
});
|
||||
return matchingSessions.map(session => ({
|
||||
createdBy: session.creatorId,
|
||||
id: session.sessionId
|
||||
}));
|
||||
}
|
||||
|
||||
function getSessionByCreatorID(creatorId: string): Session | undefined {
|
||||
return sessions.find(session => session.creatorId === creatorId);
|
||||
}
|
||||
|
||||
function getNewSessionID(): string {
|
||||
const characters = "0123456789abcdef";
|
||||
const maxAttempts = 100;
|
||||
let sessionId = "";
|
||||
|
||||
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
||||
sessionId = "64";
|
||||
for (let i = 0; i < 22; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * characters.length);
|
||||
sessionId += characters[randomIndex];
|
||||
}
|
||||
|
||||
if (!sessions.some(session => session.sessionId === sessionId)) {
|
||||
return sessionId;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("Failed to generate a unique session ID");
|
||||
}
|
||||
|
||||
function updateSession(sessionId: string, sessionData: string): boolean {
|
||||
const session = sessions.find(session => session.sessionId === sessionId);
|
||||
if (!session) return false;
|
||||
try {
|
||||
const updatedData = JSON.parse(sessionData);
|
||||
Object.assign(session, updatedData);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Invalid JSON string for session update.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function deleteSession(sessionId: string): boolean {
|
||||
const index = sessions.findIndex(session => session.sessionId === sessionId);
|
||||
if (index !== -1) {
|
||||
sessions.splice(index, 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export {
|
||||
createNewSession,
|
||||
getAllSessions,
|
||||
getSessionByID,
|
||||
getSessionByCreatorID,
|
||||
getNewSessionID,
|
||||
updateSession,
|
||||
deleteSession,
|
||||
getSession
|
||||
};
|
@ -27,6 +27,8 @@ import { surveysController } from "@/src/controllers/api/surveysController";
|
||||
import { updateChallengeProgressController } from "@/src/controllers/api/updateChallengeProgressController";
|
||||
import { updateSessionGetController, updateSessionPostController } from "@/src/controllers/api/updateSessionController";
|
||||
import { viewController } from "@/src/controllers/api/viewController";
|
||||
import { joinSessionController } from "@/src/controllers/api/joinSessionController";
|
||||
|
||||
import express from "express";
|
||||
|
||||
const apiRouter = express.Router();
|
||||
@ -65,4 +67,5 @@ apiRouter.post("/updateSession.php", updateSessionPostController);
|
||||
apiRouter.post("/missionInventoryUpdate.php", missionInventoryUpdateController);
|
||||
apiRouter.post("/genericUpdate.php", genericUpdateController);
|
||||
apiRouter.post("/rerollRandomMod.php", rerollRandomModController);
|
||||
apiRouter.post("/joinSession.php", joinSessionController);
|
||||
export { apiRouter };
|
||||
|
27
src/types/session.ts
Normal file
27
src/types/session.ts
Normal file
@ -0,0 +1,27 @@
|
||||
export interface Session {
|
||||
sessionId: string;
|
||||
creatorId: string;
|
||||
maxPlayers: number;
|
||||
minPlayers: number;
|
||||
privateSlots: number;
|
||||
scoreLimit: number;
|
||||
timeLimit: number;
|
||||
gameModeId: number;
|
||||
eloRating: number;
|
||||
regionId: number;
|
||||
difficulty: number;
|
||||
hasStarted: boolean;
|
||||
enableVoice: boolean;
|
||||
matchType: string;
|
||||
maps: string[];
|
||||
originalSessionId: string;
|
||||
customSettings: string;
|
||||
rewardSeed: number;
|
||||
guildId: string;
|
||||
buildId: number;
|
||||
platform: number;
|
||||
xplatform: boolean;
|
||||
freePublic: number;
|
||||
freePrivate: number;
|
||||
fullReset: number;
|
||||
}
|
@ -24,7 +24,7 @@ export const platformCDNs = [
|
||||
|
||||
export const Nonce = 1231231233;
|
||||
|
||||
export const NRS = ["104.237.145.11", "139.144.69.73", "170.187.231.80", "45.33.58.33", "85.159.214.213"];
|
||||
export const NRS = ["localhost"];
|
||||
|
||||
export const DTLS = 99;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user