Skip to content

Commit a8efbc0

Browse files
authored
Merge pull request #433 from CnCNet/limit-random-side-pick
Limit random side pick to all_sides.
2 parents f681ea3 + 99cafee commit a8efbc0

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

cncnet-api/app/Http/Services/QuickMatchService.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,14 +633,17 @@ public function createQmAIMatch($qmPlayer, $userPlayerTier, $maps, $gameType)
633633

634634
$qmPlayer->save();
635635

636-
$perMS = array_values(array_filter($qmMap->sides_array(), function ($s)
636+
$mapSides = array_values(array_filter($qmMap->sides_array(), fn($s) => $s >= 0));
637+
$ladderSides = $qmMatch->ladder->qmLadderRules->all_sides();
638+
$allowedForRandom = array_values(array_intersect($mapSides, $ladderSides));
639+
if (empty($allowedForRandom))
637640
{
638-
return $s >= 0;
639-
}));
641+
$allowedForRandom = $mapSides;
642+
}
640643

641644
if ($qmPlayer->actual_side == -1)
642645
{
643-
$qmPlayer->actual_side = $perMS[mt_rand(0, count($perMS) - 1)];
646+
$qmPlayer->actual_side = $allowedForRandom[mt_rand(0, count($allowedForRandom) - 1)];
644647
}
645648
$qmPlayer->save();
646649

@@ -1102,10 +1105,19 @@ public function createQmMatch(
11021105
}
11031106
$qmPlayerFresh->save();
11041107

1105-
$perMS = array_values(array_filter($qmMap->sides_array(), function ($s)
1108+
$mapSides = array_values(array_filter($qmMap->sides_array(), function ($s)
11061109
{
11071110
return $s >= 0;
11081111
}));
1112+
$ladderSides = $ladder->qmLadderRules->all_sides();
1113+
$perMS = array_values(array_intersect($mapSides, $ladderSides));
1114+
1115+
if (empty($perMS))
1116+
{
1117+
// Fallback: if intersection of map sides and allowed ladder sides is empty, use map sides.
1118+
Log::warning('No intersection between map sides and ladder sides');
1119+
$perMS = $mapSides;
1120+
}
11091121

11101122
if ($qmPlayerFresh->isObserver() == true)
11111123
{
@@ -1239,7 +1251,15 @@ private function setTeamSpawns(string $team, string $spawnOrders, Collection $te
12391251

12401252
Log::debug('[QuickMatchService::setTeamSpawns] $spawnOrder ' . json_encode($spawnOrder));
12411253

1242-
$mapAllowedSides = array_values(array_filter($qmMap->sides_array(), fn($s) => $s >= 0));
1254+
$mapSides = array_values(array_filter($qmMap->sides_array(), fn($s) => $s >= 0));
1255+
$ladderSides = $qmMatch->ladder->qmLadderRules->all_sides();
1256+
$allowedForRandom = array_values(array_intersect($mapSides, $ladderSides));
1257+
if (empty($allowedForRandom))
1258+
{
1259+
// Fallback: if intersection of map sides and allowed ladder sides is empty, use map sides.
1260+
Log::warning('No intersection between map sides and ladder sides');
1261+
$allowedForRandom = $mapSides;
1262+
}
12431263

12441264
foreach ($teamPlayers->values() as $i => $player)
12451265
{
@@ -1267,7 +1287,7 @@ private function setTeamSpawns(string $team, string $spawnOrders, Collection $te
12671287

12681288
if ($qmPlayer->actual_side == -1)
12691289
{
1270-
$qmPlayer->actual_side = $mapAllowedSides[mt_rand(0, count($mapAllowedSides) - 1)];
1290+
$qmPlayer->actual_side = $allowedForRandom[mt_rand(0, count($allowedForRandom) - 1)];
12711291
}
12721292

12731293
$qmPlayer->qm_match_id = $qmMatch->id;

cncnet-api/app/Models/QmLadderRules.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,28 @@ public function mapPools()
7474

7575
public function all_sides()
7676
{
77-
return explode(',', $this->id ? $this->all_sides : "");
77+
$raw = $this->id ? (string) $this->all_sides : '';
78+
79+
if (trim($raw) === '')
80+
{
81+
return [];
82+
}
83+
84+
$parts = explode(',', $raw);
85+
$result = [];
86+
87+
// Make sure to get an int array.
88+
foreach ($parts as $p)
89+
{
90+
$p = trim($p);
91+
if ($p === '')
92+
{
93+
continue; // Do not cast an empty part to 0.
94+
}
95+
$result[] = (int) $p;
96+
}
97+
98+
return $result;
7899
}
79100

80101
public function allowed_sides()

0 commit comments

Comments
 (0)