-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
156 lines (124 loc) · 4.05 KB
/
templates.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
// templates
package main
import (
"fmt"
"html/template"
"net/http"
"strconv"
"strings"
"github.com/dustin/go-humanize"
"github.com/morpheusxaut/lootsheeter/models"
)
var (
templates = template.Must(template.New("").Funcs(TemplateFunctions(nil)).ParseGlob("web/template/*"))
)
func TemplateFunctions(r *http.Request) template.FuncMap {
return template.FuncMap{
"FormatFloat": func(f float64) string { return FormatFloat(f) },
"IsPositiveFloat": func(f float64) bool { return IsPositiveFloat(f) },
"FloatEquals": func(f1 float64, f2 float64) bool { return FloatEquals(f1, f2) },
"HasFleetRole": func(fleet *models.Fleet, roleInt int) bool { return HasFleetRole(r, fleet, roleInt) },
"MemberHasFleetRole": func(member *models.FleetMember, roleInt int) bool { return MemberHasFleetRole(member, roleInt) },
"IsFleetCommander": func(fleet *models.Fleet) bool { return IsFleetCommander(r, fleet) },
"IsReportCreator": func(report *models.Report) bool { return IsReportCreator(r, report) },
"IsPlayerName": func(name string) bool { return IsPlayerName(r, name) },
"HasAccessMask": func(accessMask models.AccessMask) bool { return HasAccessMask(r, accessMask) },
"HasHigherAccessMask": func(accessMask models.AccessMask) bool { return HasHigherAccessMask(r, accessMask) },
"GetFleetRolePaymentModifier": func(role *models.FleetRole) float64 { return GetFleetRolePaymentModifier(role) },
}
}
func FormatFloat(f float64) string {
fString := humanize.Ftoa(f)
var formattedFloat string
if strings.Contains(fString, ".") {
fInt, err := strconv.ParseInt(fString[:strings.Index(fString, ".")], 10, 64)
if err != nil {
return fString
}
digitsAfterPoint := len(fString) - strings.Index(fString, ".")
if digitsAfterPoint > 3 {
digitsAfterPoint = 3
}
formattedFloat = fmt.Sprintf("%s%s", humanize.Comma(fInt), fString[strings.Index(fString, "."):strings.Index(fString, ".")+digitsAfterPoint])
} else {
fInt, err := strconv.ParseInt(fString, 10, 64)
if err != nil {
return fString
}
formattedFloat = fmt.Sprintf("%s.00", humanize.Comma(fInt))
}
return formattedFloat
}
func IsPositiveFloat(f float64) bool {
return f > 0
}
func FloatEquals(f1 float64, f2 float64) bool {
return f1 == f2
}
func HasFleetRole(r *http.Request, fleet *models.Fleet, roleInt int) bool {
player := session.GetPlayerFromRequest(r)
if player == nil {
return false
}
role, err := fleet.GetMemberRole(player.Name)
if err != nil {
return false
}
return role == models.FleetRole(roleInt)
}
func MemberHasFleetRole(member *models.FleetMember, roleInt int) bool {
return member.Role == models.FleetRole(roleInt)
}
func IsFleetCommander(r *http.Request, fleet *models.Fleet) bool {
player := session.GetPlayerFromRequest(r)
if player == nil {
return false
}
fleetCommanders := fleet.FleetCommanders()
if len(fleetCommanders) == 0 {
return false
}
for _, commander := range fleetCommanders {
if strings.EqualFold(player.Name, commander.Name) {
return true
}
}
return false
}
func IsReportCreator(r *http.Request, report *models.Report) bool {
player := session.GetPlayerFromRequest(r)
if player == nil {
return false
}
if strings.EqualFold(report.Creator.Name, player.Name) {
return true
}
return false
}
func IsPlayerName(r *http.Request, name string) bool {
player := session.GetPlayerFromRequest(r)
if player == nil {
return false
}
if strings.EqualFold(name, player.Name) {
return true
}
return false
}
func HasAccessMask(r *http.Request, accessMask models.AccessMask) bool {
player := session.GetPlayerFromRequest(r)
if player == nil {
return false
}
return (player.AccessMask&accessMask == accessMask)
}
func HasHigherAccessMask(r *http.Request, accessMask models.AccessMask) bool {
player := session.GetPlayerFromRequest(r)
if player == nil {
return false
}
return player.AccessMask >= accessMask
}
func GetFleetRolePaymentModifier(role *models.FleetRole) float64 {
return role.PaymentRate()
}