返回提交历史
Modified
src/services/purchaseService.ts
+43
-22
XFEstudio/XFESpaceNinjaServer
fix: not consuming ItemPrices from server-side vendor (#798)
79299db4
代码差异
1 个文件
+43
-22
@@ -47,6 +47,7 @@ export const handlePurchase = async (
47
47
): Promise<IPurchaseResponse> => {
48
48
logger.debug("purchase request", purchaseRequest);
49
49
50
const inventoryChanges: IInventoryChanges = {};
50
51
if (purchaseRequest.PurchaseParams.Source == 7) {
51
52
const manifest = getVendorManifestByOid(purchaseRequest.PurchaseParams.SourceId!);
52
53
if (!manifest) {
@@ -57,6 +58,14 @@ export const handlePurchase = async (
57
58
if (!offer) {
58
59
throw new Error(`unknown vendor offer: ${ItemId}`);
59
60
}
61
if (offer.ItemPrices) {
62
await handleItemPrices(
63
accountId,
64
offer.ItemPrices,
65
purchaseRequest.PurchaseParams.Quantity,
66
inventoryChanges
67
);
68
}
60
69
purchaseRequest.PurchaseParams.Quantity *= offer.QuantityMultiplier;
61
70
}
62
71
@@ -65,6 +74,7 @@ export const handlePurchase = async (
65
74
accountId,
66
75
purchaseRequest.PurchaseParams.Quantity
67
76
);
77
combineInventoryChanges(purchaseResponse.InventoryChanges, inventoryChanges);
68
78
69
79
if (!purchaseResponse) throw new Error("purchase response was undefined");
70
80
@@ -117,27 +127,12 @@ export const handlePurchase = async (
117
127
const vendor = ExportVendors[purchaseRequest.PurchaseParams.SourceId!];
118
128
const offer = vendor.items.find(x => x.storeItem == purchaseRequest.PurchaseParams.StoreItem);
119
129
if (offer) {
120
const inventory = await getInventory(accountId);
121
for (const item of offer.itemPrices) {
122
const invItem: IMiscItem = {
123
ItemType: item.ItemType,
124
ItemCount: item.ItemCount * purchaseRequest.PurchaseParams.Quantity * -1
125
};
126
127
addMiscItems(inventory, [invItem]);
128
129
purchaseResponse.InventoryChanges.MiscItems ??= [];
130
const change = (purchaseResponse.InventoryChanges.MiscItems as IMiscItem[]).find(
131
x => x.ItemType == item.ItemType
132
);
133
if (change) {
134
change.ItemCount += invItem.ItemCount;
135
} else {
136
(purchaseResponse.InventoryChanges.MiscItems as IMiscItem[]).push(invItem);
137
}
138
}
139
140
await inventory.save();
130
await handleItemPrices(
131
accountId,
132
offer.itemPrices,
133
purchaseRequest.PurchaseParams.Quantity,
134
purchaseResponse.InventoryChanges
135
);
141
136
}
142
137
}
143
138
break;
@@ -176,6 +171,32 @@ export const handlePurchase = async (
176
171
return purchaseResponse;
177
172
};
178
173
174
const handleItemPrices = async (
175
accountId: string,
176
itemPrices: IMiscItem[],
177
purchaseQuantity: number,
178
inventoryChanges: IInventoryChanges
179
): Promise<void> => {
180
const inventory = await getInventory(accountId);
181
for (const item of itemPrices) {
182
const invItem: IMiscItem = {
183
ItemType: item.ItemType,
184
ItemCount: item.ItemCount * purchaseQuantity * -1
185
};
186
187
addMiscItems(inventory, [invItem]);
188
189
inventoryChanges.MiscItems ??= [];
190
const change = (inventoryChanges.MiscItems as IMiscItem[]).find(x => x.ItemType == item.ItemType);
191
if (change) {
192
change.ItemCount += invItem.ItemCount;
193
} else {
194
(inventoryChanges.MiscItems as IMiscItem[]).push(invItem);
195
}
196
}
197
await inventory.save();
198
};
199
179
200
export const handleStoreItemAcquisition = async (
180
201
storeItemName: string,
181
202
accountId: string,
@@ -254,7 +275,7 @@ const handleSlotPurchase = async (
254
275
slotPurchaseNameFull: string,
255
276
accountId: string,
256
277
quantity: number
257
): Promise<{ InventoryChanges: IInventoryChanges }> => {
278
): Promise<IPurchaseResponse> => {
258
279
logger.debug(`slot name ${slotPurchaseNameFull}`);
259
280
const slotPurchaseName = parseSlotPurchaseName(
260
281
slotPurchaseNameFull.substring(slotPurchaseNameFull.lastIndexOf("/") + 1)