任务模块包含以下核心数据表,按功能分为三个部分:
存储任务的分类信息,用于对任务进行分组和管理。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| 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) |
| prerequisite_tasks | json | 是 | NULL | 前置任务ID(JSON格式) |
| level_required | int | 否 | 0 | 所需等级 |
| time_limit | int | 是 | NULL | 时间限制(秒,NULL表示无限制) |
| max_completions | int | 否 | 1 | 最大完成次数(用于限制任务可完成的次数) |
| reset_type | varchar(20) | 否 | 'none' | 重置类型(none, daily, weekly, monthly) |
| reset_interval | int | 否 | 0 | 重置间隔(秒),根据重置类型自动计算 |
| reset_time_field | varchar(50) | 是 | NULL | 重置时间字段,如"daily_reset_time",用于存储具体重置时间点 |
| display_params | json | 是 | NULL | 显示参数(JSON格式,存储与任务显示相关的参数) |
| sort_order | int | 否 | 0 | 排序权重(数值越大越靠前) |
| 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)idx_sort (sort_order)存储任务的奖励内容,一个任务可以有多个不同类型的奖励。每个奖励需要两个参数:reward_param1和reward_param2,用于更精确地定义奖励。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| task_id | int | 否 | - | 任务ID,外键关联task_tasks表 |
| reward_type | varchar(50) | 否 | - | 奖励类型(item, currency, experience, feature_unlock等) |
| reward_param1 | varchar(100) | 否 | - | 奖励参数1(如物品类型、货币类型等) |
| reward_param2 | varchar(100) | 否 | - | 奖励参数2(如物品ID、货币ID等) |
| quantity | int | 否 | 1 | 奖励数量 |
| extra_data | json | 是 | NULL | 额外数据(JSON格式) |
| sort_order | int | 否 | 0 | 排序权重(数值越大越靠前) |
| created_at | timestamp | 是 | NULL | 创建时间 |
| updated_at | timestamp | 是 | NULL | 更新时间 |
索引:
id)idx_task_id (task_id)idx_reward_type (reward_type)idx_reward_params (reward_param1, reward_param2)idx_sort (sort_order)存储接取任务所需的消耗资源,一个任务可以有多种不同类型的消耗资源。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| task_id | int | 否 | - | 任务ID,外键关联task_tasks表 |
| cost_type | varchar(50) | 否 | - | 消耗类型(currency, item, energy, ticket等) |
| cost_param1 | varchar(100) | 否 | - | 消耗参数1(如货币类型、物品类型等) |
| cost_param2 | varchar(100) | 否 | - | 消耗参数2(如货币ID、物品ID等) |
| quantity | int | 否 | 1 | 消耗数量 |
| extra_data | json | 是 | NULL | 额外数据(JSON格式) |
| sort_order | int | 否 | 0 | 排序权重(数值越大越靠前) |
| created_at | timestamp | 是 | NULL | 创建时间 |
| updated_at | timestamp | 是 | NULL | 更新时间 |
索引:
id)idx_task_id (task_id)idx_cost_type (cost_type)idx_cost_params (cost_param1, cost_param2)idx_sort (sort_order)定义系统中所有可用的条件类型,每种条件类型对应一个处理器类,负责验证和更新该类型条件的进度。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| 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表 |
| status | tinyint | 否 | 0 | 状态(0:未接取, 1:进行中, 2:已完成, 3:已领取奖励, 4:已失败, 5:已过期) |
| progress | int | 否 | 0 | 当前进度 |
| completed_at | timestamp | 是 | NULL | 完成时间 |
| rewarded_at | timestamp | 是 | NULL | 奖励发放时间 |
| expire_at | timestamp | 是 | NULL | 过期时间 |
| next_reset_time | timestamp | 是 | NULL | 下次重置时间,用于快速判断是否需要重置 |
| last_reset_time | timestamp | 是 | NULL | 上次重置时间,用于计算下次重置时间 |
| reset_count | int | 否 | 0 | 重置次数,用于统计和分析 |
| created_at | timestamp | 是 | NULL | 创建时间 |
| updated_at | timestamp | 是 | NULL | 更新时间 |
索引:
id)idx_user_task (user_id, task_id)idx_next_reset_time (next_reset_time)idx_user_next_reset (user_id, next_reset_time)idx_user_status (user_id, status)idx_task (task_id)idx_expire (expire_at)存储用户任务的详细进度信息,关联到具体的任务达成条件。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| 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 | 否 | 自增 | 主键 |
| user_id | int | 否 | - | 用户ID |
| task_id | int | 否 | - | 任务ID,外键关联task_tasks表 |
| cost_type | varchar(50) | 否 | - | 消耗类型(currency, item, energy, ticket等) |
| cost_param1 | varchar(100) | 否 | - | 消耗参数1(如货币类型、物品类型等) |
| cost_param2 | varchar(100) | 否 | - | 消耗参数2(如货币ID、物品ID等) |
| quantity | int | 否 | 1 | 消耗数量 |
| cost_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_cost_type (cost_type)idx_cost_at (cost_at)记录任务重置的情况,包括重置类型、重置时间、影响的任务数等。
| 字段名 | 类型 | 允许空 | 默认值 | 说明 |
|---|---|---|---|---|
| id | int | 否 | 自增 | 主键 |
| reset_type | varchar(20) | 否 | - | 重置类型(daily, weekly, monthly, manual, auto) |
| trigger_type | varchar(20) | 是 | NULL | 触发类型(view, accept, update, reward, admin) |
| 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)erDiagram
task_categories ||--o{ task_tasks : "包含"
task_tasks ||--o{ task_rewards : "定义奖励"
task_tasks ||--o{ task_costs : "定义消耗"
task_tasks ||--o{ task_achievement_conditions : "设置"
task_conditions ||--o{ task_achievement_conditions : "使用"
task_tasks ||--o{ task_user_tasks : "接取"
task_achievement_conditions ||--o{ task_user_progress : "进度"
task_user_tasks ||--o{ task_completion_logs : "记录完成"
task_user_tasks ||--o{ task_reward_logs : "记录奖励"
task_user_tasks ||--o{ task_cost_logs : "记录消耗"
task_reset_logs ||--o{ task_tasks : "重置"
task_categories {
int id PK "主键"
varchar name "分类名称"
varchar code "分类编码"
}
task_tasks {
int id PK "主键"
int category_id FK "分类ID"
varchar name "任务名称"
varchar type "任务类型"
json prerequisite_tasks "前置任务"
int max_completions "最大完成次数"
json display_params "显示参数"
int sort_order "排序权重"
}
task_rewards {
int id PK "主键"
int task_id FK "任务ID"
varchar reward_type "奖励类型"
varchar reward_param1 "奖励参数1"
varchar reward_param2 "奖励参数2"
int quantity "数量"
int sort_order "排序权重"
}
task_costs {
int id PK "主键"
int task_id FK "任务ID"
varchar cost_type "消耗类型"
varchar cost_param1 "消耗参数1"
varchar cost_param2 "消耗参数2"
int quantity "数量"
int sort_order "排序权重"
}
task_conditions {
int id PK "主键"
varchar code "条件代码"
varchar name "条件名称"
varchar handler_class "处理器类"
}
task_achievement_conditions {
int id PK "主键"
int task_id FK "任务ID"
int condition_id FK "条件ID"
varchar condition_type "条件类型"
int target_value "目标值"
}
task_user_tasks {
int id PK "主键"
int user_id "用户ID"
int task_id FK "任务ID"
tinyint status "状态"
int progress "进度"
}
task_user_progress {
int id PK "主键"
int user_id "用户ID"
int task_id FK "任务ID"
int achievement_condition_id FK "条件ID"
int current_value "当前值"
}
task_completion_logs {
int id PK "主键"
int user_id "用户ID"
int task_id FK "任务ID"
timestamp completed_at "完成时间"
}
task_reward_logs {
int id PK "主键"
int user_id "用户ID"
int task_id FK "任务ID"
int user_task_id FK "用户任务ID"
json rewards "奖励内容"
}
task_cost_logs {
int id PK "主键"
int user_id "用户ID"
int task_id FK "任务ID"
varchar cost_type "消耗类型"
varchar cost_param1 "消耗参数1"
varchar cost_param2 "消耗参数2"
int quantity "数量"
timestamp cost_at "消耗时间"
}
task_reset_logs {
int id PK "主键"
varchar reset_type "重置类型"
timestamp reset_time "重置时间"
int affected_count "影响数量"
}
task_categories 1 --< task_tasks
task_tasks 1 --< task_rewards
task_tasks 1 --< task_costs
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
task_user_tasks 1 --< task_cost_logs
{
"properties": {
"seed_ids": {
"type": "array",
"items": {"type": "integer"},
"description": "指定种子ID列表"
},
"land_type": {
"type": "integer",
"description": "指定土地类型"
},
"min_level": {
"type": "integer",
"description": "最低等级要求"
}
}
}
任务定义表中的display_params字段用于存储与任务显示相关的参数,例如:
{
"icon": "task_daily_login.png",
"background": "task_bg_blue.jpg",
"color_theme": "blue",
"priority": 100,
"ui_group": "main",
"show_progress": true,
"progress_style": "percentage",
"show_timer": true,
"timer_style": "countdown",
"animation": "pulse",
"jump_target": "farm",
"tags": ["new", "hot", "limited"],
"client_custom": {
"show_in_dashboard": true,
"notification_enabled": true,
"highlight_effect": "glow"
}
}
任务奖励定义表中的extra_data字段可以存储额外的奖励信息,例如:
{
"display_name": "高级化肥礼包",
"icon_url": "items/fertilizer_premium.png",
"description": "包含多种高级化肥的礼包",
"unlock_requirements": {
"level": 10,
"vip": 2
}
}
{
"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
- 参数:{}
- 运算符:=
奖励:
- 类型:currency
- 奖励ID:1(金币)
- 数量:100
任务名称:农场管理大师
任务描述:种植3种不同作物,收获10次,升级1次土地
任务类型:weekly
条件:
- 类型:plant
目标值:3
参数:{"seed_ids": [101, 102, 103, 104, 105]}
运算符:>=
是否必须:是
- 类型:harvest
目标值:10
参数:{}
运算符:>=
是否必须:是
- 类型:upgrade_land
目标值:1
参数:{}
运算符:>=
是否必须:是
奖励:
- 类型:item
- 奖励ID:2001(高级化肥)
- 数量:3
- 类型:currency
- 奖励ID:1(金币)
- 数量: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
是否必须:是
奖励:
- 类型:item
- 奖励ID:3001(稀有种子)
- 数量:1
- 类型:currency
- 奖励ID:1(金币)
- 数量:1000