-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
146 lines (125 loc) · 3.7 KB
/
app.go
File metadata and controls
146 lines (125 loc) · 3.7 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
package main
import (
"context"
"fmt"
"ghostdraft/internal/data"
"ghostdraft/internal/lcu"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
lcuClient *lcu.Client
wsClient *lcu.WebSocketClient
liveClient *lcu.LiveClient
champions *lcu.ChampionRegistry
items *lcu.ItemRegistry
championDB *data.ChampionDB
tursoClient *data.TursoClient // Turso database connection
statsProvider *data.StatsProvider // Stats queries (uses Turso with caching)
stopPoll chan struct{}
lastFetchedChamp int
lastFetchedEnemy int
lastBanFetchKey string
lastItemFetchKey string
lastCounterFetchKey string
windowVisible bool
// Champ select state - passed to in-game
lockedChampionID int
lockedChampionName string
lockedPosition string
// User identity - stored on LCU connection
currentPUUID string
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
lcuClient: lcu.NewClient(),
wsClient: lcu.NewWebSocketClient(),
liveClient: lcu.NewLiveClient(),
champions: lcu.NewChampionRegistry(),
items: lcu.NewItemRegistry(),
stopPoll: make(chan struct{}),
windowVisible: true,
}
}
// startup is called when the app starts
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Initialize champion database
if db, err := data.NewChampionDB(); err != nil {
fmt.Printf("Failed to initialize champion DB: %v\n", err)
} else {
a.championDB = db
fmt.Println("Champion database initialized")
}
// Position and size window relative to screen
screens, err := runtime.ScreenGetAll(ctx)
if err == nil && len(screens) > 0 {
screen := screens[0]
// Size: ~20% width, ~55% height (roughly matches champ select sidebar)
width := screen.Size.Width * 20 / 100
height := screen.Size.Height * 55 / 100
runtime.WindowSetSize(ctx, width, height)
// Position at right edge, vertically centered
x := screen.Size.Width - width - 20
y := (screen.Size.Height - height) / 2
runtime.WindowSetPosition(ctx, x, y)
}
// Load data from Data Dragon in parallel
go func() {
if err := a.champions.Load(); err != nil {
fmt.Printf("Failed to load champions: %v\n", err)
}
}()
go func() {
if err := a.items.Load(); err != nil {
fmt.Printf("Failed to load items: %v\n", err)
}
}()
// Initialize stats database and check for updates
go a.initStats()
// Set up champ select handler
a.wsClient.SetChampSelectHandler(a.onChampSelectUpdate)
// Set up gameflow handler
a.wsClient.SetGameflowHandler(a.onGameflowUpdate)
// Start polling for League Client
go a.pollForLeagueClient()
// Register global hotkey (Ctrl+O to toggle visibility)
a.RegisterToggleHotkey()
}
// initStats initializes the Turso connection and stats provider
func (a *App) initStats() {
// Connect to Turso
tursoClient, err := data.NewTursoClient()
if err != nil {
fmt.Printf("Failed to connect to Turso: %v\n", err)
return
}
a.tursoClient = tursoClient
// Create stats provider with caching
provider, err := data.NewStatsProvider(tursoClient)
if err != nil {
fmt.Printf("Stats provider not available: %v\n", err)
return
}
// Fetch current patch from Turso
if err := provider.FetchPatch(); err != nil {
fmt.Printf("No stats patch available: %v\n", err)
return
}
a.statsProvider = provider
fmt.Printf("Stats provider ready (patch %s)\n", provider.GetPatch())
}
// shutdown is called when the app is closing
func (a *App) shutdown(ctx context.Context) {
close(a.stopPoll)
a.wsClient.Disconnect()
a.lcuClient.Disconnect()
if a.championDB != nil {
a.championDB.Close()
}
if a.tursoClient != nil {
a.tursoClient.Close()
}
}