$this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00'))); /** * A defender with 20 swordsmen, a farm big enough to staff everything and, unless * $hospital is 0, a hospital at that level; and an attacker with a rally point. */ function wardVillages(int $hospital = 1, array $world = []): array { $attacker = startVillage(); $attacker->buildings()->create(['type' => 'rally_point', 'level' => 1]); $defender = startVillage(); $defender->buildings()->where('type', 'farm')->update(['level' => 10]); if ($hospital > 0) { $defender->buildings()->create(['type' => 'hospital', 'level' => $hospital]); } $defender->troops()->create(['home_village_id' => $defender->id, 'unit' => 'sword', 'count' => 20]); World::whereKey($defender->world_id)->update(['config' => array_replace_recursive(['combat' => ['beginner_protection' => 0, 'luck' => 0]], $world)]); return [$attacker->fresh(), $defender->fresh()]; } /** Attack with $spears spearmen and let the battle happen; returns the arrival time. */ function strike(Village $attacker, Village $defender, int $spears): CarbonImmutable { $attacker->troops()->create(['home_village_id' => $attacker->id, 'unit' => 'spear', 'count' => $spears]); app(SendTroops::class)(owner($attacker), $attacker->id, 'attack', $defender->x, $defender->y, ['spear' => $spears], null, null); $arrives = Movement::where('target_id', $defender->id)->sole()->arrives_at; test()->travelTo($arrives); test()->artisan('game:events --once')->assertSuccessful(); return $arrives; } it('brings half the fallen defenders back wounded; they eat meanwhile and stand at home after four hours', function () { [$attacker, $defender] = wardVillages(); $at = strike($attacker, $defender, 60); // 600 attack against 1000 defence: the defenders lose 0.6^1.5 of 20 → 9, 4 of them wounded. $defense = Report::where('player_id', $defender->player_id)->sole()->payload; expect($defense['defender']['lost'])->toBe(['sword' => 9]) ->and($defense['wounded'])->toBe(['units' => ['sword' => 4], 'heals_at' => $at->addHours(4)->utc()->format('Y-m-d\TH:i:s.v\Z')]) ->and(Report::where('player_id', $attacker->player_id)->sole()->payload['wounded'])->toBeNull(); $state = shown($defender); expect((array) $state['troops'])->toBe(['sword' => 11]) ->and($state['hospital'])->toMatchArray(['beds' => 20, 'occupied' => 4, 'share' => 0.5, 'heal_seconds' => 14400]) ->and((array) $state['hospital']['wounded'][0]['units'])->toBe(['sword' => 4]) // Farm 10 feeds 0.8 × 223.5 = 178.8 heads: the 11 swordsmen and 4 wounded leave 163 villagers. ->and($state['population']['villagers'])->toBe(163); $this->travel(4)->hours(); $this->artisan('game:events --once')->assertSuccessful(); $state = shown($defender); expect((array) $state['troops'])->toBe(['sword' => 15]) ->and($state['hospital'])->toMatchArray(['occupied' => 0, 'wounded' => []]) ->and($state['population']['villagers'])->toBe(163); }); it('takes in no more wounded than it has free beds', function () { [$attacker, $defender] = wardVillages(1, ['buildings' => ['hospital' => ['beds' => ['base' => 3]]]]); strike($attacker, $defender, 60); expect(Report::where('player_id', $defender->player_id)->sole()->payload['wounded']['units'])->toBe(['sword' => 3]) ->and(shown($defender)['hospital'])->toMatchArray(['beds' => 3, 'occupied' => 3]); }); it('has no wounded without a hospital', function () { [$attacker, $defender] = wardVillages(0); strike($attacker, $defender, 60); expect(Report::where('player_id', $defender->player_id)->sole()->payload['wounded'])->toBeNull() ->and((array) shown($defender)['troops'])->toBe(['sword' => 11]) ->and(shown($defender)['hospital'])->toMatchArray(['beds' => 0, 'occupied' => 0, 'wounded' => []]); }); it('takes in only the own fallen, not the support', function () { [$attacker, $defender] = wardVillages(); $ally = startVillage(); $defender->troops()->create(['home_village_id' => $ally->id, 'unit' => 'sword', 'count' => 20]); // 1500 attack against 2000 defence: 0.75^1.5 of each stack → 13 of 20 fall. strike($attacker, $defender->fresh(), 150); $defense = Report::where('player_id', $defender->player_id)->sole()->payload; expect($defense['defender']['lost'])->toBe(['sword' => 26]) ->and($defense['wounded']['units'])->toBe(['sword' => 6]); }); it('loses its wounded when the village turns barbarian', function () { [$attacker, $defender] = wardVillages(); strike($attacker, $defender, 60); expect(Wounded::count())->toBe(1); $village = Village::whereKey($defender->id)->first(); Abandon::village($village, now()->toImmutable()); $village->save(); expect(Wounded::count())->toBe(0) ->and(GameEvent::where('type', GameEvent::WOUNDED_HEALED)->whereNull('processed_at')->count())->toBe(0); });