@@ -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 ;
0 commit comments