'boolean', 'craft_count' => 'integer', ]; /** * 获取关联的配方 * * @return BelongsTo */ public function recipe(): BelongsTo { return $this->belongsTo(ItemRecipe::class, 'recipe_id'); } /** * 解锁配方 * * @return bool */ public function unlock(): bool { $this->is_unlocked = true; $this->unlock_time = now(); return $this->save(); } /** * 增加合成次数 * * @param int $count 增加的次数 * @return bool */ public function incrementCraftCount(int $count = 1): bool { $this->craft_count += $count; $this->last_craft_time = now(); return $this->save(); } /** * 检查是否在冷却中 * * @return bool */ public function isInCooldown(): bool { if (empty($this->last_craft_time)) { return false; } $cooldownSeconds = $this->recipe->cooldown_seconds ?? 0; if ($cooldownSeconds <= 0) { return false; } return $this->last_craft_time->addSeconds($cooldownSeconds)->isFuture(); } /** * 获取剩余冷却时间(秒) * * @return int */ public function getRemainingCooldown(): int { if (!$this->isInCooldown()) { return 0; } $cooldownSeconds = $this->recipe->cooldown_seconds ?? 0; $cooldownEnd = $this->last_craft_time->addSeconds($cooldownSeconds); return max(0, $cooldownEnd->diffInSeconds(now())); } }