===== LINHAS 2435 A 2480 ===== 2435: return [ 2436: 'success' => false, 2437: 'message' => '⛔ Voce ja esta em uma partida/lobby ativo e nao pode pegar outra fila.', 2438: 'queue_mode' => $this->normalizeQueueMode($activeMatch->queue_mode ?? $queueMode), 2439: 'match_found' => true, 2440: 'match_id' => (int) $activeMatch->id, 2441: 'lobby_id' => (int) $activeMatch->id, 2442: 'redirect_url' => route('lobby.index'), 2443: ]; 2444: } 2445: } 2446: 2447: $existingSql = " 2448: SELECT TOP 1 q.id, q.queue_number, q.queue_mode, qp.position 2449: FROM mix_queue_players qp WITH (NOLOCK) 2450: JOIN mix_queues q WITH (NOLOCK) ON qp.queue_id = q.id 2451: WHERE qp.user_id = ? 2452: AND q.status = 'waiting' 2453: AND q.queue_mode = ? 2454: AND (q.game_slug = ? OR (? = 'crossfire' AND (q.game_slug IS NULL OR q.game_slug = ''))) 2455: ORDER BY qp.id DESC 2456: "; 2457: $existingInQueue = DB::selectOne($existingSql, [$userId, $queueMode, $gameSlug, $gameSlug]); 2458: 2459: if ($existingInQueue) { 2460: $existingCount = (int) DB::table('mix_queue_players') 2461: ->where('queue_id', (int) $existingInQueue->id) 2462: ->count(); 2463: 2464: if ($existingCount >= self::MAX_QUEUE_SIZE) { 2465: $lobbyId = $this->createLobbyFromQueue((int) $existingInQueue->id); 2466: if ($lobbyId) { 2467: return [ 2468: 'success' => true, 2469: 'message' => '✅ Partida encontrada! O HUB Lobby está aguardando você.', 2470: 'queue_id' => (int) $existingInQueue->id, 2471: 'queue_number' => (int) $existingInQueue->queue_number, 2472: 'queue_mode' => $queueMode, 2473: 'lobby_created' => true, 2474: 'match_found' => true, 2475: 'match_id' => $lobbyId, 2476: 'lobby_id' => $lobbyId, 2477: 'redirect_url' => route('lobby.index'), 2478: ]; 2479: } 2480: } ===== LINHAS 2540 A 2610 ===== 2540: 'match_found' => true, 2541: 'match_id' => $lobbyId, 2542: 'lobby_id' => $lobbyId, 2543: 'redirect_url' => route('lobby.index'), 2544: ]; 2545: } 2546: 2547: // Se a fila cheia não gerou lobby, cria uma nova para não travar o jogador. 2548: $queue = $this->createNewQueue($gameSlug, $queueMode); 2549: $currentCount = 0; 2550: } 2551: 2552: $position = $currentCount + 1; 2553: 2554: // Remove sobras antigas do mesmo usuário em qualquer fila waiting do mesmo jogo/modo. 2555: DB::delete(" 2556: DELETE qp 2557: FROM mix_queue_players qp 2558: JOIN mix_queues q ON q.id = qp.queue_id 2559: WHERE qp.user_id = ? 2560: AND q.status = 'waiting' 2561: AND q.queue_mode = ? 2562: AND (q.game_slug = ? OR (? = 'crossfire' AND (q.game_slug IS NULL OR q.game_slug = ''))) 2563: ", [$userId, $queueMode, $gameSlug, $gameSlug]); 2564: 2565: DB::table('mix_queue_players')->insert([ 2566: 'queue_id' => (int) $queue->id, 2567: 'user_id' => $userId, 2568: 'position' => $position, 2569: 'joined_at' => Carbon::now(), 2570: 'queue_mode' => $queueMode, 2571: ]); 2572: 2573: // GIGAX LIVE V12.5A: notificacao best-effort, sem afetar a fila. 2574: $this->gigaxLiveQueueChanged($queueMode, (int) $queue->id); 2575: 2576: // PRIORIDADE: completa uma composição 9/10 existente antes de criar outra. 2577: $refill = $this->refillOldestWaitingMatchFromQueue($gameSlug, $queueMode); 2578: 2579: if ($refill && (int) ($refill['replacement_user_id'] ?? 0) === $userId) { 2580: return [ 2581: 'success' => true, 2582: 'message' => '✅ Você completou uma composição em READY. Confirme para liberar 10/10.', 2583: 'queue_id' => (int) $queue->id, 2584: 'queue_number' => (int) $queue->queue_number, 2585: 'queue_mode' => $queueMode, 2586: 'lobby_created' => false, 2587: 'match_found' => true, 2588: 'ready_replacement' => true, 2589: 'match_id' => (int) $refill['match_id'], 2590: 'lobby_id' => (int) $refill['match_id'], 2591: 'redirect_url' => route('lobby.index'), 2592: ]; 2593: } 2594: 2595: $updatedCount = (int) DB::table('mix_queue_players') 2596: ->where('queue_id', (int) $queue->id) 2597: ->count(); 2598: 2599: $lobbyId = null; 2600: if ($updatedCount >= self::MAX_QUEUE_SIZE) { 2601: $lobbyId = $this->createLobbyFromQueue((int) $queue->id); 2602: } 2603: 2604: return [ 2605: 'success' => true, 2606: 'message' => $lobbyId ? '✅ Partida encontrada! O HUB Lobby está aguardando você.' : "✅ Entrou na Fila #{$queue->queue_number}! Posição: {$position}/" . self::MAX_QUEUE_SIZE, 2607: 'queue_id' => (int) $queue->id, 2608: 'queue_number' => (int) $queue->queue_number, 2609: 'queue_mode' => $queueMode, 2610: 'position' => (int) $position, ===== LINHAS 3970 A 4050 ===== 3970: private function gigaxLiveQueueChanged(string $mode, ?int $queueId = null): void 3971: { 3972: try { 3973: if (!\App\Services\GigaxLivePublisher::queueChanged($mode, $queueId)) { 3974: // GIGAX LIVE V12.9: log false results; no queue dependency on Node. 3975: } 3976: } catch (\Throwable $ignored) { 3977: // A fila deve continuar operando se o push estiver indisponivel. 3978: } 3979: } 3980: 3981: private function removeFromQueue(int $userId, $queueId = null): array 3982: { 3983: try { 3984: // FAST MODE: saída imediata. Remove direto sem transaction pesada. 3985: $params = [$userId]; 3986: $whereQueue = ''; 3987: 3988: if ($queueId && is_numeric($queueId)) { 3989: $whereQueue = ' AND qp.queue_id = ?'; 3990: $params[] = (int) $queueId; 3991: } 3992: 3993: $player = DB::selectOne(" 3994: SELECT TOP 1 qp.id, qp.queue_id, qp.position, q.queue_number, q.queue_mode 3995: FROM mix_queue_players qp WITH (NOLOCK) 3996: JOIN mix_queues q WITH (NOLOCK) ON qp.queue_id = q.id 3997: WHERE qp.user_id = ? {$whereQueue} AND q.status = 'waiting' 3998: ORDER BY qp.id DESC 3999: ", $params); 4000: 4001: if (!$player) { 4002: return [ 4003: 'success' => true, 4004: 'message' => 'Você já está fora da fila.', 4005: 'queue_id' => $queueId ? (int) $queueId : null, 4006: 'already_out' => true, 4007: 'instant' => true, 4008: ]; 4009: } 4010: 4011: DB::delete('DELETE FROM mix_queue_players WHERE id = ?', [(int) $player->id]); 4012: 4013: // GIGAX LIVE V12.5A: saida efetiva; falha no push nao altera o resultado. 4014: $this->gigaxLiveQueueChanged((string) ($player->queue_mode ?? ''), (int) $player->queue_id); 4015: 4016: // Reordena em background leve. Se falhar, não trava saída do jogador. 4017: try { 4018: DB::statement("; 4019: WITH Ordered AS ( 4020: SELECT id, ROW_NUMBER() OVER (ORDER BY position ASC, id ASC) AS rn 4021: FROM mix_queue_players 4022: WHERE queue_id = ? 4023: ) 4024: UPDATE qp 4025: SET position = Ordered.rn 4026: FROM mix_queue_players qp 4027: JOIN Ordered ON Ordered.id = qp.id 4028: ", [(int) $player->queue_id]); 4029: } catch (\Throwable $ignored) {} 4030: 4031: return [ 4032: 'success' => true, 4033: 'message' => "✅ Saiu da Fila #{$player->queue_number}", 4034: 'queue_id' => (int) $player->queue_id, 4035: 'instant' => true, 4036: ]; 4037: } catch (\Throwable $e) { 4038: Log::error('Erro ao remover da fila', ['user_id' => $userId, 'error' => $e->getMessage()]); 4039: return ['success' => false, 'message' => 'Erro ao sair da fila: ' . $e->getMessage()]; 4040: } 4041: } 4042: 4043: private function isUserInActiveMatch(int $userId): bool 4044: { 4045: return DB::table('mix_match_players') 4046: ->where('user_id', $userId) 4047: ->whereExists(function ($query) { 4048: $query->select(DB::raw(1)) 4049: ->from('mix_active_matches') 4050: ->whereColumn('mix_active_matches.id', 'mix_match_players.match_id')