PetBattleLog.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Module\Pet\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * 宠物战斗记录模型
  7. *
  8. * field start
  9. * @property int $id
  10. * @property int $pet_id
  11. * @property int $battle_type 战斗类型:1偷菜,2守护,3争霸赛
  12. * @property int $opponent_id 对手ID(可为空)
  13. * @property int $result 战斗结果:0失败,1胜利
  14. * @property array $reward 战斗奖励
  15. * @property \Carbon\Carbon $battle_time
  16. * @property \Carbon\Carbon $created_at
  17. * field end
  18. */
  19. class PetBattleLog extends ModelCore
  20. {
  21. /**
  22. * 战斗类型常量
  23. */
  24. const BATTLE_TYPE_HARVEST = 1; // 偷菜
  25. const BATTLE_TYPE_GUARD = 2; // 守护
  26. const BATTLE_TYPE_ROYALE = 3; // 争霸赛
  27. /**
  28. * 战斗结果常量
  29. */
  30. const RESULT_FAIL = 0; // 失败
  31. const RESULT_SUCCESS = 1; // 胜利
  32. /**
  33. * 与模型关联的表名
  34. *
  35. * @var string
  36. */
  37. protected $table = 'pet_battle_logs';
  38. /**
  39. * 指示模型是否应该被打上时间戳
  40. * 由于这是日志表,只需要创建时间,不需要更新时间
  41. *
  42. * @var bool
  43. */
  44. public $timestamps = true;
  45. /**
  46. * 更新时间戳字段名
  47. * 设置为null表示不使用updated_at字段
  48. *
  49. * @var string|null
  50. */
  51. const UPDATED_AT = null;
  52. // attrlist start
  53. protected $fillable = [
  54. 'id',
  55. 'pet_id',
  56. 'battle_type',
  57. 'opponent_id',
  58. 'result',
  59. 'reward',
  60. 'battle_time',
  61. ];
  62. // attrlist end
  63. /**
  64. * 应该被转换为原生类型的属性
  65. *
  66. * @var array
  67. */
  68. protected $casts = [
  69. 'pet_id' => 'integer',
  70. 'battle_type' => 'integer',
  71. 'opponent_id' => 'integer',
  72. 'result' => 'integer',
  73. 'reward' => 'json',
  74. 'battle_time' => 'datetime',
  75. 'created_at' => 'datetime',
  76. ];
  77. /**
  78. * 获取关联的宠物
  79. *
  80. * @return BelongsTo
  81. */
  82. public function pet(): BelongsTo
  83. {
  84. return $this->belongsTo(Pet::class, 'pet_id');
  85. }
  86. /**
  87. * 获取对手宠物(如果有)
  88. *
  89. * @return BelongsTo|null
  90. */
  91. public function opponent()
  92. {
  93. if (!$this->opponent_id) {
  94. return null;
  95. }
  96. return $this->belongsTo(Pet::class, 'opponent_id');
  97. }
  98. }