Task.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Module\Task\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use App\Module\Game\Models\GameRewardGroup;
  7. use App\Module\Game\Models\GameConsumeGroup;
  8. use App\Module\Game\Models\GameConditionGroup;
  9. /**
  10. * 任务模型
  11. *
  12. * field start
  13. * @property int $id 主键
  14. * @property int $category_id 任务分类ID,外键关联task_categories表
  15. * @property string $name 任务名称
  16. * @property string $description 任务描述
  17. * @property string $type 任务类型(daily, weekly, achievement, event, tutorial, team)
  18. * @property array $prerequisite_tasks 前置任务ID(JSON格式)
  19. * @property int $level_required 所需等级
  20. * @property int $time_limit 时间限制(秒,NULL表示无限制)
  21. * @property int $max_completions 最大完成次数(用于限制任务可完成的次数)
  22. * @property string $reset_type 重置类型(none, daily, weekly, monthly)
  23. * @property array $display_params 显示参数(JSON格式,存储与任务显示相关的参数)
  24. * @property int $sort_order 排序权重(数值越大越靠前)
  25. * @property bool $is_active 是否激活(0:否, 1:是)
  26. * @property string $start_time 开始时间(NULL表示立即开始)
  27. * @property string $end_time 结束时间(NULL表示永不结束)
  28. * @property \Carbon\Carbon $created_at 创建时间
  29. * @property \Carbon\Carbon $updated_at 更新时间
  30. * @property int $reset_interval 重置间隔(秒),根据重置类型自动计算
  31. * @property string $reset_time_field 重置时间字段,如"daily_reset_time",用于存储具体重置时间点
  32. * @property int $reward_group_id 奖励组ID,关联game_reward_groups表
  33. * @property int $consume_group_id 消耗组ID,关联game_consume_groups表
  34. * @property int $prerequisite_condition_group_id 前置条件组ID,关联game_condition_groups表
  35. * @property int $achievement_condition_group_id 达成条件组ID,关联game_condition_groups表
  36. * @property bool $auto_accept 是否自动接取任务(0:否, 1:是)
  37. * @property bool $auto_complete 是否自动完成任务(0:否, 1:是)
  38. * @property bool $auto_reward 是否自动发放奖励(0:否, 1:是)
  39. * @property bool $auto_reset 是否自动重置任务(0:否, 1:是)
  40. * field end
  41. */
  42. class Task extends ModelCore
  43. {
  44. /**
  45. * 与模型关联的表名
  46. *
  47. * @var string
  48. */
  49. protected $table = 'task_tasks';
  50. /**
  51. * 主键
  52. *
  53. * @var string
  54. */
  55. protected $primaryKey = 'id';
  56. /**
  57. * 应该被转换为原生类型的属性
  58. *
  59. * @var array
  60. */
  61. protected $casts = [
  62. 'prerequisite_tasks' => 'array',
  63. 'display_params' => 'array',
  64. 'is_active' => 'boolean',
  65. 'auto_accept' => 'boolean',
  66. 'auto_complete' => 'boolean',
  67. 'auto_reward' => 'boolean',
  68. 'auto_reset' => 'boolean',
  69. ];
  70. /**
  71. * 应该被转换为日期的属性
  72. *
  73. * @var array
  74. */
  75. protected $dates = [
  76. 'start_time',
  77. 'end_time',
  78. 'created_at',
  79. 'updated_at',
  80. ];
  81. // attrlist start
  82. protected $fillable = [
  83. 'id',
  84. 'category_id',
  85. 'name',
  86. 'description',
  87. 'type',
  88. 'prerequisite_tasks',
  89. 'level_required',
  90. 'time_limit',
  91. 'max_completions',
  92. 'reset_type',
  93. 'display_params',
  94. 'sort_order',
  95. 'is_active',
  96. 'start_time',
  97. 'end_time',
  98. 'reset_interval',
  99. 'reset_time_field',
  100. 'reward_group_id',
  101. 'consume_group_id',
  102. 'prerequisite_condition_group_id',
  103. 'achievement_condition_group_id',
  104. 'auto_accept',
  105. 'auto_complete',
  106. 'auto_reward',
  107. 'auto_reset',
  108. ];
  109. // attrlist end
  110. /**
  111. * 获取任务所属分类
  112. *
  113. * @return BelongsTo
  114. */
  115. public function category(): BelongsTo
  116. {
  117. return $this->belongsTo(TaskCategory::class, 'category_id', 'id');
  118. }
  119. /**
  120. * 获取任务的奖励(已废弃,使用rewardGroup替代)
  121. *
  122. * @return HasMany
  123. * @deprecated
  124. */
  125. public function rewards(): HasMany
  126. {
  127. return $this->hasMany(TaskReward::class, 'task_id', 'id');
  128. }
  129. /**
  130. * 获取任务关联的奖励组
  131. *
  132. * @return BelongsTo
  133. */
  134. public function rewardGroup(): BelongsTo
  135. {
  136. return $this->belongsTo(GameRewardGroup::class, 'reward_group_id', 'id');
  137. }
  138. /**
  139. * 获取任务关联的消耗组
  140. *
  141. * @return BelongsTo
  142. */
  143. public function consumeGroup(): BelongsTo
  144. {
  145. return $this->belongsTo(GameConsumeGroup::class, 'consume_group_id', 'id');
  146. }
  147. /**
  148. * 获取任务关联的前置条件组
  149. *
  150. * @return BelongsTo
  151. */
  152. public function prerequisiteConditionGroup(): BelongsTo
  153. {
  154. return $this->belongsTo(GameConditionGroup::class, 'prerequisite_condition_group_id', 'id');
  155. }
  156. /**
  157. * 获取任务关联的达成条件组
  158. *
  159. * @return BelongsTo
  160. */
  161. public function achievementConditionGroup(): BelongsTo
  162. {
  163. return $this->belongsTo(GameConditionGroup::class, 'achievement_condition_group_id', 'id');
  164. }
  165. /**
  166. * 获取任务的消耗
  167. *
  168. * @return HasMany
  169. */
  170. public function costs(): HasMany
  171. {
  172. return $this->hasMany(TaskCost::class, 'task_id', 'id');
  173. }
  174. /**
  175. * 获取任务的达成条件
  176. *
  177. * @return HasMany
  178. */
  179. public function achievementConditions(): HasMany
  180. {
  181. return $this->hasMany(TaskAchievementCondition::class, 'task_id', 'id');
  182. }
  183. /**
  184. * 获取用户任务关联
  185. *
  186. * @return HasMany
  187. */
  188. public function userTasks(): HasMany
  189. {
  190. return $this->hasMany(TaskUserTask::class, 'task_id', 'id');
  191. }
  192. }