XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFESpaceNinjaServer

improve: purchases (#161)

Co-authored-by: Sainan <Sainan@users.noreply.github.com>

971d1491
Sainan <sainan@calamity.gg>
提交于

代码差异

3 个文件 +42 -8
Modified src/controllers/api/inventorySlotsController.ts +1 -1
@@ -27,7 +27,7 @@ export const inventorySlotsController: RequestHandler = async (req, res) => {
27 27
28 28 //TODO: check which slot was purchased because pvpBonus is also possible
29 29
30 const currencyChanges = await updateCurrency(-20, true, accountId);
30 const currencyChanges = await updateCurrency(20, true, accountId);
31 31 await updateSlots(accountId, SlotNameToInventoryName.LOADOUT, 1, 1);
32 32
33 33 //console.log({ InventoryChanges: currencyChanges }, " added loadout changes:");
Modified src/services/inventoryService.ts +7 -3
@@ -93,15 +93,19 @@ export const updateSlots = async (accountId: string, slotName: SlotNames, slotAm
93 93 };
94 94
95 95 export const updateCurrency = async (price: number, usePremium: boolean, accountId: string) => {
96 if (config.infiniteResources) {
97 return {};
98 }
99
96 100 const inventory = await getInventory(accountId);
97 101
98 102 if (usePremium) {
99 103 if (inventory.PremiumCreditsFree > 0) {
100 inventory.PremiumCreditsFree += price;
104 inventory.PremiumCreditsFree -= Math.min(price, inventory.PremiumCreditsFree);
101 105 }
102 inventory.PremiumCredits += price;
106 inventory.PremiumCredits -= price;
103 107 } else {
104 inventory.RegularCredits += price;
108 inventory.RegularCredits -= price;
105 109 }
106 110
107 111 const modifiedPaths = inventory.modifiedPaths();
Modified src/services/purchaseService.ts +34 -4
@@ -5,12 +5,15 @@ import {
5 5 addBooster,
6 6 addCustomization,
7 7 addMechSuit,
8 addMiscItems,
8 9 addPowerSuit,
9 10 addSentinel,
10 11 addWeapon,
12 getInventory,
11 13 updateCurrency,
12 14 updateSlots
13 15 } from "@/src/services/inventoryService";
16 import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes";
14 17 import { IPurchaseRequest, IPurchaseResponse, SlotNameToInventoryName, SlotPurchase } from "@/src/types/purchaseTypes";
15 18 import { logger } from "@/src/utils/logger";
16 19
@@ -44,11 +47,18 @@ export const handlePurchase = async (purchaseRequest: IPurchaseRequest, accountI
44 47 inventoryChanges = await handleWeaponsPurchase(internalName, accountId);
45 48 break;
46 49 case "Types":
47 inventoryChanges = await handleTypesPurchase(internalName, accountId);
50 inventoryChanges = await handleTypesPurchase(
51 internalName,
52 accountId,
53 purchaseRequest.PurchaseParams.Quantity
54 );
48 55 break;
49 56 case "Boosters":
50 57 inventoryChanges = await handleBoostersPurchase(internalName, accountId);
51 58 break;
59 case "Interface":
60 inventoryChanges = await handleCustomizationPurchase(internalName, accountId);
61 break;
52 62 default:
53 63 const errorMessage = `unknown store category: ${storeCategory} not implemented or new`;
54 64 logger.error(errorMessage);
@@ -163,18 +173,21 @@ const handlePowersuitPurchase = async (powersuitName: string, accountId: string)
163 173 };
164 174
165 175 //TODO: change to getInventory, apply changes then save at the end
166 const handleTypesPurchase = async (typesName: string, accountId: string) => {
176 const handleTypesPurchase = async (typesName: string, accountId: string, quantity: number) => {
167 177 const typeCategory = getStoreItemTypesCategory(typesName);
168 178 logger.debug(`type category ${typeCategory}`);
169 179 switch (typeCategory) {
180 case "AvatarImages":
170 181 case "SuitCustomizations":
171 return await handleSuitCustomizationsPurchase(typesName, accountId);
182 return await handleCustomizationPurchase(typesName, accountId);
172 183 // case "Recipes":
173 184 // break;
174 185 case "Sentinels":
175 186 return await handleSentinelPurchase(typesName, accountId);
176 187 case "SlotItems":
177 188 return await handleSlotPurchase(typesName, accountId);
189 case "Items":
190 return await handleMiscItemPurchase(typesName, accountId, quantity);
178 191 default:
179 192 throw new Error(`unknown Types category: ${typeCategory} not implemented or new`);
180 193 }
@@ -193,7 +206,7 @@ const handleSentinelPurchase = async (sentinelName: string, accountId: string) =
193 206 };
194 207 };
195 208
196 const handleSuitCustomizationsPurchase = async (customizationName: string, accountId: string) => {
209 const handleCustomizationPurchase = async (customizationName: string, accountId: string) => {
197 210 const customization = await addCustomization(customizationName, accountId);
198 211
199 212 return {
@@ -228,3 +241,20 @@ const handleBoostersPurchase = async (boosterStoreName: string, accountId: strin
228 241 }
229 242 };
230 243 };
244
245 const handleMiscItemPurchase = async (uniqueName: string, accountId: string, quantity: number) => {
246 const inventory = await getInventory(accountId);
247 const miscItemChanges = [
248 {
249 ItemType: uniqueName,
250 ItemCount: quantity
251 } satisfies IMiscItem
252 ];
253 addMiscItems(inventory, miscItemChanges);
254 await inventory.save();
255 return {
256 InventoryChanges: {
257 MiscItems: miscItemChanges
258 }
259 };
260 };