-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers_stats.go
More file actions
271 lines (227 loc) · 5.25 KB
/
handlers_stats.go
File metadata and controls
271 lines (227 loc) · 5.25 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
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
package main
import (
"fmt"
"math"
"os"
"sort"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
func getEconomyStats(c *gin.Context) {
currencies := getUserCreditData()
// Calculate stats
count := len(currencies)
total := 0.0
for _, currency := range currencies {
total += currency
}
average := total / float64(count)
// Calculate variance
variance := 0.0
for _, currency := range currencies {
variance += math.Pow(currency-average, 2)
}
variance = variance / float64(count)
// Currency conversion rates
c.JSON(200, gin.H{
"average": average,
"total": total,
"variance": variance,
"currency_comparison": gin.H{
"pence": fmt.Sprintf("%.2fp / credit", creditsToPence(1)),
"cents": fmt.Sprintf("%.2f¢ / credit", creditsToCents(1)),
},
})
}
func getUserCreditData() []float64 {
usersMutex.RLock()
defer usersMutex.RUnlock()
currencies := make([]float64, 0)
for _, user := range users {
userCredits := user.GetCredits()
if userCredits < 0 {
continue
}
currencies = append(currencies, userCredits)
}
// Skip calculation if no currency data is available
if len(currencies) == 0 {
return []float64{}
}
return currencies
}
func getPencePerCredit() float64 {
pencePer1000, err := strconv.Atoi(os.Getenv("PENCE_PER_1000"))
if err != nil {
pencePer1000 = 6
}
return float64(pencePer1000)
}
func creditsToPence(credits float64) float64 {
pencePerCredit := getPencePerCredit()
currencies := getUserCreditData()
total := 0.0
for _, currency := range currencies {
total += currency
}
penceRate := roundVal((float64(1000*pencePerCredit)/total)*100) / 100 // pence per credit
return penceRate * credits
}
func creditsToCents(credits float64) float64 {
pencePerCredit := getPencePerCredit()
currencies := getUserCreditData()
total := 0.0
for _, currency := range currencies {
total += currency
}
centsRate := roundVal((float64(1000*1.31*pencePerCredit)/total)*100) / 100 // cents per credit
return centsRate * credits
}
func getUserStats(c *gin.Context) {
usersMutex.RLock()
defer usersMutex.RUnlock()
stats := map[string]int{
"total_users": len(users),
"banned_users": 0,
"active_users": 0,
}
for _, user := range users {
if user.IsBanned() {
stats["banned_users"] += 1
} else {
stats["active_users"] += 1
}
}
c.JSON(200, stats)
}
func findSinceMonth(txs []Transaction, cutoff int64) int {
return sort.Search(len(txs), func(i int) bool {
return txs[i].Timestamp >= cutoff
})
}
func getMostGained(c *gin.Context) {
usersMutex.RLock()
defer usersMutex.RUnlock()
max := c.Query("max")
if max == "" {
max = "10"
}
maxInt, err := strconv.Atoi(max)
if err != nil {
c.JSON(400, gin.H{"error": "invalid max parameter"})
return
}
if maxInt > 10 {
maxInt = 10
}
monthAgo := time.Now().AddDate(0, -1, 0).UnixMilli()
type result struct {
User Username `json:"user"`
Earned float64 `json:"earned"`
}
leaderboard := make([]result, 0, len(users))
for _, user := range users {
if user.IsBanned() || user.IsPrivate() {
continue
}
txs := user.GetTransactions()
if len(txs) == 0 {
continue
}
var earned float64
start := findSinceMonth(txs, monthAgo)
for _, tx := range txs[start:] {
t := tx.Timestamp
if t < monthAgo {
continue
}
amt := tx.Amount
if amt <= 0 {
continue
}
typ := tx.Type
switch typ {
case "in", "key_sale", "tax":
earned += amt
case "out":
earned -= amt
}
}
if earned > 0 {
leaderboard = append(leaderboard, result{
User: user.GetUsername(),
Earned: earned,
})
}
}
sort.Slice(leaderboard, func(i, j int) bool {
return leaderboard[i].Earned > leaderboard[j].Earned
})
if len(leaderboard) > maxInt {
leaderboard = leaderboard[:maxInt]
}
c.JSON(200, leaderboard)
}
func getSystemStats(c *gin.Context) {
usersMutex.RLock()
defer usersMutex.RUnlock()
systems := make(map[string]int)
for _, user := range users {
if user.IsBanned() || user.IsPrivate() {
continue
}
systems[user.GetSystem()]++
}
if len(systems) == 0 {
c.JSON(404, gin.H{"error": "No system data available"})
return
}
c.JSON(200, systems)
}
func getFollowersStats(c *gin.Context) {
maxNum := c.Query("max")
if maxNum == "" {
maxNum = "10"
}
max, err := strconv.Atoi(maxNum)
if err != nil || max <= 0 {
max = 10
}
if max > 100 {
max = 100
}
type followerStats struct {
Username Username `json:"username"`
FollowerCount int `json:"follower_count"`
}
followersList := make([]followerStats, 0, max*2)
userStatusMap := make(map[Username]bool)
usersMutex.RLock()
for _, user := range users {
if user.IsBanned() || user.IsPrivate() {
continue
}
username := user.GetUsername()
userStatusMap[username.ToLower()] = true
}
usersMutex.RUnlock()
followersMutex.RLock()
for userId, data := range followersData {
username := userId.User().GetUsername()
if userStatusMap[username.ToLower()] {
followersList = append(followersList, followerStats{
Username: username,
FollowerCount: len(data.Followers),
})
}
}
followersMutex.RUnlock()
sort.Slice(followersList, func(i, j int) bool {
return followersList[i].FollowerCount > followersList[j].FollowerCount
})
if len(followersList) > max {
followersList = followersList[:max]
}
c.JSON(200, followersList)
}