fix: save login reward was claimed on milestone days (#1367)
All checks were successful
Build / build (18) (push) Successful in 47s
Build / build (20) (push) Successful in 1m17s
Build Docker image / docker (push) Successful in 34s
Build / build (22) (push) Successful in 1m21s

Reviewed-on: #1367
Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com>
Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit is contained in:
Sainan 2025-03-29 09:28:12 -07:00 committed by Sainan
parent 6a1e508109
commit e266f9e36c
3 changed files with 21 additions and 9 deletions

View File

@ -4,7 +4,8 @@ import {
claimLoginReward, claimLoginReward,
getRandomLoginRewards, getRandomLoginRewards,
ILoginRewardsReponse, ILoginRewardsReponse,
isLoginRewardAChoice isLoginRewardAChoice,
setAccountGotLoginRewardToday
} from "@/src/services/loginRewardService"; } from "@/src/services/loginRewardService";
import { getInventory } from "@/src/services/inventoryService"; import { getInventory } from "@/src/services/inventoryService";
@ -44,8 +45,11 @@ export const loginRewardsController: RequestHandler = async (req, res) => {
if (!isMilestoneDay && randomRewards.length == 1) { if (!isMilestoneDay && randomRewards.length == 1) {
response.DailyTributeInfo.HasChosenReward = true; response.DailyTributeInfo.HasChosenReward = true;
response.DailyTributeInfo.ChosenReward = randomRewards[0]; 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(); await inventory.save();
setAccountGotLoginRewardToday(account);
await account.save();
} }
res.json(response); res.json(response);
}; };

View File

@ -1,5 +1,9 @@
import { getInventory } from "@/src/services/inventoryService"; 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 { getAccountForRequest } from "@/src/services/loginService";
import { handleStoreItemAcquisition } from "@/src/services/purchaseService"; import { handleStoreItemAcquisition } from "@/src/services/purchaseService";
import { IInventoryChanges } from "@/src/types/purchaseTypes"; import { IInventoryChanges } from "@/src/types/purchaseTypes";
@ -28,9 +32,13 @@ export const loginRewardsSelectionController: RequestHandler = async (req, res)
} else { } else {
const randomRewards = getRandomLoginRewards(account, inventory); const randomRewards = getRandomLoginRewards(account, inventory);
chosenReward = randomRewards.find(x => x.StoreItemType == body.ChosenReward)!; chosenReward = randomRewards.find(x => x.StoreItemType == body.ChosenReward)!;
inventoryChanges = await claimLoginReward(account, inventory, chosenReward); inventoryChanges = await claimLoginReward(inventory, chosenReward);
} }
await inventory.save(); await inventory.save();
setAccountGotLoginRewardToday(account);
await account.save();
res.json({ res.json({
DailyTributeInfo: { DailyTributeInfo: {
NewInventory: inventoryChanges, NewInventory: inventoryChanges,

View File

@ -121,14 +121,9 @@ const getRandomLoginReward = (rng: CRng, day: number, inventory: TInventoryDatab
}; };
export const claimLoginReward = async ( export const claimLoginReward = async (
account: TAccountDocument,
inventory: TInventoryDatabaseDocument, inventory: TInventoryDatabaseDocument,
reward: ILoginReward reward: ILoginReward
): Promise<IInventoryChanges> => { ): Promise<IInventoryChanges> => {
account.LoginDays += 1;
account.LastLoginRewardDate = Math.trunc(Date.now() / 86400000) * 86400;
await account.save();
switch (reward.RewardType) { switch (reward.RewardType) {
case "RT_RESOURCE": case "RT_RESOURCE":
case "RT_STORE_ITEM": case "RT_STORE_ITEM":
@ -150,3 +145,8 @@ export const claimLoginReward = async (
} }
throw new Error(`unknown login reward type: ${reward.RewardType}`); 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;
};