game-village-politics-backend/app/Actions/ShowMap.php

143 lines
6.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Actions;
use App\Engine\Engine;
use App\Engine\Quests;
use App\Engine\Tribes;
use App\Game\GameRuleViolation;
use App\Game\Map\Continent;
use App\Game\Map\MapBounds;
use App\Game\Travel\GroupSize;
use App\Game\Tribes\Diplomacy;
use App\Models\Movement;
use App\Models\Player;
use App\Models\User;
use App\Models\Village;
use App\Models\World;
use Carbon\CarbonImmutable;
/**
* The villages in a w × h window of a world's map, centred on (x, y) the way the
* Die Stämme map is. Without a centre it opens on the player's first village there.
* Coordinates run from −size/2 to size/2 − 1; the world starts at 0|0.
*/
final class ShowMap
{
public function __invoke(User $user, int $worldId, ?int $x, ?int $y, int $w, int $h): array
{
$world = World::find($worldId) ?? throw new GameRuleViolation('not_found', 'World not found.');
$config = $world->gameConfig();
$bounds = MapBounds::of($config);
$size = $bounds->size;
$max = $config->get('world.map.max_view', 41);
$w = max(1, min($max, $w));
$h = max(1, min($max, $h));
$me = Player::with('membership')->where('user_id', $user->id)->where('world_id', $world->id)->first();
Quests::mark($me?->id, 'map.viewed');
$now = now()->toImmutable();
if ($x === null || $y === null) {
$home = Village::whereIn('player_id', Player::where('user_id', $user->id)->where('world_id', $world->id)->select('id'))
->orderBy('id')->first();
$x = $home->x ?? 0;
$y = $home->y ?? 0;
}
$x = $bounds->clamp($x);
$y = $bounds->clamp($y);
$left = max($bounds->min(), min($bounds->max() - $w + 1, $x - intdiv($w, 2)));
$top = max($bounds->min(), min($bounds->max() - $h + 1, $y - intdiv($h, 2)));
$villages = Village::where('world_id', $world->id)
->whereBetween('x', [$left, $left + $w - 1])
->whereBetween('y', [$top, $top + $h - 1])
->with('player:id,user_id,name', 'player.membership.tribe:id,tag')
->get(['id', 'player_id', 'crown', 'name', 'x', 'y', 'points']);
$relations = Tribes::relationsOf($me?->tribeId(), $now);
$relation = function (Village $v) use ($me, $relations) {
if (! $v->player) {
return 'barbarian';
}
if ($v->player->id === $me?->id) {
return 'own';
}
$tribe = $v->player->membership?->tribe_id;
return match (true) {
$tribe === null || $me?->tribeId() === null => Diplomacy::NONE,
$tribe === $me->tribeId() => Diplomacy::TRIBE,
default => $relations[$tribe] ?? Diplomacy::NONE,
};
};
$villages = $villages
->map(fn (Village $v) => [
'id' => $v->id,
'x' => $v->x,
'y' => $v->y,
'name' => $v->name,
'points' => $v->points,
'continent' => Continent::of($v->x, $v->y, $size),
'player' => $v->player ? ['id' => $v->player->id, 'name' => $v->player->name, 'tribe' => $v->player->membership?->tribe ? ['id' => $v->player->membership->tribe->id, 'tag' => $v->player->membership->tribe->tag] : null] : null,
'own' => $v->player?->user_id === $user->id,
'crown' => $v->crown,
// own, tribe, ally, nap, enemy, none (another player) or barbarian, for the colour.
'relation' => $relation($v),
])->values()->all();
return [
'world_id' => $world->id,
'size' => $size,
// Lowest coordinate on both axes; the highest is min + size − 1.
'min' => $bounds->min(),
'center' => ['x' => $x, 'y' => $y],
'x' => $left,
'y' => $top,
'w' => $w,
'h' => $h,
'villages' => $villages,
'movements' => $me ? $this->movements($me, $world, $now) : [],
];
}
/**
* Every group the player knows of on this world: their own commands and groups going
* home, and groups coming to their villages once spotted. Strangers' groups show their
* size tier only. Positions are left to the client: a straight line at constant speed
* from origin at sent_at to target at arrives_at (raiders wait at their camp until then).
*/
private function movements(Player $me, World $world, CarbonImmutable $now): array
{
$size = new GroupSize($world->gameConfig());
$mine = Village::where('player_id', $me->id)->pluck('id');
return Movement::with(['origin:id,name,x,y', 'target:id,name,x,y'])
->where('world_id', $world->id)
->where(fn ($q) => $q->where('player_id', $me->id)
->orWhere(fn ($q) => $q->whereIn('target_id', $mine)->where('type', '!=', Movement::RETURN)->where('visible_at', '<=', Engine::stamp($now))))
->where('arrives_at', '>', Engine::stamp($now))
->orderBy('arrives_at')->limit(300)->get()
->map(function (Movement $m) use ($me, $size) {
$own = $m->player_id === $me->id;
return [
'id' => $m->id,
'type' => $m->type,
'own' => $own,
'raid' => $m->player_id === null,
'from' => ['id' => $m->origin->id, 'name' => $m->origin->name, 'x' => $m->origin->x, 'y' => $m->origin->y],
'to' => ['id' => $m->target->id, 'name' => $m->target->name, 'x' => $m->target->x, 'y' => $m->target->y],
'sent_at' => self::iso($m->sent_at),
'arrives_at' => self::iso($m->arrives_at),
'size' => $size->tier($m->units),
'units' => $own ? $m->units : null,
];
})->values()->all();
}
private static function iso(CarbonImmutable $at): string
{
return $at->utc()->format('Y-m-d\TH:i:s.v\Z');
}
}