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 { unixTimesInMs } from "../../constants/timeConstants.ts";
import { getJSONfromString } from "../../helpers/stringHelpers.ts";
import { getInventory, setBooster } from "../../services/inventoryService.ts";
import { getAccountIdForRequest } from "../../services/loginService.ts";
import { getRandomInt } from "../../services/rngService.ts";
import type { RequestHandler } from "express";
import { ExportBoosters } from "warframe-public-export-plus";
import type { IOid } from "../../types/commonTypes.ts";
import { toMongoDate } from "../../helpers/inventoryHelpers.ts";

export const hubBlessingController: RequestHandler = async (req, res) => {
    const accountId = await getAccountIdForRequest(req);
    const data = getJSONfromString<IHubBlessingRequest>(String(req.body));
    if (req.query.mode == "send") {
        const boosterType = ExportBoosters[data.booster!].typeName;

        const inventory = await getInventory(accountId, "noBlessingCooldown BlessingCooldown Boosters");
        inventory.BlessingCooldown = new Date(inventory.noBlessingCooldown ? 0 : Date.now() + 23 * unixTimesInMs.hour);
        setBooster(boosterType, Math.trunc(Date.now() / 1000) + 3 * 3600, inventory); // At some point in the past, the blesser requested their own blessings, but now sending it automatically gives it to you.
        await inventory.save();

        let token = "";
        for (let i = 0; i != 32; ++i) {
            token += getRandomInt(0, 15).toString(16);
        }

        res.json({
            BlessingCooldown: toMongoDate(inventory.BlessingCooldown),
            SendTime: Math.trunc(Date.now() / 1000).toString(),
            Expiry: Math.trunc(Date.now() / 1000 + 3 * 3600).toString(), // added in 38.6.0
            Token: token
        });
    } else {
        if (data.booster) {
            // < 38.6.0 (unsure about the exact version that changed it)
            const boosterType = ExportBoosters[data.booster].typeName;

            const inventory = await getInventory(accountId, "Boosters");
            setBooster(boosterType, Math.trunc(Date.now() / 1000) + 3 * 3600, inventory);
            await inventory.save();

            res.json({
                BoosterType: data.booster,
                Sender: data.senderId
            });
        } else {
            // >= 38.6.0 (unsure about the exact version that changed it)
            const inventory = await getInventory(accountId, "Boosters");
            for (const blessing of data.PendingHubBlessings!) {
                const boosterType = ExportBoosters[blessing.booster].typeName;
                setBooster(boosterType, parseInt(blessing.sendTime) + 3 * 3600, inventory);
            }
            await inventory.save();
            res.end();
        }
    }
};

interface IHubBlessingRequest {
    booster?: string;
    senderId?: string; // mode=request
    sendTime?: string; // mode=request
    token?: string; // mode=request
    PendingHubBlessings?: {
        booster: string;
        senderId: IOid;
        sendTime: string;
        token: string;
    }[];
}