token(); if ($token === '') { return ['ok' => false, 'error' => 'GIGAX_SHARED_TOKEN não configurado no .env do Laravel.']; } try { $request = Http::acceptJson() ->timeout(8) ->withHeaders(['X-GIGAX-BOT-TOKEN' => $token]); $response = strtoupper($method) === 'GET' ? $request->get($this->baseUrl() . $path) : $request->post($this->baseUrl() . $path, $payload); $json = $response->json(); if (!is_array($json)) { $json = ['ok' => false, 'error' => $response->body()]; } if (!$response->successful()) { $json['ok'] = false; $json['http_status'] = $response->status(); } return $json; } catch (Throwable $e) { return ['ok' => false, 'error' => $e->getMessage()]; } } private function loadMatches() { $tables = ['mix_training_matches', 'mix_active_matches', 'active_matches']; foreach ($tables as $table) { if (!Schema::hasTable($table)) continue; try { $query = DB::table($table); $cols = Schema::getColumnListing($table); if (in_array('status', $cols, true)) { $query->whereNotIn('status', ['ended', 'finished', 'cancelled', 'deleted']); } return $query->orderByDesc(in_array('id', $cols, true) ? 'id' : $cols[0])->limit(100)->get(); } catch (Throwable $e) {} } return collect(); } public function index(Request $request) { $health = $this->call('GET', '/health'); $status = $this->call('GET', '/admin/status'); $events = $this->call('GET', '/admin/events'); return view('Admin.discord', [ 'health' => $health, 'discordStatus' => $status, 'discordEvents' => $events['events'] ?? [], 'matches' => $this->loadMatches(), 'selectedMatchId' => (int) $request->query('match_id', 0), 'eventUrl' => $this->baseUrl(), ]); } public function testCreate(Request $request) { $matchId = (int) ($request->input('match_id') ?: (990000 + random_int(100, 8999))); $payload = [ 'match_id' => $matchId, 'title' => 'GIGAX TESTE DISCORD #' . $matchId, 'map_name' => 'TESTE ADMIN', 'voice_mode' => 'discord', 'team_a_discord_ids' => $this->ids($request->input('team_a_discord_ids')), 'team_b_discord_ids' => $this->ids($request->input('team_b_discord_ids')), ]; $result = $this->call('POST', '/admin/test-create', $payload); return back()->with($result['ok'] ?? false ? 'success' : 'error', ($result['ok'] ?? false) ? "Sala Discord de teste #{$matchId} criada." : 'Falha ao criar sala Discord: ' . ($result['error'] ?? 'erro desconhecido') ); } public function move(Request $request) { $matchId = (int) $request->input('match_id'); $result = $this->call('POST', '/admin/move', [ 'match_id' => $matchId, 'team_a_discord_ids' => $this->ids($request->input('team_a_discord_ids')), 'team_b_discord_ids' => $this->ids($request->input('team_b_discord_ids')), ]); return back()->with($result['ok'] ?? false ? 'success' : 'error', ($result['ok'] ?? false) ? 'Movimentação Discord executada.' : 'Falha ao mover jogadores: ' . ($result['error'] ?? 'erro desconhecido') ); } public function cleanup(Request $request) { $matchId = (int) $request->input('match_id', 0); if ($request->boolean('all_tests')) { $status = $this->call('GET', '/admin/status'); if (!($status['ok'] ?? false)) { return back()->with('error', 'Nao foi possivel consultar as salas Discord.'); } $rooms = $status['rooms'] ?? []; $removed = 0; $failed = 0; foreach ($rooms as $room) { $id = (int) ($room['match_id'] ?? 0); if ($id < 990000 || $id > 999999) { continue; } if (($room['status'] ?? '') !== 'created') { continue; } $result = $this->call('POST', '/admin/cleanup', [ 'match_id' => $id, ]); if ($result['ok'] ?? false) { $removed++; } else { $failed++; } } return back()->with( $failed === 0 ? 'success' : 'error', "Limpeza dos testes: {$removed} removidas, {$failed} falhas." ); } if ($matchId < 990000 || $matchId > 999999) { return back()->with( 'error', 'Limpeza manual permitida somente para salas de teste.' ); } $result = $this->call('POST', '/admin/cleanup', [ 'match_id' => $matchId, ]); return back()->with( ($result['ok'] ?? false) ? 'success' : 'error', ($result['ok'] ?? false) ? "Sala Discord #{$matchId} removida." : 'Falha ao limpar Discord: ' . ($result['error'] ?? 'erro desconhecido') ); } public function health() { return response()->json($this->call('GET', '/health')); } private function ids($value): array { if (is_array($value)) { $parts = $value; } else { $parts = preg_split('/[\s,;]+/', trim((string) $value)) ?: []; } $ids = []; foreach ($parts as $part) { $part = preg_replace('/\D+/', '', (string) $part); if ($part !== '' && strlen($part) >= 15) $ids[] = $part; } return array_values(array_unique($ids)); } }