152 lines
7.5 KiB
PHP
152 lines
7.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use App\Actions\Ranking;
|
||
|
|
use App\Actions\SendTroops;
|
||
|
|
use App\Actions\Tribes;
|
||
|
|
use App\Models\Movement;
|
||
|
|
use App\Models\Tribe;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Village;
|
||
|
|
use Carbon\CarbonImmutable;
|
||
|
|
|
||
|
|
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
|
||
|
|
|
||
|
|
function tribes(): Tribes
|
||
|
|
{
|
||
|
|
return app(Tribes::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** A player's village with its own tribe (tag from the name), they lead it. */
|
||
|
|
function tribeOf(Village $village, string $tag): Village
|
||
|
|
{
|
||
|
|
tribes()->found(owner($village), $village->world_id, "Tribe $tag", $tag);
|
||
|
|
|
||
|
|
return $village->fresh();
|
||
|
|
}
|
||
|
|
|
||
|
|
function attackFrom(Village $from, Village $to): Village
|
||
|
|
{
|
||
|
|
$from->buildings()->firstOrCreate(['type' => 'rally_point'], ['level' => 1]);
|
||
|
|
$from->troops()->firstOrCreate(['home_village_id' => $from->id, 'unit' => 'spear'], ['count' => 10]);
|
||
|
|
|
||
|
|
return app(SendTroops::class)(owner($from), $from->id, 'attack', $to->x, $to->y, ['spear' => 1], null, null);
|
||
|
|
}
|
||
|
|
|
||
|
|
it('founds a tribe, invites, and lets the invited join', function () {
|
||
|
|
$leader = startVillage();
|
||
|
|
$other = startVillage();
|
||
|
|
$page = tribes()->found(owner($leader), $leader->world_id, 'Dorfrat', 'DRF');
|
||
|
|
expect($page['tribe'])->toMatchArray(['name' => 'Dorfrat', 'tag' => 'DRF', 'role' => 'leader'])
|
||
|
|
->and($page['tribe']['members'])->toHaveCount(1);
|
||
|
|
|
||
|
|
tribes()->invite(owner($leader), $leader->world_id, $other->player->name);
|
||
|
|
$invited = tribes()->show(owner($other), $other->world_id);
|
||
|
|
expect($invited['tribe'])->toBeNull()
|
||
|
|
->and($invited['invites'][0]['tribe']['tag'])->toBe('DRF')
|
||
|
|
->and(shown($other)['tribe'])->toBe(['tribe' => null, 'invites' => 1]);
|
||
|
|
|
||
|
|
$joined = tribes()->join(owner($other), $other->world_id, $invited['invites'][0]['tribe']['id']);
|
||
|
|
expect($joined['tribe'])->toMatchArray(['tag' => 'DRF', 'role' => 'member'])
|
||
|
|
->and($joined['tribe']['members'])->toHaveCount(2)
|
||
|
|
->and($joined['invites'])->toBe([])
|
||
|
|
->and(collect($joined['tribe']['events'])->pluck('type')->all())->toContain('joined', 'invited', 'founded')
|
||
|
|
->and(shown($other)['tribe']['tribe'])->toMatchArray(['tag' => 'DRF', 'role' => 'member']);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('checks names, tags and ranks', function () {
|
||
|
|
$leader = tribeOf(startVillage(), 'DRF');
|
||
|
|
$member = startVillage();
|
||
|
|
tribes()->invite(owner($leader), $leader->world_id, $member->player->name);
|
||
|
|
tribes()->join(owner($member), $member->world_id, Tribe::where('tag', 'DRF')->value('id'));
|
||
|
|
$third = startVillage();
|
||
|
|
|
||
|
|
expect(rule(fn () => tribes()->found(owner($third), $third->world_id, 'Other', 'DRF')))->toBe('tribe_taken')
|
||
|
|
->and(rule(fn () => tribes()->found(owner($third), $third->world_id, 'x', 'Y')))->toBe('tribe_name')
|
||
|
|
->and(rule(fn () => tribes()->found(owner($member), $member->world_id, 'Second', 'SEC')))->toBe('in_tribe')
|
||
|
|
->and(rule(fn () => tribes()->invite(owner($member), $member->world_id, $third->player->name)))->toBe('rank')
|
||
|
|
->and(rule(fn () => tribes()->join(owner($third), $third->world_id, Tribe::where('tag', 'DRF')->value('id'))))->toBe('not_found')
|
||
|
|
->and(rule(fn () => tribes()->leave(owner($leader), $leader->world_id)))->toBe('hand_over');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('hands the tribe over, removes members, and ends with its last member', function () {
|
||
|
|
$leader = tribeOf(startVillage(), 'DRF');
|
||
|
|
$member = startVillage();
|
||
|
|
$tribeId = Tribe::where('tag', 'DRF')->value('id');
|
||
|
|
tribes()->invite(owner($leader), $leader->world_id, $member->player->name);
|
||
|
|
tribes()->join(owner($member), $member->world_id, $tribeId);
|
||
|
|
|
||
|
|
tribes()->role(owner($leader), $leader->world_id, $member->player_id, 'leader');
|
||
|
|
$page = tribes()->show(owner($leader), $leader->world_id);
|
||
|
|
expect($page['tribe']['role'])->toBe('council');
|
||
|
|
|
||
|
|
tribes()->kick(owner($member), $member->world_id, $leader->player_id);
|
||
|
|
expect(tribes()->show(owner($leader), $leader->world_id)['tribe'])->toBeNull();
|
||
|
|
|
||
|
|
tribes()->leave(owner($member), $member->world_id);
|
||
|
|
expect(Tribe::count())->toBe(0);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('seals an alliance only when both tribes agree, and keeps the peace while it holds', function () {
|
||
|
|
$a = tribeOf(startVillage(), 'AAA');
|
||
|
|
$b = tribeOf(startVillage(), 'BBB');
|
||
|
|
$this->travel(4)->days();
|
||
|
|
|
||
|
|
$offered = tribes()->propose(owner($a), $a->world_id, 'BBB', 'ally');
|
||
|
|
expect($offered['tribe']['relations'][0])->toMatchArray(['state' => 'ally', 'status' => 'offered']);
|
||
|
|
$incoming = tribes()->show(owner($b), $b->world_id)['tribe']['relations'][0];
|
||
|
|
expect($incoming)->toMatchArray(['state' => 'ally', 'status' => 'incoming']);
|
||
|
|
|
||
|
|
$sealed = tribes()->accept(owner($b), $b->world_id, $incoming['tribe']['id']);
|
||
|
|
expect($sealed['tribe']['relations'])->toBe([['tribe' => Tribe::where('tag', 'AAA')->first()->summary(), 'state' => 'ally', 'status' => 'holds', 'ends_at' => null]])
|
||
|
|
->and(rule(fn () => attackFrom($a, $b)))->toBe('pact');
|
||
|
|
|
||
|
|
// Ending it takes a day of notice; the alliance holds until then.
|
||
|
|
$ending = tribes()->end(owner($a), $a->world_id, Tribe::where('tag', 'BBB')->value('id'));
|
||
|
|
expect($ending['tribe']['relations'][0])->toMatchArray(['status' => 'ending', 'ends_at' => '2026-11-03T12:00:00.000Z'])
|
||
|
|
->and(rule(fn () => attackFrom($a, $b)))->toBe('pact');
|
||
|
|
|
||
|
|
$this->travel(1)->days();
|
||
|
|
expect(tribes()->show(owner($a), $a->world_id)['tribe']['relations'])->toBe([]);
|
||
|
|
attackFrom($a, $b);
|
||
|
|
expect(Movement::where('origin_id', $a->id)->count())->toBe(1);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('seals at once when both offered the same, and keeps tribe members from attacking each other', function () {
|
||
|
|
$a = tribeOf(startVillage(), 'AAA');
|
||
|
|
$b = tribeOf(startVillage(), 'BBB');
|
||
|
|
tribes()->propose(owner($b), $b->world_id, 'AAA', 'nap');
|
||
|
|
$page = tribes()->propose(owner($a), $a->world_id, 'BBB', 'nap');
|
||
|
|
expect($page['tribe']['relations'][0])->toMatchArray(['state' => 'nap', 'status' => 'holds']);
|
||
|
|
|
||
|
|
$mate = startVillage(User::factory()->create());
|
||
|
|
tribes()->invite(owner($a), $a->world_id, $mate->player->name);
|
||
|
|
tribes()->join(owner($mate), $mate->world_id, Tribe::where('tag', 'AAA')->value('id'));
|
||
|
|
$this->travel(4)->days();
|
||
|
|
expect(rule(fn () => attackFrom($a, $mate)))->toBe('own_tribe');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('declares enmity alone, but not over a pact', function () {
|
||
|
|
$a = tribeOf(startVillage(), 'AAA');
|
||
|
|
$b = tribeOf(startVillage(), 'BBB');
|
||
|
|
tribes()->enemy(owner($a), $a->world_id, 'BBB');
|
||
|
|
expect(tribes()->show(owner($b), $b->world_id)['tribe']['relations'][0])->toMatchArray(['state' => 'enemy', 'status' => 'hostile'])
|
||
|
|
->and(collect(tribes()->list(owner($b), $b->world_id))->firstWhere('tag', 'AAA')['relation'])->toBe('enemy');
|
||
|
|
|
||
|
|
tribes()->end(owner($a), $a->world_id, Tribe::where('tag', 'BBB')->value('id'));
|
||
|
|
tribes()->propose(owner($a), $a->world_id, 'BBB', 'ally');
|
||
|
|
tribes()->accept(owner($b), $b->world_id, Tribe::where('tag', 'AAA')->value('id'));
|
||
|
|
expect(rule(fn () => tribes()->enemy(owner($b), $b->world_id, 'AAA')))->toBe('pact_active');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('ranks tribes by their members points and tags players in the ranking', function () {
|
||
|
|
$a = tribeOf(startVillage(), 'AAA');
|
||
|
|
$b = startVillage();
|
||
|
|
$b->update(['points' => 500]);
|
||
|
|
tribeOf($b, 'BBB');
|
||
|
|
|
||
|
|
$ranking = app(Ranking::class)->list(owner($a), $a->world_id, 'tribes');
|
||
|
|
expect(array_column(array_column($ranking['rows'], 'tribe'), 'tag'))->toBe(['BBB', 'AAA'])
|
||
|
|
->and($ranking['rows'][1])->toMatchArray(['rank' => 2, 'members' => 1, 'own' => true])
|
||
|
|
->and(app(Ranking::class)->list(owner($a), $a->world_id, 'players')['rows'][0]['player']['tribe']['tag'])->toBe('BBB');
|
||
|
|
});
|