forked from johansundell/cocbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopwarplayers.go
More file actions
78 lines (74 loc) · 1.92 KB
/
topwarplayers.go
File metadata and controls
78 lines (74 loc) · 1.92 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
package main
import (
"context"
"fmt"
"regexp"
"sort"
"strconv"
"github.com/johansundell/cocapi"
)
func init() {
key := commandFunc{"!top war players", "To see our top war whores", "", categoryStats}
lockMap.Lock()
defer lockMap.Unlock()
botFuncs[key] = func(ctx context.Context, command string) (string, error) {
if key.command == command {
list, err := cocClient.GetMembers(myClanTag)
if err != nil {
return "", err
}
players := []cocapi.Player{}
for _, v := range list.Items {
p, _ := cocClient.GetPlayerInfo(v.Tag)
players = append(players, p)
}
sort.Slice(players, func(i, j int) bool { return players[i].WarStars > players[j].WarStars })
msg := ""
for i := 0; i < len(players); i++ {
p := players[i]
if i > 5 {
break
}
msg += fmt.Sprintf("%d stars for %s\n", p.WarStars, p.Name)
}
return msg, nil
}
return "", nil
}
key2 := commandFunc{"!top [0-9]+ war players", "To see our top NN war whores", "[0-9]+", categoryStats}
botFuncs[key2] = func(ctx context.Context, command string) (string, error) {
if found, _ := regexp.MatchString(key2.command, command); found {
reg, err := regexp.Compile(key2.extracter)
if err != nil {
return "", err
}
result := reg.FindString(command)
if result != "" {
n, err := strconv.Atoi(result)
if err != nil {
return "", err
}
list, err := cocClient.GetMembers(myClanTag)
if err != nil {
return "", err
}
players := []cocapi.Player{}
for _, v := range list.Items {
p, _ := cocClient.GetPlayerInfo(v.Tag)
players = append(players, p)
}
sort.Slice(players, func(i, j int) bool { return players[i].WarStars > players[j].WarStars })
msg := ""
for i := 0; i < len(players); i++ {
p := players[i]
if i > n-1 {
break
}
msg += fmt.Sprintf("%d stars for %s\n", p.WarStars, p.Name)
}
return msg, nil
}
}
return "", nil
}
}