$this->travelTo(CarbonImmutable::parse('2026-10-28 12:00:00'))); it('settles resources by rate and elapsed time, up to the storage cap', function () { $village = startVillage(); $this->travel(2)->hours(); expect(shown($village)['resources'])->toBe(['wood' => 510.0, 'clay' => 510.0, 'iron' => 510.0, 'food' => 312.0]); $this->travel(200)->hours(); expect(shown($village)['resources']['wood'])->toBe(1000.0); }); it('pays up front, reserves the space and finishes on time', function () { $village = startVillage(); app(QueueBuild::class)(owner($village), $village->id, 'timber_camp'); $state = shown($village); expect($state['resources']['wood'])->toBe(450.0) ->and($state['space'])->toBe(['used' => 21, 'max' => 40]) ->and($state['queue'][0])->toMatchArray([ 'type' => 'timber_camp', 'action' => 'build', 'space' => 4, 'builder' => 1, 'starts_at' => '2026-10-28T12:00:00.000Z', 'finishes_at' => '2026-10-28T12:01:30.000Z', ]); $this->travel(89)->seconds(); expect(shown($village)['buildings'])->toHaveCount(3); $this->travel(1)->seconds(); $state = shown($village); expect($state['queue'])->toBe([]) ->and(collect($state['buildings'])->firstWhere('type', 'timber_camp'))->toMatchArray(['level' => 0, 'space' => 4]) ->and($state['rates']['wood'])->toBe(35.0) ->and($state['points'])->toBe(27); }); it('refuses a building that does not fit the free building space', function () { // Start buildings take 17 of 40; four timber camps and six hiding places bring it to 39. $village = startVillage(); $village->buildings()->createMany([ ...array_fill(0, 4, ['type' => 'timber_camp', 'level' => 0]), ...array_fill(0, 6, ['type' => 'hiding_place', 'level' => 0]), ]); $options = collect(shown($village)['buildable'])->keyBy('type'); expect($options['timber_camp']['blocked'])->toBe('no_space') ->and($options['rally_point'])->toMatchArray(['space' => 1, 'blocked' => null]) ->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'storage')))->toBe('no_space'); // Queuing reserves the space right away. app(QueueBuild::class)(owner($village), $village->id, 'rally_point'); $state = shown($village); expect($state['space'])->toBe(['used' => 40, 'max' => 40]) ->and(collect($state['buildable'])->firstWhere('type', 'hiding_place')['blocked'])->toBe('no_space'); }); it('refuses a second headquarters and what it cannot pay, but lets the wall be built right away', function () { $village = startVillage(); expect(collect(shown($village)['buildable'])->firstWhere('type', 'wall'))->toMatchArray(['blocked' => null, 'requires' => []]); $village->update(['wood' => 10]); expect(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'headquarters')))->toBe('max_per_village') ->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'storage')))->toBe('resources'); }); it('lists the requirements an advanced building still misses', function () { // No M1 building has requirements; advanced ones (barracks, academy) will. config(['game.buildings.rally_point.requires' => ['headquarters' => 3, 'storage' => 0]]); $village = startVillage(); expect(collect(shown($village)['buildable'])->firstWhere('type', 'rally_point')) ->toMatchArray(['blocked' => 'requires', 'requires' => [['type' => 'headquarters', 'level' => 3]]]) ->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'rally_point')))->toBe('requires'); }); it('makes a later instance of a type cost more', function () { $village = startVillage(); $farms = collect(shown($village)['buildable'])->firstWhere('type', 'farm'); expect($farms['cost'])->toBe(['wood' => 68, 'clay' => 60, 'iron' => 45]); }); it('keeps a third order waiting and starts it when a builder frees up', function () { $village = startVillage(); $user = owner($village); app(QueueBuild::class)($user, $village->id, 'timber_camp'); // 90 s app(QueueBuild::class)($user, $village->id, 'hiding_place'); // 60 s app(QueueBuild::class)($user, $village->id, 'rally_point'); // 60 s, waits expect(collect(shown($village)['queue'])->pluck('builder')->all())->toBe([1, 2, null]); // Caught up long after: the rally point started when the hiding place finished. $this->travel(10)->minutes(); expect(shown($village)['queue'])->toBe([]) ->and(GameEvent::where('type', GameEvent::BUILD_ORDER_FINISHED)->orderBy('id')->pluck('fire_at')->map->format('H:i:s')->all()) ->toBe(['12:01:30', '12:01:00', '12:02:00']); }); it('queues upgrades of one building one after another', function () { $village = startVillage(); $hq = $village->buildings->firstWhere('type', 'headquarters'); app(QueueUpgrade::class)(owner($village), $village->id, $hq->id); app(QueueUpgrade::class)(owner($village), $village->id, $hq->id); $state = shown($village); expect(collect($state['queue'])->map(fn (array $o) => Arr::only($o, ['target_level', 'builder']))->all())->toBe([ ['target_level' => 1, 'builder' => 1], ['target_level' => 2, 'builder' => null], ]) ->and(collect($state['buildings'])->firstWhere('type', 'headquarters')['upgrade']['target_level'])->toBe(3) ->and($state['resources']['wood'])->toBe(500.0 - 113 - 143); // HQ 1 takes 144 s; HQ 2 then builds with HQ 1's speed-up: 172.8 / 1.05 → 165 s. $this->travel(144 + 165)->seconds(); expect(collect(shown($village)['buildings'])->firstWhere('type', 'headquarters')['level'])->toBe(2); }); it('cancels an order, the orders that build on it, and refunds them in full', function () { $village = startVillage(); $hq = $village->buildings->firstWhere('type', 'headquarters'); app(QueueUpgrade::class)(owner($village), $village->id, $hq->id); app(QueueUpgrade::class)(owner($village), $village->id, $hq->id); $first = shown($village)['queue'][0]['id']; app(CancelOrder::class)(owner($village), $village->id, $first); $state = shown($village); expect($state['queue'])->toBe([]) ->and($state['resources']['wood'])->toBe(500.0) ->and(GameEvent::whereNull('processed_at')->count())->toBe(0); }); it('demolishes with a refund on finish and removes a building below level 0', function () { $village = startVillage(); app(QueueBuild::class)(owner($village), $village->id, 'timber_camp'); $this->travel(90)->seconds(); $camp = collect(shown($village)['buildings'])->firstWhere('type', 'timber_camp'); app(QueueDemolish::class)(owner($village), $village->id, $camp['id']); expect(shown($village)['queue'][0])->toMatchArray(['action' => 'demolish', 'target_level' => -1]); $woodBefore = shown($village)['resources']['wood']; $this->travel(45)->seconds(); $state = shown($village); expect(collect($state['buildings'])->firstWhere('type', 'timber_camp'))->toBeNull() ->and($state['resources']['wood'])->toEqualWithDelta($woodBefore + 25 + 35 * 45 / 3600, 0.01); }); it('keeps core buildings at level 0', function () { $village = startVillage(); $hq = $village->buildings->firstWhere('type', 'headquarters'); expect(collect(shown($village)['buildings'])->firstWhere('type', 'headquarters')['demolish'])->toBeNull() ->and(rule(fn () => app(QueueDemolish::class)(owner($village), $village->id, $hq->id)))->toBe('core_building'); }); it('moves workers to the buildings with the highest priority', function () { // 24 villagers: the farm takes 2, the clay pit at priority 9 the other 22. $village = startVillage(); $village->buildings()->create(['type' => 'timber_camp', 'level' => 30]); $clay = $village->buildings()->create(['type' => 'clay_pit', 'level' => 30]); app(SetPriority::class)(owner($village), $village->id, $clay->id, 12); $buildings = collect(shown($village)['buildings'])->keyBy('type'); expect($buildings['clay_pit']['workers'])->toBe(['assigned' => 22, 'required' => 60]) ->and($buildings['clay_pit']['priority'])->toBe(9) ->and($buildings['timber_camp']['workers']['assigned'])->toBe(0) ->and($buildings['farm']['workers']['assigned'])->toBe(2); }); it('lets the worker finish orders of idle villages', function () { $village = startVillage(); app(QueueBuild::class)(owner($village), $village->id, 'timber_camp'); $this->travel(2)->minutes(); $this->artisan('game:events --once')->assertSuccessful(); expect($village->buildings()->where('type', 'timber_camp')->exists())->toBeTrue() ->and(GameEvent::whereNull('processed_at')->count())->toBe(0); }); it('grows the building space by 20 with every headquarters level', function () { $village = startVillage(); $hq = $village->buildings->firstWhere('type', 'headquarters'); app(QueueUpgrade::class)(owner($village), $village->id, $hq->id); // Space comes with the finished level, not the queued one. expect(shown($village)['space']['max'])->toBe(40); $this->travel(1)->hours(); expect(shown($village)['space']['max'])->toBe(60); }); it('keeps the headquarters from shrinking below what stands, and plans with a queued demolition', function () { $village = startVillage(); $hq = $village->buildings->firstWhere('type', 'headquarters'); $hq->update(['level' => 2]); // 17 + 7 × 4 = 45 of 80: level 1 (60) still fits, level 0 (40) would not. $village->buildings()->createMany(array_fill(0, 7, ['type' => 'timber_camp', 'level' => 0])); app(QueueDemolish::class)(owner($village), $village->id, $hq->id); $state = shown($village); expect($state['space'])->toBe(['used' => 45, 'max' => 60]) ->and(rule(fn () => app(QueueDemolish::class)(owner($village), $village->id, $hq->id)))->toBe('no_space'); });