-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_gui.go
397 lines (333 loc) · 8.72 KB
/
web_gui.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package main
import (
"errors"
"html/template"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/gosimple/slug"
"github.com/russross/blackfriday/v2"
"github.com/toferc/rq_web/models"
"github.com/toferc/runequest"
)
// WebView is a container for Web_gui data
type WebView struct {
SessionUser string
User *models.User
Characters []*models.CharacterModel
CharacterModel *models.CharacterModel
IsLoggedIn string
IsAdmin string
IsAuthor bool
Actor []*models.CharacterModel
Notes []*models.Note
Note *models.Note
Seasons []string
Weeks []string
Index string
}
// WebUser represents a generic user struct
type WebUser struct {
IsAuthor bool
SessionUser string
IsLoggedIn string
IsAdmin string
Users []*models.User
UserCount int
CharacterCount int
Index string
Limit int
Offset int
}
// WebChar is a framework to send objects & data to a Web view
type WebChar struct {
User *models.User
CharacterModel *models.CharacterModel
HomelandModel *models.HomelandModel
OccupationModel *models.OccupationModel
CultModel *models.CultModel
Faction *models.Faction
Encounter *models.Encounter
Campaign *models.Campaign
IsAuthor bool
SessionUser string
IsLoggedIn string
IsAdmin string
IsAuthorized string
Wounds map[string][]int
// IndexModels
CharacterModels []*models.CharacterModel
HomelandModels map[string]*models.HomelandModel
OccupationModels map[string]*models.OccupationModel
CultModels map[string]*models.CultModel
Factions []*models.Faction
FactionCharacters []*models.CharacterModel
Encounters []*models.Encounter
FactionMap map[string][]*models.CharacterModel
Campaigns []*models.Campaign
Passions []string
CategoryOrder []string
WeaponCategories []string
Roles []string
BaseWeapons []*runequest.Weapon
MeleeAttacks map[string]*runequest.Attack
RangedAttacks map[string]*runequest.Attack
StandardsOfLiving []string
PowerRunes []string
ElementalRunes []string
Skills map[string]runequest.Skill
SpiritMagic []runequest.Spell
RuneSpells []runequest.Spell
TotalSpiritMagic []*runequest.Spell
TotalRuneSpells []*runequest.Spell
NumRunePoints []int
NumSpiritMagic []int
Counter []int
MidCounter []int
BigCounter []int
RuneArray []string
StringArray []string
StatBlock string
HitLocationForms map[string]map[string]*runequest.HitLocation
Flashes []interface{}
UserCount int
CharacterCount int
// Search and database
Query string
Offset int
Limit int
Index string
}
// SplitLines transfomrs results text string into slice
func SplitLines(s string) []string {
sli := strings.Split(s, "/n")
return sli
}
func sliceString(s string, i int) string {
l := len(s)
if l > i {
return s[:i] + "..."
}
return s[:l]
}
func skillRoll(id int64, sk *runequest.Skill) string {
text := ""
return text
}
func statRoll(id int64, s *runequest.Statistic) string {
text := ""
return text
}
func subtract(a, b int) int {
return a - b
}
func add(a, b int) int {
return a + b
}
func multiply(a, b int) int {
return a * b
}
func isIn(s []int, t int) bool {
for _, n := range s {
if n == t {
return true
}
}
return false
}
func isInString(s []string, t string) bool {
for _, n := range s {
if n == t {
return true
}
}
return false
}
func statBlock(c *runequest.Character) string {
return c.StatBlock()
}
func formatStringArray(a []string) string {
text := ""
end := len(a)
for i, t := range a {
if i+1 == end {
text += t
} else {
text += t + ", "
}
}
return text
}
func formatSpellArray(sa map[string]runequest.Spell) string {
text := ""
end := len(sa)
counter := 0
for _, v := range sa {
if counter+1 == end {
text += v.String()
} else {
text += v.String() + ", "
counter++
}
}
return text
}
func formatIntArray(a []int) string {
text := strconv.Itoa(a[0])
end := len(a)
if len(a) > 1 {
for i, t := range a {
if i+1 == end {
str := strconv.Itoa(t)
text += "-" + str
}
}
}
return text
}
func indexSpell(str string, spells []runequest.Spell) (int, error) {
err := errors.New("spell not found")
for i, spell := range spells {
if str == spell.CoreString {
return i, nil
}
}
return 0, err
}
// Skills Functions
func sortedSkills(skills map[string]*runequest.Skill) []*runequest.Skill {
skillArray := []*runequest.Skill{}
for _, v := range skills {
v.UpdateSkill()
skillArray = append(skillArray, v)
}
total := func(s1, s2 *runequest.Skill) bool {
return s1.Total > s2.Total
}
By(total).Sort(skillArray)
if len(skillArray) < 9 {
return skillArray
}
return skillArray
}
// By is the type of a "less" function that defines the ordering of its Planet arguments.
type By func(s1, s2 *runequest.Skill) bool
// Sort is a method on the function type, By, that sorts the argument slice according to the function.
func (by By) Sort(skills []*runequest.Skill) {
ss := &skillSorter{
skills: skills,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Sort(ss)
}
// skillSorter joins a By function and a slice of Planets to be sorted.
type skillSorter struct {
skills []*runequest.Skill
by func(p1, p2 *runequest.Skill) bool // Closure used in the Less method.
}
// Len is part of sort.Interface.
func (s *skillSorter) Len() int {
return len(s.skills)
}
// Swap is part of sort.Interface.
func (s *skillSorter) Swap(i, j int) {
s.skills[i], s.skills[j] = s.skills[j], s.skills[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *skillSorter) Less(i, j int) bool {
return s.by(s.skills[i], s.skills[j])
}
func splitSkills(skills map[string]*runequest.Skill) []map[string]*runequest.Skill {
split := []map[string]*runequest.Skill{
{},
{},
{},
}
if len(skills) < 20 {
split[2] = skills
return split
}
for k, v := range skills {
updates := 0
for _, u := range v.Updates {
updates += u.Value
}
switch {
case (v.HomelandValue+v.OccupationValue+v.CultValue+updates == 0 || v.Base+v.CategoryValue == v.Total) && v.Total < 41:
split[0][k] = v
case v.Total < 41:
split[1][k] = v
default:
split[2][k] = v
}
}
return split
}
// Other Functions
// Generate URL for next step of Character creation
func generateCharacterCreationURL(cStep map[string]bool) string {
url := ""
switch {
case !cStep["Personal History"]:
url = "cc12_personal_history"
case !cStep["Rune Affinities"]:
url = "cc2_choose_runes"
case !cStep["Roll Stats"]:
url = "cc3_roll_stats"
case !cStep["Apply Homeland"]:
url = "cc4_apply_homeland"
case !cStep["Apply Occupation"]:
url = "cc5_apply_occupation"
case !cStep["Apply Cult"]:
url = "cc6_apply_cult"
case !cStep["Personal Skills"]:
url = "cc7_personal_skills"
case !cStep["Finishing Touches"]:
url = "cc8_finishing_touches"
}
return url
}
func slugify(st string) string {
return slug.Make(st)
}
func toMarkdown(st string) template.HTML {
input := []byte(st)
output := template.HTML(blackfriday.Run(input))
return output
}
func renderDate(dt time.Time) string {
date := dt.Format("2006/01/02")
return date
}
// Render combines templates, funcs and renders all Web pages in the app
func Render(w http.ResponseWriter, filename string, data interface{}) {
tmpl := make(map[string]*template.Template)
// Set up FuncMap
funcMap := template.FuncMap{
"skillRoll": skillRoll,
"statRoll": statRoll,
"subtract": subtract,
"add": add,
"multiply": multiply,
"isIn": isIn,
"sliceString": sliceString,
"isInString": isInString,
"generateCharacterCreationURL": generateCharacterCreationURL,
"formatStringArray": formatStringArray,
"formatIntArray": formatIntArray,
"formatSpellArray": formatSpellArray,
"sortedSkills": sortedSkills,
"splitSkills": splitSkills,
"slugify": slugify,
"toMarkdown": toMarkdown,
"statBlock": statBlock,
"renderDate": renderDate,
}
baseTemplate := "templates/layout.html"
tmpl[filename] = template.Must(template.New("").Funcs(funcMap).ParseFiles(filename, baseTemplate))
if err := tmpl[filename].ExecuteTemplate(w, "base", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}