任务奖励

This commit is contained in:
chnxianyi
2025-09-11 13:51:35 +08:00
parent ea390c6461
commit 8c3ded3e20
2 changed files with 29 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
const TasksModel = require("./tasks.model.js"); const TasksModel = require("./tasks.model.js");
const TasksTool = require("./tasks.tool.js");
// Get all task configurations // Get all task configurations
exports.getAllTasks = async (req, res) => { exports.getAllTasks = async (req, res) => {
@@ -40,11 +41,33 @@ exports.savePlayerTasks = async (req, res) => {
const tasksData = req.body; const tasksData = req.body;
tasksData.userId = userId; tasksData.userId = userId;
const historyTasks = await TasksModel.getPlayerTasks(userId);
const updatedTasks = await TasksModel.savePlayerTasks(tasksData); const updatedTasks = await TasksModel.savePlayerTasks(tasksData);
if (!updatedTasks) { if (!updatedTasks) {
return res.status(500).send({ error: "Failed to save player tasks" }); return res.status(500).send({ error: "Failed to save player tasks" });
} }
// Give reward for completing a task
for (let i = 0; i < req.body.tasks.length; i++) {
const task = req.body.tasks[i];
const historyTask = historyTasks.tasks.find(
(t) => t.taskId === task.taskId
);
if (historyTask) {
if (historyTask.status != task.status) {
if (
task.status === TasksTool.TASK_STATUS.CLAIMED &&
historyTask.status === TasksTool.TASK_STATUS.COMPLETED
) {
// 任务可以领取奖励 且 历史任务状态为完成
const taskConfig = await TasksModel.getTaskConfigById(task.taskId);
await TasksTool.giveTaskReward(userId, taskConfig);
}
}
}
}
res.status(200).send({ success: true }); res.status(200).send({ success: true });
} catch (error) { } catch (error) {
res.status(500).send({ error: "Internal server error" }); res.status(500).send({ error: "Internal server error" });

View File

@@ -1,5 +1,5 @@
const TasksModel = require("./tasks.model.js"); const TasksModel = require("./tasks.model.js");
const UserModel = require("../users/users.model.js");
// Task status constants // Task status constants
const TASK_STATUS = { const TASK_STATUS = {
ACTIVE: 0, ACTIVE: 0,
@@ -86,7 +86,6 @@ exports.updateTaskProgress = async (playerTask, taskConfig, increment = 1) => {
exports.giveTaskReward = async (userId, taskConfig) => { exports.giveTaskReward = async (userId, taskConfig) => {
// In a real implementation, this would integrate with the rewards system // In a real implementation, this would integrate with the rewards system
// For now, we'll just log the reward information // For now, we'll just log the reward information
console.log(`Giving reward to user ${userId} for task ${taskConfig.id}`);
for (let i = 0; i < taskConfig.rewardTypes.length; i++) { for (let i = 0; i < taskConfig.rewardTypes.length; i++) {
const rewardType = taskConfig.rewardTypes[i]; const rewardType = taskConfig.rewardTypes[i];
@@ -94,8 +93,11 @@ exports.giveTaskReward = async (userId, taskConfig) => {
switch (rewardType) { switch (rewardType) {
case TASK_REWARD_TYPES.COINS: case TASK_REWARD_TYPES.COINS:
console.log(`Rewarding ${rewardNum} coins to user ${userId}`);
// Here we would call the rewards system to give coins to the user // Here we would call the rewards system to give coins to the user
const user = await UserModel.getById(userId);
user.coins += rewardNum;
await user.save();
break; break;
default: default:
console.log(`Unknown reward type: ${rewardType}`); console.log(`Unknown reward type: ${rewardType}`);