返回提交历史
Modified
src/controllers/api/startRecipeController.ts
+36
-3
Deleted
src/services/recipeService.ts
+0
-39
XFEstudio/SpaceNinjaServer
chore: remove recipeService (#659)
230c0303
代码差异
2 个文件
+36
-42
@@ -1,8 +1,11 @@
1
1
import { getAccountIdForRequest } from "@/src/services/loginService";
2
2
import { getJSONfromString } from "@/src/helpers/stringHelpers";
3
import { startRecipe } from "@/src/services/recipeService";
4
3
import { logger } from "@/src/utils/logger";
5
4
import { RequestHandler } from "express";
5
import { getRecipe } from "@/src/services/itemDataService";
6
import { addMiscItems, getInventory, updateCurrency } from "@/src/services/inventoryService";
7
import { unixTimesInMs } from "@/src/constants/timeConstants";
8
import { Types } from "mongoose";
6
9
7
10
interface IStartRecipeRequest {
8
11
RecipeName: string;
@@ -15,6 +18,36 @@ export const startRecipeController: RequestHandler = async (req, res) => {
15
18
16
19
const accountId = await getAccountIdForRequest(req);
17
20
18
const newRecipeId = await startRecipe(startRecipeRequest.RecipeName, accountId);
19
res.json(newRecipeId);
21
const recipeName = startRecipeRequest.RecipeName;
22
const recipe = getRecipe(recipeName);
23
24
if (!recipe) {
25
logger.error(`unknown recipe ${recipeName}`);
26
throw new Error(`unknown recipe ${recipeName}`);
27
}
28
29
await updateCurrency(recipe.buildPrice, false, accountId);
30
31
const ingredientsInverse = recipe.ingredients.map(component => ({
32
ItemType: component.ItemType,
33
ItemCount: component.ItemCount * -1
34
}));
35
36
const inventory = await getInventory(accountId);
37
addMiscItems(inventory, ingredientsInverse);
38
39
//buildtime is in seconds
40
const completionDate = new Date(Date.now() + recipe.buildTime * unixTimesInMs.second);
41
42
inventory.PendingRecipes.push({
43
ItemType: recipeName,
44
CompletionDate: completionDate,
45
_id: new Types.ObjectId()
46
});
47
48
const newInventory = await inventory.save();
49
50
res.json({
51
RecipeId: { $oid: newInventory.PendingRecipes[newInventory.PendingRecipes.length - 1]._id.toString() }
52
});
20
53
};
@@ -1,39 +0,0 @@
1
import { unixTimesInMs } from "@/src/constants/timeConstants";
2
import { addMiscItems, getInventory, updateCurrency } from "@/src/services/inventoryService";
3
import { getRecipe } from "@/src/services/itemDataService";
4
import { logger } from "@/src/utils/logger";
5
import { Types } from "mongoose";
6
7
export const startRecipe = async (recipeName: string, accountId: string): Promise<{ RecipeId: { $oid: string } }> => {
8
const recipe = getRecipe(recipeName);
9
10
if (!recipe) {
11
logger.error(`unknown recipe ${recipeName}`);
12
throw new Error(`unknown recipe ${recipeName}`);
13
}
14
15
await updateCurrency(recipe.buildPrice, false, accountId);
16
17
const ingredientsInverse = recipe.ingredients.map(component => ({
18
ItemType: component.ItemType,
19
ItemCount: component.ItemCount * -1
20
}));
21
22
const inventory = await getInventory(accountId);
23
addMiscItems(inventory, ingredientsInverse);
24
25
//buildtime is in seconds
26
const completionDate = new Date(Date.now() + recipe.buildTime * unixTimesInMs.second);
27
28
inventory.PendingRecipes.push({
29
ItemType: recipeName,
30
CompletionDate: completionDate,
31
_id: new Types.ObjectId()
32
});
33
34
const newInventory = await inventory.save();
35
36
return {
37
RecipeId: { $oid: newInventory.PendingRecipes[newInventory.PendingRecipes.length - 1]._id.toString() }
38
};
39
};