68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @property int $id
|
||
|
|
* @property int $character_id
|
||
|
|
* @property string $action_type
|
||
|
|
* @property \Illuminate\Support\Carbon $last_action_at
|
||
|
|
* @property \Illuminate\Support\Carbon $created_at
|
||
|
|
* @property \Illuminate\Support\Carbon $updated_at
|
||
|
|
*/
|
||
|
|
class Cooldown extends Model
|
||
|
|
{
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'character_id',
|
||
|
|
'action_type',
|
||
|
|
'last_action_at',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'last_action_at' => 'datetime',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the character that owns this cooldown.
|
||
|
|
*/
|
||
|
|
public function character(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Character::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if this cooldown has expired
|
||
|
|
*/
|
||
|
|
public function hasExpired(): bool
|
||
|
|
{
|
||
|
|
$cooldownSeconds = config("game.cooldowns.{$this->action_type}", config('game.cooldowns.default', 60));
|
||
|
|
$secondsSinceAction = now()->diffInSeconds($this->last_action_at);
|
||
|
|
return $secondsSinceAction >= $cooldownSeconds;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get remaining cooldown time in seconds
|
||
|
|
*/
|
||
|
|
public function getRemainingSecondsAttribute(): int
|
||
|
|
{
|
||
|
|
$cooldownSeconds = config("game.cooldowns.{$this->action_type}", config('game.cooldowns.default', 60));
|
||
|
|
$secondsSinceAction = now()->diffInSeconds($this->last_action_at);
|
||
|
|
return max(0, $cooldownSeconds - $secondsSinceAction);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the cooldown to now
|
||
|
|
*/
|
||
|
|
public function refresh(): void
|
||
|
|
{
|
||
|
|
$this->last_action_at = now();
|
||
|
|
$this->save();
|
||
|
|
}
|
||
|
|
}
|