diff --git a/app/Actions/ShowMap.php b/app/Actions/ShowMap.php index 997da99..567f6dc 100644 --- a/app/Actions/ShowMap.php +++ b/app/Actions/ShowMap.php @@ -2,14 +2,20 @@ 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 @@ -28,7 +34,9 @@ final class ShowMap $w = max(1, min($max, $w)); $h = max(1, min($max, $h)); - Quests::mark(Player::where('user_id', $user->id)->where('world_id', $world->id)->value('id'), 'map.viewed'); + $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')) @@ -44,8 +52,25 @@ final class ShowMap $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') - ->get(['id', 'player_id', 'crown', 'name', 'x', 'y', 'points']) + ->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, @@ -53,9 +78,11 @@ final class ShowMap '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] : null, + '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 [ @@ -69,6 +96,47 @@ final class ShowMap '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'); + } } diff --git a/app/Game/Travel/GroupSize.php b/app/Game/Travel/GroupSize.php new file mode 100644 index 0000000..c8010ee --- /dev/null +++ b/app/Game/Travel/GroupSize.php @@ -0,0 +1,31 @@ + $units */ + public function tier(array $units): string + { + $heads = array_sum($units); + $steps = $this->config->get('movement.size_tiers', [50, 250, 1000, 5000]); + $tier = 0; + foreach ($steps as $i => $from) { + if ($heads >= $from) { + $tier = $i + 1; + } + } + + return self::TIERS[min($tier, count(self::TIERS) - 1)]; + } +} diff --git a/config/game/movement.php b/config/game/movement.php index 466e381..907c78b 100644 --- a/config/game/movement.php +++ b/config/game/movement.php @@ -17,6 +17,10 @@ return [ // Multiplies every unit's speed on this world, on top of the world speed. 'unit_speed' => 1.0, + // Heads from which a group on the map looks small, medium, large and huge; below the + // first step it is tiny. Strangers see only this, never the units. + 'size_tiers' => [50, 250, 1000, 5000], + // Fields around a village within which it spots incoming groups, before a watchtower. 'detection' => 2, diff --git a/docs/architecture.md b/docs/architecture.md index 4260f34..faf5f0b 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -188,7 +188,7 @@ The world grows the way Die Stämme's does (measured on de261, see the research - A village whose player has been inactive for too long turns barbarian (M6). - `php artisan game:reset-world {slug}` wipes a world back to empty (villages, players, movements, reports, events) and keeps user accounts and the world's config. -`map.view {world_id, x, y, w, h}` returns the villages in a `w × h` window (at most `map.max_view` per side) centred on `x|y`, kept inside the map, plus `size` and `min` (the lowest coordinate); without a centre it opens on the player's first village, else on 0|0. Each village comes with its coordinates, continent, name, points, owner (null for barbarians) and whether it is the player's own. The client draws it like the Die Stämme map: a top-down field grid with coordinate rulers and continent lines. +`map.view {world_id, x, y, w, h}` returns the villages in a `w × h` window (at most `map.max_view` per side) centred on `x|y`, kept inside the map, plus `size` and `min` (the lowest coordinate); without a centre it opens on the player's first village, else on 0|0. Each village comes with its coordinates, continent, name, points, owner (null for barbarians) with their tribe tag, whether it is the player's own, and its `relation` to the viewer for the colour: own, tribe, ally, nap, enemy, none (another player) or barbarian. It also carries `movements`: the player's own commands and groups going home (with units), and groups coming to their villages once spotted (with the size tier only: tiny under 50 heads, then small, medium, large and huge from `movement.size_tiers`). The client draws each as a line from origin to target with the group at its position, a straight line at constant speed from `sent_at` to `arrives_at`; raiders wait at their camp until they march. The client draws it like the Die Stämme map: a top-down field grid with coordinate rulers and continent lines. ## Movements and encounters diff --git a/docs/roadmap.md b/docs/roadmap.md index ab2ca9d..90835e4 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -87,6 +87,7 @@ The world pushes back, so a world with few players is a game too: something thre - [ ] Tribe relations applied at encounter time (with M5) - [x] Village switcher in the header - [ ] Overviews across villages (production, troops, buildings per village) +- [x] Map colours by relation (own, tribe, ally, NAP, enemy, others, barbarians) and every known movement drawn on the map with its size - [x] Restart on the same world after losing every village - [ ] Inactive villages turn barbarian diff --git a/tests/Feature/MapRelationsTest.php b/tests/Feature/MapRelationsTest.php new file mode 100644 index 0000000..1baef2a --- /dev/null +++ b/tests/Feature/MapRelationsTest.php @@ -0,0 +1,63 @@ + $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00'))); + +function mapAround(Village $village): array +{ + return app(ShowMap::class)(owner($village), $village->world_id, $village->x, $village->y, 41, 41); +} + +function relationOn(array $map, Village $village): ?string +{ + return collect($map['villages'])->firstWhere('id', $village->id)['relation'] ?? null; +} + +it('colours villages by what their owner is to the viewer', function () { + $me = startVillage(); + $mate = startVillage(); + $ally = startVillage(); + $foe = startVillage(); + $stranger = startVillage(); + $barbarian = app(VillageFactory::class)->create($me->world, null, 'Barbarian village', $me->x + 2, $me->y + 2, ['headquarters' => 1]); + foreach ([[$me, 'AAA'], [$ally, 'BBB'], [$foe, 'CCC']] as [$v, $tag]) { + app(Tribes::class)->found(owner($v), $v->world_id, "Tribe $tag", $tag); + } + app(Tribes::class)->invite(owner($me), $me->world_id, $mate->player->name); + app(Tribes::class)->join(owner($mate), $mate->world_id, Tribe::where('tag', 'AAA')->value('id')); + app(Tribes::class)->propose(owner($me), $me->world_id, 'BBB', 'ally'); + app(Tribes::class)->accept(owner($ally), $ally->world_id, Tribe::where('tag', 'AAA')->value('id')); + app(Tribes::class)->enemy(owner($foe), $foe->world_id, 'AAA'); + + $map = mapAround($me); + expect(relationOn($map, $me))->toBe('own') + ->and(relationOn($map, $mate))->toBe('tribe') + ->and(relationOn($map, $ally))->toBe('ally') + ->and(relationOn($map, $foe))->toBe('enemy') + ->and(relationOn($map, $stranger))->toBe('none') + ->and(relationOn($map, $barbarian))->toBe('barbarian') + ->and(collect($map['villages'])->firstWhere('id', $ally->id)['player']['tribe']['tag'])->toBe('BBB'); +}); + +it('shows own groups with units and spotted incoming ones by size only', function () { + $me = startVillage(); + $other = startVillage(); + $barbarian = app(VillageFactory::class)->create($me->world, null, 'Barbarian village', $me->x + 3, $me->y, ['headquarters' => 1]); + $me->buildings()->create(['type' => 'rally_point', 'level' => 1]); + $me->troops()->create(['home_village_id' => $me->id, 'unit' => 'spear', 'count' => 60]); + app(SendTroops::class)(owner($me), $me->id, 'attack', $barbarian->x, $barbarian->y, ['spear' => 60], null, null); + + $mine = mapAround($me)['movements']; + expect($mine)->toHaveCount(1) + ->and($mine[0])->toMatchArray(['type' => 'attack', 'own' => true, 'raid' => false, 'size' => 'small', 'units' => ['spear' => 60]]) + ->and($mine[0]['from']['id'])->toBe($me->id) + ->and($mine[0]['to']['id'])->toBe($barbarian->id) + ->and(mapAround($other)['movements'])->toBe([]); +}); diff --git a/tests/Unit/Game/GroupSizeTest.php b/tests/Unit/Game/GroupSizeTest.php new file mode 100644 index 0000000..5936d5a --- /dev/null +++ b/tests/Unit/Game/GroupSizeTest.php @@ -0,0 +1,15 @@ +tier(['spear' => 1]))->toBe('tiny') + ->and($size->tier(['spear' => 49]))->toBe('tiny') + ->and($size->tier(['spear' => 30, 'sword' => 20]))->toBe('small') + ->and($size->tier(['axe' => 999]))->toBe('medium') + ->and($size->tier(['spear' => 1000]))->toBe('large') + ->and($size->tier(['spear' => 20000]))->toBe('huge') + ->and($size->tier([]))->toBe('tiny'); +});