From 9c5278d62ab86289df0c0f78062f088d9c1b09da Mon Sep 17 00:00:00 2001 From: Crypta Eve Date: Sat, 25 Jan 2025 13:15:17 +1030 Subject: [PATCH] feat: add faction squad filter (#691) * feat: add faction squad filter * chore: bump eveapi dep * tests: add tests for faction squad filter * fix: copy-pasta name fixed --- composer.json | 2 +- src/Config/web.characterfilter.php | 1 + .../Support/FastLookupController.php | 32 +++ src/Http/Routes/Support/FastLookup.php | 4 + tests/Squads/FactionRuleTest.php | 198 ++++++++++++++++++ 5 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 tests/Squads/FactionRuleTest.php diff --git a/composer.json b/composer.json index 3d8f78267..acae2ae86 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "doctrine/dbal": "^3.0", "erusev/parsedown": "^1.7", "eveseat/eseye": "^3.0", - "eveseat/eveapi": "^5.0.15", + "eveseat/eveapi": "^5.0.18", "eveseat/services": "^5.0.8", "guzzlehttp/guzzle": "^7.0", "intervention/image": "^2.0", diff --git a/src/Config/web.characterfilter.php b/src/Config/web.characterfilter.php index f1e3dbf9b..baf8f1598 100644 --- a/src/Config/web.characterfilter.php +++ b/src/Config/web.characterfilter.php @@ -25,6 +25,7 @@ ['name' => 'character', 'src' => 'seatcore::fastlookup.characters', 'path' => '', 'field' => 'character_id', 'label' => 'Character'], ['name' => 'title', 'src' => 'seatcore::fastlookup.titles', 'path' => 'titles', 'field' => 'id', 'label' => 'Title'], ['name' => 'corporation', 'src' => 'seatcore::fastlookup.corporations', 'path' => 'affiliation', 'field' => 'corporation_id', 'label' => 'Corporation'], + ['name' => 'factions', 'src' => 'seatcore::fastlookup.factions', 'path' => 'affiliation', 'field' => 'corporation_id', 'label' => 'Faction'], ['name' => 'alliance', 'src' => 'seatcore::fastlookup.alliances', 'path' => 'affiliation', 'field' => 'alliance_id', 'label' => 'Alliance'], ['name' => 'skill', 'src' => 'seatcore::fastlookup.skills', 'path' => 'skills', 'field' => 'skill_id', 'label' => 'Skill'], ['name' => 'skill_level', 'src' => [['id' => 1, 'text' => 'Level 1'], ['id' => 2, 'text' => 'Level 2'], ['id' => 3, 'text' => 'Level 3'], ['id' => 4, 'text' => 'Level 4'], ['id' => 5, 'text' => 'Level 5']], 'path' => 'skills', 'field' => 'trained_skill_level', 'label' => 'Skill Level'], diff --git a/src/Http/Controllers/Support/FastLookupController.php b/src/Http/Controllers/Support/FastLookupController.php index a58128719..22ea7ff29 100644 --- a/src/Http/Controllers/Support/FastLookupController.php +++ b/src/Http/Controllers/Support/FastLookupController.php @@ -29,6 +29,7 @@ use Seat\Eveapi\Models\Corporation\CorporationInfo; use Seat\Eveapi\Models\Corporation\CorporationRole; use Seat\Eveapi\Models\Corporation\CorporationTitle; +use Seat\Eveapi\Models\Sde\ChrFaction; use Seat\Eveapi\Models\Sde\Constellation; use Seat\Eveapi\Models\Sde\InvType; use Seat\Eveapi\Models\Sde\Region; @@ -197,6 +198,37 @@ public function getCorporations(Request $request) } + /** + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\JsonResponse + */ + public function getFactions(Request $request) + { + if ($request->query('_type', 'query') == 'find') { + $faction = ChrFaction::find($request->query('q', 0)); + + return response()->json([ + 'id' => $faction->factionID, + 'text' => $faction->name, + ]); + } + + $factions = ChrFaction::where('factionName', 'like', '%' . $request->query('q', '') . '%') + ->orderBy('factionName') + ->get() + ->map(function ($faction, $key) { + return [ + 'id' => $faction->factionID, + 'text' => $faction->name, + ]; + }); + + return response()->json([ + 'results' => $factions, + ]); + + } + /** * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse diff --git a/src/Http/Routes/Support/FastLookup.php b/src/Http/Routes/Support/FastLookup.php index c84b44a7c..423ea8bd5 100644 --- a/src/Http/Routes/Support/FastLookup.php +++ b/src/Http/Routes/Support/FastLookup.php @@ -40,6 +40,10 @@ ->name('seatcore::fastlookup.corporations') ->uses('FastLookupController@getCorporations'); +Route::get('/factions') + ->name('seatcore::fastlookup.factions') + ->uses('FastLookupController@getFactions'); + Route::get('/alliances') ->name('seatcore::fastlookup.alliances') ->uses('FastLookupController@getAlliances'); diff --git a/tests/Squads/FactionRuleTest.php b/tests/Squads/FactionRuleTest.php new file mode 100644 index 000000000..bc52712a0 --- /dev/null +++ b/tests/Squads/FactionRuleTest.php @@ -0,0 +1,198 @@ +set('database.default', 'testbench'); + $app['config']->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + $app['config']->set('database.redis.client', 'mock'); + } + + /** + * @param \Illuminate\Foundation\Application $app + * @return array|string[] + */ + protected function getPackageProviders($app) + { + return [ + RedisMockServiceProvider::class, + WebServiceProvider::class, + ]; + } + + protected function setUp(): void + { + parent::setUp(); + + $this->loadMigrationsFrom(realpath(__DIR__ . '/../database/migrations')); + + Event::fake(); + + CharacterInfo::factory(50) + ->create() + ->each(function($character) { + $character->affiliation()->save(CharacterAffiliation::factory()->make()); + }); + + User::factory(10) + ->create() + ->each(function ($user) { + CharacterInfo::whereDoesntHave('refresh_token')->get() + ->random(rand(1, 5))->each(function ($character) use ($user) { + RefreshToken::factory()->create([ + 'character_id' => $character->character_id, + 'user_id' => $user->id, + ]); + }); + }); + } + + public function testUserHasNoCharacterInFaction() + { + // spawn test squad + $squad = new Squad([ + 'name' => 'Testing Squad', + 'description' => 'Some description', + 'type' => 'auto', + 'filters' => json_encode([ + 'and' => [ + [ + 'name' => 'faction', + 'path' => 'affiliation', + 'field' => 'faction_id', + 'operator' => '=', + 'criteria' => 500001, + 'text' => 'Random Faction', + ], + ], + ]), + ]); + + // pickup users + $users = User::all(); + + // ensure no users are eligible + foreach ($users as $user) { + $this->assertFalse($squad->isUserEligible($user)); + } + } + + public function testUserHasCharacterInFaction() + { + // spawn test squad + $squad = new Squad([ + 'name' => 'Testing Squad', + 'description' => 'Some description', + 'type' => 'auto', + 'filters' => json_encode([ + 'and' => [ + [ + 'name' => 'faction', + 'path' => 'affiliation', + 'field' => 'faction_id', + 'operator' => '=', + 'criteria' => 500001, + 'text' => 'Random Faction', + ], + ], + ]), + ]); + + // update an user to match criteria + $reference_user = User::first(); + $reference_user->characters->first()->affiliation->update(['faction_id' => 500001]); + + $users = User::all(); + + foreach ($users as $user) { + $user->id == $reference_user->id ? + $this->assertTrue($squad->isUserEligible($user)) : + $this->assertFalse($squad->isUserEligible($user)); + } + } + + /** + * This test checks whether a character from a corp outside an alliance is eligible for a squad with a alliance is not filter. + * In SeAT 4, this was not working properly + */ + public function testCharacterHasNoFactionWithFactionIsNotFilter(){ + $squad = new Squad([ + 'name' => 'Testing Squad', + 'description' => 'Some description', + 'type' => 'auto', + 'filters' => json_encode([ + 'and' => [ + [ + 'name' => 'faction', + 'path' => 'affiliation', + 'field' => 'faction_id', + 'operator' => '<>', + 'criteria' => 500001, + 'text' => 'Random Faction', + ], + ], + ]), + ]); + + $user = User::first(); + + $user->characters->each(function ($character){ + $character->affiliation->update([ + 'faction_id' => 500001, + ]); + }); + $this->assertFalse($squad->isUserEligible($user)); + + $user->characters->each(function ($character){ + $character->affiliation->update([ + 'faction_id' => null, + ]); + }); + $this->assertTrue($squad->isUserEligible($user)); + } +}