SpaceNinjaServer/src/controllers/api/loginRewardsController.ts
Sainan c3802b49ca
All checks were successful
Build / build (22) (push) Successful in 42s
Build / build (18) (push) Successful in 1m11s
Build / build (20) (push) Successful in 1m7s
Build / build (18) (pull_request) Successful in 45s
Build / build (20) (pull_request) Successful in 1m10s
Build / build (22) (pull_request) Successful in 1m10s
feat: daily tribute
2025-03-20 14:30:37 +01:00

41 lines
1.7 KiB
TypeScript

import { RequestHandler } from "express";
import { getAccountForRequest } from "@/src/services/loginService";
import { claimLoginReward, getRandomLoginRewards, ILoginRewardsReponse } from "@/src/services/loginRewardService";
import { getInventory } from "@/src/services/inventoryService";
export const loginRewardsController: RequestHandler = async (req, res) => {
const account = await getAccountForRequest(req);
const today = Math.trunc(Date.now() / 86400000) * 86400;
if (today == account.LastLoginRewardDate) {
res.end();
return;
}
account.LoginDays += 1;
account.LastLoginRewardDate = today;
await account.save();
const inventory = await getInventory(account._id.toString());
const randomRewards = getRandomLoginRewards(account, inventory);
const isMilestoneDay = account.LoginDays == 5 || account.LoginDays % 50 == 0;
const response: ILoginRewardsReponse = {
DailyTributeInfo: {
Rewards: randomRewards,
IsMilestoneDay: isMilestoneDay,
IsChooseRewardSet: randomRewards.length != 1,
LoginDays: account.LoginDays,
//NextMilestoneReward: "",
NextMilestoneDay: account.LoginDays < 5 ? 5 : (Math.trunc(account.LoginDays / 50) + 1) * 50,
HasChosenReward: false
},
LastLoginRewardDate: today
};
if (!isMilestoneDay && randomRewards.length == 1) {
response.DailyTributeInfo.HasChosenReward = true;
response.DailyTributeInfo.ChosenReward = randomRewards[0];
response.DailyTributeInfo.NewInventory = await claimLoginReward(inventory, randomRewards[0]);
await inventory.save();
}
res.json(response);
};