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 { getJSONfromString } from "../../helpers/stringHelpers.ts";
import { getAccountForRequest, getBuildVersion } from "../../services/loginService.ts";
import { addCalendarProgress, addChallenges, addKahlProgress, getInventory } from "../../services/inventoryService.ts";
import type {
    IChallengeProgress,
    ISeasonChallenge,
    IWeeklyMissionChallengeInfo
} from "../../types/inventoryTypes/inventoryTypes.ts";
import type { IAffiliationMods, IInventoryChanges } from "../../types/purchaseTypes.ts";
import { getEntriesUnsafe } from "../../utils/ts-utils.ts";
import { logger } from "../../utils/logger.ts";

export const updateChallengeProgressController: RequestHandler = async (req, res) => {
    const challenges = getJSONfromString<IUpdateChallengeProgressRequest>(String(req.body));
    const account = await getAccountForRequest(req);
    logger.debug(`challenge report:`, challenges);

    const inventory = await getInventory(account._id, undefined); // Challenge completion can give any item in theory
    const response: IUpdateChallengeProgressResponse = {
        AffiliationMods: [],
        InventoryChanges: {}
    };
    if (challenges.ChallengeProgress) {
        const buildVersion = getBuildVersion(req, account);
        response.AffiliationMods = await addChallenges(
            buildVersion,
            inventory,
            challenges.ChallengeProgress,
            challenges.SeasonChallengeCompletions,
            response.InventoryChanges
        );
    }
    for (const [key, value] of getEntriesUnsafe(challenges)) {
        if (value === undefined) {
            logger.error(`Challenge progress update key ${key} has no value`);
            continue;
        }
        switch (key) {
            case "ChallengesFixVersion":
                inventory.ChallengesFixVersion = value;
                break;

            case "SeasonChallengeHistory":
                value.forEach(({ challenge, id }) => {
                    const itemIndex = inventory.SeasonChallengeHistory.findIndex(i => i.challenge === challenge);
                    if (itemIndex !== -1) {
                        inventory.SeasonChallengeHistory[itemIndex].id = id;
                    } else {
                        inventory.SeasonChallengeHistory.push({ challenge, id });
                    }
                });
                break;

            case "CalendarProgress":
                addCalendarProgress(inventory, value);
                break;

            case "WeeklyMissionChallengeInfo":
                addKahlProgress(inventory, value, response.InventoryChanges);
                response.ProcessedWeeklyMissionChallengeInfos = value;
                break;

            case "ChallengeProgress":
            case "SeasonChallengeCompletions":
            case "ChallengePTS":
            case "crossPlaySetting":
                break;
            default:
                logger.warn(`unknown challenge progress entry`, { key, value });
        }
    }
    await inventory.save();

    res.json(response);
};

interface IUpdateChallengeProgressRequest {
    ChallengePTS?: number;
    ChallengesFixVersion?: number;
    ChallengeProgress?: IChallengeProgress[];
    SeasonChallengeHistory?: ISeasonChallenge[];
    SeasonChallengeCompletions?: ISeasonChallenge[];
    CalendarProgress?: { challenge: string }[];
    WeeklyMissionChallengeInfo?: IWeeklyMissionChallengeInfo[];
    crossPlaySetting?: string;
}

interface IUpdateChallengeProgressResponse {
    AffiliationMods: IAffiliationMods[];
    InventoryChanges: IInventoryChanges; // U41+
    ProcessedWeeklyMissionChallengeInfos?: IWeeklyMissionChallengeInfo[];
}