From 8c3ded3e208f529f946e06676ee94dff11874316 Mon Sep 17 00:00:00 2001 From: chnxianyi Date: Thu, 11 Sep 2025 13:51:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=A5=96=E5=8A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tasks/tasks.controller.js | 25 ++++++++++++++++++++++++- tasks/tasks.tool.js | 8 +++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/tasks/tasks.controller.js b/tasks/tasks.controller.js index 09228a9..ec669d9 100644 --- a/tasks/tasks.controller.js +++ b/tasks/tasks.controller.js @@ -1,4 +1,5 @@ const TasksModel = require("./tasks.model.js"); +const TasksTool = require("./tasks.tool.js"); // Get all task configurations exports.getAllTasks = async (req, res) => { @@ -40,13 +41,35 @@ exports.savePlayerTasks = async (req, res) => { const tasksData = req.body; tasksData.userId = userId; + const historyTasks = await TasksModel.getPlayerTasks(userId); + const updatedTasks = await TasksModel.savePlayerTasks(tasksData); if (!updatedTasks) { 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 }); } catch (error) { res.status(500).send({ error: "Internal server error" }); } -}; \ No newline at end of file +}; diff --git a/tasks/tasks.tool.js b/tasks/tasks.tool.js index a443467..e56f432 100644 --- a/tasks/tasks.tool.js +++ b/tasks/tasks.tool.js @@ -1,5 +1,5 @@ const TasksModel = require("./tasks.model.js"); - +const UserModel = require("../users/users.model.js"); // Task status constants const TASK_STATUS = { ACTIVE: 0, @@ -86,7 +86,6 @@ exports.updateTaskProgress = async (playerTask, taskConfig, increment = 1) => { exports.giveTaskReward = async (userId, taskConfig) => { // In a real implementation, this would integrate with the rewards system // 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++) { const rewardType = taskConfig.rewardTypes[i]; @@ -94,8 +93,11 @@ exports.giveTaskReward = async (userId, taskConfig) => { switch (rewardType) { 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 + const user = await UserModel.getById(userId); + user.coins += rewardNum; + + await user.save(); break; default: console.log(`Unknown reward type: ${rewardType}`);