$this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00'))); /** A rally point and own troops at home. */ function rallied(Village $village, array $units = []): Village { $village->buildings()->create(['type' => 'rally_point', 'level' => 1]); foreach ($units as $unit => $count) { $village->troops()->create(['home_village_id' => $village->id, 'unit' => $unit, 'count' => $count]); } return $village->fresh(); } /** A barbarian village at the offset, or the next free field to the east when another player spawned there. */ function barbarianNear(Village $near, int $dx, int $dy): Village { while (Village::where('world_id', $near->world_id)->where('x', $near->x + $dx)->where('y', $near->y + $dy)->exists()) { $dx++; } return app(VillageFactory::class)->create($near->world, null, 'Barbarian village', $near->x + $dx, $near->y + $dy, ['headquarters' => 1, 'storage' => 1, 'farm' => 1]); } function worldRules(Village $village, array $config): void { World::whereKey($village->world_id)->update(['config' => $config]); } function send(Village $from, Village $to, array $units, string $type = 'attack', ?string $mode = null, ?string $catapultTarget = null): Village { return app(SendTroops::class)(owner($from), $from->id, $type, $to->x, $to->y, $units, $mode, $catapultTarget); } it('raids an empty barbarian village and carries the loot home', function () { $village = rallied(startVillage(), ['spear' => 10]); $barbarian = barbarianNear($village, 3, 4); // 5 fields at 18 minutes each. $state = VillagePresenter::present(send($village, $barbarian, ['spear' => 10]), now()->toImmutable()); expect((array) $state['troops'])->toBe([]) ->and($state['movements'])->toHaveCount(1) ->and($state['movements'][0])->toMatchArray(['type' => 'attack', 'direction' => 'out', 'units' => ['spear' => 10], 'arrives_at' => '2026-10-29T13:30:00.000Z']); $this->travel(5400)->seconds(); $this->artisan('game:events --once')->assertSuccessful(); $state = shown($village); // 250 carry, split evenly over 507 of each. expect($state['movements'][0])->toMatchArray(['type' => 'return', 'direction' => 'home', 'cargo' => ['wood' => 84, 'clay' => 83, 'iron' => 83], 'arrives_at' => '2026-10-29T15:00:00.000Z']) ->and($state['unread_reports'])->toBe(1) ->and(Report::sole()->payload)->toMatchArray(['won' => true, 'loot' => ['wood' => 84, 'clay' => 83, 'iron' => 83], 'capacity' => 250]) ->and($barbarian->fresh()->wood)->toEqualWithDelta(507.5 - 84, 0.01); $this->travel(5399)->seconds(); $before = shown($village)['resources']['wood']; $this->travel(1)->seconds(); $this->artisan('game:events --once')->assertSuccessful(); $state = shown($village); expect((array) $state['troops'])->toBe(['spear' => 10]) ->and($state['movements'])->toBe([]) ->and($state['resources']['wood'])->toEqualWithDelta($before + 84, 0.1); }); it('loses an attack against a ten times stronger defence, which loses nobody, and both sides get a report', function () { $attacker = rallied(startVillage(), ['spear' => 10]); $defender = startVillage(); $defender->troops()->create(['home_village_id' => $defender->id, 'unit' => 'sword', 'count' => 20]); worldRules($attacker, ['combat' => ['beginner_protection' => 0, 'luck' => 0]]); send($attacker, $defender, ['spear' => 10]); $this->travel(1)->days(); $this->artisan('game:events --once')->assertSuccessful(); expect((array) shown($attacker)['troops'])->toBe([]) ->and(shown($attacker)['movements'])->toBe([]) ->and((array) shown($defender)['troops'])->toBe(['sword' => 20]); $attack = Report::where('player_id', $attacker->player_id)->sole(); $defense = Report::where('player_id', $defender->player_id)->sole(); expect($attack->type)->toBe('attack') ->and($attack->payload['won'])->toBeFalse() ->and($attack->payload['defender']['units'])->toBeNull() ->and($defense->type)->toBe('defense') ->and($defense->payload['defender'])->toMatchArray(['units' => ['sword' => 20], 'lost' => ['sword' => 0]]) ->and($defense->payload['attacker']['lost'])->toBe(['spear' => 10]); }); it('stations support at the host, who feeds it and can send it home', function () { $home = rallied(startVillage(), ['spear' => 5]); $host = startVillage(); send($home, $host, ['spear' => 5], 'support'); $this->travel(1)->days(); $state = shown($host); expect($state['support']['here'])->toHaveCount(1) ->and($state['support']['here'][0]['village']['id'])->toBe($home->id) ->and((array) $state['support']['here'][0]['units'])->toBe(['spear' => 5]) ->and($state['population']['villagers'])->toBe(19) ->and(shown($home)['support']['away'][0]['village']['id'])->toBe($host->id) ->and(Report::where('type', 'support')->count())->toBe(2); app(SendSupportHome::class)(owner($host), $host->id, $home->id); expect(shown($host)['support']['here'])->toBe([]) ->and(shown($home)['movements'][0])->toMatchArray(['type' => 'return', 'direction' => 'home', 'units' => ['spear' => 5]]); $this->travel(1)->days(); expect((array) shown($home)['troops'])->toBe(['spear' => 5]); }); it('lets the owner call support back from the host', function () { $home = rallied(startVillage(), ['spear' => 5]); $host = startVillage(); send($home, $host, ['spear' => 5], 'support'); $this->travel(1)->days(); $state = VillagePresenter::present(app(WithdrawSupport::class)(owner($home), $home->id, $host->id), now()->toImmutable()); expect($state['movements'][0])->toMatchArray(['direction' => 'home', 'units' => ['spear' => 5]]) ->and($state['support']['away'])->toBe([]) ->and(shown($host)['support']['here'])->toBe([]); $this->travel(1)->days(); expect((array) shown($home)['troops'])->toBe(['spear' => 5]); }); it('calls an attack back within the cancel window', function () { $village = rallied(startVillage(), ['spear' => 10]); $barbarian = barbarianNear($village, 3, 4); send($village, $barbarian, ['spear' => 10]); $this->travel(60)->seconds(); app(CancelMovement::class)(owner($village), $village->id, Movement::sole()->id); expect(shown($village)['movements'][0])->toMatchArray(['direction' => 'home', 'arrives_at' => '2026-10-29T12:02:00.000Z', 'cancel_until' => null]) ->and(GameEvent::where('type', GameEvent::MOVEMENT_ARRIVES)->whereNull('processed_at')->count())->toBe(1); $this->travel(60)->seconds(); expect((array) shown($village)['troops'])->toBe(['spear' => 10]) ->and(Report::count())->toBe(0); send($village, $barbarian, ['spear' => 10]); $this->travel(11)->minutes(); expect(rule(fn () => app(CancelMovement::class)(owner($village), $village->id, Movement::sole()->id)))->toBe('too_late'); }); it('draws a called-back command where it turned round, not at the target', function () { $village = rallied(startVillage(), ['spear' => 10]); $barbarian = barbarianNear($village, 3, 4); // 5 fields at 18 minutes: 5400 s out. send($village, $barbarian, ['spear' => 10]); $this->travel(600)->seconds(); app(CancelMovement::class)(owner($village), $village->id, Movement::sole()->id); $back = Movement::sole(); // On the line from the target home at the pace it marched, it stands 600 s from home. $total = $back->arrives_at->getTimestamp() - $back->sent_at->getTimestamp(); $done = now()->getTimestamp() - $back->sent_at->getTimestamp(); expect($back->origin_id)->toBe($barbarian->id) ->and($back->target_id)->toBe($village->id) ->and($back->arrives_at->toIso8601ZuluString())->toBe('2026-10-29T12:20:00Z') ->and($total)->toBe(5400) ->and($done)->toBe(4800); $this->travel(600)->seconds(); expect((array) shown($village)['troops'])->toBe(['spear' => 10]); }); it('refuses what the rally point cannot do', function () { $village = startVillage(); $other = startVillage(); $village->troops()->create(['home_village_id' => $village->id, 'unit' => 'spear', 'count' => 5]); expect(rule(fn () => send($village, $other, ['spear' => 5])))->toBe('no_rally_point'); rallied($village); expect(rule(fn () => send($village, $other, ['spear' => 6])))->toBe('troops') ->and(rule(fn () => send($village, $other, ['spear' => 0])))->toBe('count') ->and(rule(fn () => send($village, $village, ['spear' => 5])))->toBe('same_village') ->and(rule(fn () => send($village, $other, ['spear' => 5])))->toBe('protected') ->and(rule(fn () => send($village, barbarianNear($village, 3, 4), ['spear' => 5], 'support')))->toBe('barbarian') ->and(Movement::count())->toBe(0); // Beginner protection lasts five real days. $this->travel(5 * 86400 - 1)->seconds(); expect(rule(fn () => send($village, $other, ['spear' => 5])))->toBe('protected'); $this->travel(1)->seconds(); send($village, $other, ['spear' => 5]); expect(Movement::count())->toBe(1); }); it('counts beginner protection in real days on a fast world', function () { $village = rallied(startVillage(), ['spear' => 10]); $other = rallied(startVillage(), ['spear' => 10]); worldRules($village, ['world' => ['speed' => 100]]); // Divided by world speed, five days would be 72 minutes here. $this->travel(4)->days(); expect(rule(fn () => send($village, $other, ['spear' => 5])))->toBe('protected') ->and(rule(fn () => send($other, $village, ['spear' => 5])))->toBe('protected') ->and(shown($village)['rally']['protected_until'])->toBe('2026-11-03T12:00:00.000Z') ->and(app(ListWorlds::class)->settings($village->world_id)['settings']['pace'])->toMatchArray(['speed' => 100.0, 'protection' => 5 * 86400]); $this->travel(1)->days(); send($village, $other, ['spear' => 5]); expect(Movement::count())->toBe(1); }); it('shows an incoming attack once it is within sight, a sneaky one only on arrival', function () { $attacker = rallied(startVillage(), ['spear' => 10]); $defender = startVillage(); $defender->update(['x' => $attacker->x + 34, 'y' => $attacker->y]); worldRules($attacker, ['combat' => ['beginner_protection' => 0]]); send($attacker, $defender->fresh(), ['spear' => 5]); send($attacker, $defender->fresh(), ['spear' => 5], 'attack', 'sneaky'); expect(shown($defender)['movements'])->toBe([]); // 34 fields; normal visibility 15 plus detection 2 is 17: spotted halfway. $this->travel(18 * 17)->minutes(); $incoming = shown($defender)['movements']; expect($incoming)->toHaveCount(1) ->and($incoming[0])->toMatchArray(['direction' => 'in', 'type' => 'attack', 'units' => null, 'mode' => null, 'sent_at' => null]); }); it('rams the wall down first and lets catapults hit the chosen building, refunding its queued orders', function () { $attacker = rallied(startVillage(), ['light_cavalry' => 50, 'ram' => 20, 'catapult' => 20]); $defender = startVillage(); worldRules($attacker, ['combat' => ['beginner_protection' => 0, 'luck' => 0]]); $defender->update(['x' => $attacker->x + 1, 'y' => $attacker->y, 'wood' => 10000, 'clay' => 10000, 'iron' => 10000]); $defender->buildings()->where('type', 'headquarters')->update(['level' => 20]); $defender->buildings()->where('type', 'storage')->update(['level' => 15]); $defender->buildings()->create(['type' => 'wall', 'level' => 10]); app(QueueUpgrade::class)(owner($defender), $defender->id, $defender->buildings()->where('type', 'headquarters')->value('id')); expect(shown($defender)['queue'])->toHaveCount(1); send($attacker, $defender->fresh(), ['light_cavalry' => 50, 'ram' => 20, 'catapult' => 20], 'attack', null, 'headquarters'); $this->travel(30)->minutes(); $this->artisan('game:events --once')->assertSuccessful(); $report = Report::where('player_id', $defender->player_id)->sole()->payload; $state = shown($defender); $levels = collect($state['buildings'])->pluck('level', 'type'); expect($report['won'])->toBeTrue() ->and($report['wall'])->toBe(['before' => 10, 'after' => 5]) ->and($report['building'])->toBe(['type' => 'headquarters', 'before' => 20, 'after' => 18]) ->and($state['queue'])->toBe([]) ->and($levels['headquarters'])->toBe(18) ->and($levels['wall'])->toBe(5); }); it('sends hungry support home before own soldiers desert', function () { $home = startVillage(); $host = startVillage(); $host->troops()->create(['home_village_id' => $home->id, 'unit' => 'spear', 'count' => 30]); $host->troops()->create(['home_village_id' => $host->id, 'unit' => 'spear', 'count' => 2]); $host->update(['food' => 0]); expect(shown($host)['starvation']['since'])->not->toBeNull(); $this->travel(24)->hours(); $this->artisan('game:events --once')->assertSuccessful(); $state = shown($host); expect($state['support']['here'])->toBe([]) ->and((array) $state['troops'])->toBe(['spear' => 2]) ->and($state['starvation']['since'])->toBeNull() ->and(shown($home)['movements'][0])->toMatchArray(['type' => 'return', 'units' => ['spear' => 30]]); }); it('scouts a village without a fight, and loses every scout against more defending ones', function () { $attacker = rallied(startVillage(), ['scout' => 10]); $defender = startVillage(); $defender->troops()->create(['home_village_id' => $defender->id, 'unit' => 'sword', 'count' => 20]); worldRules($attacker, ['combat' => ['beginner_protection' => 0]]); send($attacker, $defender, ['scout' => 5]); $this->travel(1)->days(); $this->artisan('game:events --once')->assertSuccessful(); $report = Report::where('player_id', $attacker->player_id)->sole()->payload; expect($report['won'])->toBeTrue() ->and($report['defender']['units'])->toBe(['sword' => 20]) ->and($report['defender']['lost'])->toBe(['sword' => 0]) ->and($report['scouting'])->toMatchArray(['sent' => 5, 'lost' => 0, 'through' => true]) ->and($report['scouting']['buildings'])->toMatchArray(['headquarters' => [1], 'storage' => [1], 'farm' => [1]]) ->and($report['scouting']['resources']['wood'])->toBeGreaterThan(500) ->and((array) shown($attacker)['troops'])->toBe(['scout' => 10]) ->and((array) shown($defender)['troops'])->toBe(['sword' => 20]); $defender->troops()->create(['home_village_id' => $defender->id, 'unit' => 'scout', 'count' => 8]); send($attacker, $defender, ['scout' => 5]); $this->travel(1)->days(); $this->artisan('game:events --once')->assertSuccessful(); $report = Report::where('player_id', $attacker->player_id)->latest('id')->first()->payload; expect($report['won'])->toBeFalse() ->and($report['scouting'])->toMatchArray(['lost' => 5, 'through' => false, 'resources' => null]) ->and($report['defender']['units'])->toBeNull() ->and((array) shown($attacker)['troops'])->toBe(['scout' => 5]) ->and((array) shown($defender)['troops'])->toBe(['sword' => 20, 'scout' => 8]); });