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

XFESpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 0 Star 0
UTF-8
import type { RequestHandler } from "express";
import { getSession } from "../../services/sessionService.ts";
import { logger } from "../../utils/logger.ts";
import type { IFindSessionRequest, IFindSessionResponse } from "../../types/sessionTypes.ts";
import { getNrsAddresses } from "../../services/configService.ts";

export const findSessionsController: RequestHandler = async (_req, res) => {
    const req = JSON.parse(String(_req.body)) as IFindSessionRequest;
    logger.debug("FindSession Request:", req);
    const sessions = await getSession(req);

    if (!sessions.length && "id" in req) {
        // (OpenWF-specific) Maybe NRS can tell us who the host of this session is...
        logger.debug(`Unknown session id, asking NRS...`);
        try {
            for (const [nrsAddr, nrsPort] of getNrsAddresses()) {
                const res = await fetch(`http://${nrsAddr}:${nrsPort}/api/session/${req.id}`);
                const json = (await res.json()) as INrsSessionInfo;
                if (json.hid) {
                    sessions.push({ id: req.id, createdBy: json.hid });
                    break;
                }
            }
        } catch (e) {
            /* empty */
        }
    }

    logger.debug("FindSession Result:", { sessions });
    if (sessions.length) res.json({ queryId: req.queryId, Sessions: sessions } satisfies IFindSessionResponse);
    else res.json({});
};

interface INrsSessionInfo {
    id?: string;
    hid?: string;
}