forked from oplancelot/shelllab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_creatures.go
More file actions
212 lines (189 loc) · 7.04 KB
/
Copy pathapp_creatures.go
File metadata and controls
212 lines (189 loc) · 7.04 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
package main
import (
"fmt"
"runtime/debug"
"inklab/backend/database"
"inklab/backend/services"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// GetCreatureTypes returns all creature types with counts
func (a *App) GetCreatureTypes() []*database.CreatureType {
fmt.Println("[API] GetCreatureTypes called")
types, err := a.creatureRepo.GetCreatureTypes()
if err != nil {
fmt.Printf("[API] Error getting creature types: %v\n", err)
return []*database.CreatureType{}
}
fmt.Printf("[API] GetCreatureTypes returning %d types\n", len(types))
return types
}
// BrowseCreaturesByType returns creatures filtered by type
func (a *App) BrowseCreaturesByType(creatureType int, nameFilter string) []*database.Creature {
fmt.Printf("[API] BrowseCreaturesByType called type=%d filter='%s'\n", creatureType, nameFilter)
creatures, _, err := a.creatureRepo.GetCreaturesByType(creatureType, nameFilter, 9999, 0)
if err != nil {
fmt.Printf("[API] Error browsing creatures: %v\n", err)
return []*database.Creature{}
}
fmt.Printf("[API] BrowseCreaturesByType returning %d creatures\n", len(creatures))
return creatures
}
// CreaturePageResult is the result of paginated creature query
type CreaturePageResult struct {
Creatures []*database.Creature `json:"creatures"`
Total int `json:"total"`
HasMore bool `json:"hasMore"`
}
// BrowseCreaturesByTypePaged returns creatures with pagination support
func (a *App) BrowseCreaturesByTypePaged(creatureType int, nameFilter string, limit, offset int) *CreaturePageResult {
fmt.Printf("[API] BrowseCreaturesByTypePaged: type=%d filter='%s' limit=%d offset=%d\n", creatureType, nameFilter, limit, offset)
creatures, total, err := a.creatureRepo.GetCreaturesByType(creatureType, nameFilter, limit, offset)
if err != nil {
fmt.Printf("[API] Error browsing creatures: %v\n", err)
return &CreaturePageResult{
Creatures: []*database.Creature{},
Total: 0,
HasMore: false,
}
}
return &CreaturePageResult{
Creatures: creatures,
Total: total,
HasMore: (offset + len(creatures)) < total,
}
}
// GetBeastFamilies returns beast families (with counts) for sub-filtering Beasts.
func (a *App) GetBeastFamilies() []*database.BeastFamily {
families, err := a.creatureRepo.GetBeastFamilies()
if err != nil {
fmt.Printf("[API] Error getting beast families: %v\n", err)
return []*database.BeastFamily{}
}
return families
}
// BrowseCreaturesByFamilyPaged returns Beast-type creatures of a family, paged.
func (a *App) BrowseCreaturesByFamilyPaged(family int, nameFilter string, limit, offset int) *CreaturePageResult {
creatures, total, err := a.creatureRepo.GetCreaturesByFamily(family, nameFilter, limit, offset)
if err != nil {
fmt.Printf("[API] Error browsing creatures by family: %v\n", err)
return &CreaturePageResult{Creatures: []*database.Creature{}, Total: 0, HasMore: false}
}
return &CreaturePageResult{
Creatures: creatures,
Total: total,
HasMore: (offset + len(creatures)) < total,
}
}
// SearchCreatures searches for creatures by name
func (a *App) SearchCreatures(query string) []*database.Creature {
creatures, err := a.creatureRepo.SearchCreatures(query, 50)
if err != nil {
fmt.Printf("Error searching creatures: %v\n", err)
return []*database.Creature{}
}
return creatures
}
// GetCreatureDetail returns full details for a creature
func (a *App) GetCreatureDetail(entry int) (*database.CreatureDetail, error) {
c, err := a.creatureRepo.GetCreatureDetail(entry)
if err != nil {
fmt.Printf("Error getting creature detail [%d]: %v\n", entry, err)
return nil, err
}
return c, nil
}
// GetCreatureLoot returns the loot for a creature
func (a *App) GetCreatureLoot(entry int) []*database.LootItem {
loot, err := a.lootRepo.GetCreatureLoot(entry)
if err != nil {
fmt.Printf("Error getting creature loot: %v\n", err)
return []*database.LootItem{}
}
return loot
}
// GetNpcDetails returns full details for an NPC (Scraped + DB). A recover guard
// keeps a panic from killing the IPC pump and logs the full stack so the exact
// line is captured instead of just Wails' recovered summary.
func (a *App) GetNpcDetails(entry int) (res *services.NpcFullDetails) {
fmt.Printf("[API] GetNpcDetails called for %d\n", entry)
if a.npcService == nil {
fmt.Println("[API] GetNpcDetails: NPC service not ready yet (startup in progress)")
return nil
}
defer func() {
if r := recover(); r != nil {
fmt.Printf("[API] GetNpcDetails PANIC for %d: %v\n%s\n", entry, r, debug.Stack())
res = nil
}
}()
details, err := a.npcService.GetNpcDetails(entry)
if err != nil {
fmt.Printf("Error getting NPC details: %v\n", err)
return nil
}
return details
}
// SyncNpcData forces a re-sync of NPC data
func (a *App) SyncNpcData(entry int) *services.NpcFullDetails {
fmt.Printf("[API] SyncNpcData called for %d\n", entry)
if a.npcService == nil {
return nil
}
err := a.npcService.SyncNpcData(entry)
if err != nil {
fmt.Printf("Error syncing NPC data: %v\n", err)
}
// Return fresh details regardless of sync error (might be partial)
details, _ := a.npcService.GetNpcDetails(entry)
return details
}
// RefreshNpcImages re-fetches only the model/map images (and visual metadata)
// for an NPC, without re-syncing creature_template stats from MySQL.
func (a *App) RefreshNpcImages(entry int) *services.NpcFullDetails {
fmt.Printf("[API] RefreshNpcImages called for %d\n", entry)
if a.npcService == nil {
return nil
}
if err := a.npcService.RefreshNpcImages(entry); err != nil {
fmt.Printf("Error refreshing NPC images: %v\n", err)
}
details, _ := a.npcService.GetNpcDetails(entry)
return details
}
// FullSyncNpcs re-syncs all NPC data (Web + MySQL) starting from a specific ID
func (a *App) FullSyncNpcs(startFrom int, delayMs int) string {
fmt.Printf("[API] FullSyncNpcs called with startFrom=%d, delayMs=%d\n", startFrom, delayMs)
if delayMs <= 0 {
delayMs = 200 // Default delay
}
if a.npcService == nil {
return "NPC service not ready"
}
a.npcService.ResetStop()
go func() {
progressCb := func(current, total int, id int) {
runtime.EventsEmit(a.ctx, "sync:npc_full:progress", map[string]interface{}{
"current": current,
"total": total,
"id": id,
})
}
failed, err := a.npcService.FullSyncNpcs(startFrom, delayMs, progressCb)
if err != nil {
fmt.Printf("Error syncing all NPCs: %v\n", err)
runtime.EventsEmit(a.ctx, "sync:npc_full:error", err.Error())
} else {
msg := "Full NPC sync complete"
if len(failed) > 0 {
preview := failed
if len(preview) > 5 {
preview = preview[:5]
}
msg = fmt.Sprintf("Full NPC sync complete — %d NPC(s) failed to scrape (kept their existing data), e.g. %v. Re-run to retry them.", len(failed), preview)
fmt.Printf("[FullSyncNpcs] entries that failed to scrape: %v\n", failed)
}
runtime.EventsEmit(a.ctx, "sync:npc_full:complete", msg)
}
}()
return "Started"
}