From 1875c98777d230e6dba4f256ba7156a7b468726f Mon Sep 17 00:00:00 2001 From: rohsyl Date: Mon, 18 Mar 2024 14:50:59 +0100 Subject: [PATCH] #277 writing tests --- .../Commands/Matchup/PlayerMatchupHandler.php | 1 + .../Controllers/ApiQuickMatchController.php | 3 + cncnet-api/app/Models/GameObjectSchema.php | 3 + cncnet-api/app/Models/Ladder.php | 2 + cncnet-api/app/Models/LadderHistory.php | 3 + cncnet-api/app/Models/Map.php | 2 + cncnet-api/app/Models/MapPool.php | 3 + cncnet-api/app/Models/QmLadderRules.php | 2 + cncnet-api/app/Models/QmMap.php | 4 + cncnet-api/app/Models/Side.php | 9 ++ cncnet-api/composer.json | 3 +- .../factories/GameObjectSchemaFactory.php | 23 +++++ .../database/factories/LadderFactory.php | 29 ++++++ .../factories/LadderHistoryFactory.php | 25 +++++ cncnet-api/database/factories/MapFactory.php | 26 +++++ .../database/factories/MapPoolFactory.php | 23 +++++ .../factories/QmLadderRulesFactory.php | 38 ++++++++ .../database/factories/QmMapFactory.php | 23 +++++ cncnet-api/database/factories/SideFactory.php | 23 +++++ .../MatchRequest/PlayerMatchRequestTest.php | 45 ++++----- .../Api/Qm/MatchRequest/QmPlayerHelper.php | 94 ++++++------------- docker/Dockerfile | 5 +- 22 files changed, 301 insertions(+), 88 deletions(-) create mode 100644 cncnet-api/database/factories/GameObjectSchemaFactory.php create mode 100644 cncnet-api/database/factories/LadderFactory.php create mode 100644 cncnet-api/database/factories/LadderHistoryFactory.php create mode 100644 cncnet-api/database/factories/MapFactory.php create mode 100644 cncnet-api/database/factories/MapPoolFactory.php create mode 100644 cncnet-api/database/factories/QmLadderRulesFactory.php create mode 100644 cncnet-api/database/factories/QmMapFactory.php create mode 100644 cncnet-api/database/factories/SideFactory.php diff --git a/cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php b/cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php index b0da6961..3b915bf4 100644 --- a/cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php +++ b/cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php @@ -148,6 +148,7 @@ public function matchup() foreach ($qmMaps as $qmMap) { $match = true; + if ( array_key_exists( $qmMap->bit_idx, diff --git a/cncnet-api/app/Http/Controllers/ApiQuickMatchController.php b/cncnet-api/app/Http/Controllers/ApiQuickMatchController.php index 2ae4c6ad..305b6f46 100644 --- a/cncnet-api/app/Http/Controllers/ApiQuickMatchController.php +++ b/cncnet-api/app/Http/Controllers/ApiQuickMatchController.php @@ -396,6 +396,9 @@ public function matchRequest(Request $request, $ladderAbbrev = null, $playerName */ private function onMatchMeUp($request, $ladder, $player, $qmPlayer) { + + Log::info('request : ' . json_encode($request->all())); + $ladderRules = $ladder->qmLadderRules()->first(); $history = $ladder->currentHistory(); $user = $player->user; diff --git a/cncnet-api/app/Models/GameObjectSchema.php b/cncnet-api/app/Models/GameObjectSchema.php index cc12cf5e..85f9d25b 100644 --- a/cncnet-api/app/Models/GameObjectSchema.php +++ b/cncnet-api/app/Models/GameObjectSchema.php @@ -1,9 +1,12 @@ 'datetime', diff --git a/cncnet-api/app/Models/Map.php b/cncnet-api/app/Models/Map.php index 7695eeab..34f2678a 100755 --- a/cncnet-api/app/Models/Map.php +++ b/cncnet-api/app/Models/Map.php @@ -2,10 +2,12 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Map extends Model { + use HasFactory; protected $table = 'maps'; protected $fillable = ['name', 'hash', 'ladder_id', 'spawn_count']; public $timestamps = false; diff --git a/cncnet-api/app/Models/MapPool.php b/cncnet-api/app/Models/MapPool.php index e68be836..fbe421dc 100644 --- a/cncnet-api/app/Models/MapPool.php +++ b/cncnet-api/app/Models/MapPool.php @@ -1,9 +1,12 @@ + */ +class GameObjectSchemaFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->words(3, true) + ]; + } +} diff --git a/cncnet-api/database/factories/LadderFactory.php b/cncnet-api/database/factories/LadderFactory.php new file mode 100644 index 00000000..f256ed4b --- /dev/null +++ b/cncnet-api/database/factories/LadderFactory.php @@ -0,0 +1,29 @@ + + */ +class LadderFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->words(3, true), + 'abbreviation' => $this->faker->randomLetter() . $this->faker->randomLetter(), + 'game' => 'yr', + 'clans_allowed' => false, + 'game_object_schema_id' => GameObjectSchema::factory(), + 'private' => false, + ]; + } +} diff --git a/cncnet-api/database/factories/LadderHistoryFactory.php b/cncnet-api/database/factories/LadderHistoryFactory.php new file mode 100644 index 00000000..2db382b0 --- /dev/null +++ b/cncnet-api/database/factories/LadderHistoryFactory.php @@ -0,0 +1,25 @@ + + */ +class LadderHistoryFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'starts' => today()->startOfMonth(), + 'ends' => today()->endOfMonth(), + 'short' => today()->startOfMonth()->format('n-Y') + ]; + } +} diff --git a/cncnet-api/database/factories/MapFactory.php b/cncnet-api/database/factories/MapFactory.php new file mode 100644 index 00000000..eac578b5 --- /dev/null +++ b/cncnet-api/database/factories/MapFactory.php @@ -0,0 +1,26 @@ + + */ +class MapFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->words(2, true), + 'hash' => Hash::make($this->faker->name), + 'spawn_count' => 2, + ]; + } +} diff --git a/cncnet-api/database/factories/MapPoolFactory.php b/cncnet-api/database/factories/MapPoolFactory.php new file mode 100644 index 00000000..4b1fc995 --- /dev/null +++ b/cncnet-api/database/factories/MapPoolFactory.php @@ -0,0 +1,23 @@ + + */ +class MapPoolFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->name, + ]; + } +} diff --git a/cncnet-api/database/factories/QmLadderRulesFactory.php b/cncnet-api/database/factories/QmLadderRulesFactory.php new file mode 100644 index 00000000..554e11aa --- /dev/null +++ b/cncnet-api/database/factories/QmLadderRulesFactory.php @@ -0,0 +1,38 @@ + + */ +class QmLadderRulesFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'player_count' => 2, + 'map_vetoes' => 0, + 'max_difference' => 498, + 'all_sides' => '0,1,2,3,4,5,6,7,8,9', + 'allowed_sides' => '0,1,2', + 'bail_time' => 0, + 'bail_fps' => 0, + 'tier2_rating' => 1200, + 'rating_per_second' => 0.75, + 'max_points_difference' => 400, + 'points_per_second' => 1, + 'use_elo_points' => 1, + 'wol_k' => 64, + 'show_map_preview' => 1, + 'reduce_map_repeats' => 5, + 'use_ranked_map_picker' => 0, + ]; + } +} diff --git a/cncnet-api/database/factories/QmMapFactory.php b/cncnet-api/database/factories/QmMapFactory.php new file mode 100644 index 00000000..7b5e4a36 --- /dev/null +++ b/cncnet-api/database/factories/QmMapFactory.php @@ -0,0 +1,23 @@ + + */ +class QmMapFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/cncnet-api/database/factories/SideFactory.php b/cncnet-api/database/factories/SideFactory.php new file mode 100644 index 00000000..b8e6a020 --- /dev/null +++ b/cncnet-api/database/factories/SideFactory.php @@ -0,0 +1,23 @@ + + */ +class SideFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->country + ]; + } +} diff --git a/cncnet-api/tests/Feature/Api/Qm/MatchRequest/PlayerMatchRequestTest.php b/cncnet-api/tests/Feature/Api/Qm/MatchRequest/PlayerMatchRequestTest.php index 64c518cd..847979c7 100644 --- a/cncnet-api/tests/Feature/Api/Qm/MatchRequest/PlayerMatchRequestTest.php +++ b/cncnet-api/tests/Feature/Api/Qm/MatchRequest/PlayerMatchRequestTest.php @@ -34,7 +34,6 @@ protected function setUp(): void } - public function test_match_me_up_on_empty_q(): void { $ladderName = $this->ladder->abbreviation; @@ -49,7 +48,7 @@ public function test_match_me_up_on_empty_q(): void 'version' => '1.83', 'type' => 'match me up', 'map_bitfield' => 234325324, - 'side' => 6 + 'side' => 1 ]); $json = $response->json(); @@ -76,24 +75,23 @@ public function test_check_back() 'version' => '1.83', 'type' => 'match me up', 'map_bitfield' => 234325324, - 'side' => 6 + 'side' => 1 ]); Carbon::setTestNow($d->clone()->addSeconds(8)); $response = $this - ->jwtAuth($this->user1) - ->post('/api/v1/qm/'.$ladderName.'/'.$playerName, [ - 'version' => '1.83', - 'type' => 'update', - /*'status' => '', - 'seed' => '', - 'peers' => '',*/ - ]); + ->jwtAuth($this->user1) + ->post('/api/v1/qm/'.$ladderName.'/'.$playerName, [ + 'version' => '1.83', + 'type' => 'match me up', + 'map_bitfield' => 234325324, + 'side' => 1 + ]); $json = $response->json(); - $this->assertEquals('update', $json['type']); + $this->assertEquals('please wait', $json['type']); } @@ -107,6 +105,10 @@ public function test_match_me_up_and_find_opponent(): void $lh = $this->makeLadderHistory($this->ladder); $this->makePlayerHistory($this->player1, $lh); + $u2 = $this->makeUser('test2'); + $p2 = $this->makePlayerForLadder('test2', $this->ladder, $u2); + $this->makePlayerHistory($p2, $lh); + // queue a 1st player $this ->jwtAuth($this->user1) @@ -114,38 +116,39 @@ public function test_match_me_up_and_find_opponent(): void 'version' => '1.83', 'type' => 'match me up', 'map_bitfield' => 0xffffffff, - 'side' => 1 + 'side' => 1, + 'map_sides' => [1,1,1,1] ]); - $u2 = $this->makeUser('test2'); - $p2 = $this->makePlayerForLadder('test2', $this->ladder, $u2); - $this->makePlayerHistory($p2, $lh); Carbon::setTestNow($d->clone()->addSeconds(8)); // queue a 2nd player - $response = $this + $this ->jwtAuth($u2) ->post('/api/v1/qm/'.$ladderName.'/'.$p2->username, [ 'version' => '1.83', 'type' => 'match me up', 'map_bitfield' => 0xffffffff, - 'side' => 2 + 'side' => 1, + 'map_sides' => [1,1,1,1] ]); Carbon::setTestNow($d->clone()->addSeconds(8)); + // queue a 2nd player $response = $this ->jwtAuth($u2) ->post('/api/v1/qm/'.$ladderName.'/'.$p2->username, [ 'version' => '1.83', 'type' => 'match me up', 'map_bitfield' => 0xffffffff, - 'side' => 2 + 'side' => 1, + 'map_sides' => [1,1,1,1] ]); + $json = $response->json(); - - dd($response->json()); + $this->assertEquals('spawn', $json['type']); } } diff --git a/cncnet-api/tests/Feature/Api/Qm/MatchRequest/QmPlayerHelper.php b/cncnet-api/tests/Feature/Api/Qm/MatchRequest/QmPlayerHelper.php index bf06f91f..50155049 100644 --- a/cncnet-api/tests/Feature/Api/Qm/MatchRequest/QmPlayerHelper.php +++ b/cncnet-api/tests/Feature/Api/Qm/MatchRequest/QmPlayerHelper.php @@ -11,6 +11,7 @@ use App\Models\PlayerHistory; use App\Models\QmLadderRules; use App\Models\QmMap; +use App\Models\Side; use App\Models\User; use App\Models\UserSettings; use Illuminate\Support\Facades\Hash; @@ -35,93 +36,58 @@ protected function makeUser($name) { protected function makeLadder($playerCount = 2) { - $gos = GameObjectSchema::create([ - 'name' => 'Yuri\'s Revenge Schema', - ]); - - - $ladder = Ladder::create([ + $ladder = Ladder::factory()->create([ 'name' => 'Test ladder', 'abbreviation' => 'tl', - 'game' => 'yr', - 'clans_allowed' => false, - 'game_object_schema_id' => $gos->id, - 'private' => false, ]); - $qmlr = QmLadderRules::create([ + Side::factory()->count(3) + ->sequence(fn ($sequence) => ['local_id' => $sequence->index]) + ->create([ + 'ladder_id' => $ladder->id, + ]); + $qmlr = QmLadderRules::factory()->create([ 'ladder_id' => $ladder->id, 'player_count' => $playerCount, - 'map_vetoes' => 0, - 'max_difference' => 498, - 'all_sides' => '0,1,2,3,4,5,6,7,8,9', - 'allowed_sides' => '-1,0,1,2,3,4,5,6,7,8,9', - 'bail_time' => 0, - 'bail_fps' => 0, - 'tier2_rating' => 1200, - 'rating_per_second' => 0.75, - 'max_points_difference' => 400, - 'points_per_second' => 1, - 'use_elo_points' => 1, - 'wol_k' => 64, - 'show_map_preview' => 1, - 'reduce_map_repeats' => 5, - 'use_ranked_map_picker' => 0, + 'allowed_sides' => '-1,' . join(',', $ladder->sides->pluck('local_id')->toArray()), ]); - $mpool = MapPool::create([ - 'name' => 'Pool', + $mpool = MapPool::factory()->create([ 'ladder_id' => $ladder->id, ]); - // wtf is this ahah + // set current map_pool $ladder->update([ 'map_pool_id' => $mpool->id, ]); - - $maps = []; - $maps[] = $this->makeMap('m1', $ladder, $playerCount); - $maps[] = $this->makeMap('m2', $ladder, $playerCount); - $maps[] = $this->makeMap('m3', $ladder, $playerCount); - $maps[] = $this->makeMap('m4', $ladder, $playerCount); - - foreach($maps as $map) { - $this->makeQmMap($map, $ladder, $mpool); + $maps = Map::factory() + ->count(4) + ->create([ + 'spawn_count' => $playerCount, + 'ladder_id' => $ladder->id, + ]); + + foreach($maps as $index => $map) { + QmMap::factory()->create([ + 'ladder_id' => $ladder->id, + 'map_pool_id' => $mpool->id, + 'map_id' => $map->id, + 'description' => $map->name, + 'valid' => 1, + 'bit_idx' => $index, + 'spawn_order' => join(',', array_fill(0, $playerCount, 0)), + 'allowed_sides' => $qmlr->allowed_sides, + ]); } - return $ladder; } - protected function makeMap($name, Ladder $ladder, $playerCount) { - return Map::create([ - 'name' => $name, - 'hash' => Hash::make($name), - 'spawn_count' => $playerCount, - 'ladder_id' => $ladder->id, - ]); - } - - protected function makeQmMap(Map $map, Ladder $ladder, MapPool $pool) { - return QmMap::create([ - 'ladder_id' => $ladder->id, - 'map_pool_id' => $pool->id, - 'map_id' => $map->id, - 'valid' => 1, - 'description' => $map->name, - ]); - } - protected function makeLadderHistory(Ladder $ladder) { - $lh = new LadderHistory([ + return LadderHistory::factory()->create([ 'ladder_id' => $ladder->id, - 'starts' => today()->startOfMonth(), - 'ends' => today()->endOfMonth(), - 'short' => today()->startOfMonth()->format('n-Y') ]); - $lh->save(); - return $lh; } protected function makePlayerForLadder($playername, Ladder $ladder, User $user) { diff --git a/docker/Dockerfile b/docker/Dockerfile index ddc8d8b8..b8ab1075 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -23,7 +23,8 @@ RUN apt-get update && \ cron \ nano \ zip \ - libxml2-dev + libxml2-dev \ + default-mysql-client COPY docker/custom-php.ini /usr/local/etc/php/conf.d/custom-php.ini @@ -81,7 +82,7 @@ RUN docker-php-ext-install gd && \ ##################################### # Install the xdebug extension -RUN pecl install xdebug-3.3.0 +RUN pecl install xdebug-3.3.0 && docker-php-ext-enable xdebug ##################################### # PHP Memcached: