任务模块包含以下核心数据表:
存储任务的分类信息,用于对任务进行分组和管理。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| name | varchar(100) | 否 | - | 分类名称 |
| code | varchar(50) | 否 | - | 分类编码(唯一) |
| description | varchar(255) | 是 | NULL | 分类描述 |
| sort_order | int | 否 | 0 | 排序顺序 |
| created_at | timestamp | 是 | NULL | 创建时间 |
| updated_at | timestamp | 是 | NULL | 更新时间 |
索引:
id)idx_code (code)存储任务的基本定义信息,包括任务名称、描述、奖励等。目标相关字段已拆分到任务达成条件表。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| category_id | int | 否 | - | 任务分类ID,外键关联task_categories表 |
| name | varchar(100) | 否 | - | 任务名称 |
| description | varchar(500) | 是 | NULL | 任务描述 |
| type | varchar(20) | 否 | - | 任务类型(daily, weekly, achievement, event, tutorial, team) |
| rewards | json | 否 | - | 奖励内容(JSON格式) |
| prerequisite_tasks | json | 是 | NULL | 前置任务ID(JSON格式) |
| level_required | int | 否 | 0 | 所需等级 |
| time_limit | int | 是 | NULL | 时间限制(秒,NULL表示无限制) |
| reset_type | varchar(20) | 否 | 'none' | 重置类型(none, daily, weekly, monthly) |
| is_active | tinyint | 否 | 1 | 是否激活(0:否, 1:是) |
| start_time | timestamp | 是 | NULL | 开始时间(NULL表示立即开始) |
| end_time | timestamp | 是 | NULL | 结束时间(NULL表示永不结束) |
| created_at | timestamp | 是 | NULL | 创建时间 |
| updated_at | timestamp | 是 | NULL | 更新时间 |
索引:
id)idx_category (category_id)idx_type (type)idx_active_time (is_active, start_time, end_time)存储用户与任务的关联信息,包括任务状态、进度、完成时间等。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| user_id | int | 否 | - | 用户ID |
| task_id | int | 否 | - | 任务ID,外键关联task_tasks表 |
| status | tinyint | 否 | 0 | 状态(0:未接取, 1:进行中, 2:已完成, 3:已领取奖励, 4:已失败, 5:已过期) |
| progress | int | 否 | 0 | 当前进度 |
| completed_at | timestamp | 是 | NULL | 完成时间 |
| rewarded_at | timestamp | 是 | NULL | 奖励发放时间 |
| expire_at | timestamp | 是 | NULL | 过期时间 |
| created_at | timestamp | 是 | NULL | 创建时间 |
| updated_at | timestamp | 是 | NULL | 更新时间 |
索引:
id)idx_user_task (user_id, task_id)idx_user_status (user_id, status)idx_task (task_id)idx_expire (expire_at)定义系统中所有可用的条件类型,每种条件类型对应一个处理器类,负责验证和更新该类型条件的进度。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| code | varchar(50) | 否 | - | 条件代码(唯一),如'login', 'plant', 'harvest' |
| name | varchar(100) | 否 | - | 条件名称,如'登录游戏', '种植作物', '收获作物' |
| description | varchar(255) | 是 | NULL | 条件描述 |
| param_schema | json | 是 | NULL | 参数模式,定义此条件需要的参数及其类型 |
| handler_class | varchar(255) | 是 | NULL | 处理此条件的类名 |
| is_active | tinyint | 否 | 1 | 是否激活(0:否, 1:是) |
| created_at | timestamp | 是 | NULL | 创建时间 |
| updated_at | timestamp | 是 | NULL | 更新时间 |
索引:
id)idx_code (code)关联任务和具体的条件,定义了完成任务需要满足的条件。一个任务可以有多个条件,通过这个表可以实现复杂的任务逻辑。条件分为两种类型:前置条件和进度条件。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| task_id | int | 否 | - | 任务ID,外键关联task_tasks表 |
| condition_id | int | 否 | - | 条件ID,外键关联task_conditions表 |
| condition_type | varchar(20) | 否 | 'progress' | 条件类型('prerequisite'=前置条件,'progress'=进度条件) |
| target_value | int | 否 | 1 | 目标值,如需要完成的次数 |
| params | json | 是 | NULL | 条件参数,如特定物品ID、特定作物ID等 |
| operator | varchar(10) | 否 | '=' | 运算符,如'=', '>=', '<=' |
| sort_order | int | 否 | 0 | 排序顺序,用于多条件任务 |
| is_required | tinyint | 否 | 1 | 是否必须满足此条件(0:否, 1:是) |
| created_at | timestamp | 是 | NULL | 创建时间 |
| updated_at | timestamp | 是 | NULL | 更新时间 |
索引:
id)idx_task_id (task_id)idx_condition_id (condition_id)idx_task_condition (task_id, condition_id)idx_condition_type (condition_type)存储用户任务的详细进度信息,关联到具体的任务达成条件。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| user_id | int | 否 | - | 用户ID |
| task_id | int | 否 | - | 任务ID,外键关联task_tasks表 |
| achievement_condition_id | int | 否 | - | 达成条件ID,外键关联task_achievement_conditions表 |
| current_value | int | 否 | 0 | 当前值 |
| last_update_time | timestamp | 是 | NULL | 最后更新时间 |
| created_at | timestamp | 是 | NULL | 创建时间 |
| updated_at | timestamp | 是 | NULL | 更新时间 |
索引:
id)idx_user_task_condition (user_id, task_id, achievement_condition_id)idx_user_task (user_id, task_id)idx_achievement_condition (achievement_condition_id)记录用户完成任务的详细信息,用于审计和数据分析。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| user_id | int | 否 | - | 用户ID |
| task_id | int | 否 | - | 任务ID,外键关联task_tasks表 |
| completed_at | timestamp | 否 | - | 完成时间 |
| time_spent | int | 是 | NULL | 完成耗时(秒) |
| ip_address | varchar(45) | 是 | NULL | IP地址 |
| device_info | varchar(255) | 是 | NULL | 设备信息 |
| created_at | timestamp | 是 | NULL | 创建时间 |
索引:
id)idx_user (user_id)idx_task (task_id)idx_completed_at (completed_at)记录任务奖励的发放情况,包括奖励内容、发放时间等。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| user_id | int | 否 | - | 用户ID |
| task_id | int | 否 | - | 任务ID,外键关联task_tasks表 |
| user_task_id | int | 否 | - | 用户任务ID,外键关联task_user_tasks表 |
| rewards | json | 否 | - | 奖励内容(JSON格式) |
| rewarded_at | timestamp | 否 | - | 奖励发放时间 |
| ip_address | varchar(45) | 是 | NULL | IP地址 |
| device_info | varchar(255) | 是 | NULL | 设备信息 |
| created_at | timestamp | 是 | NULL | 创建时间 |
索引:
id)idx_user (user_id)idx_task (task_id)idx_user_task (user_task_id)idx_rewarded_at (rewarded_at)记录任务重置的情况,包括重置类型、重置时间、影响的任务数等。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| reset_type | varchar(20) | 否 | - | 重置类型(daily, weekly, monthly) |
| reset_time | timestamp | 否 | - | 重置时间 |
| affected_tasks | json | 是 | NULL | 受影响的任务ID列表(JSON格式) |
| affected_count | int | 否 | 0 | 受影响的任务数量 |
| created_at | timestamp | 是 | NULL | 创建时间 |
索引:
id)idx_reset_type (reset_type)idx_reset_time (reset_time)task_categories 1 --< task_tasks
task_conditions 1 --< task_achievement_conditions
task_tasks 1 --< task_achievement_conditions
task_tasks 1 --< task_user_tasks
task_achievement_conditions 1 --< task_user_progress
task_user_tasks 1 --< task_completion_logs
task_user_tasks 1 --< task_reward_logs
{
"properties": {
"seed_ids": {
"type": "array",
"items": {"type": "integer"},
"description": "指定种子ID列表"
},
"land_type": {
"type": "integer",
"description": "指定土地类型"
},
"min_level": {
"type": "integer",
"description": "最低等级要求"
}
}
}
{
"items": [
{"item_id": 1001, "quantity": 10},
{"item_id": 1002, "quantity": 5}
],
"experience": 100,
"special_rewards": {
"type": "unlock_feature",
"feature_id": 3
}
}
{
"required_tasks": [101, 102], // 必须完成的任务ID
"optional_tasks": [103, 104], // 可选完成的任务ID
"min_optional_count": 1 // 最少需要完成的可选任务数
}
获取用户当前进行中的任务
idx_user_status 索引优化获取特定类型的可用任务
idx_type 和 idx_active_time 索引优化检查用户是否完成特定任务
idx_user_task 索引优化获取即将过期的任务
idx_expire 索引优化任务进度更新是高频操作,使用 task_user_progress 表分担 task_user_tasks 表的写入压力
日志表(task_completion_logs, task_reward_logs)可考虑按时间分区,提高写入性能
每种条件类型都有一个对应的处理器类,负责验证和更新该类型条件的进度。
interface ConditionHandlerInterface
{
/**
* 验证事件是否满足条件
*
* @param array $event 事件数据
* @param array $params 条件参数
* @return bool
*/
public function matches(array $event, array $params): bool;
/**
* 计算事件对条件进度的贡献值
*
* @param array $event 事件数据
* @param array $params 条件参数
* @return int
*/
public function calculateProgress(array $event, array $params): int;
/**
* 检查条件是否已完成
*
* @param int $currentValue 当前值
* @param int $targetValue 目标值
* @param string $operator 运算符
* @return bool
*/
public function isCompleted(int $currentValue, int $targetValue, string $operator): bool;
}
abstract class BaseConditionHandler implements ConditionHandlerInterface
{
/**
* 检查条件是否已完成
*
* @param int $currentValue 当前值
* @param int $targetValue 目标值
* @param string $operator 运算符
* @return bool
*/
public function isCompleted(int $currentValue, int $targetValue, string $operator): bool
{
switch ($operator) {
case '=':
return $currentValue == $targetValue;
case '>=':
return $currentValue >= $targetValue;
case '>':
return $currentValue > $targetValue;
case '<=':
return $currentValue <= $targetValue;
case '<':
return $currentValue < $targetValue;
default:
return false;
}
}
}
前置条件是指用户必须满足才能接取任务的条件,但不计入任务的完成进度。前置条件的特点:
进度条件是指用户需要完成才能完成任务的条件,有明确的进度计算。进度条件的特点:
前置条件处理:
进度条件处理:
任务名称:每日登录
任务描述:今天登录游戏
任务类型:daily
条件:
- 类型:login
- 目标值:1
- 参数:{}
- 运算符:=
奖励:
- 金币 x 100
任务名称:农场管理大师
任务描述:种植3种不同作物,收获10次,升级1次土地
任务类型:weekly
条件:
- 类型:plant
目标值:3
参数:{"seed_ids": [101, 102, 103, 104, 105]}
运算符:>=
是否必须:是
- 类型:harvest
目标值:10
参数:{}
运算符:>=
是否必须:是
- 类型:upgrade_land
目标值:1
参数:{}
运算符:>=
是否必须:是
奖励:
- 高级化肥 x 3
- 金币 x 500
任务名称:高级农场主
任务描述:已经达到农场等级10级的玩家,可以尝试种植高级作物
任务类型:achievement
前置条件:
- 类型:level_check
目标值:10
参数:{"level_type": "farm"}
运算符:>=
条件类型:prerequisite
- 类型:task_completed
目标值:1
参数:{"task_ids": [101, 102]}
运算符:>=
条件类型:prerequisite
进度条件:
- 类型:plant
目标值:5
参数:{"seed_ids": [201, 202]}
运算符:>=
条件类型:progress
是否必须:是
- 类型:harvest
目标值:20
参数:{"crop_ids": [301, 302]}
运算符:>=
条件类型:progress
是否必须:是
奖励:
- 稀有种子 x 1
- 金币 x 1000