43 lines
1.0 KiB
PHP
43 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class RecruitOrder extends Model
|
|
{
|
|
protected $guarded = [];
|
|
|
|
protected $dateFormat = 'Y-m-d H:i:s.v';
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'count' => 'integer',
|
|
'done' => 'integer',
|
|
'seconds_per_unit' => 'integer',
|
|
'cost' => 'array',
|
|
'starts_at' => 'immutable_datetime',
|
|
'finishes_at' => 'immutable_datetime',
|
|
];
|
|
}
|
|
|
|
/** Units trained by $at, never more than ordered. */
|
|
public function doneAt(CarbonImmutable $at): int
|
|
{
|
|
if ($at->lessThan($this->starts_at)) {
|
|
return 0;
|
|
}
|
|
$elapsed = ($at->getTimestampMs() - $this->starts_at->getTimestampMs()) / 1000;
|
|
|
|
return min($this->count, (int) floor($elapsed / $this->seconds_per_unit));
|
|
}
|
|
|
|
public function building(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Building::class);
|
|
}
|
|
}
|