Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add faction squad filter #691

Merged
merged 4 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/Config/web.characterfilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
32 changes: 32 additions & 0 deletions src/Http/Controllers/Support/FastLookupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Http/Routes/Support/FastLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
198 changes: 198 additions & 0 deletions tests/Squads/FactionRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

/*
* This file is part of SeAT
*
* Copyright (C) 2015 to 2020 Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

namespace Seat\Tests\Web\Squads;

use Illuminate\Support\Facades\Event;
use Lunaweb\RedisMock\Providers\RedisMockServiceProvider;
use Orchestra\Testbench\TestCase;
use Seat\Eveapi\Models\Character\CharacterAffiliation;
use Seat\Eveapi\Models\Character\CharacterInfo;
use Seat\Eveapi\Models\RefreshToken;
use Seat\Web\Models\Squads\Squad;
use Seat\Web\Models\User;
use Seat\Web\WebServiceProvider;

/**
* Class FactionRuleTest.
*
* @package Seat\Tests\Web\Squads
*/
class FactionRuleTest extends TestCase
{
/**
* @param \Illuminate\Foundation\Application $app
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->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));
}
}
Loading