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

Feature/random colors #243

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions cncnet-api/app/Commands/Matchup/ClanMatchupHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -80,7 +80,7 @@ public function matchup()
}
}

private function getPlayerNamesInQueue($readyQMQueueEntries)
public static function getPlayerNamesInQueue($readyQMQueueEntries)
{
$playerNames = [];

Expand Down
4 changes: 2 additions & 2 deletions cncnet-api/app/Commands/Matchup/PlayerMatchupHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
{
Expand Down
13 changes: 8 additions & 5 deletions cncnet-api/app/Http/Controllers/ClanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
48 changes: 40 additions & 8 deletions cncnet-api/app/Http/Services/QuickMatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\QmMatchPlayer;
use App\QmQueueEntry;
use Illuminate\Support\Facades\Log;
use App\Commands\Matchup\ClanMatchupHandler;

class QuickMatchService
{
Expand Down Expand Up @@ -268,6 +269,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;
Expand All @@ -279,7 +289,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;
Expand Down Expand Up @@ -361,10 +371,14 @@ public function createQmMatch(
# Set up player specific information
# Color will be used for spawn location
$qmPlayer = \App\QmMatchPlayer::where('id', $qmPlayer->id)->first();
if (!$teamSpotsAssigned) //spots were team assigned
$colorsArr = getColorsArr($actualPlayerCount, $ladder->clans_allowed && !$teamSpotsAssigned);
Log::info("Color values created: " . implode(",", $colorsArr));
$i = 0;
if (!$teamSpotsAssigned) //spots were not 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;
Expand All @@ -388,7 +402,6 @@ public function createQmMatch(
return $s >= 0;
}));

$color = 1;
foreach ($otherQMQueueEntries as $qOpn)
{
$opn = $qOpn->qmPlayer;
Expand Down Expand Up @@ -416,10 +429,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;
Expand All @@ -433,6 +447,24 @@ 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;
}
}

/**
* 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];

if ($randomize)
shuffle($possibleColors);

return array_slice($possibleColors, 0, $numPlayers);
}
31 changes: 15 additions & 16 deletions cncnet-api/app/Http/Services/QuickMatchSpawnService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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++;
Expand All @@ -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;
}
}
Expand Down