$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 village 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)); // 3 coins pay for a 2nd and a 3rd village; the 4th needs 6. expect($state['conquest'])->toMatchArray(['coins' => 3, 'slots' => 2, 'used' => 0, 'next_slot_coins' => 6]) ->and($state['resources']['wood'])->toBe(400000.0 - 3 * 28000); }); it('trains noblemen without coins, and they take no slot', 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(); // Only the villagers limit it, not the coins. expect(collect(shown($village)['military'])->firstWhere('type', 'academy')['units'][0]['max'])->toBe(2); app(Recruit::class)(owner($village), $village->id, $academy->id, 'nobleman', 2); expect((int) $village->recruits()->sum('count'))->toBe(2) ->and(shown($village)['conquest'])->toMatchArray(['coins' => 0, 'slots' => 0, 'used' => 0, 'next_slot_coins' => 1]); }); 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($report['loyalty']['no_coins'])->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'])->toMatchArray(['after' => 0, 'conquered' => true, 'no_coins' => false]) ->and(array_column(shown($village)['villages'], 'id'))->toBe([$village->id, $barbarian->id]) ->and(shown($village)['conquest'])->toMatchArray(['coins' => 1, 'slots' => 1, 'used' => 1, 'next_slot_coins' => 3]); }); it('leaves a village at loyalty 0 when the coins pay for no more villages, and the noblemen go home', function () { $victim = startVillage(); $village = nobleVillage(['spear' => 100, 'nobleman' => 2]); $village->player->update(['coins' => 0]); $this->travel(6)->days(); $victim->update(['loyalty' => 0, 'loyalty_at' => now()]); app(SendTroops::class)(owner($village), $village->id, 'attack', $victim->x, $victim->y, ['spear' => 100, 'nobleman' => 2], null, null); $this->travelTo(Movement::sole()->arrives_at); $this->artisan('game:events --once')->assertSuccessful(); $victim->refresh(); $back = Movement::sole(); $attack = Report::where('type', Report::ATTACK)->sole()->payload; $defense = Report::where('type', Report::DEFENSE)->sole()->payload; expect($victim->player_id)->not->toBe($village->player_id) ->and((float) $victim->loyalty)->toBe(0.0) // Every nobleman lives and walks home with the spearmen and the loot. ->and($back->type)->toBe(Movement::RETURN) ->and($back->target_id)->toBe($village->id) ->and($back->units)->toBe(['spear' => 100, 'nobleman' => 2]) ->and(array_sum($attack['loot']))->toBeGreaterThan(0) ->and(array_keys($attack['loyalty']))->toBe(['before', 'after', 'conquered', 'no_coins']) ->and($attack['loyalty'])->toMatchArray(['after' => 0, 'conquered' => false, 'no_coins' => true]) ->and($defense['loyalty'])->toBe($attack['loyalty']) ->and(shown($village)['conquest'])->toMatchArray(['used' => 0, 'slots' => 0]); }); it('takes the 2nd village for 1 coin and the 3rd for 3 coins in total', function () { $village = nobleVillage(['spear' => 150, 'nobleman' => 3]); $second = barbarianAt($village, 2, 0, 10); $third = barbarianAt($village, 0, 2, 10); $lastLoyalty = fn () => Report::where('player_id', $village->player_id)->where('type', Report::ATTACK)->latest('id')->first()->payload['loyalty']; attackWith($village, $second, ['spear' => 50, 'nobleman' => 1]); expect($second->fresh()->player_id)->toBe($village->player_id); // 1 coin pays for one village beyond the first, not two. attackWith($village, $third, ['spear' => 50, 'nobleman' => 1]); expect($third->fresh()->player_id)->toBeNull() ->and((float) $third->fresh()->loyalty)->toBe(0.0) ->and($lastLoyalty())->toMatchArray(['conquered' => false, 'no_coins' => true]); $village->player->update(['coins' => 2]); $this->travel(3)->hours(); $this->artisan('game:events --once')->assertSuccessful(); attackWith($village, $third, ['spear' => 50, 'nobleman' => 1]); expect($third->fresh()->player_id)->toBeNull() ->and($lastLoyalty()['no_coins'])->toBeTrue(); $village->player->update(['coins' => 3]); attackWith($village, $third, ['spear' => 50, 'nobleman' => 1]); expect($third->fresh()->player_id)->toBe($village->player_id) ->and($lastLoyalty())->toMatchArray(['conquered' => true, 'no_coins' => false]) ->and(shown($village)['conquest'])->toMatchArray(['coins' => 3, 'slots' => 2, 'used' => 2, 'next_slot_coins' => 6]); }); 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(6)->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'); });