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

60 lines
1.7 KiB
PHP
Raw Permalink Normal View History

<?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';
// Merchants with cargo on their way to another village, and empty on their way home.
public const TRADE = 'trade';
public const TRADE_RETURN = 'trade_return';
/** Merchants, not troops: they never fight and stay out of the rally lists, the map and the noble slots. */
public const TRADES = [self::TRADE, self::TRADE_RETURN];
protected $guarded = [];
protected $dateFormat = 'Y-m-d H:i:s.v';
protected function casts(): array
{
return [
'units' => 'array',
'cargo' => 'array',
'rng_seed' => 'integer',
'merchants' => 'integer',
'route_id' => 'integer',
'offer' => 'boolean',
'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 in_array($this->type, [self::RETURN, self::TRADE_RETURN], true) ? $this->target_id : $this->origin_id;
}
}