-
Notifications
You must be signed in to change notification settings - Fork 0
/
bestiary.go
196 lines (179 loc) · 5.77 KB
/
bestiary.go
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
package bestiary
import (
"fmt"
"html/template"
"net/http"
"strconv"
"github.com/bountyeq/web/config"
"github.com/bountyeq/web/model"
"github.com/bountyeq/web/sanitize"
"github.com/bountyeq/web/site"
"github.com/gin-gonic/gin"
)
// FactionEntry is used for a faction standing
type FactionEntry struct {
ID uint32
Name string
Value int32
}
// List returns a list of creatures
func List(c *gin.Context) {
var err error
search, ok := c.Get("search")
fmt.Println("search", search)
if ok {
fmt.Println("search", search)
}
id := c.Param("id")
if len(id) > 0 {
err = getBestiary(c, id)
if err != nil {
c.Error(err)
return
}
return
}
page := 1
pageStr, ok := c.GetQuery("page")
if ok {
page, err = strconv.Atoi(pageStr)
if err != nil {
page = 1
}
}
if page < 1 {
page = 1
}
limit := 10
npcs := []model.NpcTypes{}
err = config.ContentDB.Limit(limit).Offset(limit*page - 1).Find(&npcs).Error
if err != nil {
c.Error(fmt.Errorf("find NpcTypes: %w", err))
return
}
site := site.Fetch()
site.Title = "Bestiary List"
previousDisplay := fmt.Sprintf("%d", page-1)
previous := fmt.Sprintf("/bestiary?page=%s", previousDisplay)
if page == 1 {
previous = ""
previousDisplay = ""
}
site.Page.Generate("/bestiary", "Start", previous, previousDisplay, fmt.Sprintf("%d", page), fmt.Sprintf("/bestiary?page=%d", page+1), fmt.Sprintf("%d", page+1))
c.HTML(http.StatusOK, "bestiary_list.tmpl", gin.H{
"Title": "BountyEQ",
"Npcs": npcs,
"Site": site,
})
}
func getBestiary(c *gin.Context, strID string) error {
description := ""
id, err := strconv.Atoi(strID)
if err != nil {
return fmt.Errorf("parse id %s: %w", strID, err)
}
if id == 0 {
return fmt.Errorf("invalid id %s", strID)
}
npc := &model.NpcTypes{}
config.ContentDB.Where("id = ?", id).First(npc)
if err != nil {
return fmt.Errorf("could not find NPC ID %d: %w", id, err)
}
description += fmt.Sprintf("<p>%s is a level %d %s %s that hits for %d to %d damage and has %d hitpoints.</p>", npc.Name, npc.Level, npc.ClassName(), npc.RaceName(), npc.Mindmg, npc.Maxdmg, npc.Hp)
spells := []*model.SpellsNew{}
if npc.NpcSpellsID > 0 {
description += fmt.Sprintf("<p>%s is known to", npc.Name)
npcSpell := model.NpcSpells{}
config.ContentDB.Where("id = ?", npc.NpcSpellsID).Find(&npcSpell)
if npcSpell.AttackProc > 0 {
spell := &model.SpellsNew{}
config.ContentDB.Where("id = ?", npcSpell.AttackProc).First(spell)
description += fmt.Sprintf(" proc <a href='/spell/%d'>%s</a> while attacking, which has a %d%% chance to trigger a %s", spell.ID, spell.Name.String, npcSpell.ProcChance, spell.Summary())
spell.Name.String = fmt.Sprintf("%s (Attack Proc)", spell.Name.String)
spells = append(spells, spell)
}
if npcSpell.DefensiveProc > 0 {
spell := &model.SpellsNew{}
config.ContentDB.Where("id = ?", npcSpell.DefensiveProc).First(spell)
description += fmt.Sprintf(" proc <a href='/spell/%d'>%s</a> while being attacked, which has a %d%% chance to trigger a %s", spell.ID, spell.Name.String, npcSpell.DprocChance, spell.Summary())
spell.Name.String = fmt.Sprintf("%s (Defensive Proc)", spell.Name.String)
spells = append(spells, spell)
}
if npcSpell.AttackProc > 0 {
description += " as well as cast "
} else {
description += " cast "
}
spellEntries := []model.NpcSpellsEntries{}
config.ContentDB.Where("npc_spells_id = ?", npc.NpcSpellsID).Find(&spellEntries)
for i, se := range spellEntries {
spell := &model.SpellsNew{}
config.ContentDB.Where("id = ?", se.Spellid).First(spell)
spells = append(spells, spell)
if i == len(spellEntries)-1 {
description += fmt.Sprintf("and <a href='/spell/%d'>%s</a> causing %s", spell.ID, spell.Name.String, spell.Summary())
} else {
description += fmt.Sprintf("<a href='/spell/%d'>%s</a> causing %s, ", spell.ID, spell.Name.String, spell.Summary())
}
}
description += ".</p>"
}
if npc.NpcSpellsEffectsID > 0 {
spellEntries := []model.NpcSpellsEffectsEntries{}
config.ContentDB.Where("npc_spells_effect_id = ?", npc.NpcSpellsEffectsID).Find(&spellEntries)
for _, se := range spellEntries {
spell := &model.SpellsNew{}
config.ContentDB.Where("id = ?", se.SpellEffectID).First(spell)
spells = append(spells, spell)
}
}
factions := []*FactionEntry{}
if npc.NpcFactionID > 0 {
npcFactions := []*model.NpcFactionEntries{}
config.ContentDB.Where("npc_faction_id = ?", npc.NpcFactionID).Find(&npcFactions)
for _, faction := range npcFactions {
npcFaction := model.FactionList{}
config.ContentDB.Where("id = ?", faction.FactionID).First(&npcFaction)
f := &FactionEntry{
ID: faction.FactionID,
Name: npcFaction.Name,
Value: faction.NpcValue,
}
factions = append(factions, f)
}
}
items := []*model.Items{}
if npc.LoottableID > 0 {
rare := ""
lootTables := []*model.LoottableEntries{}
config.ContentDB.Where("loottable_id = ?", npc.LoottableID).Find(&lootTables)
for _, lt := range lootTables {
lootDrops := []*model.LootdropEntries{}
config.ContentDB.Where("lootdrop_id = ?", lt.LootdropID).Find(&lootDrops)
for _, ld := range lootDrops {
item := &model.Items{}
config.ContentDB.Where("id = ?", ld.ItemID).First(item)
items = append(items, item)
if ld.Chance < 0.5 {
if len(rare) == 0 {
rare = ", with rare drops including "
}
rare += fmt.Sprintf("<a href='/item/%d'>%s</a>, ", item.ID, item.Name)
}
}
}
description += fmt.Sprintf("<p>%s has a drop pool of %d total items%s.</p>", npc.Name, len(items), rare)
}
site := site.Fetch()
site.Title = sanitize.CleanName(npc.Name)
c.HTML(http.StatusOK, "bestiary_view.tmpl", gin.H{
"Npc": npc,
"Spells": spells,
"Factions": factions,
"Items": items,
"Description": template.HTML(description),
"Site": site,
})
return nil
}