forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
150 lines (131 loc) · 3.32 KB
/
api.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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Web API for providing JSON-formatted event data.
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"net/http"
)
type MatchWithResult struct {
Match
Result *MatchResult
}
type RankingWithNickname struct {
Ranking
Nickname string
}
// Generates a JSON dump of the matches and results.
func MatchesApiHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsReader(w, r) {
return
}
vars := mux.Vars(r)
matches, err := db.GetMatchesByType(vars["type"])
if err != nil {
handleWebErr(w, err)
return
}
matchesWithResults := make([]MatchWithResult, len(matches))
for i, match := range matches {
matchesWithResults[i].Match = match
matchResult, err := db.GetMatchResultForMatch(match.Id)
if err != nil {
handleWebErr(w, err)
return
}
matchesWithResults[i].Result = matchResult
}
jsonData, err := json.MarshalIndent(matchesWithResults, "", " ")
if err != nil {
handleWebErr(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonData)
if err != nil {
handleWebErr(w, err)
return
}
}
// Generates a JSON dump of the sponsor slides for use by the audience display.
func SponsorSlidesApiHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsReader(w, r) {
return
}
sponsors, err := db.GetAllSponsorSlides()
if err != nil {
handleWebErr(w, err)
return
}
jsonData, err := json.MarshalIndent(sponsors, "", " ")
if err != nil {
handleWebErr(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonData)
if err != nil {
handleWebErr(w, err)
return
}
}
// Generates a JSON dump of the qualification rankings, primarily for use by the pit display.
func RankingsApiHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsReader(w, r) {
return
}
rankings, err := db.GetAllRankings()
if err != nil {
handleWebErr(w, err)
return
}
var rankingsWithNicknames []RankingWithNickname
if rankings == nil {
// Go marshals an empty slice to null, so explicitly create it so that it appears as an empty JSON array.
rankingsWithNicknames = make([]RankingWithNickname, 0)
} else {
rankingsWithNicknames = make([]RankingWithNickname, len(rankings))
}
// Get team info so that nicknames can be displayed.
teams, err := db.GetAllTeams()
if err != nil {
handleWebErr(w, err)
return
}
teamNicknames := make(map[int]string)
for _, team := range teams {
teamNicknames[team.Id] = team.Nickname
}
for i, ranking := range rankings {
rankingsWithNicknames[i] = RankingWithNickname{ranking, teamNicknames[ranking.TeamId]}
}
// Get the last match scored so we can report that on the display.
matches, err := db.GetMatchesByType("qualification")
if err != nil {
handleWebErr(w, err)
return
}
highestPlayedMatch := ""
for _, match := range matches {
if match.Status == "complete" {
highestPlayedMatch = match.DisplayName
}
}
data := struct {
Rankings []RankingWithNickname
HighestPlayedMatch string
}{rankingsWithNicknames, highestPlayedMatch}
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
handleWebErr(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonData)
if err != nil {
handleWebErr(w, err)
return
}
}