2026-10-28 18:45:03 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Actions;
|
|
|
|
|
|
|
2026-10-30 10:00:51 +00:00
|
|
|
|
use App\Engine\Engine;
|
2026-10-29 09:05:55 +00:00
|
|
|
|
use App\Engine\Quests;
|
2026-10-30 10:00:51 +00:00
|
|
|
|
use App\Engine\Tribes;
|
2026-10-28 18:45:03 +00:00
|
|
|
|
use App\Game\GameRuleViolation;
|
|
|
|
|
|
use App\Game\Map\Continent;
|
2026-10-29 00:51:37 +00:00
|
|
|
|
use App\Game\Map\MapBounds;
|
2026-10-30 10:00:51 +00:00
|
|
|
|
use App\Game\Travel\GroupSize;
|
|
|
|
|
|
use App\Game\Tribes\Diplomacy;
|
|
|
|
|
|
use App\Models\Movement;
|
2026-10-28 18:45:03 +00:00
|
|
|
|
use App\Models\Player;
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
use App\Models\Village;
|
|
|
|
|
|
use App\Models\World;
|
2026-10-30 10:00:51 +00:00
|
|
|
|
use Carbon\CarbonImmutable;
|
2026-10-28 18:45:03 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 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.
|
2026-10-29 00:51:37 +00:00
|
|
|
|
* Coordinates run from −size/2 to size/2 − 1; the world starts at 0|0.
|
2026-10-28 18:45:03 +00:00
|
|
|
|
*/
|
|
|
|
|
|
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();
|
2026-10-29 00:51:37 +00:00
|
|
|
|
$bounds = MapBounds::of($config);
|
|
|
|
|
|
$size = $bounds->size;
|
2026-10-28 18:45:03 +00:00
|
|
|
|
$max = $config->get('world.map.max_view', 41);
|
|
|
|
|
|
$w = max(1, min($max, $w));
|
|
|
|
|
|
$h = max(1, min($max, $h));
|
|
|
|
|
|
|
2026-10-30 10:00:51 +00:00
|
|
|
|
$me = Player::with('membership')->where('user_id', $user->id)->where('world_id', $world->id)->first();
|
|
|
|
|
|
Quests::mark($me?->id, 'map.viewed');
|
|
|
|
|
|
$now = now()->toImmutable();
|
2026-10-29 09:05:55 +00:00
|
|
|
|
|
2026-10-28 18:45:03 +00:00
|
|
|
|
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();
|
2026-10-29 00:51:37 +00:00
|
|
|
|
$x = $home->x ?? 0;
|
|
|
|
|
|
$y = $home->y ?? 0;
|
2026-10-28 18:45:03 +00:00
|
|
|
|
}
|
2026-10-29 00:51:37 +00:00
|
|
|
|
$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)));
|
2026-10-28 18:45:03 +00:00
|
|
|
|
|
|
|
|
|
|
$villages = Village::where('world_id', $world->id)
|
|
|
|
|
|
->whereBetween('x', [$left, $left + $w - 1])
|
|
|
|
|
|
->whereBetween('y', [$top, $top + $h - 1])
|
2026-10-30 10:00:51 +00:00
|
|
|
|
->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
|
2026-10-28 18:45:03 +00:00
|
|
|
|
->map(fn (Village $v) => [
|
|
|
|
|
|
'id' => $v->id,
|
|
|
|
|
|
'x' => $v->x,
|
|
|
|
|
|
'y' => $v->y,
|
|
|
|
|
|
'name' => $v->name,
|
|
|
|
|
|
'points' => $v->points,
|
2026-10-29 00:51:37 +00:00
|
|
|
|
'continent' => Continent::of($v->x, $v->y, $size),
|
2026-10-30 10:00:51 +00:00
|
|
|
|
'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,
|
2026-10-28 18:45:03 +00:00
|
|
|
|
'own' => $v->player?->user_id === $user->id,
|
2026-10-29 15:31:34 +00:00
|
|
|
|
'crown' => $v->crown,
|
2026-10-30 10:00:51 +00:00
|
|
|
|
// own, tribe, ally, nap, enemy, none (another player) or barbarian, for the colour.
|
|
|
|
|
|
'relation' => $relation($v),
|
2026-10-28 18:45:03 +00:00
|
|
|
|
])->values()->all();
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
'world_id' => $world->id,
|
|
|
|
|
|
'size' => $size,
|
2026-10-29 00:51:37 +00:00
|
|
|
|
// Lowest coordinate on both axes; the highest is min + size − 1.
|
|
|
|
|
|
'min' => $bounds->min(),
|
2026-10-28 18:45:03 +00:00
|
|
|
|
'center' => ['x' => $x, 'y' => $y],
|
|
|
|
|
|
'x' => $left,
|
|
|
|
|
|
'y' => $top,
|
|
|
|
|
|
'w' => $w,
|
|
|
|
|
|
'h' => $h,
|
|
|
|
|
|
'villages' => $villages,
|
2026-10-30 10:00:51 +00:00
|
|
|
|
'movements' => $me ? $this->movements($me, $world, $now) : [],
|
2026-10-28 18:45:03 +00:00
|
|
|
|
];
|
|
|
|
|
|
}
|
2026-10-30 10:00:51 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 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');
|
|
|
|
|
|
}
|
2026-10-28 18:45:03 +00:00
|
|
|
|
}
|