-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_sync.go
More file actions
331 lines (269 loc) · 9.68 KB
/
Copy pathapp_sync.go
File metadata and controls
331 lines (269 loc) · 9.68 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"fmt"
"path/filepath"
"shelllab/backend/services"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// ============================================================================
// Icon Fix APIs
// ============================================================================
// FixMissingIconsResult holds the result of icon fixing operation
type FixMissingIconsResult struct {
TotalMissing int `json:"totalMissing"`
Fixed int `json:"fixed"`
Failed int `json:"failed"`
Message string `json:"message"`
}
// FixSingleItemIcon fixes icon for a single item
func (a *App) FixSingleItemIcon(itemID int) *FixMissingIconsResult {
fmt.Printf("[API] FixSingleItemIcon called for item %d\n", itemID)
iconFixService := services.NewIconFixService(a.db.DB(), filepath.Join(a.DataDir, "icons"))
success, iconName, err := iconFixService.FixSingleItem(a.db.DB(), itemID)
if err != nil {
return &FixMissingIconsResult{
TotalMissing: 1,
Fixed: 0,
Failed: 1,
Message: err.Error(),
}
}
if success {
fmt.Printf("[API] Successfully fixed icon for item %d: %s\n", itemID, iconName)
return &FixMissingIconsResult{
TotalMissing: 1,
Fixed: 1,
Failed: 0,
Message: fmt.Sprintf("Successfully updated icon to: %s", iconName),
}
}
return &FixMissingIconsResult{
TotalMissing: 1,
Fixed: 0,
Failed: 1,
Message: "Unknown error",
}
}
// FixMissingIcons manually triggers the icon fix process
func (a *App) FixMissingIcons(iconType string, maxItems int) *FixMissingIconsResult {
fmt.Printf("[API] FixMissingIcons called with type=%s, maxItems=%d\n", iconType, maxItems)
iconFixService := services.NewIconFixService(a.db.DB(), filepath.Join(a.DataDir, "icons"))
var allMissing []services.MissingIconItem
var err error
if iconType == "spell" {
allMissing, err = iconFixService.GetMissingSpellIcons()
} else {
allMissing, err = iconFixService.GetMissingIcons()
}
if err != nil {
return &FixMissingIconsResult{
Message: fmt.Sprintf("Error: %v", err),
}
}
totalMissing := len(allMissing)
if totalMissing == 0 {
return &FixMissingIconsResult{
TotalMissing: 0,
Fixed: 0,
Failed: 0,
Message: fmt.Sprintf("All %s icons are already fixed!", iconType),
}
}
itemsToFix := allMissing
if maxItems > 0 && maxItems < totalMissing {
itemsToFix = allMissing[:maxItems]
}
successCount := 0
failedCount := 0
fmt.Printf("Fixing %d %s icons...\n", len(itemsToFix), iconType)
for _, item := range itemsToFix {
var success bool
var iconName string
if iconType == "spell" {
success, iconName, err = iconFixService.FixSingleSpell(a.db.DB(), item.Entry)
} else {
success, iconName, err = iconFixService.FixSingleItem(a.db.DB(), item.Entry)
}
if err != nil || !success {
failedCount++
fmt.Printf(" Failed %s %d: %v\n", iconType, item.Entry, err)
} else {
successCount++
fmt.Printf(" Fixed %s %d: %s\n", iconType, item.Entry, iconName)
}
}
result := &FixMissingIconsResult{
TotalMissing: totalMissing,
Fixed: successCount,
Failed: failedCount,
Message: fmt.Sprintf("Fixed %d %s icons, %d failed, %d remaining", successCount, iconType, failedCount, totalMissing-successCount),
}
fmt.Printf("[API] FixMissingIcons result: %+v\n", result)
return result
}
// ============================================================================
// Database Sync APIs (turtlecraft.gg)
// ============================================================================
// GetSyncStats returns statistics about local Turtle WoW data
func (a *App) GetSyncStats() map[string]interface{} {
fmt.Println("[API] GetSyncStats called")
return a.syncService.GetSyncStats()
}
// CheckNewItems checks for new items on turtlecraft.gg beyond local max ID
func (a *App) CheckNewItems(maxChecks int, delayMs int) []services.RemoteItem {
fmt.Printf("[API] CheckNewItems called with maxChecks=%d, delayMs=%d\n", maxChecks, delayMs)
// Allow 0 for unlimited
if maxChecks < 0 {
maxChecks = 100
}
if delayMs <= 0 {
delayMs = 200
}
a.syncService.ResetStop()
items, err := a.syncService.CheckNewItems(maxChecks, delayMs, nil)
if err != nil {
fmt.Printf("[API] Error checking new items: %v\n", err)
return []services.RemoteItem{}
}
fmt.Printf("[API] Found %d new items\n", len(items))
return items
}
// CheckNewQuests checks for new quests on turtlecraft.gg beyond local max ID
func (a *App) CheckNewQuests(maxChecks int, delayMs int) []services.RemoteQuest {
fmt.Printf("[API] CheckNewQuests called with maxChecks=%d, delayMs=%d\n", maxChecks, delayMs)
// Allow 0 for unlimited
if maxChecks < 0 {
maxChecks = 100
}
if delayMs <= 0 {
delayMs = 200
}
a.syncService.ResetStop()
quests, err := a.syncService.CheckNewQuests(maxChecks, delayMs, nil)
if err != nil {
fmt.Printf("[API] Error checking new quests: %v\n", err)
return []services.RemoteQuest{}
}
fmt.Printf("[API] Found %d new quests\n", len(quests))
return quests
}
// GetMissingAtlasLootItems returns items in AtlasLoot that don't exist in item_template
func (a *App) GetMissingAtlasLootItems(limit int) []services.MissingItem {
fmt.Printf("[API] GetMissingAtlasLootItems called with limit=%d\n", limit)
if limit <= 0 {
limit = 100
}
items, err := a.syncService.GetMissingAtlasLootItems(limit)
if err != nil {
fmt.Printf("[API] Error getting missing items: %v\n", err)
return []services.MissingItem{}
}
fmt.Printf("[API] Found %d missing items\n", len(items))
return items
}
// SyncMissingAtlasLoot syncs all missing AtlasLoot items
func (a *App) SyncMissingAtlasLoot(maxItems int, delayMs int) *services.ImportResult {
fmt.Printf("[API] SyncMissingAtlasLoot called with maxItems=%d\n", maxItems)
a.syncService.ResetStop()
result, err := a.syncService.ImportMissingItems(maxItems, delayMs)
if err != nil {
return &services.ImportResult{
Errors: []string{err.Error()},
}
}
return result
}
// SyncSingleItem fetches and imports a single item from turtlecraft.gg
func (a *App) SyncSingleItem(itemID int) *services.SyncItemResult {
fmt.Printf("[API] SyncSingleItem called for item %d\n", itemID)
return a.syncService.FetchAndImportItem(itemID)
}
// FullSyncItems re-syncs all Turtle items from turtlecraft.gg
// startFrom: if > 0, resume sync from this ID (for progress recovery)
func (a *App) FullSyncItems(delayMs int, fixIcons bool, startFrom int) string {
fmt.Printf("[API] FullSyncItems called with delayMs=%d, fixIcons=%v, startFrom=%d\n", delayMs, fixIcons, startFrom)
if delayMs <= 0 {
delayMs = 200
}
a.syncService.ResetStop()
iconDir := filepath.Join(a.DataDir, "icons")
go func() {
// Create progress callback that emits events to frontend
progressCb := func(current, total int, itemID int, itemName string) {
runtime.EventsEmit(a.ctx, "sync:progress", map[string]interface{}{
"current": current,
"total": total,
"itemId": itemID,
"itemName": itemName,
})
}
result := a.syncService.FullSyncItems(delayMs, fixIcons, iconDir, startFrom, progressCb)
if len(result.Errors) > 0 && result.Updated == 0 {
runtime.EventsEmit(a.ctx, "sync:item_full:error", result.Message)
} else {
runtime.EventsEmit(a.ctx, "sync:item_full:complete", result.Message)
}
}()
return "Started"
}
// FullSyncSpells re-syncs all spells referenced by items
func (a *App) FullSyncSpells(delayMs int, fixIcons bool, startFrom int) string {
fmt.Printf("[API] FullSyncSpells called with delayMs=%d, fixIcons=%v, startFrom=%d\n", delayMs, fixIcons, startFrom)
if delayMs <= 0 {
delayMs = 200
}
a.syncService.ResetStop()
iconDir := filepath.Join(a.DataDir, "icons")
go func() {
// Create progress callback that emits events to frontend
progressCb := func(current, total int, itemID int, itemName string) {
runtime.EventsEmit(a.ctx, "sync:spells:progress", map[string]interface{}{
"current": current,
"total": total,
"itemId": itemID,
"itemName": itemName,
})
}
result := a.syncService.FullSyncSpells(delayMs, fixIcons, iconDir, startFrom, progressCb)
runtime.EventsEmit(a.ctx, "sync:spells_full:complete", result.Message)
}()
return "Started"
}
// FullSyncQuests re-syncs all quests
func (a *App) FullSyncQuests(delayMs int, startFrom int) string {
fmt.Printf("[API] FullSyncQuests called with delayMs=%d, startFrom=%d\n", delayMs, startFrom)
if delayMs <= 0 {
delayMs = 200
}
a.syncService.ResetStop()
go func() {
// Create progress callback that emits events to frontend
progressCb := func(current, total int, itemID int, itemName string) {
runtime.EventsEmit(a.ctx, "sync:quests:progress", map[string]interface{}{
"current": current,
"total": total,
"itemId": itemID,
"itemName": itemName,
})
}
result := a.syncService.FullSyncQuests(delayMs, startFrom, progressCb)
runtime.EventsEmit(a.ctx, "sync:quests_full:complete", result.Message)
}()
return "Started"
}
func (a *App) SyncSingleSpell(spellID int) *services.SyncSpellResult {
fmt.Printf("[API] SyncSingleSpell called for spell %d\n", spellID)
iconDir := filepath.Join(a.DataDir, "icons")
return a.syncService.FetchAndImportSpell(spellID, iconDir)
}
// StopSync requests all ongoing sync processes to stop
func (a *App) StopSync() string {
fmt.Println("[API] StopSync called")
if a.syncService != nil {
a.syncService.RequestStop()
}
if a.npcService != nil {
a.npcService.RequestStop()
}
return "Stop requested"
}