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()); $window = (int) $world->gameConfig()->get('movement.cancel_window', 600); $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, $window, $now) { $own = $m->player_id === $me->id; $cancelUntil = $m->sent_at->addSeconds($window); 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, // An own attack or support can be called back until then. 'cancel_until' => $own && $m->type !== Movement::RETURN && $cancelUntil->greaterThan($now) ? self::iso($cancelUntil) : null, ]; })->values()->all(); } private static function iso(CarbonImmutable $at): string { return $at->utc()->format('Y-m-d\TH:i:s.v\Z'); } }