game-village-politics-backend/tests/Feature/SeasonTest.php

98 lines
4.1 KiB
PHP

<?php
use App\Actions\JoinWorld;
use App\Actions\QueueBuild;
use App\Engine\Barbarians;
use App\Engine\Nobility;
use App\Engine\Seasons;
use App\Models\GameEvent;
use App\Models\Movement;
use App\Models\User;
use App\Models\Village;
use App\Models\World;
use Carbon\CarbonImmutable;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
/** The world's crown forts, in id order. */
function crowns(World $world)
{
return Village::where('world_id', $world->id)->where('crown', true)->orderBy('id')->get();
}
/** A conquest of a crown, the way the engine hands a village over. */
function takeCrown(Village $crown, int $playerId): void
{
$crown = $crown->fresh();
Nobility::of($crown)->handOver($crown, $playerId, now()->toImmutable());
$crown->save();
}
it('places the crown forts round 0|0 once the season is far enough along', function () {
$village = startVillage();
$world = $village->world;
$this->travel(29)->days();
$this->artisan('game:events --once')->assertSuccessful();
expect(crowns($world))->toHaveCount(0)
->and(shown($village)['season'])->toMatchArray(['count' => 5, 'needed' => 3, 'crowns' => [], 'holder' => null, 'ended_at' => null]);
$this->travel(2)->days();
$this->artisan('game:events --once')->assertSuccessful();
$crowns = crowns($world);
expect($crowns)->toHaveCount(5)
->and($crowns->every(fn (Village $c) => $c->player_id === null && hypot($c->x, $c->y) <= 12))->toBeTrue()
->and(Barbarians::of($crowns[0])->current())->toEqual(['archer' => 150, 'heavy_cavalry' => 50, 'spear' => 300, 'sword' => 300])
->and($crowns[0]->buildings()->where('type', 'wall')->value('level'))->toBe(20)
->and(shown($village)['season']['crowns'])->toHaveCount(5);
// Only once, and crowns never grow.
Seasons::tick(now()->toImmutable());
expect(crowns($world))->toHaveCount(5)
->and(GameEvent::where('type', 'barbarian.grows')->whereIn('village_id', $crowns->pluck('id'))->exists())->toBeFalse();
});
it('lets a player who holds three crowns for a week win, and opens the next world', function () {
$village = startVillage();
$world = $village->world;
$rival = startVillage();
Seasons::placeCrowns($world->id, now()->toImmutable());
[$a, $b, $c, $d] = crowns($world);
takeCrown($a, $village->player_id);
takeCrown($b, $village->player_id);
expect($world->fresh()->holder_player_id)->toBeNull();
takeCrown($c, $village->player_id);
expect($world->fresh())->holder_player_id->toBe($village->player_id)
->and($world->fresh()->ends_at->toDateTimeString())->toBe('2026-11-05 12:00:00')
->and(shown($village)['season']['holder'])->toBe(['id' => $village->player_id, 'name' => $village->player->name]);
// The rival takes one back: the clock stops; three again starts it over.
$this->travel(3)->days();
takeCrown($c, $rival->player_id);
expect($world->fresh()->holder_player_id)->toBeNull()->and($world->fresh()->ends_at)->toBeNull();
takeCrown($d, $village->player_id);
expect($world->fresh()->ends_at->toDateTimeString())->toBe('2026-11-08 12:00:00');
$this->travel(7)->days();
$this->artisan('game:events --once')->assertSuccessful();
$ended = $world->fresh();
expect($ended->ended_at->toDateTimeString())->toBe('2026-11-08 12:00:00')
->and($ended->winner_player_id)->toBe($village->player_id)
->and(World::where('slug', 'w2')->first())->name->toBe('World 2')
->and(shown($village)['season']['winner']['id'])->toBe($village->player_id)
->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'timber_camp')))->toBe('world_ended')
->and(rule(fn () => app(JoinWorld::class)(User::factory()->create(), $world->id)))->toBe('world_ended');
});
it('keeps crowns out of raiding', function () {
config(['game.raids.enabled' => true]);
$village = startVillage();
Seasons::placeCrowns($village->world_id, now()->toImmutable());
$village->update(['x' => 0, 'y' => -8]);
$this->travel(3)->days();
$this->artisan('game:events --once')->assertSuccessful();
expect(Movement::count())->toBe(0);
});