$this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00'))); /** A village with a rally point, an academy and these troops at home. */ function nobleVillage(array $units = [], ?Village $village = null): Village { $village ??= startVillage(); $village->buildings()->create(['type' => 'rally_point', 'level' => 1]); $village->buildings()->create(['type' => 'academy', 'level' => 1]); foreach ($units as $unit => $count) { $village->troops()->create(['home_village_id' => $village->id, 'unit' => $unit, 'count' => $count]); } $village->player->update(['coins' => 1]); return $village->fresh(); } function barbarianAt(Village $near, int $dx, int $dy, float $loyalty = 100): Village { $barbarian = app(VillageFactory::class)->create($near->world, null, 'Barbarian village', $near->x + $dx, $near->y + $dy, ['headquarters' => 1, 'storage' => 1, 'farm' => 1]); $barbarian->update(['loyalty' => $loyalty, 'loyalty_at' => now()]); return $barbarian; } function attackWith(Village $from, Village $to, array $units): void { app(SendTroops::class)(owner($from), $from->id, 'attack', $to->x, $to->y, $units, null, null); test()->travel(2)->hours(); test()->artisan('game:events --once')->assertSuccessful(); } it('mints coins at an academy and turns them into noble slots', function () { $village = startVillage(); $village->update(['wood' => 400000, 'clay' => 400000, 'iron' => 400000]); expect(rule(fn () => app(MintCoins::class)(owner($village), $village->id, 1)))->toBe('requires'); $village->buildings()->create(['type' => 'academy', 'level' => 1]); $village->buildings()->create(['type' => 'storage', 'level' => 30]); $state = shown(app(MintCoins::class)(owner($village), $village->id, 3)); expect($state['conquest'])->toMatchArray(['coins' => 3, 'slots' => 2, 'used' => 0, 'next_slot_coins' => 3]) ->and($state['resources']['wood'])->toBe(400000.0 - 3 * 28000); }); it('needs a free noble slot to train a nobleman', function () { $village = nobleVillage(); $village->player->update(['coins' => 0]); $village->buildings()->create(['type' => 'storage', 'level' => 30]); $village->update(['wood' => 200000, 'clay' => 200000, 'iron' => 200000]); $academy = $village->buildings()->where('type', 'academy')->first(); expect(rule(fn () => app(Recruit::class)(owner($village), $village->id, $academy->id, 'nobleman', 1)))->toBe('coins'); $village->player->update(['coins' => 1]); app(Recruit::class)(owner($village), $village->id, $academy->id, 'nobleman', 1); $state = shown($village); expect($state['conquest']['used'])->toBe(1) ->and(collect($state['military'])->firstWhere('type', 'academy')['units'][0]['max'])->toBe(0) ->and(rule(fn () => app(Recruit::class)(owner($village), $village->id, $academy->id, 'nobleman', 1)))->toBe('coins'); }); it('lowers loyalty with every surviving nobleman, and loyalty grows back', function () { $village = nobleVillage(['spear' => 50, 'nobleman' => 1]); $barbarian = barbarianAt($village, 2, 0); attackWith($village, $barbarian, ['spear' => 50, 'nobleman' => 1]); $barbarian->refresh(); $report = Report::where('player_id', $village->player_id)->first()->payload; expect($barbarian->player_id)->toBeNull() ->and($report['loyalty']['before'])->toBe(100) ->and($report['loyalty']['after'])->toBeBetween(65, 80) ->and($report['loyalty']['conquered'])->toBeFalse() ->and((float) $barbarian->loyalty)->toEqual($report['loyalty']['after'] + 0.0); }); it('conquers a village whose loyalty reaches zero', function () { $village = nobleVillage(['spear' => 50, 'nobleman' => 1]); $barbarian = barbarianAt($village, 2, 0, 10); attackWith($village, $barbarian, ['spear' => 50, 'nobleman' => 1]); $barbarian->refresh(); expect($barbarian->player_id)->toBe($village->player_id) ->and((float) $barbarian->loyalty)->toBe(25.0) // The nobleman settled; the spearmen stay as support from home. ->and(UnitStack::where('village_id', $barbarian->id)->get(['home_village_id', 'unit', 'count'])->toArray()) ->toBe([['home_village_id' => $village->id, 'unit' => 'spear', 'count' => 50]]) ->and(Movement::count())->toBe(0) ->and(Report::where('player_id', $village->player_id)->first()->payload['loyalty']['conquered'])->toBeTrue() ->and(array_column(shown($village)['villages'], 'id'))->toBe([$village->id, $barbarian->id]) ->and(shown($village)['conquest']['used'])->toBe(1); }); it('takes a player village with its queues, and the old owner starts over', function () { $victim = startVillage(); $victim->buildings()->create(['type' => 'rally_point', 'level' => 1]); $victim->troops()->create(['home_village_id' => $victim->id, 'unit' => 'spear', 'count' => 3]); $village = nobleVillage(['spear' => 100, 'nobleman' => 1]); $this->travel(4)->days(); $victim->update(['loyalty' => 5, 'loyalty_at' => now()]); // The victim has a build order and support standing in the attacker's village. app(QueueUpgrade::class)(owner($victim), $victim->id, $victim->buildings()->where('type', 'storage')->value('id')); $village->troops()->create(['home_village_id' => $victim->id, 'unit' => 'spear', 'count' => 2]); $victim->troops()->where('unit', 'spear')->update(['count' => 1]); app(SendTroops::class)(owner($village), $village->id, 'attack', $victim->x, $victim->y, ['spear' => 100, 'nobleman' => 1], null, null); // Long enough for any spawn distance, short enough that loyalty stays below one drop. $this->travel(12)->hours(); $this->artisan('game:events --once')->assertSuccessful(); $victim->refresh(); expect($victim->player_id)->toBe($village->player_id) ->and($victim->orders()->count())->toBe(0) ->and(UnitStack::where('home_village_id', $victim->id)->where('village_id', '!=', $victim->id)->count())->toBe(0) ->and(Report::where('type', Report::DEFENSE)->first()->payload['loyalty']['conquered'])->toBeTrue(); $loser = User::whereKeyNot(owner($village)->id)->first(); $fresh = app(JoinWorld::class)($loser, $victim->world_id); expect($fresh->id)->not->toBe($victim->id) ->and($fresh->player->user_id)->toBe($loser->id); }); it('keeps noblemen within their marching range', function () { $village = nobleVillage(['nobleman' => 1]); $far = barbarianAt($village, 101, 0); expect(rule(fn () => app(SendTroops::class)(owner($village), $village->id, 'attack', $far->x, $far->y, ['nobleman' => 1], null, null)))->toBe('too_far'); });