This commit is contained in:
yaoyanwei
2025-09-08 16:43:50 +08:00
parent 43803f024b
commit e26f405ea8
13 changed files with 1043 additions and 45 deletions

111
tasks/tasks.tool.js Normal file
View File

@@ -0,0 +1,111 @@
const TasksModel = require("./tasks.model.js");
// Task status constants
const TASK_STATUS = {
ACTIVE: 0,
COMPLETED: 1,
EXPIRED: 2,
CLAIMED: 3
};
// Task condition types
const TASK_CONDITION_TYPES = {
LOGIN_GAME: 1,
PLAY_GAMES: 2,
WIN_GAMES: 3,
DEFEAT_HERO_WITH_ATTRIBUTES: 4,
SUMMON_HERO_WITH_ATTRIBUTES: 5,
USE_HERO_SKILL_WITH_ATTRIBUTES: 6
};
// Task reward types
const TASK_REWARD_TYPES = {
COINS: 0
};
// Check if a task is expired
exports.isTaskExpired = (task) => {
return new Date() > new Date(task.expireTime);
};
// Check if a task is completed
exports.isTaskCompleted = (task) => {
return task.status === TASK_STATUS.COMPLETED;
};
// Check if a task is claimed
exports.isTaskClaimed = (task) => {
return task.status === TASK_STATUS.CLAIMED;
};
// Get a random task from the configuration
exports.getRandomTask = async () => {
const tasks = await TasksModel.getAllTaskConfigs();
if (tasks.length === 0) {
return null;
}
const randomIndex = Math.floor(Math.random() * tasks.length);
return tasks[randomIndex];
};
// Create a new player task based on task configuration
exports.createPlayerTask = async (taskConfig) => {
const now = new Date();
const expireTime = new Date(now.getTime() + (taskConfig.durationHours * 60 * 60 * 1000));
return {
taskId: taskConfig.id,
assignedTime: now,
expireTime: expireTime,
status: TASK_STATUS.ACTIVE,
progress: 0
};
};
// Update task progress
exports.updateTaskProgress = async (playerTask, taskConfig, increment = 1) => {
// Check if task is already completed or claimed
if (playerTask.status === TASK_STATUS.COMPLETED || playerTask.status === TASK_STATUS.CLAIMED) {
return playerTask;
}
// Update progress
playerTask.progress += increment;
// Check if task is completed
if (playerTask.progress >= taskConfig.value1) {
playerTask.progress = taskConfig.value1; // Cap at the required value
playerTask.status = TASK_STATUS.COMPLETED;
}
return playerTask;
};
// Give reward for completing a task
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];
const rewardNum = taskConfig.rewardNums[i];
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
break;
default:
console.log(`Unknown reward type: ${rewardType}`);
}
}
return true;
};
// Export constants
exports.TASK_STATUS = TASK_STATUS;
exports.TASK_CONDITION_TYPES = TASK_CONDITION_TYPES;
exports.TASK_REWARD_TYPES = TASK_REWARD_TYPES;