game-village-politics-backend/app/Models/Movement.php

49 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/** A group on the map: an attack, support, or troops going home. Lives until it arrives. */
class Movement extends Model
{
public const ATTACK = 'attack';
public const SUPPORT = 'support';
public const RETURN = 'return';
protected $guarded = [];
protected $dateFormat = 'Y-m-d H:i:s.v';
protected function casts(): array
{
return [
'units' => 'array',
'cargo' => 'array',
'rng_seed' => 'integer',
'sent_at' => 'immutable_datetime',
'arrives_at' => 'immutable_datetime',
'visible_at' => 'immutable_datetime',
];
}
public function origin(): BelongsTo
{
return $this->belongsTo(Village::class, 'origin_id');
}
public function target(): BelongsTo
{
return $this->belongsTo(Village::class, 'target_id');
}
/** Where the troops belong: a return trip goes there, the others start there. */
public function homeId(): int
{
return $this->type === self::RETURN ? $this->target_id : $this->origin_id;
}
}