-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
195 lines (171 loc) · 4.22 KB
/
util.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
package main
import (
"fmt"
"html/template"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/hako/durafmt"
)
// In background, update the server list
func backgroundUpdateList() {
for {
time.Sleep(BGFetchInterval)
FetchLock.Lock()
fetchServerList()
FetchLock.Unlock()
}
}
// Parse the template
func parseTemplate() {
var err error
tmpl, err = template.ParseFiles("data/template.html")
if err != nil {
panic(err)
}
}
// Pretty-print durations
var tUnits durafmt.Units
func setupDurafmt() {
var err error
tUnits, err = durafmt.DefaultUnitsCoder.Decode("yr:yrs,wk:wks,day:days,hr:hrs,min:mins,sec:secs,ms:ms,μs:μs")
if err != nil {
panic(err)
}
}
func (item ServerListItem) hasTag(search string) bool {
for _, tag := range item.Tags {
if strings.EqualFold(tag, search) {
return true
}
}
return false
}
func sortServers(promote bool, list []ServerListItem, sortBy int) []ServerListItem {
if sortBy == SORT_NAME {
sort.Slice(list, func(i, j int) bool {
if promote {
iNum := len(list[i].Players)
jNum := len(list[j].Players)
if iNum > 0 && list[i].hasTag("m45") {
return true
}
if jNum > 0 && list[j].hasTag("m45") {
return false
}
}
return list[i].Name < list[j].Name
})
} else if sortBy == SORT_TIME {
sort.Slice(list, func(i, j int) bool {
return list[i].Local.Minutes < list[j].Local.Minutes
})
} else if sortBy == SORT_RTIME {
sort.Slice(list, func(i, j int) bool {
return list[i].Local.Minutes > list[j].Local.Minutes
})
} else {
sort.Slice(list, func(i, j int) bool {
iNum := len(list[i].Players)
jNum := len(list[j].Players)
if promote {
if iNum > 0 && list[i].hasTag("m45") {
return true
}
if jNum > 0 && list[j].hasTag("m45") {
return false
}
}
if iNum == jNum {
return list[i].Name > list[j].Name
}
return iNum > jNum
})
}
return list
}
// Sort servers by sortBy
func sortVersions(list []VersionData) []VersionData {
sort.Slice(list, func(i, j int) bool {
if list[i].Count > list[j].Count {
return true
}
verA := parseVersion(list[i].Version)
verB := parseVersion(list[j].Version)
if verA.a > verB.a {
return true
}
if verA.a == verB.a && verA.b > verB.b {
return true
}
if verA.a == verB.a && verA.b == verB.b && verA.c > verB.c {
return true
}
return false
})
return list
}
func parseVersion(input string) versionInt {
parts := strings.Split(input, ".")
if len(parts) != 3 {
return versionInt{}
}
a, _ := strconv.ParseInt(parts[0], 10, 64)
b, _ := strconv.ParseInt(parts[1], 10, 64)
c, _ := strconv.ParseInt(parts[2], 10, 64)
return versionInt{a: int(a), b: int(b), c: int(c)}
}
// Update map time (string)
func updateTime(mins int) string {
if mins == 0 {
return "0 min"
}
return durafmt.Parse(time.Duration(mins) * time.Minute).LimitFirstN(2).Format(tUnits)
}
// Safely convert interface{} to integer
func getMinutes(item ServerListItem) int {
played, err := time.ParseDuration(fmt.Sprintf("%vm", item.Game_time_elapsed))
if err == nil {
return int(played.Minutes())
}
return 0
}
// Factorio tag removal
var (
remFactTag *regexp.Regexp = regexp.MustCompile(`\[/[^][]+\]`)
remFactCloseTag *regexp.Regexp = regexp.MustCompile(`\[(.*?)=(.*?)\]`)
)
func RemoveFactorioTags(input string) string {
buf := input
buf = remFactCloseTag.ReplaceAllString(buf, "")
buf = remFactTag.ReplaceAllString(buf, "")
buf = strings.Replace(buf, "\n\r", "\n", -1)
buf = strings.Replace(buf, "\r", "\n", -1)
buf = strings.Replace(buf, "\n\n", "\n", -1)
buf = strings.Replace(buf, "\n", " ", -1)
return buf
}
// Generate a quick-connect link
func MakeSteamURL(host string) string {
buf := fmt.Sprintf("https://go-game.net/gosteam/427520.--mp-connect%%20%v", host)
return buf
}
func getVersions() {
versionList := []VersionData{}
for _, server := range sParam.ServerList.Servers {
foundVersion := false
for v, vItem := range versionList {
if server.Application_version.Game_version == vItem.Version {
foundVersion = true
versionList[v].Count++
break
}
}
if !foundVersion {
versionList = append(versionList, VersionData{Version: server.Application_version.Game_version, Count: 1})
}
}
sParam.VersionList = sortVersions(versionList)
}