diff --git a/src/controllers/api/loginRewardsController.ts b/src/controllers/api/loginRewardsController.ts index 16d77261..5280b77f 100644 --- a/src/controllers/api/loginRewardsController.ts +++ b/src/controllers/api/loginRewardsController.ts @@ -4,7 +4,8 @@ import { claimLoginReward, getRandomLoginRewards, ILoginRewardsReponse, - isLoginRewardAChoice + isLoginRewardAChoice, + setAccountGotLoginRewardToday } from "@/src/services/loginRewardService"; import { getInventory } from "@/src/services/inventoryService"; @@ -44,8 +45,11 @@ export const loginRewardsController: RequestHandler = async (req, res) => { if (!isMilestoneDay && randomRewards.length == 1) { response.DailyTributeInfo.HasChosenReward = true; response.DailyTributeInfo.ChosenReward = randomRewards[0]; - response.DailyTributeInfo.NewInventory = await claimLoginReward(account, inventory, randomRewards[0]); + response.DailyTributeInfo.NewInventory = await claimLoginReward(inventory, randomRewards[0]); await inventory.save(); + + setAccountGotLoginRewardToday(account); + await account.save(); } res.json(response); }; diff --git a/src/controllers/api/loginRewardsSelectionController.ts b/src/controllers/api/loginRewardsSelectionController.ts index 8bd14dd7..4b6fc210 100644 --- a/src/controllers/api/loginRewardsSelectionController.ts +++ b/src/controllers/api/loginRewardsSelectionController.ts @@ -1,5 +1,9 @@ import { getInventory } from "@/src/services/inventoryService"; -import { claimLoginReward, getRandomLoginRewards } from "@/src/services/loginRewardService"; +import { + claimLoginReward, + getRandomLoginRewards, + setAccountGotLoginRewardToday +} from "@/src/services/loginRewardService"; import { getAccountForRequest } from "@/src/services/loginService"; import { handleStoreItemAcquisition } from "@/src/services/purchaseService"; import { IInventoryChanges } from "@/src/types/purchaseTypes"; @@ -28,9 +32,13 @@ export const loginRewardsSelectionController: RequestHandler = async (req, res) } else { const randomRewards = getRandomLoginRewards(account, inventory); chosenReward = randomRewards.find(x => x.StoreItemType == body.ChosenReward)!; - inventoryChanges = await claimLoginReward(account, inventory, chosenReward); + inventoryChanges = await claimLoginReward(inventory, chosenReward); } await inventory.save(); + + setAccountGotLoginRewardToday(account); + await account.save(); + res.json({ DailyTributeInfo: { NewInventory: inventoryChanges, diff --git a/src/services/loginRewardService.ts b/src/services/loginRewardService.ts index f54499e5..d32789b6 100644 --- a/src/services/loginRewardService.ts +++ b/src/services/loginRewardService.ts @@ -121,14 +121,9 @@ const getRandomLoginReward = (rng: CRng, day: number, inventory: TInventoryDatab }; export const claimLoginReward = async ( - account: TAccountDocument, inventory: TInventoryDatabaseDocument, reward: ILoginReward ): Promise => { - account.LoginDays += 1; - account.LastLoginRewardDate = Math.trunc(Date.now() / 86400000) * 86400; - await account.save(); - switch (reward.RewardType) { case "RT_RESOURCE": case "RT_STORE_ITEM": @@ -150,3 +145,8 @@ export const claimLoginReward = async ( } throw new Error(`unknown login reward type: ${reward.RewardType}`); }; + +export const setAccountGotLoginRewardToday = (account: TAccountDocument): void => { + account.LoginDays += 1; + account.LastLoginRewardDate = Math.trunc(Date.now() / 86400000) * 86400; +};