travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')); config([ 'game.raids.enabled' => true, // One profile, so the band is known: 0.7 × 10 + 0.3 × 25 = 14.5 attack a head. 'game.world.map.barbarians.garrison.profiles' => ['spearmen' => ['spear' => 0.7, 'sword' => 0.3]], ]); }); /** A barbarian camp at full strength 3 fields east of the village. */ function raidCamp(Village $near, int $dx = 3): Village { $types = ['headquarters', 'storage', 'farm', 'timber_camp', 'clay_pit', 'iron_mine']; $camp = app(VillageFactory::class)->create($near->world, null, 'Barbarian village', $near->x + $dx, $near->y, array_fill_keys($types, 8)); Barbarians::of($camp)->fill(); return $camp->fresh(); } function pendingRaid(Village $village): ?CarbonImmutable { return GameEvent::where('type', GameEvent::RAID_PLANNED)->where('village_id', $village->id)->whereNull('processed_at')->value('fire_at'); } /** Past beginner protection, with the band announced. */ function announced(Village $village): Movement { test()->travel(3)->days(); test()->artisan('game:events --once')->assertSuccessful(); return Movement::where('target_id', $village->id)->sole(); } function arrive(Movement $movement): void { test()->travelTo($movement->arrives_at); test()->artisan('game:events --once')->assertSuccessful(); } it('plans the first raid for when beginner protection ends', function () { $village = startVillage(); expect(pendingRaid($village)->toDateTimeString())->toBe('2026-11-01 12:00:00'); }); it('announces a band from the nearest camp hours before it arrives, and plans the next raid', function () { $village = startVillage(); raidCamp($village, 6); $camp = raidCamp($village); $movement = announced($village); // 50 + 1.5 × 21 points = 81.5 attack: 6 heads. They gather 3 h, then march 3 fields at 22 min. expect($movement->origin_id)->toBe($camp->id) ->and($movement->player_id)->toBeNull() ->and($movement->units)->toBe(['spear' => 4, 'sword' => 2]) ->and($movement->arrives_at->toDateTimeString())->toBe('2026-11-01 16:06:00'); $incoming = shown($village)['movements'][0]; expect($incoming)->toMatchArray(['type' => 'attack', 'direction' => 'in', 'raid' => true, 'marches_at' => '2026-11-01T15:00:00.000Z', 'arrives_at' => '2026-11-01T16:06:00.000Z']) ->and($incoming['units'])->toBeNull(); $next = pendingRaid($village); expect($next->greaterThanOrEqualTo($movement->arrives_at->addHours(20)))->toBeTrue() ->and($next->lessThanOrEqualTo($movement->arrives_at->addHours(40)))->toBeTrue(); }); it('times raids in real hours on a fast world; only the march runs at world speed', function () { $village = startVillage(); raidCamp($village); World::whereKey($village->world_id)->update(['config' => ['world' => ['speed' => 100]]]); $movement = announced($village); // Three real hours of gathering, then 3 fields at 22 minutes / 100. expect($movement->sent_at->toDateTimeString())->toBe('2026-11-01 15:00:00') ->and($movement->sent_at->diffInSeconds($movement->arrives_at))->toBeLessThan(60); $next = pendingRaid($village); expect($next->greaterThanOrEqualTo($movement->arrives_at->addHours(20)))->toBeTrue() ->and($next->lessThanOrEqualTo($movement->arrives_at->addHours(40)))->toBeTrue() ->and(app(ListWorlds::class)->settings($village->world_id)['settings']['raids'])->toMatchArray(['every' => [20 * 3600, 40 * 3600], 'warning' => 3 * 3600]); }); it('stays home when the village is far too strong for the band, and tries again later', function () { $village = startVillage(); raidCamp($village); // 100 swordsmen hold 5000 against the band's 90: less than 30% of the defence. $village->troops()->create(['home_village_id' => $village->id, 'unit' => 'sword', 'count' => 100]); $this->travel(3)->days(); $this->artisan('game:events --once')->assertSuccessful(); $next = pendingRaid($village); expect(Movement::count())->toBe(0) ->and(Report::count())->toBe(0) ->and($next->greaterThanOrEqualTo(now()->addHours(20)))->toBeTrue() ->and($next->lessThanOrEqualTo(now()->addHours(40)))->toBeTrue(); // Once the swordsmen are gone, the next band comes. $village->troops()->delete(); $this->travelTo($next); $this->artisan('game:events --once')->assertSuccessful(); expect(Movement::where('target_id', $village->id)->count())->toBe(1); }); it('brings a ram for every two wall levels; they fire half before the battle and the rest after winning it', function () { $village = startVillage(); raidCamp($village); $village->buildings()->create(['type' => 'wall', 'level' => 10]); $village->update(['points' => 1000]); $movement = announced($village); expect($movement->units['ram'])->toBe(5); arrive($movement); // 2.5 rams can't take wall 10 (4.35 needed); after the won battle the other 2.5 can. $report = Report::where('player_id', $village->player_id)->sole()->payload; expect($report['won'])->toBeTrue() ->and($report['wall'])->toBe(['before' => 10, 'after' => 9]) ->and($village->buildings()->where('type', 'wall')->value('level'))->toBe(9); }); it('loots an undefended village and carries the loot back to its camp', function () { $village = startVillage(); $camp = raidCamp($village); $movement = announced($village); $campWood = $camp->fresh()->wood; arrive($movement); $report = Report::where('player_id', $village->player_id)->sole(); // 4 spears and 2 swords carry 4 × 25 + 2 × 15 = 130. expect($report->type)->toBe('defense') ->and($report->payload['won'])->toBeTrue() ->and($report->payload['raid'])->toBeTrue() ->and(array_sum($report->payload['loot']))->toBe(130); $home = $movement->fresh(); expect($home->type)->toBe('return')->and($home->target_id)->toBe($camp->id); arrive($home); expect($camp->fresh()->wood)->toBeGreaterThan($campWood) ->and(Barbarians::of($camp->fresh())->current())->toEqual(Barbarians::of($camp->fresh())->full()); }); it('pays a bounty when the village beats the band', function () { $village = startVillage(); raidCamp($village); // 4 swordsmen hold 200 against the band's 90: strong enough to win, not to scare it off. $village->troops()->create(['home_village_id' => $village->id, 'unit' => 'sword', 'count' => 4]); $movement = announced($village); $wood = shown($village)['resources']['wood']; arrive($movement); $report = Report::where('player_id', $village->player_id)->sole(); // 4 × 10 + 2 × 25 = 90 attack → 45, a third each. expect($report->payload['won'])->toBeFalse() ->and($report->payload['bounty'])->toBe(['wood' => 15, 'clay' => 15, 'iron' => 15]) ->and($report->payload['attacker']['lost'])->toBe(['spear' => 4, 'sword' => 2]) ->and(Movement::count())->toBe(0) // 6 raiders at 1 food each count as opponents defeated in defence. ->and(DB::table('player_counters')->where('player_id', $village->player_id)->where('key', 'defeated.defense')->value('value'))->toBe(6) ->and(shown($village)['resources']['wood'])->toBeGreaterThan($wood + 14); }); it('sends a weaker band from a weakened camp, and none from a beaten one', function () { $village = startVillage(); $camp = raidCamp($village); $movement = announced($village); $camp->troops()->update(['count' => 0]); $camp->troops()->where('unit', 'spear')->update(['count' => 6]); arrive($movement); // 6 of 10 spears and no swords: 6 of 10 food → the band at 60%. $report = Report::where('player_id', $village->player_id)->sole(); expect($report->payload['attacker']['units'])->toBe(['spear' => 2, 'sword' => 1]); // Once the band is back, the camp is beaten down to nothing. arrive($movement->fresh()); $camp->troops()->delete(); $this->travelTo(pendingRaid($village)); $this->artisan('game:events --once')->assertSuccessful(); arrive(Movement::where('target_id', $village->id)->where('type', 'attack')->sole()); $scattered = Report::where('player_id', $village->player_id)->latest('id')->first(); expect($scattered->payload)->toMatchArray(['raid' => true, 'scattered' => true]) ->and(Movement::where('target_id', $village->id)->where('type', 'attack')->exists())->toBeFalse(); }); it('plans raids for villages from before raids on deploy', function () { $village = startVillage(); GameEvent::where('type', GameEvent::RAID_PLANNED)->delete(); (new WorldSeeder)->run(); expect(pendingRaid($village)->toDateTimeString())->toBe('2026-11-01 12:00:00'); (new WorldSeeder)->run(); expect(GameEvent::where('type', GameEvent::RAID_PLANNED)->count())->toBe(1); });