返回提交历史
Modified
src/controllers/api/deleteSessionController.ts
+5
-7
Modified
src/controllers/api/findSessionsController.ts
+3
-3
Modified
src/controllers/api/hostSessionController.ts
+6
-8
Modified
src/controllers/api/joinSessionController.ts
+3
-3
Modified
src/controllers/api/updateSessionController.ts
+5
-5
Modified
src/controllers/dynamic/aggregateSessionsController.ts
+3
-3
Added
src/models/sessionModel.ts
+39
-0
Renamed
src/services/sessionService.ts
+96
-52
Renamed
src/types/sessionTypes.ts
+5
-1
XFEstudio/XFESpaceNinjaServer
chore: use mongodb for sessions (#3904)
Closes #3893 Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3904 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
d9550a9e
代码差异
9 个文件
+165
-82
@@ -1,18 +1,16 @@
1
1
import type { RequestHandler } from "express";
2
import { deleteSession } from "../../managers/sessionManager.ts";
2
import { deleteSession } from "../../services/sessionService.ts";
3
3
import { getAccountForRequest } from "../../services/loginService.ts";
4
4
import { version_compare } from "../../helpers/inventoryHelpers.ts";
5
5
import gameToBuildVersion from "../../constants/gameToBuildVersion.ts";
6
6
7
const deleteSessionController: RequestHandler = async (_req, res) => {
8
const account = await getAccountForRequest(_req);
9
deleteSession(_req.query.sessionId as string);
7
export const deleteSessionController: RequestHandler = async (req, res) => {
8
const account = await getAccountForRequest(req);
9
await deleteSession(req.query.sessionId as string);
10
10
if (account.BuildLabel && version_compare(account.BuildLabel, gameToBuildVersion["18.16.0"]) < 0) {
11
11
// Pre-Specters of the Rail
12
res.send(_req.query.sessionId as string); // Unsure if this is correct, but the client is chill with it
12
res.send(req.query.sessionId as string); // Unsure if this is correct, but the client is chill with it
13
13
} else {
14
14
res.sendStatus(200);
15
15
}
16
16
};
17
18
export { deleteSessionController };
@@ -1,13 +1,13 @@
1
1
import type { RequestHandler } from "express";
2
import { getSession } from "../../managers/sessionManager.ts";
2
import { getSession } from "../../services/sessionService.ts";
3
3
import { logger } from "../../utils/logger.ts";
4
import type { IFindSessionRequest } from "../../types/session.ts";
4
import type { IFindSessionRequest } from "../../types/sessionTypes.ts";
5
5
import { getNrsAddresses } from "../../services/configService.ts";
6
6
7
7
export const findSessionsController: RequestHandler = async (_req, res) => {
8
8
const req = JSON.parse(String(_req.body)) as IFindSessionRequest;
9
9
logger.debug("FindSession Request:", req);
10
const sessions = getSession(req);
10
const sessions = await getSession(req);
11
11
12
12
if (!sessions.length && "id" in req) {
13
13
// (OpenWF-specific) Maybe NRS can tell us who the host of this session is...
@@ -1,25 +1,23 @@
1
1
import type { RequestHandler } from "express";
2
2
import { getAccountForRequest } from "../../services/loginService.ts";
3
import { createNewSession } from "../../managers/sessionManager.ts";
3
import { createNewSession } from "../../services/sessionService.ts";
4
4
import { logger } from "../../utils/logger.ts";
5
import type { IHostSessionRequest } from "../../types/session.ts";
5
import type { IHostSessionRequest } from "../../types/sessionTypes.ts";
6
6
import { JSONParse } from "json-with-bigint";
7
7
import { toOid2, version_compare } from "../../helpers/inventoryHelpers.ts";
8
8
import gameToBuildVersion from "../../constants/gameToBuildVersion.ts";
9
9
10
const hostSessionController: RequestHandler = async (req, res) => {
10
export const hostSessionController: RequestHandler = async (req, res) => {
11
11
const account = await getAccountForRequest(req);
12
12
const hostSessionRequest = JSONParse(String(req.body)) as IHostSessionRequest;
13
13
logger.debug("HostSession Request", { hostSessionRequest });
14
const session = createNewSession(hostSessionRequest, account._id);
14
const session = await createNewSession(hostSessionRequest, account._id);
15
15
logger.debug(`New Session Created`, { session });
16
16
17
17
if (account.BuildLabel && version_compare(account.BuildLabel, gameToBuildVersion["18.16.0"]) < 0) {
18
18
// Pre-Specters of the Rail
19
res.send(session.sessionId.toString());
19
res.send(session._id.toString());
20
20
} else {
21
res.json({ sessionId: toOid2(session.sessionId, account.BuildLabel), rewardSeed: session.rewardSeed });
21
res.json({ sessionId: toOid2(session._id, account.BuildLabel), rewardSeed: session.rewardSeed });
22
22
}
23
23
};
24
25
export { hostSessionController };
@@ -1,5 +1,5 @@
1
1
import type { RequestHandler } from "express";
2
import { getSessionByID } from "../../managers/sessionManager.ts";
2
import { getSessionByID } from "../../services/sessionService.ts";
3
3
import { logger } from "../../utils/logger.ts";
4
4
import { getAccountForRequest } from "../../services/loginService.ts";
5
5
import { toOid2 } from "../../helpers/inventoryHelpers.ts";
@@ -8,7 +8,7 @@ import { generateRewardSeed } from "../../services/rngService.ts";
8
8
export const joinSessionGetController: RequestHandler = async (req, res) => {
9
9
const account = await getAccountForRequest(req);
10
10
const sessionId = req.query.sessionId as string;
11
const session = getSessionByID(sessionId);
11
const session = await getSessionByID(sessionId);
12
12
if (!session) {
13
13
logger.warn(`joining an unknown session; rewardSeed will not be in sync`);
14
14
}
@@ -23,7 +23,7 @@ export const joinSessionPostController: RequestHandler = async (req, res) => {
23
23
const reqBody = JSON.parse(String(req.body)) as IJoinSessionRequest;
24
24
logger.debug(`JoinSession Request`, { reqBody });
25
25
const sessionId = reqBody.sessionIds[0];
26
const session = getSessionByID(sessionId);
26
const session = await getSessionByID(sessionId);
27
27
if (!session) {
28
28
logger.warn(`joining an unknown session; rewardSeed will not be in sync`);
29
29
}
@@ -1,15 +1,15 @@
1
1
import type { RequestHandler } from "express";
2
import { updateSession } from "../../managers/sessionManager.ts";
2
import { updateSession } from "../../services/sessionService.ts";
3
3
4
const updateSessionGetController: RequestHandler = (_req, res) => {
4
export const updateSessionGetController: RequestHandler = (_req, res) => {
5
5
res.json({});
6
6
};
7
const updateSessionPostController: RequestHandler = (_req, res) => {
7
8
export const updateSessionPostController: RequestHandler = async (req, res) => {
8
9
//console.log("UpdateSessions POST Request:", JSON.parse(String(_req.body)));
9
10
//console.log("ReqID:", _req.query.sessionId as string);
10
if (!updateSession(_req.query.sessionId as string, String(_req.body))) {
11
if (!(await updateSession(req.query.sessionId as string, String(req.body)))) {
11
12
res.status(400);
12
13
}
13
14
res.json({});
14
15
};
15
export { updateSessionGetController, updateSessionPostController };
@@ -1,9 +1,9 @@
1
1
import type { RequestHandler } from "express";
2
import { aggregateSessions } from "../../managers/sessionManager.ts";
2
import { aggregateSessions } from "../../services/sessionService.ts";
3
3
4
export const aggregateSessionsController: RequestHandler = (_req, res) => {
4
export const aggregateSessionsController: RequestHandler = async (_req, res) => {
5
5
res.json({
6
Results: aggregateSessions()
6
Results: await aggregateSessions()
7
7
} satisfies IAggregateSessionsResponse);
8
8
};
9
9
@@ -0,0 +1,39 @@
1
import { model, Schema, Types } from "mongoose";
2
import type { ISessionDatabase } from "../types/sessionTypes.ts";
3
4
export const sessionSchema = new Schema<ISessionDatabase>({
5
maxPlayers: Number,
6
minPlayers: Number,
7
privateSlots: Number,
8
scoreLimit: Number,
9
timeLimit: Number,
10
gameModeId: Number,
11
eloRating: Number,
12
regionId: Number,
13
difficulty: Number,
14
hasStarted: Boolean,
15
enableVoice: Boolean,
16
matchType: String,
17
maps: [String],
18
originalSessionId: String,
19
customSettings: String,
20
guildId: String,
21
buildId: BigInt,
22
freePublic: Number,
23
freePrivate: Number,
24
fullReset: Number,
25
26
creatorId: Types.ObjectId,
27
rewardSeed: BigInt,
28
platform: Number,
29
xplatform: Boolean,
30
31
lastUpdate: Date
32
});
33
34
sessionSchema.index({ originalSessionId: 1 });
35
36
// The client seems to send an updateSession request at least once every 2 minutes.
37
sessionSchema.index({ lastUpdate: 1 }, { expireAfterSeconds: 5 * 60 });
38
39
export const Session = model<ISessionDatabase>("Session", sessionSchema);
@@ -1,21 +1,25 @@
1
import { generateRewardSeed } from "../services/rngService.ts";
1
import { Session } from "../models/sessionModel.ts";
2
import { generateRewardSeed } from "./rngService.ts";
2
3
import { Platform } from "../types/loginTypes.ts";
3
4
import type {
4
5
ISession,
5
6
IFindSessionRequest,
6
7
IFindSessionResponseSession,
7
IHostSessionRequest
8
} from "../types/session.ts";
8
IHostSessionRequest,
9
ISessionDatabase
10
} from "../types/sessionTypes.ts";
9
11
import { logger } from "../utils/logger.ts";
10
12
import { JSONParse } from "json-with-bigint";
11
import { Types } from "mongoose";
13
import { Types, type QueryFilter } from "mongoose";
12
14
13
const sessions: ISession[] = [];
15
//const sessions: ISession[] = [];
14
16
15
function createNewSession(sessionData: IHostSessionRequest, Creator: Types.ObjectId): ISession {
16
const sessionId = new Types.ObjectId();
17
const newSession: ISession = {
18
sessionId,
17
export const createNewSession = async (
18
sessionData: IHostSessionRequest,
19
Creator: Types.ObjectId
20
): Promise<ISession> => {
21
const newSession: ISessionDatabase = {
22
_id: new Types.ObjectId(),
19
23
creatorId: Creator,
20
24
maxPlayers: sessionData.maxPlayers, // || 4
21
25
minPlayers: sessionData.minPlayers, // || 1
@@ -39,30 +43,57 @@ function createNewSession(sessionData: IHostSessionRequest, Creator: Types.Objec
39
43
xplatform: sessionData.xplatform ?? false,
40
44
freePublic: sessionData.freePublic, // || 3
41
45
freePrivate: sessionData.freePrivate, // || 0
42
fullReset: 0
46
fullReset: 0,
47
48
lastUpdate: new Date()
43
49
};
44
50
if (newSession.rewardSeed == -1) {
45
51
newSession.rewardSeed = generateRewardSeed();
46
52
}
47
sessions.push(newSession);
53
54
await Session.create(newSession);
55
//sessions.push(newSession);
56
48
57
return newSession;
49
}
58
};
50
59
51
function getAllSessions(): ISession[] {
52
return sessions;
53
}
60
export const getSessionByID = async (sessionId: string | Types.ObjectId): Promise<ISessionDatabase | null> => {
61
return await Session.findById(sessionId);
62
};
54
63
55
function getSessionByID(sessionId: string | Types.ObjectId): ISession | undefined {
56
return sessions.find(session => session.sessionId.equals(sessionId));
57
}
64
export const getSession = async (request: IFindSessionRequest): Promise<IFindSessionResponseSession[]> => {
65
const query: QueryFilter<ISessionDatabase> = {};
66
if ("id" in request) {
67
query._id = request.id;
68
} else if ("originalSessionId" in request) {
69
query.originalSessionId = request.originalSessionId;
70
} else {
71
query.hasStarted = false;
72
query.buildId = request.buildId;
73
query.gameModeId = request.gameModeId;
74
if (request.freePublic) {
75
query.freePublic = { $gte: 1 };
76
}
77
query.regionId = request.regionId;
78
query.eloRating = {
79
$gte: request.eloRating - request.maxEloDifference,
80
$lte: request.eloRating + request.maxEloDifference
81
};
82
if (request.maps) {
83
query.maps = request.maps;
84
}
85
}
86
return (await Session.find(query, "creatorId")).map(session => ({
87
createdBy: session.creatorId.toString(),
88
id: session._id.toString()
89
}));
58
90
59
function getSession(request: IFindSessionRequest): IFindSessionResponseSession[] {
60
return sessions
91
/*return sessions
61
92
.filter(session => {
62
93
if ("id" in request) {
63
return session.sessionId.equals(request.id);
94
return session._id.equals(request.id);
64
95
} else if ("originalSessionId" in request) {
65
return session.sessionId.equals(request.originalSessionId);
96
return session._id.equals(request.originalSessionId);
66
97
} else {
67
98
return (
68
99
!session.hasStarted &&
@@ -77,17 +108,16 @@ function getSession(request: IFindSessionRequest): IFindSessionResponseSession[]
77
108
})
78
109
.map(session => ({
79
110
createdBy: session.creatorId.toString(),
80
id: session.sessionId.toString()
81
}));
82
}
83
84
function getSessionByCreatorID(creatorId: string | Types.ObjectId): ISession | undefined {
85
return sessions.find(session => session.creatorId.equals(creatorId));
86
}
111
id: session._id.toString()
112
}));*/
113
};
87
114
88
function updateSession(sessionId: string | Types.ObjectId, updateData: string): boolean {
115
export const updateSession = async (sessionId: string | Types.ObjectId, updateData: string): Promise<boolean> => {
89
116
logger.debug(`session update: ${updateData}`);
90
const session = sessions.find(session => session.sessionId.equals(sessionId));
117
118
//const session = sessions.find(session => session._id.equals(sessionId));
119
const session = await Session.findById(sessionId);
120
91
121
if (!session) {
92
122
return false;
93
123
}
@@ -127,20 +157,45 @@ function updateSession(sessionId: string | Types.ObjectId, updateData: string):
127
157
}
128
158
}
129
159
//logger.debug(`session after update:`, session);
160
161
session.lastUpdate = new Date();
162
await session.save();
163
130
164
return true;
131
}
165
};
166
167
export const deleteSession = async (sessionId: string | Types.ObjectId): Promise<void> => {
168
await Session.deleteOne({ _id: sessionId });
132
169
133
function deleteSession(sessionId: string | Types.ObjectId): boolean {
134
const index = sessions.findIndex(session => session.sessionId.equals(sessionId));
170
/*const index = sessions.findIndex(session => session._id.equals(sessionId));
135
171
if (index !== -1) {
136
172
sessions.splice(index, 1);
137
return true;
138
}
139
return false;
140
}
173
}*/
174
};
175
176
export const aggregateSessions = (): Promise<{ gameModeId: number; count: number }[]> => {
177
return Session.aggregate([
178
{
179
$match: {
180
freePublic: { $ne: 0 }
181
}
182
},
183
{
184
$group: {
185
_id: "$gameModeId",
186
count: { $sum: 1 }
187
}
188
},
189
{
190
$project: {
191
_id: 0,
192
gameModeId: "$_id",
193
count: 1
194
}
195
}
196
]);
141
197
142
function aggregateSessions(): { gameModeId: number; count: number }[] {
143
const result: { gameModeId: number; count: number }[] = [];
198
/*const result: { gameModeId: number; count: number }[] = [];
144
199
for (const session of sessions) {
145
200
if (session.freePublic != 0) {
146
201
const obj = result.find(x => x.gameModeId == session.gameModeId);
@@ -151,16 +206,5 @@ function aggregateSessions(): { gameModeId: number; count: number }[] {
151
206
}
152
207
}
153
208
}
154
return result;
155
}
156
157
export {
158
createNewSession,
159
getAllSessions,
160
getSessionByID,
161
getSessionByCreatorID,
162
updateSession,
163
deleteSession,
164
getSession,
165
aggregateSessions
209
return result;*/
166
210
};
@@ -28,13 +28,17 @@ export interface IHostSessionRequest {
28
28
}
29
29
30
30
export interface ISession extends Omit<IHostSessionRequest, "rewardSeed" | "platform" | "xplatform"> {
31
sessionId: Types.ObjectId;
31
_id: Types.ObjectId;
32
32
creatorId: Types.ObjectId;
33
33
rewardSeed: number | bigint;
34
34
platform: Platform;
35
35
xplatform: boolean;
36
36
}
37
37
38
export interface ISessionDatabase extends ISession {
39
lastUpdate: Date;
40
}
41
38
42
export type IFindSessionRequest = { queryId: number } & (
39
43
| {
40
44
id: string;