$this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00'))); /** A start village (farm 0: 24 villagers' worth of food) with soldiers stationed at home. */ function garrisoned(array $troops): Village { $village = startVillage(); foreach ($troops as $unit => $count) { $village->troops()->create(['home_village_id' => $village->id, 'unit' => $unit, 'count' => $count]); } return $village->fresh(); } function iso(CarbonImmutable $at): string { return $at->utc()->format('Y-m-d\TH:i:s.v\Z'); } it('knows when the food runs out and starts the grace timer right then', function () { // 10 light cavalry eat 30 food/h: no villagers are left to work the farm. $village = garrisoned(['light_cavalry' => 10]); $state = shown($village); $hours = 300 / -$state['rates']['food']; expect($state['starvation'])->toMatchArray(['since' => null, 'desertion_at' => null]) ->and(CarbonImmutable::parse($state['starvation']['food_empty_at'])->getTimestampMs()) ->toEqualWithDelta(now()->getTimestampMs() + $hours * 3_600_000, 1000); $this->travel(12)->hours(); $this->artisan('game:events --once')->assertSuccessful(); $state = shown($village); $since = CarbonImmutable::parse($state['starvation']['since']); expect($state['resources']['food'])->toBe(0.0) ->and($state['starvation']['food_empty_at'])->toBeNull() ->and($since->getTimestampMs())->toEqualWithDelta(now()->subHours(12)->getTimestampMs() + $hours * 3_600_000, 1000) ->and($state['starvation']['desertion_at'])->toBe(iso($since->addDay())); }); it('lets just enough soldiers desert after the grace, highest upkeep first', function () { // 35 food/h of soldiers; the farm needs 2 workers, so they may eat at most 22. $village = garrisoned(['spear' => 5, 'light_cavalry' => 10]); shown($village); $this->travel(12)->hours(); $this->artisan('game:events --once')->assertSuccessful(); $this->travel(1)->days(); $this->artisan('game:events --once')->assertSuccessful(); $state = shown($village); expect((array) $state['troops'])->toBe(['spear' => 5, 'light_cavalry' => 5]) ->and($state['rates']['food'])->toBeGreaterThanOrEqual(0) ->and($state['starvation'])->toBe(['food_empty_at' => null, 'since' => null, 'desertion_at' => null]) ->and(GameEvent::whereIn('type', [GameEvent::FOOD_RUNS_OUT, GameEvent::DESERTION])->whereNull('processed_at')->count())->toBe(0); }); it('stops the grace timer as soon as income covers upkeep again', function () { // Drafting every villager leaves the farm empty; with little food it runs out fast. $village = startVillage(); $village->update(['food' => 50]); app(Levy::class)(owner($village), $village->id, 24); $this->travel(3)->hours(); $this->artisan('game:events --once')->assertSuccessful(); expect(shown($village)['starvation']['since'])->not->toBeNull(); app(EndLevy::class)(owner($village), $village->id); expect(shown($village)['starvation'])->toMatchArray(['since' => null, 'desertion_at' => null]) ->and(GameEvent::where('type', GameEvent::DESERTION)->whereNull('processed_at')->count())->toBe(0); }); it('ends the levy when no soldiers are left to desert', function () { $village = startVillage(); World::whereKey($village->world_id)->update(['config' => ['economy' => ['levy' => ['duration' => 48 * 3600]]]]); $village->update(['food' => 50]); app(Levy::class)(owner($village), $village->id, 24); $this->travel(3)->hours(); $this->artisan('game:events --once')->assertSuccessful(); $this->travel(1)->days(); $this->artisan('game:events --once')->assertSuccessful(); $state = shown($village); expect($state['levy'])->toMatchArray(['militia' => 0, 'until' => null]) ->and($state['starvation']['since'])->toBeNull(); });