-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_connection.go
More file actions
167 lines (145 loc) · 4.02 KB
/
app_connection.go
File metadata and controls
167 lines (145 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// pollForLeagueClient continuously checks for League Client
func (a *App) pollForLeagueClient() {
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
wasConnected := false
// Try immediately on startup
a.tryConnect()
if a.lcuClient.IsConnected() {
wasConnected = true
a.connectWebSocket()
}
for {
select {
case <-a.stopPoll:
return
case <-ticker.C:
isConnected := a.lcuClient.IsConnected()
if isConnected && !wasConnected {
// Just connected
wasConnected = true
a.connectWebSocket()
} else if !isConnected {
// If we were connected before, emit disconnect event
if wasConnected {
a.wsClient.Disconnect()
runtime.EventsEmit(a.ctx, "lcu:status", map[string]interface{}{
"connected": false,
"message": "League Disconnected. Waiting...",
})
runtime.EventsEmit(a.ctx, "champselect:update", map[string]interface{}{
"inChampSelect": false,
})
runtime.EventsEmit(a.ctx, "build:update", map[string]interface{}{
"hasBuild": false,
})
fmt.Println("League Disconnected. Waiting for reconnection...")
wasConnected = false
}
// Try to reconnect
a.tryConnect()
if a.lcuClient.IsConnected() {
wasConnected = true
a.connectWebSocket()
}
} else if isConnected && !a.wsClient.IsConnected() {
// HTTP connected but WebSocket disconnected, try to reconnect WS
a.connectWebSocket()
}
}
}
}
// connectWebSocket establishes WebSocket connection
func (a *App) connectWebSocket() {
creds := a.lcuClient.GetCredentials()
if creds == nil {
return
}
err := a.wsClient.Connect(creds)
if err != nil {
fmt.Printf("WebSocket connection failed: %v\n", err)
return
}
fmt.Println("WebSocket connected - Listening for champ select...")
// Fetch initial gameflow state
go a.fetchInitialGameflow()
}
// tryConnect attempts to connect to the League Client
func (a *App) tryConnect() {
err := a.lcuClient.Connect()
if err != nil {
runtime.EventsEmit(a.ctx, "lcu:status", map[string]interface{}{
"connected": false,
"message": "Waiting for League...",
})
return
}
// Successfully connected
runtime.EventsEmit(a.ctx, "lcu:status", map[string]interface{}{
"connected": true,
"message": "League Connected!",
"port": a.lcuClient.GetPort(),
})
// Store current user's PUUID for in-game identification
if puuid, err := a.lcuClient.GetCurrentSummonerPUUID(); err == nil && puuid != "" {
a.currentPUUID = puuid
if len(puuid) > 8 {
fmt.Printf("Stored user PUUID: %s...\n", puuid[:8])
} else {
fmt.Printf("Stored user PUUID: %s\n", puuid)
}
}
fmt.Printf("League Connected! Port: %s\n", a.lcuClient.GetPort())
}
// GetConnectionStatus returns the current LCU connection status
func (a *App) GetConnectionStatus() map[string]interface{} {
if a.lcuClient.IsConnected() {
return map[string]interface{}{
"connected": true,
"message": "League Connected!",
"port": a.lcuClient.GetPort(),
}
}
return map[string]interface{}{
"connected": false,
"message": "Waiting for League...",
}
}
// fetchInitialGameflow fetches the current gameflow phase on connect
func (a *App) fetchInitialGameflow() {
phase, err := a.lcuClient.GetGameflowPhase()
if err != nil {
fmt.Printf("Failed to get initial gameflow phase: %v\n", err)
return
}
fmt.Printf("Initial gameflow phase: %s\n", phase)
a.onGameflowUpdate(phase)
}
// GetGameflowPhase returns the current gameflow phase (exposed to frontend for debugging)
func (a *App) GetGameflowPhase() map[string]interface{} {
if !a.lcuClient.IsConnected() {
return map[string]interface{}{
"error": "Not connected",
}
}
phase, err := a.lcuClient.GetGameflowPhase()
if err != nil {
return map[string]interface{}{
"error": err.Error(),
}
}
// If in game, also trigger build and scouting fetch
if phase == "InProgress" {
go a.fetchAndEmitInGameBuild()
go a.fetchAndEmitScouting()
}
return map[string]interface{}{
"phase": phase,
}
}