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

70 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use App\Game\GameRuleViolation;
use App\Game\WorldConfig;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class World extends Model
{
protected $guarded = [];
protected function casts(): array
{
return [
'config' => 'array',
'started_at' => 'immutable_datetime',
'crowns_placed_at' => 'immutable_datetime',
// When the holder wins if they keep the crowns; ended_at once someone has.
'ends_at' => 'immutable_datetime',
'ended_at' => 'immutable_datetime',
];
}
/** The config/game/* defaults with this world's overrides on top. */
public function gameConfig(): WorldConfig
{
return WorldConfig::make([
'world' => config('game.world'),
'economy' => config('game.economy'),
'buildings' => config('game.buildings'),
'units' => config('game.units'),
'combat' => config('game.combat'),
'movement' => config('game.movement'),
'raids' => config('game.raids'),
'quests' => config('game.quests'),
], $this->config ?? []);
}
/** Changes are refused once the season is over; the world stays to be looked at. */
public function ensureOpen(): void
{
if ($this->ended_at) {
throw new GameRuleViolation('world_ended', 'This world has ended.');
}
}
public function holder(): BelongsTo
{
return $this->belongsTo(Player::class, 'holder_player_id');
}
public function winner(): BelongsTo
{
return $this->belongsTo(Player::class, 'winner_player_id');
}
public function players(): HasMany
{
return $this->hasMany(Player::class);
}
public function villages(): HasMany
{
return $this->hasMany(Village::class);
}
}