返回提交历史
Modified
src/controllers/api/claimCompletedRecipeController.ts
+37
-14
Modified
src/services/itemDataService.ts
+1
-2
Modified
src/services/recipeService.ts
+10
-40
XFEstudio/XFESpaceNinjaServer
feat: handle costs of recipes (#329)
be5a2716
代码差异
3 个文件
+48
-56
@@ -3,11 +3,11 @@
3
3
4
4
import { RequestHandler } from "express";
5
5
import { logger } from "@/src/utils/logger";
6
import { getItemByBlueprint } from "@/src/services/itemDataService";
6
import { getRecipe } from "@/src/services/itemDataService";
7
7
import { IOid } from "@/src/types/commonTypes";
8
8
import { getJSONfromString } from "@/src/helpers/stringHelpers";
9
9
import { getAccountIdForRequest } from "@/src/services/loginService";
10
import { getInventory, updateCurrency, addItem } from "@/src/services/inventoryService";
10
import { getInventory, updateCurrency, addItem, addMiscItems, addRecipes } from "@/src/services/inventoryService";
11
11
12
12
export interface IClaimCompletedRecipeRequest {
13
13
RecipeIds: IOid[];
@@ -37,28 +37,51 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
37
37
inventory.PendingRecipes.pull(pendingRecipe._id);
38
38
await inventory.save();
39
39
40
const buildable = getItemByBlueprint(pendingRecipe.ItemType);
41
if (!buildable) {
40
const recipe = getRecipe(pendingRecipe.ItemType);
41
if (!recipe) {
42
42
logger.error(`no completed item found for recipe ${pendingRecipe._id}`);
43
43
throw new Error(`no completed item found for recipe ${pendingRecipe._id}`);
44
44
}
45
45
46
46
if (req.query.cancel) {
47
// TODO: Refund items
48
res.json({});
47
const currencyChanges = await updateCurrency(recipe.buildPrice * -1, false, accountId);
48
49
const inventory = await getInventory(accountId);
50
addMiscItems(inventory, recipe.ingredients);
51
await inventory.save();
52
53
// Not a bug: In the specific case of cancelling a recipe, InventoryChanges are expected to be the root.
54
res.json({
55
...currencyChanges,
56
MiscItems: recipe.ingredients
57
});
49
58
} else {
50
logger.debug("Claiming Recipe", { buildable, pendingRecipe });
51
if (buildable.consumeOnUse) {
52
// TODO: Remove one instance of this recipe, and include that in InventoryChanges.
59
logger.debug("Claiming Recipe", { recipe, pendingRecipe });
60
let InventoryChanges = {};
61
if (recipe.consumeOnUse) {
62
const recipeChanges = [
63
{
64
ItemType: pendingRecipe.ItemType,
65
ItemCount: -1
66
}
67
];
68
69
InventoryChanges = { ...InventoryChanges, Recipes: recipeChanges };
70
71
const inventory = await getInventory(accountId);
72
addRecipes(inventory, recipeChanges);
73
await inventory.save();
53
74
}
54
let currencyChanges = {};
55
if (req.query.rush && buildable.skipBuildTimePrice) {
56
currencyChanges = await updateCurrency(buildable.skipBuildTimePrice, true, accountId);
75
if (req.query.rush) {
76
InventoryChanges = {
77
...InventoryChanges,
78
...(await updateCurrency(recipe.skipBuildTimePrice, true, accountId))
79
};
57
80
}
58
81
res.json({
59
82
InventoryChanges: {
60
...currencyChanges,
61
...(await addItem(accountId, buildable.resultType, buildable.num)).InventoryChanges
83
...InventoryChanges,
84
...(await addItem(accountId, recipe.resultType, recipe.num)).InventoryChanges
62
85
}
63
86
});
64
87
}
@@ -97,8 +97,7 @@ export const blueprintNames = Object.fromEntries(
97
97
.map(name => [name, craftNames[name]])
98
98
);
99
99
100
// Gets a recipe by its uniqueName
101
export const getItemByBlueprint = (uniqueName: string): IRecipe | undefined => {
100
export const getRecipe = (uniqueName: string): IRecipe | undefined => {
102
101
return ExportRecipes[uniqueName];
103
102
};
104
103
@@ -1,60 +1,30 @@
1
1
import { unixTimesInMs } from "@/src/constants/timeConstants";
2
import { getInventory } from "@/src/services/inventoryService";
3
import { getItemByBlueprint } from "@/src/services/itemDataService";
2
import { addMiscItems, getInventory, updateCurrency } from "@/src/services/inventoryService";
3
import { getRecipe } from "@/src/services/itemDataService";
4
4
import { logger } from "@/src/utils/logger";
5
5
import { Types } from "mongoose";
6
6
7
export interface IResource {
8
uniqueName: string;
9
count: number;
10
}
11
12
// export const updateResources = async (accountId: string, components: IResource[]) => {
13
// const inventory = await getInventory(accountId);
14
15
// for (const component of components) {
16
// const category = getItemCategoryByUniqueName(component.uniqueName) as keyof typeof inventory;
17
// //validate category
18
19
// console.log(component.uniqueName);
20
// console.log("cate", category);
21
22
// const invItem = inventory[category];
23
// console.log("invItem", invItem);
24
25
// inventory["MiscItems"];
26
// }
27
// };
28
29
7
export const startRecipe = async (recipeName: string, accountId: string) => {
30
const recipe = getItemByBlueprint(recipeName);
8
const recipe = getRecipe(recipeName);
31
9
32
10
if (!recipe) {
33
11
logger.error(`unknown recipe ${recipeName}`);
34
12
throw new Error(`unknown recipe ${recipeName}`);
35
13
}
36
14
37
const componentsNeeded = recipe.ingredients.map(component => ({
38
uniqueName: component.ItemType,
39
count: component.ItemCount
40
}));
15
await updateCurrency(recipe.buildPrice, false, accountId);
41
16
42
if (!componentsNeeded) {
43
logger.error(`recipe ${recipeName} has no components`);
44
throw new Error(`recipe ${recipeName} has no components`);
45
}
17
const ingredientsInverse = recipe.ingredients.map(component => ({
18
ItemType: component.ItemType,
19
ItemCount: component.ItemCount * -1
20
}));
46
21
47
//TODO: consume components used
48
//await updateResources(accountId, componentsNeeded);
22
const inventory = await getInventory(accountId);
23
addMiscItems(inventory, ingredientsInverse);
49
24
50
if (!recipe.buildTime) {
51
logger.error(`recipe ${recipeName} has no build time`);
52
throw new Error(`recipe ${recipeName} has no build time`);
53
}
54
25
//buildtime is in seconds
55
26
const completionDate = new Date(Date.now() + recipe.buildTime * unixTimesInMs.second);
56
27
57
const inventory = await getInventory(accountId);
58
28
inventory.PendingRecipes.push({
59
29
ItemType: recipeName,
60
30
CompletionDate: completionDate,