From 8128457a3b4ae095427dbeeae60adce570775055 Mon Sep 17 00:00:00 2001 From: alexp8 Date: Mon, 22 May 2023 19:45:59 -0400 Subject: [PATCH 1/6] random colors --- .../app/Http/Controllers/ClanController.php | 13 ++-- .../app/Http/Services/QuickMatchService.php | 60 ++++++++++++++++--- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/cncnet-api/app/Http/Controllers/ClanController.php b/cncnet-api/app/Http/Controllers/ClanController.php index 42c1996b..d2deda3a 100644 --- a/cncnet-api/app/Http/Controllers/ClanController.php +++ b/cncnet-api/app/Http/Controllers/ClanController.php @@ -94,10 +94,14 @@ public function editLadderClan(Request $request, $ladderAbbrev, $clanId) return $p->clanPlayer !== null && $p->clanPlayer->clan_id == $clan->id; })->first(); - if ($player === null) + if ($player === null && !$user->isGod()) { return response('Not Authorized', 403); } + else if ($player === null && $user->isGod()) + { + $player = $clan->clanPlayers[0]->player; + } $invitations = $clan->invitations; @@ -484,16 +488,15 @@ public function saveMembers(Request $request, $ladderAbbrev, $clanId) } $player = \App\Player::find($request->player_id); + $user = $request->user(); - if ($player === null || !$player->clanPlayer->isOwner()) + if (($player === null || !$player->clanPlayer->isOwner()) && !$user->isGod()) { $request->session()->flash('error', "You don't have permission to do that."); return redirect()->back(); } - $user = $request->user(); - - if ($player->user_id != $user->id) + if ($player->user_id != $user->id && !$user->isGod()) { $request->session()->flash('error', "You don't have permission to do that."); return redirect()->back(); diff --git a/cncnet-api/app/Http/Services/QuickMatchService.php b/cncnet-api/app/Http/Services/QuickMatchService.php index eccbd15e..4a8c3f2c 100644 --- a/cncnet-api/app/Http/Services/QuickMatchService.php +++ b/cncnet-api/app/Http/Services/QuickMatchService.php @@ -268,6 +268,15 @@ public function createQmMatch( $ladder = \App\Ladder::where('id', $qmPlayer->ladder_id)->first(); + $actualPlayerCount = count($otherQMQueueEntries) + 1; //total player counts equals myself plus other players to be matched + $expectedPlayerCount = $ladder->qmLadderRules->player_count; + if ($actualPlayerCount != $expectedPlayerCount) + { + Log::error("Only found $actualPlayerCount players, expected $expectedPlayerCount."); + Log::error(implode(",", $actualPlayerCount) . ", " . $qmPlayer->player->username); + // return null + } + # Create the Game $game = Game::genQmEntry($qmMatch, $gameType); $qmMatch->game_id = $game->id; @@ -279,7 +288,7 @@ public function createQmMatch( $qmMap = $qmMatch->map; $spawnOrder = explode(',', $qmMap->spawn_order); - if ($qmMap->random_spawns && $qmMap->map->spawn_count > 2) //this map uses random spawns + if ($qmMap->random_spawns && $qmMap->map->spawn_count > 2 && $expectedPlayerCount == 2) //this map uses 1v1 random spawns { $spawnOrder = []; $numSpawns = $qmMap->map->spawn_count; @@ -361,10 +370,13 @@ public function createQmMatch( # Set up player specific information # Color will be used for spawn location $qmPlayer = \App\QmMatchPlayer::where('id', $qmPlayer->id)->first(); + $colorsArr = getColorsArr($actualPlayerCount, $ladder->clans_allowed); + $i = 0; if (!$teamSpotsAssigned) //spots were team assigned { - $qmPlayer->color = 0; - $qmPlayer->location = $spawnOrder[$qmPlayer->color] - 1; + $qmPlayer->color = $colorsArr[$i]; + $qmPlayer->location = $spawnOrder[$i] - 1; + $i++; } $qmPlayer->qm_match_id = $qmMatch->id; $qmPlayer->tunnel_id = $qmMatch->seed + $qmPlayer->color; @@ -388,7 +400,6 @@ public function createQmMatch( return $s >= 0; })); - $color = 1; foreach ($otherQMQueueEntries as $qOpn) { $opn = $qOpn->qmPlayer; @@ -416,10 +427,11 @@ public function createQmMatch( $opn->actual_side = $perMS[mt_rand(0, count($perMS) - 1)]; } - if (!$teamSpotsAssigned) //spots were team assigned + if (!$teamSpotsAssigned) //spots were not team assigned { - $opn->color = $color++; - $opn->location = $spawnOrder[$opn->color] - 1; + $opn->color = $colorsArr[$i]; + $opn->location = $spawnOrder[$i] - 1; + $i++; } $opn->qm_match_id = $qmMatch->id; @@ -436,3 +448,37 @@ public function createQmMatch( return $qmMatch; } } + +/** + * Given $numPlayers, return an array of numbers, length of array = $numPlayers. + * + * Clan ladder matches should randomize the colors. 1v1 should use red vs yellow. + */ +function getColorsArr($numPlayers, $randomize) +{ + $possibleColors = [0, 1, 2, 3, 4, 5, 6, 7]; + + $colors = []; + $i = 0; + while (count($colors) != $numPlayers) + { + if ($randomize) //random colors + { + $randomColorIndex = mt_rand(0, count($possibleColors) - 1); + $randomColor = $possibleColors[$randomColorIndex]; + + if (!in_array($randomColor, $colors)) + $colors[] = $randomColor; + else + continue; + } + else //not random + { + $colors[] = $possibleColors[$i]; + } + + $i++; + } + + return $colors; +} From 089b780be602177429d9b561a2f94b64f8e99898 Mon Sep 17 00:00:00 2001 From: alexp8 Date: Mon, 22 May 2023 20:04:08 -0400 Subject: [PATCH 2/6] add some logs --- cncnet-api/app/Commands/Matchup/ClanMatchupHandler.php | 4 ++-- cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php | 4 ++-- cncnet-api/app/Http/Services/QuickMatchService.php | 4 ++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cncnet-api/app/Commands/Matchup/ClanMatchupHandler.php b/cncnet-api/app/Commands/Matchup/ClanMatchupHandler.php index 663a0e41..55e089d9 100644 --- a/cncnet-api/app/Commands/Matchup/ClanMatchupHandler.php +++ b/cncnet-api/app/Commands/Matchup/ClanMatchupHandler.php @@ -71,7 +71,7 @@ public function matchup() else { $playerNames = implode(",", $this->getPlayerNamesInQueue($readyQMQueueEntries)); - Log::info("Launching clan match with players $playerNames"); + Log::info("Launching clan match with players $playerNames, " . $currentPlayer->username); return $this->createMatch( $commonQmMaps, $readyQMQueueEntries @@ -80,7 +80,7 @@ public function matchup() } } - private function getPlayerNamesInQueue($readyQMQueueEntries) + public static function getPlayerNamesInQueue($readyQMQueueEntries) { $playerNames = []; diff --git a/cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php b/cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php index 4c3955ad..80d50a46 100644 --- a/cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php +++ b/cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php @@ -206,7 +206,7 @@ public function matchup() return !is_null($value); }); - Log::info("FindOpponent ** Recent played maps from player1 ($currentPlayer->username): $recentMaps"); + // Log::info("FindOpponent ** Recent played maps from player1 ($currentPlayer->username): $recentMaps"); foreach ($recentMaps as $recentMap) { @@ -235,7 +235,7 @@ public function matchup() return !is_null($value); }); - Log::info("FindOpponent ** Recent played maps from player2 ($oppPlayer->username): $recentMaps"); + // Log::info("FindOpponent ** Recent played maps from player2 ($oppPlayer->username): $recentMaps"); foreach ($recentMaps as $recentMap) //remove the opponent's recent maps from common_qm_maps { diff --git a/cncnet-api/app/Http/Services/QuickMatchService.php b/cncnet-api/app/Http/Services/QuickMatchService.php index 4a8c3f2c..a6899f3a 100644 --- a/cncnet-api/app/Http/Services/QuickMatchService.php +++ b/cncnet-api/app/Http/Services/QuickMatchService.php @@ -7,6 +7,7 @@ use App\QmMatchPlayer; use App\QmQueueEntry; use Illuminate\Support\Facades\Log; +use App\Commands\Matchup\ClanMatchupHandler; class QuickMatchService { @@ -445,6 +446,9 @@ public function createQmMatch( } $qmPlayer->save(); + $playerNames = implode(",", ClanMatchupHandler::getPlayerNamesInQueue($otherQMQueueEntries)); + Log::info("Launching match with players $playerNames, " . $qmPlayer->player->username . " on map: " . $qmMatch->map->name); + return $qmMatch; } } From 0895ebda2b42bfcb1980969de732b0cbfa047f1d Mon Sep 17 00:00:00 2001 From: alexp8 Date: Mon, 22 May 2023 20:16:55 -0400 Subject: [PATCH 3/6] testing --- cncnet-api/app/Http/Services/QuickMatchService.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cncnet-api/app/Http/Services/QuickMatchService.php b/cncnet-api/app/Http/Services/QuickMatchService.php index a6899f3a..9521e61f 100644 --- a/cncnet-api/app/Http/Services/QuickMatchService.php +++ b/cncnet-api/app/Http/Services/QuickMatchService.php @@ -372,8 +372,9 @@ public function createQmMatch( # Color will be used for spawn location $qmPlayer = \App\QmMatchPlayer::where('id', $qmPlayer->id)->first(); $colorsArr = getColorsArr($actualPlayerCount, $ladder->clans_allowed); + Log::info("Color values created: " . implode(",", $colorsArr)); $i = 0; - if (!$teamSpotsAssigned) //spots were team assigned + if (!$teamSpotsAssigned) //spots were not team assigned { $qmPlayer->color = $colorsArr[$i]; $qmPlayer->location = $spawnOrder[$i] - 1; From 907759c91c4d970e1851ca6a03d43aff05544ead Mon Sep 17 00:00:00 2001 From: alexp8 Date: Tue, 23 May 2023 15:42:44 -0400 Subject: [PATCH 4/6] refactor some variable names --- .../Http/Services/QuickMatchSpawnService.php | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/cncnet-api/app/Http/Services/QuickMatchSpawnService.php b/cncnet-api/app/Http/Services/QuickMatchSpawnService.php index 90eea447..8b5fc504 100644 --- a/cncnet-api/app/Http/Services/QuickMatchSpawnService.php +++ b/cncnet-api/app/Http/Services/QuickMatchSpawnService.php @@ -110,9 +110,8 @@ function ($var) */ public static function appendOthersAndTeamAlliancesToSpawnIni($spawnStruct, $qmPlayer, $otherQmPlayers) { - $otherIdx = 1; - $multiIdx = $qmPlayer->color + 1; - $myIndex = $multiIdx; + $otherIdx = 1; //create the [Other] objects, starting from 1 [Other1] + $myIndex = $qmPlayer->color + 1; $observerIndex = -1; # Checks if player is observer @@ -126,7 +125,7 @@ public static function appendOthersAndTeamAlliancesToSpawnIni($spawnStruct, $qmP Log::info("Setting $myPlayerUsername getting set as spectator"); } - $spawnStruct["spawn"]["SpawnLocations"]["Multi{$multiIdx}"] = $qmPlayer->location; + $spawnStruct["spawn"]["SpawnLocations"]["Multi{$myIndex}"] = $qmPlayer->location; if ($qmPlayer->player->user->userSettings->skip_score_screen) { @@ -158,8 +157,8 @@ public static function appendOthersAndTeamAlliancesToSpawnIni($spawnStruct, $qmP $observerIndex = $otherIdx; } - $multiIdx = $opn->color + 1; - $spawnStruct["spawn"]["SpawnLocations"]["Multi{$multiIdx}"] = $opn->location; + $opponentIndex = $opn->color + 1; + $spawnStruct["spawn"]["SpawnLocations"]["Multi{$otherIdx}"] = $opn->location; # Check if other player is in my clan, if so add alliance if ($qmPlayer->clan_id && $qmPlayer->clan_id == $opn->clan_id) @@ -168,9 +167,9 @@ public static function appendOthersAndTeamAlliancesToSpawnIni($spawnStruct, $qmP $p2Name = $opn->player->username; Log::info("PlayerIndex ** assigning $p1Name with $p2Name"); - $spawnStruct["spawn"]["Multi{$myIndex}_Alliances"]["HouseAllyOne"] = $multiIdx - 1; - $spawnStruct["spawn"]["Multi{$multiIdx}_Alliances"]["HouseAllyOne"] = $myIndex - 1; - $myTeamIndices[] = $multiIdx; + $spawnStruct["spawn"]["Multi{$myIndex}_Alliances"]["HouseAllyOne"] = $opponentIndex - 1; + $spawnStruct["spawn"]["Multi{$opponentIndex}_Alliances"]["HouseAllyOne"] = $myIndex - 1; + $myTeamIndices[] = $opponentIndex; } $otherIdx++; @@ -189,25 +188,25 @@ public static function appendOthersAndTeamAlliancesToSpawnIni($spawnStruct, $qmP $completed = false; foreach ($otherQmPlayers as $opn) { - $multiIdx = $opn->color + 1; + $opponent1Index = $opn->color + 1; - if (!in_array($multiIdx, $myTeamIndices)) //this index is opponent's team + if (!in_array($opponent1Index, $myTeamIndices)) //this index is opponent's team { foreach ($otherQmPlayers as $opn2) //find teammate(s) { - $otherIdx = $opn2->color + 1; + $opponent2Index = $opn2->color + 1; - if ($otherIdx == $multiIdx) //self + if ($otherIdx == $opponent1Index) //self continue; - if (!in_array($otherIdx, $myTeamIndices)) //this index is opponent's teammate + if (!in_array($opponent2Index, $myTeamIndices)) //this index is opponent's teammate { $p1Name = $opn->player->username; $p2Name = $opn2->player->username; Log::info("PlayerIndex ** assigning opponents $p1Name with $p2Name"); - $spawnStruct["spawn"]["Multi{$otherIdx}_Alliances"]["HouseAllyOne"] = $multiIdx - 1; - $spawnStruct["spawn"]["Multi{$multiIdx}_Alliances"]["HouseAllyOne"] = $otherIdx - 1; + $spawnStruct["spawn"]["Multi{$opponent2Index}_Alliances"]["HouseAllyOne"] = $opponent1Index - 1; + $spawnStruct["spawn"]["Multi{$opponent1Index}_Alliances"]["HouseAllyOne"] = $opponent2Index - 1; $completed = true; } } From 5af4d945688f71047ab1b64608791246443f0703 Mon Sep 17 00:00:00 2001 From: alexp8 Date: Sat, 27 May 2023 10:48:41 -0400 Subject: [PATCH 5/6] refactor getColorsArr() --- .../app/Http/Services/QuickMatchService.php | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/cncnet-api/app/Http/Services/QuickMatchService.php b/cncnet-api/app/Http/Services/QuickMatchService.php index 9521e61f..623a6561 100644 --- a/cncnet-api/app/Http/Services/QuickMatchService.php +++ b/cncnet-api/app/Http/Services/QuickMatchService.php @@ -463,27 +463,8 @@ function getColorsArr($numPlayers, $randomize) { $possibleColors = [0, 1, 2, 3, 4, 5, 6, 7]; - $colors = []; - $i = 0; - while (count($colors) != $numPlayers) - { - if ($randomize) //random colors - { - $randomColorIndex = mt_rand(0, count($possibleColors) - 1); - $randomColor = $possibleColors[$randomColorIndex]; - - if (!in_array($randomColor, $colors)) - $colors[] = $randomColor; - else - continue; - } - else //not random - { - $colors[] = $possibleColors[$i]; - } - - $i++; - } + if ($randomize) + shuffle($possibleColors); - return $colors; + return array_slice($possibleColors, 0, $numPlayers); } From 8656347ce7bf474cafe7ad5f8a61a0f85df06f6c Mon Sep 17 00:00:00 2001 From: alexp8 Date: Sat, 27 May 2023 11:04:00 -0400 Subject: [PATCH 6/6] commit --- cncnet-api/app/Http/Services/QuickMatchService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cncnet-api/app/Http/Services/QuickMatchService.php b/cncnet-api/app/Http/Services/QuickMatchService.php index 623a6561..e800cfff 100644 --- a/cncnet-api/app/Http/Services/QuickMatchService.php +++ b/cncnet-api/app/Http/Services/QuickMatchService.php @@ -371,7 +371,7 @@ public function createQmMatch( # Set up player specific information # Color will be used for spawn location $qmPlayer = \App\QmMatchPlayer::where('id', $qmPlayer->id)->first(); - $colorsArr = getColorsArr($actualPlayerCount, $ladder->clans_allowed); + $colorsArr = getColorsArr($actualPlayerCount, $ladder->clans_allowed && !$teamSpotsAssigned); Log::info("Color values created: " . implode(",", $colorsArr)); $i = 0; if (!$teamSpotsAssigned) //spots were not team assigned