-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_scoreboard.go
70 lines (58 loc) · 1.9 KB
/
api_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
package rest
import (
"database/sql"
"net/http"
"github.com/inconshreveable/log15"
"github.com/nsec/askgod/api"
"github.com/nsec/askgod/internal/utils"
)
func (r *rest) getScoreboard(writer http.ResponseWriter, request *http.Request, logger log15.Logger) {
// If scoreboard hidden and not a team, show empty board
if r.config.Scoring.HideOthers && !r.hasAccess("team", request) {
r.jsonResponse([]api.ScoreboardEntry{}, writer, request)
return
}
// Get the full scoreboard
scoreboard, err := r.db.GetScoreboard()
if err != nil {
logger.Error("Failed to get the scoreboard", log15.Ctx{"error": err})
r.errorResponse(500, "Internal Server Error", writer, request)
return
}
// Filter the results
if (r.config.Scoring.HideOthers || len(r.hiddenTeams) > 0) && !r.hasAccess("admin", request) {
// Extract the client IP
ip, err := r.getIP(request)
if err != nil {
logger.Error("Failed to get the client's IP", log15.Ctx{"error": err})
r.errorResponse(500, "Internal Server Error", writer, request)
return
}
// Look for a matching team
var team *api.AdminTeam
if r.hasAccess("team", request) {
team, err = r.db.GetTeamForIP(*ip)
if err == sql.ErrNoRows {
logger.Warn("No team found for IP", log15.Ctx{"ip": ip.String()})
r.errorResponse(404, "No team found for IP", writer, request)
return
} else if err != nil {
logger.Error("Failed to get the team", log15.Ctx{"error": err})
r.errorResponse(500, "Internal Server Error", writer, request)
return
}
}
newBoard := []api.ScoreboardEntry{}
for _, entry := range scoreboard {
if r.config.Scoring.HideOthers && (team == nil || entry.Team.ID != team.ID) {
continue
}
if utils.Int64InSlice(entry.Team.ID, r.hiddenTeams) && (team == nil || team.ID != entry.Team.ID) {
continue
}
newBoard = append(newBoard, entry)
}
scoreboard = newBoard
}
r.jsonResponse(scoreboard, writer, request)
}