|
4 | 4 |
|
5 | 5 | use App\Models\Game;
|
6 | 6 | use App\Models\QmQueueEntry;
|
| 7 | +use App\Models\QmLadderRules; |
7 | 8 | use Illuminate\Support\Collection;
|
8 | 9 | use Illuminate\Support\Facades\Log;
|
9 | 10 |
|
@@ -36,7 +37,10 @@ public function matchup(): void
|
36 | 37 |
|
37 | 38 | // Find opponents that can be matched with current player.
|
38 | 39 | $matchableOpponents = $this->quickMatchService->getEntriesInPointRange($this->qmQueueEntry, $matchableOpponents);
|
39 |
| - |
| 40 | + |
| 41 | + // TESTING new 2v2 matching logic |
| 42 | + $this->testNew2v2Logic($this->qmQueueEntry, $matchableOpponents); |
| 43 | + |
40 | 44 | $opponentCount = $matchableOpponents->count();
|
41 | 45 | Log::debug("FindOpponent ** inQueue={$playerInQueue}, amount of matchable opponent after point filter: {$opponentCount} of {$count}");
|
42 | 46 |
|
@@ -108,4 +112,206 @@ private function createTeamMatch(Collection $maps, Collection $teamAPlayers, Col
|
108 | 112 | $stats
|
109 | 113 | );
|
110 | 114 | }
|
| 115 | + |
| 116 | + /** |
| 117 | + * Solely used for testing function `getEntriesInPointRange2v2()` in production |
| 118 | + * Result of function is logged out. |
| 119 | + * Errors are caught and then logged out. |
| 120 | + */ |
| 121 | + public function testNew2v2Logic(QmQueueEntry $currentQmQueueEntry, Collection $opponents) |
| 122 | + { |
| 123 | + try |
| 124 | + { |
| 125 | + $match2v2 = $this->getEntriesInPointRange2v2($currentQmQueueEntry, $opponents); |
| 126 | + |
| 127 | + Log::debug( |
| 128 | + "2v2 matching result for {$currentQmQueueEntry->qmPlayer?->player?->username}: " . |
| 129 | + ($match2v2->isNotEmpty() |
| 130 | + ? 'MATCH FOUND: ' . $match2v2->pluck('qmPlayer.player.username')->implode(', ') |
| 131 | + : 'No match found') |
| 132 | + ); |
| 133 | + } |
| 134 | + catch (\Throwable $e) |
| 135 | + { |
| 136 | + Log::error("Error during TEST 2v2 matchmaking for queueEntry {$currentQmQueueEntry->id}: {$e->getMessage()}", [ |
| 137 | + 'trace' => $e->getTraceAsString() |
| 138 | + ]); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Attempt to find a valid 2v2 match for the given player. |
| 144 | + * Ensures that: |
| 145 | + * - The current player is always included in the match. |
| 146 | + * - All players in the match are within each other's allowed point range. |
| 147 | + * - The result is sorted by closeness in point range to improve match fairness. |
| 148 | + * |
| 149 | + * @param QmQueueEntry $currentQmQueueEntry |
| 150 | + * @param Collection|QmQueueEntry[] $opponents |
| 151 | + * @return Collection|QmQueueEntry[] A collection containing the matched players (including current), or empty if no match found |
| 152 | + */ |
| 153 | + public function getEntriesInPointRange2v2(QmQueueEntry $currentQmQueueEntry, Collection $opponents): Collection |
| 154 | + { |
| 155 | + $rules = $currentQmQueueEntry->ladderHistory->ladder->qmLadderRules; |
| 156 | + $playerName = $currentQmQueueEntry->qmPlayer?->player?->username ?? 'Unknown'; |
| 157 | + |
| 158 | + Log::debug("2v2 Search start: queueEntry={$currentQmQueueEntry->id}, name={$playerName}, opponentsInQueue=" . count($opponents)); |
| 159 | + |
| 160 | + // Filter opponents to those that pass point range check with the current player |
| 161 | + $potentialOpponents = $this->filterOpponentsInRange($currentQmQueueEntry, $opponents, $rules); |
| 162 | + |
| 163 | + Log::debug("Potential opponents for {$playerName}: " . $potentialOpponents->pluck('qmPlayer.player.username')->implode(', ')); |
| 164 | + |
| 165 | + if ($potentialOpponents->count() < 3) |
| 166 | + { |
| 167 | + Log::debug("Not enough valid opponents for {$playerName} to form a 2v2 match"); |
| 168 | + return collect(); |
| 169 | + } |
| 170 | + |
| 171 | + // Sort opponents by closeness in points to the current player |
| 172 | + $sortedOpponents = $potentialOpponents->sortBy(function ($opponent) use ($currentQmQueueEntry) |
| 173 | + { |
| 174 | + return abs($currentQmQueueEntry->points - $opponent->points); |
| 175 | + })->values(); |
| 176 | + |
| 177 | + // Try all possible 3-player combinations (since the current player makes 4) |
| 178 | + foreach ($sortedOpponents->combinations(3) as $threeOthers) |
| 179 | + { |
| 180 | + $matchPlayers = collect([$currentQmQueueEntry])->merge($threeOthers); |
| 181 | + |
| 182 | + if ($this->allPlayersInRange($matchPlayers, $rules)) |
| 183 | + { |
| 184 | + Log::debug( |
| 185 | + "✅ Found valid 2v2 match for {$playerName}: " . |
| 186 | + $matchPlayers->pluck('qmPlayer.player.username')->implode(', ') |
| 187 | + ); |
| 188 | + return $matchPlayers; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + Log::debug("❌ No valid 2v2 match found for {$playerName}"); |
| 193 | + return collect(); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Filter opponents that are within point range of the current player. |
| 198 | + * |
| 199 | + * @param QmQueueEntry $current |
| 200 | + * @param Collection|QmQueueEntry[] $opponents |
| 201 | + * @param QmLadderRules $rules |
| 202 | + * @return Collection|QmQueueEntry[] |
| 203 | + */ |
| 204 | + private function filterOpponentsInRange(QmQueueEntry $current, Collection $opponents, QmLadderRules $rules): Collection |
| 205 | + { |
| 206 | + $pointsPerSecond = $rules->points_per_second; |
| 207 | + $maxPointsDifference = $rules->max_points_difference; |
| 208 | + $currentPointFilter = $current->qmPlayer->player->user->userSettings->disabledPointFilter; |
| 209 | + |
| 210 | + $matchable = collect(); |
| 211 | + |
| 212 | + foreach ($opponents as $opponent) |
| 213 | + { |
| 214 | + if (!isset($opponent->qmPlayer) || $opponent->qmPlayer->isObserver()) |
| 215 | + { |
| 216 | + continue; |
| 217 | + } |
| 218 | + |
| 219 | + $diff = abs($current->points - $opponent->points); |
| 220 | + $waitTimeBonus = (strtotime($current->updated_at) - strtotime($current->created_at)) * $pointsPerSecond; |
| 221 | + |
| 222 | + $inNormalRange = $waitTimeBonus + $maxPointsDifference > $diff; |
| 223 | + $inDisabledFilterRange = $currentPointFilter |
| 224 | + && $opponent->qmPlayer->player->user->userSettings->disabledPointFilter |
| 225 | + && $diff < 1000 |
| 226 | + && $current->points > 400 |
| 227 | + && $opponent->points > 400; |
| 228 | + |
| 229 | + if ($inNormalRange || $inDisabledFilterRange) |
| 230 | + { |
| 231 | + $matchable->push($opponent); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + return $matchable; |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Check if all players in the given collection are within point range of each other. |
| 240 | + * Logs a pass/fail table for each comparison. |
| 241 | + * |
| 242 | + * @param Collection|QmQueueEntry[] $players |
| 243 | + * @param QmLadderRules $rules |
| 244 | + * @return bool |
| 245 | + */ |
| 246 | + private function allPlayersInRange(Collection $players, QmLadderRules $rules): bool |
| 247 | + { |
| 248 | + $pointsPerSecond = $rules->points_per_second; |
| 249 | + $maxPointsDifference = $rules->max_points_difference; |
| 250 | + $comparisonResults = []; |
| 251 | + |
| 252 | + foreach ($players as $i => $p1) |
| 253 | + { |
| 254 | + foreach ($players as $j => $p2) |
| 255 | + { |
| 256 | + if ($i >= $j) continue; // Skip self and duplicate checks |
| 257 | + |
| 258 | + $diff = abs($p1->points - $p2->points); |
| 259 | + $waitTimeBonusP1 = (strtotime($p1->updated_at) - strtotime($p1->created_at)) * $pointsPerSecond; |
| 260 | + $waitTimeBonusP2 = (strtotime($p2->updated_at) - strtotime($p2->created_at)) * $pointsPerSecond; |
| 261 | + |
| 262 | + $passesNormalRange = ($waitTimeBonusP1 + $maxPointsDifference >= $diff) |
| 263 | + || ($waitTimeBonusP2 + $maxPointsDifference >= $diff); |
| 264 | + |
| 265 | + $passesDisabledFilter = $p1->qmPlayer->player->user->userSettings->disabledPointFilter |
| 266 | + && $p2->qmPlayer->player->user->userSettings->disabledPointFilter |
| 267 | + && $diff < 1000 |
| 268 | + && $p1->points > 400 |
| 269 | + && $p2->points > 400; |
| 270 | + |
| 271 | + $pass = $passesNormalRange || $passesDisabledFilter; |
| 272 | + |
| 273 | + $comparisonResults[] = [ |
| 274 | + 'p1' => $p1->qmPlayer?->player?->username ?? 'Unknown', |
| 275 | + 'p2' => $p2->qmPlayer?->player?->username ?? 'Unknown', |
| 276 | + 'points1' => $p1->points, |
| 277 | + 'points2' => $p2->points, |
| 278 | + 'diff' => $diff, |
| 279 | + 'pass' => $pass |
| 280 | + ]; |
| 281 | + |
| 282 | + if (!$pass) |
| 283 | + { |
| 284 | + $this->logComparisonTable($comparisonResults); |
| 285 | + return false; |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + $this->logComparisonTable($comparisonResults); |
| 291 | + return true; |
| 292 | + } |
| 293 | + |
| 294 | + /** |
| 295 | + * Logs the comparison results for all player pairs. |
| 296 | + * |
| 297 | + * @param array $comparisonResults |
| 298 | + */ |
| 299 | + private function logComparisonTable(array $comparisonResults): void |
| 300 | + { |
| 301 | + Log::debug("=== 2v2 Player Comparison Table ==="); |
| 302 | + foreach ($comparisonResults as $result) |
| 303 | + { |
| 304 | + $status = $result['pass'] ? '✅ PASS' : '❌ FAIL'; |
| 305 | + Log::debug(sprintf( |
| 306 | + "%-15s (%4d pts) ↔ %-15s (%4d pts) | Diff: %4d | %s", |
| 307 | + $result['p1'], |
| 308 | + $result['points1'], |
| 309 | + $result['p2'], |
| 310 | + $result['points2'], |
| 311 | + $result['diff'], |
| 312 | + $status |
| 313 | + )); |
| 314 | + } |
| 315 | + Log::debug("==================================="); |
| 316 | + } |
111 | 317 | }
|
0 commit comments