diff --git a/tests/Feature/ConquestTest.php b/tests/Feature/ConquestTest.php index 3640579..c191c28 100644 --- a/tests/Feature/ConquestTest.php +++ b/tests/Feature/ConquestTest.php @@ -44,7 +44,7 @@ function attackWith(Village $from, Village $to, array $units): void test()->artisan('game:events --once')->assertSuccessful(); } -it('mints coins at an academy and turns them into noble slots', function () { +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'); @@ -53,25 +53,24 @@ it('mints coins at an academy and turns them into noble slots', function () { $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]) + // 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('needs a free noble slot to train a nobleman', function () { +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(); - expect(rule(fn () => app(Recruit::class)(owner($village), $village->id, $academy->id, 'nobleman', 1)))->toBe('coins'); + // Only the villagers limit it, not the coins. + expect(collect(shown($village)['military'])->firstWhere('type', 'academy')['units'][0]['max'])->toBe(2); - $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'); + 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 () { @@ -86,6 +85,7 @@ it('lowers loyalty with every surviving nobleman, and loyalty grows back', funct ->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); }); @@ -102,9 +102,66 @@ it('conquers a village whose loyalty reaches zero', function () { ->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(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']['used'])->toBe(1); + ->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(4)->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 () {