diff --git a/admin/src/App.vue b/admin/src/App.vue index 3c5b8fb..b4f44ec 100644 --- a/admin/src/App.vue +++ b/admin/src/App.vue @@ -129,7 +129,7 @@ onMounted(async () => { serverUrl.value = config.serverUrl // Connect to SSE - adminService.connect((data) => { + await adminService.connect((data) => { if (data.type === 'connected') { isConnected.value = true } else if (data.type === 'gameStats') { diff --git a/admin/src/services/adminService.ts b/admin/src/services/adminService.ts index 6ab0ca7..a11e9cc 100644 --- a/admin/src/services/adminService.ts +++ b/admin/src/services/adminService.ts @@ -26,8 +26,26 @@ class AdminService { private connectionCallback: ConnectionCallback | null = null private isConnected = false private serverUrl: string = 'http://localhost:2567' // Default to Colyseus server + private initialized = false - connect(callback: AdminCallback): void { + private async initializeServerUrl(): Promise { + if (this.initialized) return + + try { + const response = await fetch('/api/config') + const config = await response.json() + this.serverUrl = config.serverUrl || 'http://localhost:2567' + this.initialized = true + } catch (error) { + console.warn('Failed to fetch server config, using default URL:', error) + this.serverUrl = 'http://localhost:2567' + this.initialized = true + } + } + + async connect(callback: AdminCallback): Promise { + await this.initializeServerUrl() + this.callback = callback this.eventSource = new EventSource('/api/sse') @@ -67,6 +85,7 @@ class AdminService { // Admin control methods async kickPlayer(playerId: string): Promise { + await this.initializeServerUrl() const response = await fetch(`${this.serverUrl}/api/admin/kick-player`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -79,6 +98,7 @@ class AdminService { } async pauseGame(): Promise { + await this.initializeServerUrl() const response = await fetch(`${this.serverUrl}/api/admin/pause-game`, { method: 'POST', headers: { 'Content-Type': 'application/json' } @@ -90,6 +110,7 @@ class AdminService { } async resumeGame(): Promise { + await this.initializeServerUrl() const response = await fetch(`${this.serverUrl}/api/admin/resume-game`, { method: 'POST', headers: { 'Content-Type': 'application/json' } @@ -101,6 +122,7 @@ class AdminService { } async cancelGame(gameId: string): Promise { + await this.initializeServerUrl() const response = await fetch(`${this.serverUrl}/api/admin/cancel-game`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -113,6 +135,7 @@ class AdminService { } async kickAllPlayers(): Promise { + await this.initializeServerUrl() const response = await fetch(`${this.serverUrl}/api/admin/kick-all-players`, { method: 'POST', headers: { 'Content-Type': 'application/json' } @@ -124,6 +147,7 @@ class AdminService { } async advanceRound(): Promise { + await this.initializeServerUrl() const response = await fetch(`${this.serverUrl}/api/admin/advance-round`, { method: 'POST', headers: { 'Content-Type': 'application/json' } @@ -135,6 +159,7 @@ class AdminService { } async previousRound(): Promise { + await this.initializeServerUrl() const response = await fetch(`${this.serverUrl}/api/admin/previous-round`, { method: 'POST', headers: { 'Content-Type': 'application/json' }