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

feat: joining foreign sessions (#3656)

Given the current situation of people sharing the same NRS without necessarily sharing the same SNS, this seems like a neat albeit niche capability to have. Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3656 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>

02714f53
Sainan <63328889+Sainan@users.noreply.github.com>
提交于

代码差异

2 个文件 +42 -7
Modified src/controllers/api/findSessionsController.ts +23 -1
@@ -2,12 +2,34 @@ import type { RequestHandler } from "express";
2 2 import { getSession } from "../../managers/sessionManager.ts";
3 3 import { logger } from "../../utils/logger.ts";
4 4 import type { IFindSessionRequest } from "../../types/session.ts";
5 import { getNrsAddress } from "../../services/configService.ts";
5 6
6 export const findSessionsController: RequestHandler = (_req, res) => {
7 export const findSessionsController: RequestHandler = async (_req, res) => {
7 8 const req = JSON.parse(String(_req.body)) as IFindSessionRequest;
8 9 logger.debug("FindSession Request:", req);
9 10 const sessions = getSession(req);
11
12 if (!sessions.length && "id" in req) {
13 // (OpenWF-specific) Maybe NRS can tell us who the host of this session is...
14 logger.debug(`Unknown session id, asking NRS...`);
15 try {
16 const [nrsAddr, nrsPort] = getNrsAddress();
17 const res = await fetch(`http://${nrsAddr}:${nrsPort}/api/session/${req.id}`);
18 const json = (await res.json()) as INrsSessionInfo;
19 if (json.hid) {
20 sessions.push({ id: req.id, createdBy: json.hid });
21 }
22 } catch (e) {
23 /* empty */
24 }
25 }
26
10 27 logger.debug("FindSession Result:", { sessions });
11 28 if (sessions.length) res.json({ queryId: req.queryId, Sessions: sessions });
12 29 else res.json({});
13 30 };
31
32 interface INrsSessionInfo {
33 id?: string;
34 hid?: string;
35 }
Modified src/controllers/api/joinSessionController.ts +19 -6
@@ -3,21 +3,34 @@ import { getSessionByID } from "../../managers/sessionManager.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";
6 import { generateRewardSeed } from "../../services/rngService.ts";
6 7
7 8 export const joinSessionGetController: RequestHandler = async (req, res) => {
8 9 const account = await getAccountForRequest(req);
9 const session = getSessionByID(req.query.sessionId as string);
10 if (session) res.json({ rewardSeed: session.rewardSeed, sessionId: toOid2(session.sessionId, account.BuildLabel) });
11 else res.json({});
10 const sessionId = req.query.sessionId as string;
11 const session = getSessionByID(sessionId);
12 if (!session) {
13 logger.warn(`joining an unknown session; rewardSeed will not be in sync`);
14 }
15 res.json({
16 rewardSeed: session?.rewardSeed ?? generateRewardSeed(),
17 sessionId: toOid2(sessionId, account.BuildLabel)
18 });
12 19 };
13 20
14 21 export const joinSessionPostController: RequestHandler = async (req, res) => {
15 22 const account = await getAccountForRequest(req);
16 23 const reqBody = JSON.parse(String(req.body)) as IJoinSessionRequest;
17 24 logger.debug(`JoinSession Request`, { reqBody });
18 const session = getSessionByID(reqBody.sessionIds[0]);
19 if (session) res.json({ rewardSeed: session.rewardSeed, sessionId: toOid2(session.sessionId, account.BuildLabel) });
20 else res.json({});
25 const sessionId = reqBody.sessionIds[0];
26 const session = getSessionByID(sessionId);
27 if (!session) {
28 logger.warn(`joining an unknown session; rewardSeed will not be in sync`);
29 }
30 res.json({
31 rewardSeed: session?.rewardSeed ?? generateRewardSeed(),
32 sessionId: toOid2(sessionId, account.BuildLabel)
33 });
21 34 };
22 35
23 36 interface IJoinSessionRequest {