@extends('layouts.app') @push('styles') @endpush @section('title', 'Dashboard - GIGAX ARENA') @section('page_title', 'Visão Geral') @section('content') @php use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Carbon; $user = Auth::user(); if (!$user) { $sid = (int) session('user_id', 0); if ($sid > 0) { try { $user = \App\Models\User::find($sid); } catch (\Throwable $e) { $user = null; } } } $userId = (int) ($user->id ?? session('user_id', 0)); if ($userId > 0) { try { $fresh = DB::table('users')->where('id', $userId)->first(); if ($fresh) $user = $fresh; } catch (\Throwable $e) {} } $name = $kruxName ?? $user->nickname ?? $user->name ?? session('user_nickname', 'Jogador'); $go = static fn(string $route, string $fallback) => Route::has($route) ? route($route) : url($fallback); // ESTATÍSTICAS REAIS: histórico finalizado + stats do próprio jogador. $matches = 0; $wins = 0; $losses = 0; $kills = 0; $deaths = 0; $assists = 0; $headshots = 0; try { if ($userId > 0 && Schema::hasTable('mix_training_history') && Schema::hasTable('mix_training_player_stats')) { $st = DB::selectOne("\n SELECT\n COUNT(*) AS matches,\n SUM(CASE WHEN th.winning_team = ps.team THEN 1 ELSE 0 END) AS wins,\n SUM(CASE WHEN th.winning_team <> ps.team THEN 1 ELSE 0 END) AS losses,\n SUM(ISNULL(ps.kills,0)) AS kills,\n SUM(ISNULL(ps.deaths,0)) AS deaths,\n SUM(ISNULL(ps.assists,0)) AS assists\n FROM mix_training_history th\n INNER JOIN mix_training_player_stats ps ON ps.match_id = th.match_id\n WHERE ps.user_id = ?\n ", [$userId]); $matches=(int)($st->matches??0); $wins=(int)($st->wins??0); $losses=(int)($st->losses??0); $kills=(int)($st->kills??0); $deaths=(int)($st->deaths??0); $assists=(int)($st->assists??0); } else { $matches=(int)($user->total_matches??0); $wins=(int)($user->wins??0); $losses=(int)($user->losses??0); $kills=(int)($user->kills??0); $deaths=(int)($user->deaths??0); $assists=(int)($user->assists??0); } $headshots=(int)($user->headshots??0); } catch (\Throwable $e) {} $winRateValue = $matches > 0 ? (($wins/max(1,$matches))*100) : 0; $winRate = number_format($winRateValue,1,',','.') . '%'; $kd = $deaths > 0 ? number_format($kills/$deaths,2,'.','') : number_format($kills,2,'.',''); $hsRate = $kills > 0 ? number_format(($headshots/$kills)*100,1,',','.') . '%' : '0%'; // XP/nível vêm de uma única fonte; conta nova ou reset = nível 1 / 0 XP. $level = 1; $xpValue = 0; $xpMaxValue = 10000; try { if ($userId > 0 && Schema::hasTable('gx_player_experience')) { $xpRow = DB::table('gx_player_experience')->where('user_id',$userId)->first(); if ($xpRow) { $level=(int)($xpRow->level??1); $xpValue=(int)($xpRow->monthly_xp??0); } } else { $level=(int)($user->skill_level??1); } } catch (\Throwable $e) {} $level=max(1,$level); $xpValue=max(0,$xpValue); $xpPercent=$xpMaxValue>0?min(100,max(0,($xpValue/$xpMaxValue)*100)):0; // Presença real do AC e versão publicada. $publishedAcVersion='—'; try { $vf=public_path('Atualizacoes/versao.txt'); if (is_file($vf)) $publishedAcVersion=trim((string)file_get_contents($vf)) ?: '—'; } catch (\Throwable $e) {} $acOnline=false; $heartbeatOk=false; $liveAcPlayers=collect(); $onlineCount=0; try { $acSummary=app(\App\Services\GigaxAcPresenceService::class)->summary(); $liveAcPlayers=collect($acSummary['players']??[])->take(5)->values(); $onlineCount=(int)($acSummary['online_count']??$liveAcPlayers->count()); $meAc=$liveAcPlayers->first(fn($p)=>(int)($p->uid??$p->user_id??0)===$userId); $acOnline=(bool)($verificationSummary['ac_online']??($meAc?true:false)); $heartbeatOk=(bool)($verificationSummary['heartbeat_ok']??$acOnline); } catch (\Throwable $e) { $acOnline=(bool)($verificationSummary['ac_online']??false); $heartbeatOk=(bool)($verificationSummary['heartbeat_ok']??false); } // Fila: apenas filas waiting e posição do jogador atual. $queueCount=0; $queuePosition=0; $queueMode='5v5 Competitivo'; try { if (Schema::hasTable('mix_queue_players') && Schema::hasTable('mix_queues')) { $queueCount=(int)DB::table('mix_queue_players as qp')->join('mix_queues as q','q.id','=','qp.queue_id')->where('q.status','waiting')->count(); if ($userId>0) { $uq=DB::table('mix_queue_players as qp')->join('mix_queues as q','q.id','=','qp.queue_id')->where('q.status','waiting')->where('qp.user_id',$userId)->select('qp.position')->orderByDesc('qp.id')->first(); $queuePosition=(int)($uq->position??0); } } } catch (\Throwable $e) {} $queueEta=$queueCount>=10?'Pronta':'Aguardando'; // GIGAX V12: usa as mesmas tabelas e status do LobbyController::index. // Mostra salas abertas mesmo quando o visitante nao participa delas. $dashboardLobbies = collect(); $dashboardLiveCount = 0; try { if (Schema::hasTable('mix_training_matches') && Schema::hasTable('mix_training_participants')) { $lobbyPlayerCounts = DB::table('mix_training_participants') ->select('training_match_id', DB::raw('COUNT(*) AS current_players')) ->whereNull('left_at') ->groupBy('training_match_id'); $dashboardLobbyBase = DB::table('mix_training_matches as m') ->leftJoinSub($lobbyPlayerCounts, 'pc', function ($join) { $join->on('pc.training_match_id', '=', 'm.id'); }) ->whereIn('m.status', ['waiting', 'ready_check', 'veto', 'in_progress']) ->select('m.id', 'm.title', 'm.training_type', 'm.map_name', 'm.status', 'm.max_players', 'm.created_at', 'm.started_at', DB::raw('COALESCE(pc.current_players, 0) AS current_players')); $dashboardLobbies = (clone $dashboardLobbyBase) ->orderByRaw("CASE WHEN m.status = 'in_progress' THEN 0 WHEN m.status = 'ready_check' THEN 1 WHEN m.status = 'veto' THEN 2 ELSE 3 END") ->orderByDesc('m.created_at') ->limit(5) ->get(); $dashboardLiveCount = (clone $dashboardLobbyBase) ->where('m.status', 'in_progress')->count(); } } catch (\Throwable $e) { // Falha de consulta nao deve impedir a abertura do dashboard. $dashboardLobbies = collect(); $dashboardLiveCount = 0; } // Ranking mensal real: só entra quem possui XP mensal > 0. Sem atividade = ranking vazio, não ranking falso por ID. $ranking=collect(); try { if (Schema::hasTable('gx_player_experience') && Schema::hasTable('users')) { $ranking=DB::table('gx_player_experience as x')->join('users as u','u.id','=','x.user_id') ->where('x.monthly_xp','>',0)->orderByDesc('x.monthly_xp')->orderBy('u.nickname') ->limit(5)->get(['u.id as user_id','u.nickname','u.avatar_url','x.monthly_xp as score','x.synced_wins as wins']); } } catch (\Throwable $e) {} // Youtubers cadastrados pelo painel Admin. Se a tabela ainda não existir, mantém compatibilidade com dados antigos. $streams=collect(); try { if (Schema::hasTable('gigax_youtubers')) { $streams=DB::table('gigax_youtubers') ->where('active',1) ->where('is_live',1) ->orderBy('sort_order') ->orderBy('channel_name') ->limit(4) ->get(); } else { $streams=collect($youtubeLives??$liveStreams??$streamersLive??[])->take(8); } } catch (\Throwable $e) { $streams=collect($youtubeLives??$liveStreams??$streamersLive??[])->take(8); } // Dashboard V34: escalacoes reais da ultima partida e das salas selecionaveis. $gxLineups = []; $gxLastMatch = null; try { if (Schema::hasTable('mix_training_matches')) { $gxMatchIds = $dashboardLobbies->pluck('id')->map(fn($v)=>(int)$v)->all(); $gxLastMatch = DB::table('mix_training_matches') ->whereIn('status', ['finished','completed','finalized','ended','in_progress']) ->orderByDesc('id')->first(); if ($gxLastMatch) $gxMatchIds[] = (int)$gxLastMatch->id; $gxMatchIds = array_values(array_unique($gxMatchIds)); if ($gxMatchIds && Schema::hasTable('mix_training_participants')) { $gxPcols = Schema::getColumnListing('mix_training_participants'); $gxUcols = Schema::hasTable('users') ? Schema::getColumnListing('users') : []; $gxPquery = DB::table('mix_training_participants as p')->whereIn('p.training_match_id',$gxMatchIds); if (in_array('left_at',$gxPcols)) $gxPquery->whereNull('p.left_at'); if (in_array('user_id',$gxPcols) && $gxUcols) { $gxPquery->leftJoin('users as u','u.id','=','p.user_id'); $gxPquery->addSelect('u.id as gx_user_id'); if (in_array('nickname',$gxUcols)) $gxPquery->addSelect('u.nickname as gx_nickname'); if (in_array('avatar_url',$gxUcols)) $gxPquery->addSelect('u.avatar_url as gx_avatar'); } $gxPquery->addSelect('p.*'); foreach ($gxPquery->limit(250)->get() as $p) { $team = strtolower(trim((string)($p->team ?? $p->team_side ?? $p->side ?? ''))); $side = in_array($team,['a','bl','black_list','blacklist','1','team_a']) ? 'bl' : (in_array($team,['b','gr','global_risk','globalrisk','2','team_b']) ? 'gr' : null); if (!$side) continue; $mid = (int)$p->training_match_id; if (count($gxLineups[$mid][$side] ?? []) >= 5) continue; $gxLineups[$mid][$side][] = ['name'=>(string)($p->gx_nickname ?? $p->nickname ?? 'Jogador'), 'avatar'=>(string)($p->gx_avatar ?? ''), 'user_id'=>(int)($p->gx_user_id ?? $p->user_id ?? 0)]; } } } } catch (\Throwable $e) { $gxLineups=[]; } $gxRankings = [ 'MIX' => $ranking, 'CLÃS' => collect(), 'DRAFT' => collect(), 'FANTASMA' => collect(), '5L' => collect(), ]; // V35: serializar estruturas antes do Blade para evitar erro do parser @json. $gxV35JsonFlags = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_INVALID_UTF8_SUBSTITUTE; $gxV35RoomsData = $dashboardLobbies->map(static function ($r) { return [ 'id' => (int) $r->id, 'mode' => (string) ($r->training_type ?? 'Lobby'), 'map' => (string) ($r->map_name ?? ''), 'status' => (string) $r->status, 'players' => (int) $r->current_players, 'max' => (int) $r->max_players, ]; })->values()->all(); $gxV35RoomsJson = json_encode($gxV35RoomsData, $gxV35JsonFlags) ?: '[]'; $gxV35LastData = $gxLastMatch ? [ 'id' => (int) $gxLastMatch->id, 'mode' => (string) ($gxLastMatch->training_type ?? 'Lobby'), 'map' => (string) ($gxLastMatch->map_name ?? ''), 'status' => (string) ($gxLastMatch->status ?? ''), 'players' => null, 'max' => null, ] : null; $gxV35LastJson = json_encode($gxV35LastData, $gxV35JsonFlags) ?: 'null'; // Outros rankings so podem ser preenchidos apos identificar suas fontes oficiais. $shortcutItems=[ ['⚡','Jogar agora','Entrar na fila MIX',$go('lobby.index','/lobby'),'orange'], ['⚔','Partidas','Ver minhas partidas',$go('partidas','/partidas'),'red'], ['▦','Rankings','Ver classificação',$go('ranking','/ranking'),'blue'], ['●','Meu Perfil','Editar perfil',$go('profile','/profile'),'purple'], ['🛒','Loja','Ver itens',$go('loja','/loja'),'purple'], ['🏆','Torneios','Campeonatos',$go('tournaments.index','/tournaments'),'gold'], ]; $avatarUrl=\App\Support\GigaxAvatar::fromUser($user) ?: \App\Support\GigaxAvatar::url(session('user_avatar')); $avatarInitial=mb_strtoupper(mb_substr(trim((string)$name),0,1)); @endphp
COMUNIDADE FORTE