/
cmd_scoreboard.go
195 lines (156 loc) · 3.89 KB
/
cmd_scoreboard.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 (
"encoding/json"
"fmt"
"os"
"sort"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
"github.com/nsec/askgod/api"
"github.com/nsec/askgod/internal/utils"
)
// Sorting
type byPointsAndLastSubmitTime []api.ScoreboardEntry
func (a byPointsAndLastSubmitTime) Len() int {
return len(a)
}
func (a byPointsAndLastSubmitTime) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a byPointsAndLastSubmitTime) Less(i, j int) bool {
if a[i].Value != a[j].Value {
return a[i].Value > a[j].Value
}
return a[i].LastSubmitTime.Before(a[j].LastSubmitTime)
}
func (c *client) cmdScoreboard(ctx *cli.Context) error {
board := []api.ScoreboardEntry{}
const layout = "2006/01/02 15:04"
drawTable := func(board []api.ScoreboardEntry) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Team", "Points", "Last submit"})
table.SetBorder(false)
table.SetAutoWrapText(false)
for _, entry := range board {
lastSubmitTime := "never"
if !entry.LastSubmitTime.IsZero() {
lastSubmitTime = entry.LastSubmitTime.Local().Format(layout)
}
table.Append([]string{
fmt.Sprintf("<%s> %s ", entry.Team.Country, entry.Team.Name),
fmt.Sprintf("%d", entry.Value),
lastSubmitTime,
})
}
table.Render()
}
if ctx.Bool("live") {
// Setup websocket connection
chReady := make(chan bool, 1)
chUpdate := make(chan bool, 1)
conn, err := c.websocket("/events?type=timeline")
if err != nil {
return err
}
go func() {
<-chReady
for {
_, data, err := conn.ReadMessage()
if err != nil {
close(chUpdate)
break
}
event := api.Event{}
err = json.Unmarshal(data, &event)
if err != nil {
continue
}
entry := api.EventTimeline{}
err = json.Unmarshal(event.Metadata, &entry)
if err != nil {
continue
}
// Ignore events we don't care about
if !utils.StringInSlice(entry.Type, []string{"reload", "team-updated", "team-removed", "score-updated"}) {
continue
}
// Server requests a reload of the data
if entry.Type == "reload" {
// Get a new dump
board = []api.ScoreboardEntry{}
err = c.queryStruct("GET", "/scoreboard", nil, &board)
if err != nil {
close(chUpdate)
break
}
}
// Try to find the line
found := false
for i, line := range board {
if line.Team.ID != entry.TeamID {
continue
}
// Update an existing existing
found = true
// Team is completely gone
if entry.Type == "team-removed" {
copy(board[i:], board[i+1:])
board = board[:len(board)-1]
break
}
// Team may have changed
if entry.Team != nil {
board[i].Team = api.Team{TeamPut: *entry.Team, ID: entry.TeamID}
}
// Score may have changed
if entry.Score != nil {
board[i].Value = entry.Score.Total
board[i].LastSubmitTime = event.Timestamp
}
found = true
break
}
// Add a new line
if !found && entry.Team != nil {
newEntry := api.ScoreboardEntry{
Team: api.Team{TeamPut: *entry.Team, ID: entry.TeamID},
LastSubmitTime: event.Timestamp,
}
if entry.Score != nil {
newEntry.Value = entry.Score.Total
}
board = append(board, newEntry)
}
// Sort the updated board ourselves
sort.Sort(byPointsAndLastSubmitTime(board))
chUpdate <- true
}
}()
// Get the initial data
err = c.queryStruct("GET", "/scoreboard", nil, &board)
if err != nil {
return err
}
// Ready to get websocket events
close(chReady)
// Refresh loop
for {
fmt.Print("\033[H\033[2J")
drawTable(board)
// Wait for an update
_, ok := <-chUpdate
if !ok {
break
}
}
} else {
// Get the data
err := c.queryStruct("GET", "/scoreboard", nil, &board)
if err != nil {
return err
}
sort.Sort(byPointsAndLastSubmitTime(board))
drawTable(board)
}
return nil
}