-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.go
More file actions
257 lines (216 loc) · 7.59 KB
/
Copy pathapp.go
File metadata and controls
257 lines (216 loc) · 7.59 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"context"
_ "embed" // Use blank import to ensure it sticks, though explicit usage should be enough
"fmt"
"os"
"path/filepath"
"shelllab/backend/database"
"shelllab/backend/services"
"github.com/joho/godotenv"
)
// App struct
type App struct {
ctx context.Context
db *database.SQLiteDB
DataDir string // Path to data directory
// Repositories
itemRepo *database.ItemRepository
creatureRepo *database.CreatureRepository
questRepo *database.QuestRepository
spellRepo *database.SpellRepository
lootRepo *database.LootRepository
factionRepo *database.FactionRepository
objectRepo *database.GameObjectRepository
categoryRepo *database.CategoryRepository
atlasLootRepo *database.AtlasLootRepository
favoriteRepo *database.FavoriteRepository
// Cache for category lookups
categoryCache map[int]*database.Category
rootCategoryByName map[string]int
// Services
npcService *services.NpcService
syncService *services.SyncService
scraper *services.ScraperService
mysqlDB *database.MySQLConnection
// Mode
isDevMode bool
}
// NewApp creates a new App application struct
func NewApp(dataDir string, isDevMode bool) *App {
return &App{
DataDir: dataDir,
isDevMode: isDevMode,
categoryCache: make(map[int]*database.Category),
rootCategoryByName: make(map[string]int),
}
}
// startup is called when the app starts
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
fmt.Println("Initializing ShellLab (SQLite Version)...")
// Load .env
if err := godotenv.Load(); err != nil {
fmt.Println("Warning: .env file not found, MySQL features disabled")
}
// Initialize SQLite database
dbPath := filepath.Join(a.DataDir, "shelllab.db")
db, err := database.NewSQLiteDB(dbPath)
if err != nil {
fmt.Printf("ERROR: Failed to open database: %v\n", err)
return
}
// Ensure schema exists
if err := db.InitSchema(); err != nil {
fmt.Printf("ERROR: Failed to initialize schema: %v\n", err)
return
}
a.db = db
// Initialize all repositories
a.itemRepo = database.NewItemRepository(db)
a.creatureRepo = database.NewCreatureRepository(db)
a.questRepo = database.NewQuestRepository(db)
a.spellRepo = database.NewSpellRepository(db)
a.lootRepo = database.NewLootRepository(db)
a.factionRepo = database.NewFactionRepository(db)
a.objectRepo = database.NewGameObjectRepository(db)
a.categoryRepo = database.NewCategoryRepository(db)
a.atlasLootRepo = database.NewAtlasLootRepository(db)
a.favoriteRepo = database.NewFavoriteRepository(db)
// Initialize favorites schema
if err := a.favoriteRepo.InitSchema(); err != nil {
fmt.Printf("ERROR: Failed to initialize favorites schema: %v\n", err)
}
// Initialize MySQL (Optional)
mysqlUser := os.Getenv("MYSQL_USER")
if mysqlUser != "" {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
os.Getenv("MYSQL_USER"),
os.Getenv("MYSQL_PASSWORD"),
os.Getenv("MYSQL_HOST"),
os.Getenv("MYSQL_PORT"),
os.Getenv("MYSQL_DATABASE"),
)
mysqlConn, err := database.NewMySQLConnection(dsn)
if err != nil {
fmt.Printf("MySQL Connection Failed: %v\n", err)
} else {
a.mysqlDB = mysqlConn
fmt.Println("✓ MySQL Connected")
// Inject into CreatureRepository
a.creatureRepo.SetMySQL(mysqlConn.DB())
}
}
// Print stats
itemCount, _ := a.itemRepo.GetItemCount()
catCount, _ := a.categoryRepo.GetCategoryCount()
fmt.Printf("✓ Database Connected: %s\n", dbPath)
fmt.Printf(" - Items: %d\n", itemCount)
fmt.Printf(" - Categories: %d\n", catCount)
// Build category cache
a.buildCategoryCache()
// Data import using importers
// dataDir is already set in a.DataDir
// If database is already populated (itemCount > 0), skip costly imports
if itemCount > 0 {
fmt.Println("Database already populated. Skipping initialization imports.")
} else {
// Import Item Sets
fmt.Println("Checking item sets...")
itemSetImporter := database.NewItemSetImporter(db)
if err := itemSetImporter.CheckAndImport(a.DataDir); err != nil {
fmt.Printf("ERROR: Failed to import item sets: %v\n", err)
}
// Import Factions
fmt.Println("Checking faction data...")
factionImporter := database.NewFactionImporter(db)
factionImporter.CheckAndImport(a.DataDir)
}
// Import Metadata (Zones, Skills) - Always run this as it checks internally and initializes static data
fmt.Println("Checking metadata...")
metadataImporter := database.NewMetadataImporter(db)
metadataImporter.ImportAll(a.DataDir)
// Initialize MySQL Connection (Development mode only)
if a.isDevMode {
mysqlDB, err := database.NewMySQLConnection(filepath.Join(".", ".env"))
if err != nil {
fmt.Printf("⚠️ MySQL connection failed: %v. NPC sync from MySQL will be unavailable.\n", err)
} else {
a.mysqlDB = mysqlDB
fmt.Println("✓ MySQL Connected (dev mode)")
}
}
// 4. Import Data (Developer Mode Only - users use pre-built DB)
if a.isDevMode {
// Import AtlasLoot
fmt.Println("Checking AtlasLoot data...")
alImporter := database.NewAtlasLootImporter(a.db)
if err := alImporter.CheckAndImport(a.DataDir); err != nil {
fmt.Printf("ERROR: Failed to import AtlasLoot: %v\n", err)
}
// Import MySQL Tables
a.importFullTables(a.DataDir)
}
// Icon downloading is now on-demand via fix button
// No need to auto-download on startup
// Initialize NPC Service
a.scraper = services.NewScraperService()
a.npcService = services.NewNpcService(a.db.DB(), a.mysqlDB, a.scraper, a.itemRepo, a.creatureRepo, a.DataDir)
a.syncService = services.NewSyncService(a.db.DB())
// Async sync creature spawns for dev convenience
if a.isDevMode && a.mysqlDB != nil {
var spawnCount int
a.db.DB().QueryRow("SELECT COUNT(*) FROM creature_spawn").Scan(&spawnCount)
if spawnCount == 0 {
fmt.Println("⚡ Starting async creature spawn sync (First Run)...")
go func() {
// No progress callback necessary for background startup task
err := a.npcService.SyncAllCreatureSpawns(nil)
if err != nil {
fmt.Printf("Startup spawn sync warning: %v\n", err)
} else {
fmt.Println("✓ Creature spawn sync complete")
}
}()
} else {
fmt.Printf("⏭️ creature_spawn already has %d rows, skipping startup sync\n", spawnCount)
}
}
fmt.Println("✓ ShellLab ready!")
}
// importFullTables imports data from MySQL if available
// The MySQL importer checks each table individually - only empty tables are imported
func (a *App) importFullTables(dataDir string) {
if a.mysqlDB == nil {
fmt.Println("⚠️ No MySQL connection available. Database import skipped.")
return
}
fmt.Println("⚡ Checking database tables and importing from MySQL if needed...")
importer := database.NewMySQLImporter(a.db.DB(), a.mysqlDB.DB())
if err := importer.ImportAllFromMySQL(); err != nil {
fmt.Printf("❌ MySQL Import Failed: %v\n", err)
} else {
fmt.Println("✓ MySQL Import Check Complete")
}
}
// buildCategoryCache builds a cache of categories for faster lookups
func (a *App) buildCategoryCache() {
roots, err := a.categoryRepo.GetRootCategories()
if err != nil {
return
}
for _, cat := range roots {
a.categoryCache[cat.ID] = cat
a.rootCategoryByName[cat.Name] = cat.ID
}
}
// shutdown is called when the app is closing
func (a *App) shutdown(ctx context.Context) {
if a.db != nil {
a.db.Close()
}
}
// WaitForReady waits for the app to be ready (max 5 seconds)
func (a *App) WaitForReady() bool {
return a.db != nil
}