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 "@/src/constants/timeConstants";
import { addMiscItems, getInventory, updateCurrency } from "@/src/services/inventoryService";
import { getRecipe } from "@/src/services/itemDataService";
import { logger } from "@/src/utils/logger";
import { Types } from "mongoose";

export const startRecipe = async (recipeName: string, accountId: string) => {
    const recipe = getRecipe(recipeName);

    if (!recipe) {
        logger.error(`unknown recipe ${recipeName}`);
        throw new Error(`unknown recipe ${recipeName}`);
    }

    await updateCurrency(recipe.buildPrice, false, accountId);

    const ingredientsInverse = recipe.ingredients.map(component => ({
        ItemType: component.ItemType,
        ItemCount: component.ItemCount * -1
    }));

    const inventory = await getInventory(accountId);
    addMiscItems(inventory, ingredientsInverse);

    //buildtime is in seconds
    const completionDate = new Date(Date.now() + recipe.buildTime * unixTimesInMs.second);

    inventory.PendingRecipes.push({
        ItemType: recipeName,
        CompletionDate: completionDate,
        _id: new Types.ObjectId()
    });

    const newInventory = await inventory.save();

    return {
        RecipeId: { $id: newInventory.PendingRecipes[newInventory.PendingRecipes.length - 1]._id?.toString() }
    };
};