| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace App\Module\Pet\Models;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 宠物战斗记录模型
- *
- * field start
- * @property int $id
- * @property int $pet_id
- * @property int $battle_type 战斗类型:1偷菜,2守护,3争霸赛
- * @property int $opponent_id 对手ID(可为空)
- * @property int $result 战斗结果:0失败,1胜利
- * @property array $reward 战斗奖励
- * @property \Carbon\Carbon $battle_time
- * @property \Carbon\Carbon $created_at
- * field end
- */
- class PetBattleLog extends ModelCore
- {
- /**
- * 战斗类型常量
- */
- const BATTLE_TYPE_HARVEST = 1; // 偷菜
- const BATTLE_TYPE_GUARD = 2; // 守护
- const BATTLE_TYPE_ROYALE = 3; // 争霸赛
- /**
- * 战斗结果常量
- */
- const RESULT_FAIL = 0; // 失败
- const RESULT_SUCCESS = 1; // 胜利
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'pet_battle_logs';
- /**
- * 指示模型是否应该被打上时间戳
- * 由于这是日志表,只需要创建时间,不需要更新时间
- *
- * @var bool
- */
- public $timestamps = true;
- /**
- * 更新时间戳字段名
- * 设置为null表示不使用updated_at字段
- *
- * @var string|null
- */
- const UPDATED_AT = null;
- // attrlist start
- protected $fillable = [
- 'id',
- 'pet_id',
- 'battle_type',
- 'opponent_id',
- 'result',
- 'reward',
- 'battle_time',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'pet_id' => 'integer',
- 'battle_type' => 'integer',
- 'opponent_id' => 'integer',
- 'result' => 'integer',
- 'reward' => 'json',
- 'battle_time' => 'datetime',
- 'created_at' => 'datetime',
- ];
- /**
- * 获取关联的宠物
- *
- * @return BelongsTo
- */
- public function pet(): BelongsTo
- {
- return $this->belongsTo(Pet::class, 'pet_id');
- }
- /**
- * 获取对手宠物(如果有)
- *
- * @return BelongsTo|null
- */
- public function opponent()
- {
- if (!$this->opponent_id) {
- return null;
- }
- return $this->belongsTo(Pet::class, 'opponent_id');
- }
- }
|