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 { checkCalendarAutoAdvance, getCalendarProgress, getInventory } from "../../services/inventoryService.ts";
import { getAccountIdForRequest } from "../../services/loginService.ts";
import { handleStoreItemAcquisition } from "../../services/purchaseService.ts";
import { getWorldState } from "../../services/worldStateService.ts";
import type { IInventoryChanges } from "../../types/purchaseTypes.ts";
import type { RequestHandler } from "express";

// GET request; query parameters: CompletedEventIdx=0&Iteration=4&Version=19&Season=CST_SUMMER
export const completeCalendarEventController: RequestHandler = async (req, res) => {
    const accountId = await getAccountIdForRequest(req);
    const inventory = await getInventory(accountId, undefined);
    const calendarProgress = getCalendarProgress(inventory);
    const currentSeason = getWorldState().KnownCalendarSeasons[0];
    let inventoryChanges: IInventoryChanges = {};
    const dayIndex = calendarProgress.SeasonProgress.LastCompletedDayIdx + 1;
    const day = currentSeason.Days[dayIndex];
    if (day.events.length != 0) {
        if (day.events[0].type == "CET_CHALLENGE") {
            throw new Error(`completeCalendarEvent should not be used for challenges`);
        }
        const selection = day.events[parseInt(req.query.CompletedEventIdx as string)];
        if (selection.type == "CET_REWARD") {
            inventoryChanges = (await handleStoreItemAcquisition(selection.reward!, inventory)).InventoryChanges;
        } else if (selection.type == "CET_UPGRADE") {
            calendarProgress.YearProgress.Upgrades.push(selection.upgrade!);
        } else if (selection.type != "CET_PLOT") {
            throw new Error(`unexpected selection type: ${selection.type}`);
        }
    }
    calendarProgress.SeasonProgress.LastCompletedDayIdx = dayIndex;
    checkCalendarAutoAdvance(inventory, currentSeason);
    await inventory.save();
    res.json({
        InventoryChanges: inventoryChanges,
        CalendarProgress: inventory.CalendarProgress
    });
};