blob: d1907c7145f1c8741f8520f5f80f8e4265e9e4e8 [file] [log] [blame]
Philipp Schradercdb5cfc2022-02-20 14:57:07 -08001package requests
2
3import (
Philipp Schraderfae8a7e2022-03-13 22:51:54 -07004 "encoding/base64"
Philipp Schraderd3fac192022-03-02 20:35:46 -08005 "errors"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -08006 "fmt"
7 "io"
Philipp Schraderfae8a7e2022-03-13 22:51:54 -07008 "log"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -08009 "net/http"
Emily Markovabf24c9e2023-02-08 20:31:11 -080010 "sort"
Philipp Schraderd3fac192022-03-02 20:35:46 -080011 "strconv"
12 "strings"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080013
Philipp Schrader8747f1b2022-02-23 23:56:22 -080014 "github.com/frc971/971-Robot-Code/scouting/db"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080015 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/error_response"
Emily Markova290147d2023-03-03 22:40:06 -080016 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_2023_data_scouting"
17 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_2023_data_scouting_response"
Filip Kujawaf882e022022-12-14 13:14:08 -080018 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_driver_rankings"
19 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_driver_rankings_response"
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080020 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches"
21 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches_response"
Filip Kujawaf882e022022-12-14 13:14:08 -080022 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_notes"
23 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_notes_response"
Alex Perry81f96ba2022-03-13 18:26:19 -070024 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_notes_for_team"
25 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_notes_for_team_response"
Milo Lin1d59f0c2022-06-22 20:30:58 -070026 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_shift_schedule"
27 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_shift_schedule_response"
Sabina Leaver759090b2023-01-14 20:42:56 -080028 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_actions"
29 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_actions_response"
Filip Kujawa210a03b2022-11-24 14:41:11 -080030 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_driver_ranking"
31 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_driver_ranking_response"
Alex Perry81f96ba2022-03-13 18:26:19 -070032 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_notes"
33 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_notes_response"
Milo Lin1d59f0c2022-06-22 20:30:58 -070034 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_shift_schedule"
35 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_shift_schedule_response"
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080036 "github.com/frc971/971-Robot-Code/scouting/webserver/server"
37 flatbuffers "github.com/google/flatbuffers/go"
38)
39
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080040type RequestAllMatches = request_all_matches.RequestAllMatches
41type RequestAllMatchesResponseT = request_all_matches_response.RequestAllMatchesResponseT
Filip Kujawaf882e022022-12-14 13:14:08 -080042type RequestAllDriverRankings = request_all_driver_rankings.RequestAllDriverRankings
43type RequestAllDriverRankingsResponseT = request_all_driver_rankings_response.RequestAllDriverRankingsResponseT
44type RequestAllNotes = request_all_notes.RequestAllNotes
45type RequestAllNotesResponseT = request_all_notes_response.RequestAllNotesResponseT
Emily Markova290147d2023-03-03 22:40:06 -080046type Request2023DataScouting = request_2023_data_scouting.Request2023DataScouting
47type Request2023DataScoutingResponseT = request_2023_data_scouting_response.Request2023DataScoutingResponseT
Alex Perry81f96ba2022-03-13 18:26:19 -070048type SubmitNotes = submit_notes.SubmitNotes
49type SubmitNotesResponseT = submit_notes_response.SubmitNotesResponseT
50type RequestNotesForTeam = request_notes_for_team.RequestNotesForTeam
51type RequestNotesForTeamResponseT = request_notes_for_team_response.RequestNotesForTeamResponseT
Milo Lin1d59f0c2022-06-22 20:30:58 -070052type RequestShiftSchedule = request_shift_schedule.RequestShiftSchedule
53type RequestShiftScheduleResponseT = request_shift_schedule_response.RequestShiftScheduleResponseT
54type SubmitShiftSchedule = submit_shift_schedule.SubmitShiftSchedule
55type SubmitShiftScheduleResponseT = submit_shift_schedule_response.SubmitShiftScheduleResponseT
Filip Kujawa210a03b2022-11-24 14:41:11 -080056type SubmitDriverRanking = submit_driver_ranking.SubmitDriverRanking
57type SubmitDriverRankingResponseT = submit_driver_ranking_response.SubmitDriverRankingResponseT
Sabina Leaver759090b2023-01-14 20:42:56 -080058type SubmitActions = submit_actions.SubmitActions
Sabina Leaver9b4eb312023-02-20 19:58:17 -080059type Action = submit_actions.Action
Sabina Leaver759090b2023-01-14 20:42:56 -080060type SubmitActionsResponseT = submit_actions_response.SubmitActionsResponseT
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080061
Philipp Schrader8747f1b2022-02-23 23:56:22 -080062// The interface we expect the database abstraction to conform to.
63// We use an interface here because it makes unit testing easier.
64type Database interface {
Emily Markovabf24c9e2023-02-08 20:31:11 -080065 AddToMatch(db.TeamMatch) error
Milo Lin1d59f0c2022-06-22 20:30:58 -070066 AddToShift(db.Shift) error
Emily Markova290147d2023-03-03 22:40:06 -080067 AddToStats2023(db.Stats2023) error
Emily Markovabf24c9e2023-02-08 20:31:11 -080068 ReturnMatches() ([]db.TeamMatch, error)
Filip Kujawaf882e022022-12-14 13:14:08 -080069 ReturnAllNotes() ([]db.NotesData, error)
70 ReturnAllDriverRankings() ([]db.DriverRankingData, error)
Milo Lin1d59f0c2022-06-22 20:30:58 -070071 ReturnAllShifts() ([]db.Shift, error)
Emily Markova290147d2023-03-03 22:40:06 -080072 ReturnStats2023() ([]db.Stats2023, error)
Philipp Schrader0f7b6362023-03-11 14:02:48 -080073 ReturnStats2023ForTeam(teamNumber string, matchNumber int32, setNumber int32, compLevel string) ([]db.Stats2023, error)
Milo Lin1d59f0c2022-06-22 20:30:58 -070074 QueryAllShifts(int) ([]db.Shift, error)
Philipp Schradereecb8962022-06-01 21:02:42 -070075 QueryNotes(int32) ([]string, error)
Filip Kujawaf947cb42022-11-21 10:00:30 -080076 AddNotes(db.NotesData) error
Filip Kujawa210a03b2022-11-24 14:41:11 -080077 AddDriverRanking(db.DriverRankingData) error
Sabina Leaver9b4eb312023-02-20 19:58:17 -080078 AddAction(db.Action) error
Philipp Schrader8747f1b2022-02-23 23:56:22 -080079}
80
Philipp Schradercdb5cfc2022-02-20 14:57:07 -080081// Handles unknown requests. Just returns a 404.
82func unknown(w http.ResponseWriter, req *http.Request) {
83 w.WriteHeader(http.StatusNotFound)
84}
85
86func respondWithError(w http.ResponseWriter, statusCode int, errorMessage string) {
87 builder := flatbuffers.NewBuilder(1024)
88 builder.Finish((&error_response.ErrorResponseT{
89 ErrorMessage: errorMessage,
90 }).Pack(builder))
91 w.WriteHeader(statusCode)
92 w.Write(builder.FinishedBytes())
93}
94
95func respondNotImplemented(w http.ResponseWriter) {
96 respondWithError(w, http.StatusNotImplemented, "")
97}
98
Philipp Schraderb7e75932022-03-26 16:18:34 -070099func parseRequest[T interface{}](w http.ResponseWriter, buf []byte, requestName string, parser func([]byte, flatbuffers.UOffsetT) *T) (*T, bool) {
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800100 success := true
101 defer func() {
102 if r := recover(); r != nil {
Philipp Schraderb7e75932022-03-26 16:18:34 -0700103 respondWithError(w, http.StatusBadRequest, fmt.Sprintf("Failed to parse %s: %v", requestName, r))
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800104 success = false
105 }
106 }()
Philipp Schraderb7e75932022-03-26 16:18:34 -0700107 result := parser(buf, 0)
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800108 return result, success
109}
110
Philipp Schraderfae8a7e2022-03-13 22:51:54 -0700111// Parses the authorization information that the browser inserts into the
112// headers. The authorization follows this format:
113//
Philipp Schrader35bb1532023-03-05 13:49:12 -0800114// req.Headers["Authorization"] = []string{"Basic <base64 encoded username:password>"}
Philipp Schraderfae8a7e2022-03-13 22:51:54 -0700115func parseUsername(req *http.Request) string {
116 auth, ok := req.Header["Authorization"]
117 if !ok {
118 return "unknown"
119 }
120
121 parts := strings.Split(auth[0], " ")
122 if !(len(parts) == 2 && parts[0] == "Basic") {
123 return "unknown"
124 }
125
126 info, err := base64.StdEncoding.DecodeString(parts[1])
127 if err != nil {
128 log.Println("ERROR: Failed to parse Basic authentication.")
129 return "unknown"
130 }
131
132 loginParts := strings.Split(string(info), ":")
133 if len(loginParts) != 2 {
134 return "unknown"
135 }
136 return loginParts[0]
137}
138
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800139// Handles a RequestAllMaches request.
140type requestAllMatchesHandler struct {
141 db Database
142}
143
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800144// Change structure of match objects in the database(1 per team) to
145// the old match structure(1 per match) that the webserver uses.
146// We use the information in this struct to identify which match object
147// corresponds to which old match structure object.
148type MatchAssemblyKey struct {
149 MatchNumber int32
150 SetNumber int32
151 CompLevel string
152}
153
Emily Markovabf24c9e2023-02-08 20:31:11 -0800154func findIndexInList(list []string, comp_level string) (int, error) {
155 for index, value := range list {
156 if value == comp_level {
157 return index, nil
158 }
159 }
160 return -1, errors.New(fmt.Sprint("Failed to find comp level ", comp_level, " in list ", list))
161}
162
Emily Markovab8551572023-03-22 19:49:39 -0700163func (handler requestAllMatchesHandler) teamHasBeenDataScouted(key MatchAssemblyKey, teamNumber string) (bool, error) {
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800164 stats, err := handler.db.ReturnStats2023ForTeam(
Emily Markovab8551572023-03-22 19:49:39 -0700165 teamNumber, key.MatchNumber, key.SetNumber, key.CompLevel)
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800166 if err != nil {
167 return false, err
168 }
169 return (len(stats) > 0), nil
170}
171
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800172func (handler requestAllMatchesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
173 requestBytes, err := io.ReadAll(req.Body)
174 if err != nil {
175 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
176 return
177 }
178
Philipp Schraderb7e75932022-03-26 16:18:34 -0700179 _, success := parseRequest(w, requestBytes, "RequestAllMatches", request_all_matches.GetRootAsRequestAllMatches)
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800180 if !success {
181 return
182 }
183
184 matches, err := handler.db.ReturnMatches()
185 if err != nil {
Philipp Schraderfae8a7e2022-03-13 22:51:54 -0700186 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to query database: ", err))
Philipp Schrader2e7eb0002022-03-02 22:52:39 -0800187 return
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800188 }
189
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800190 assembledMatches := map[MatchAssemblyKey]request_all_matches_response.MatchT{}
Emily Markovabf24c9e2023-02-08 20:31:11 -0800191
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800192 for _, match := range matches {
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800193 key := MatchAssemblyKey{match.MatchNumber, match.SetNumber, match.CompLevel}
194
195 // Retrieve the converted match structure we have assembled so
196 // far. If we haven't started assembling one yet, then start a
197 // new one.
Emily Markovabf24c9e2023-02-08 20:31:11 -0800198 entry, ok := assembledMatches[key]
199 if !ok {
200 entry = request_all_matches_response.MatchT{
201 MatchNumber: match.MatchNumber,
202 SetNumber: match.SetNumber,
203 CompLevel: match.CompLevel,
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800204 DataScouted: &request_all_matches_response.ScoutedLevelT{},
Emily Markovabf24c9e2023-02-08 20:31:11 -0800205 }
206 }
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800207
Emily Markovab8551572023-03-22 19:49:39 -0700208 var team *string
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800209 var dataScoutedTeam *bool
210
211 // Fill in the field for the match that we have in in the
212 // database. In the database, each match row only has 1 team
213 // number.
Emily Markovabf24c9e2023-02-08 20:31:11 -0800214 switch match.Alliance {
215 case "R":
216 switch match.AlliancePosition {
217 case 1:
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800218 team = &entry.R1
219 dataScoutedTeam = &entry.DataScouted.R1
Emily Markovabf24c9e2023-02-08 20:31:11 -0800220 case 2:
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800221 team = &entry.R2
222 dataScoutedTeam = &entry.DataScouted.R2
Emily Markovabf24c9e2023-02-08 20:31:11 -0800223 case 3:
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800224 team = &entry.R3
225 dataScoutedTeam = &entry.DataScouted.R3
Emily Markovabf24c9e2023-02-08 20:31:11 -0800226 default:
227 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Unknown red position ", strconv.Itoa(int(match.AlliancePosition)), " in match ", strconv.Itoa(int(match.MatchNumber))))
228 return
229 }
230 case "B":
231 switch match.AlliancePosition {
232 case 1:
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800233 team = &entry.B1
234 dataScoutedTeam = &entry.DataScouted.B1
Emily Markovabf24c9e2023-02-08 20:31:11 -0800235 case 2:
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800236 team = &entry.B2
237 dataScoutedTeam = &entry.DataScouted.B2
Emily Markovabf24c9e2023-02-08 20:31:11 -0800238 case 3:
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800239 team = &entry.B3
240 dataScoutedTeam = &entry.DataScouted.B3
Emily Markovabf24c9e2023-02-08 20:31:11 -0800241 default:
242 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Unknown blue position ", strconv.Itoa(int(match.AlliancePosition)), " in match ", strconv.Itoa(int(match.MatchNumber))))
243 return
244 }
245 default:
246 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Unknown alliance ", match.Alliance, " in match ", strconv.Itoa(int(match.AlliancePosition))))
247 return
248 }
Philipp Schrader0f7b6362023-03-11 14:02:48 -0800249
250 *team = match.TeamNumber
251
252 // Figure out if this team has been data scouted already.
253 *dataScoutedTeam, err = handler.teamHasBeenDataScouted(key, match.TeamNumber)
254 if err != nil {
255 respondWithError(w, http.StatusInternalServerError, fmt.Sprint(
256 "Failed to determine data scouting status for team ",
257 strconv.Itoa(int(match.AlliancePosition)),
258 " in match ",
259 strconv.Itoa(int(match.MatchNumber)),
260 err))
261 return
262 }
263
Emily Markovabf24c9e2023-02-08 20:31:11 -0800264 assembledMatches[key] = entry
265 }
266
267 var response RequestAllMatchesResponseT
268 for _, match := range assembledMatches {
269 copied_match := match
270 response.MatchList = append(response.MatchList, &copied_match)
271 }
272
273 var MATCH_TYPE_ORDERING = []string{"qm", "ef", "qf", "sf", "f"}
274
275 err = nil
276 sort.Slice(response.MatchList, func(i, j int) bool {
277 if err != nil {
278 return false
279 }
280 a := response.MatchList[i]
281 b := response.MatchList[j]
282
Emily Markovaabcac6e2023-02-18 17:50:03 -0800283 aMatchTypeIndex, err2 := findIndexInList(MATCH_TYPE_ORDERING, a.CompLevel)
284 if err2 != nil {
285 err = errors.New(fmt.Sprint("Comp level ", a.CompLevel, " not found in sorting list ", MATCH_TYPE_ORDERING, " : ", err2))
Emily Markovabf24c9e2023-02-08 20:31:11 -0800286 return false
287 }
Emily Markovaabcac6e2023-02-18 17:50:03 -0800288 bMatchTypeIndex, err2 := findIndexInList(MATCH_TYPE_ORDERING, b.CompLevel)
289 if err2 != nil {
290 err = errors.New(fmt.Sprint("Comp level ", b.CompLevel, " not found in sorting list ", MATCH_TYPE_ORDERING, " : ", err2))
Emily Markovabf24c9e2023-02-08 20:31:11 -0800291 return false
292 }
293
294 if aMatchTypeIndex < bMatchTypeIndex {
295 return true
296 }
297 if aMatchTypeIndex > bMatchTypeIndex {
298 return false
299 }
300
301 // Then sort by match number. E.g. in semi finals, all match 1 rounds
302 // are done first. Then come match 2 rounds. And then, if necessary,
303 // the match 3 rounds.
304 aMatchNumber := a.MatchNumber
305 bMatchNumber := b.MatchNumber
306 if aMatchNumber < bMatchNumber {
307 return true
308 }
309 if aMatchNumber > bMatchNumber {
310 return false
311 }
312 // Lastly, sort by set number. I.e. Semi Final 1 Match 1 happens first.
313 // Then comes Semi Final 2 Match 1. Then comes Semi Final 1 Match 2. Then
314 // Semi Final 2 Match 2.
315 aSetNumber := a.SetNumber
316 bSetNumber := b.SetNumber
317 if aSetNumber < bSetNumber {
318 return true
319 }
320 if aSetNumber > bSetNumber {
321 return false
322 }
323 return true
324 })
325
326 if err != nil {
327 // check if error happened during sorting and notify webpage if that
328 respondWithError(w, http.StatusInternalServerError, fmt.Sprint(err))
329 return
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800330 }
331
332 builder := flatbuffers.NewBuilder(50 * 1024)
333 builder.Finish((&response).Pack(builder))
334 w.Write(builder.FinishedBytes())
335}
336
Alex Perry81f96ba2022-03-13 18:26:19 -0700337type submitNoteScoutingHandler struct {
338 db Database
339}
340
341func (handler submitNoteScoutingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
342 requestBytes, err := io.ReadAll(req.Body)
343 if err != nil {
344 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
345 return
346 }
347
Philipp Schraderb7e75932022-03-26 16:18:34 -0700348 request, success := parseRequest(w, requestBytes, "SubmitNotes", submit_notes.GetRootAsSubmitNotes)
Alex Perry81f96ba2022-03-13 18:26:19 -0700349 if !success {
350 return
351 }
352
Filip Kujawaf947cb42022-11-21 10:00:30 -0800353 err = handler.db.AddNotes(db.NotesData{
Filip Kujawa7ddd5652023-03-07 19:56:15 -0800354 TeamNumber: request.Team(),
355 Notes: string(request.Notes()),
356 GoodDriving: bool(request.GoodDriving()),
357 BadDriving: bool(request.BadDriving()),
Filip Kujawa11dc4c92023-04-13 08:55:43 -0700358 SolidPlacing: bool(request.SolidPlacing()),
Filip Kujawa7ddd5652023-03-07 19:56:15 -0800359 SketchyPlacing: bool(request.SketchyPlacing()),
360 GoodDefense: bool(request.GoodDefense()),
361 BadDefense: bool(request.BadDefense()),
362 EasilyDefended: bool(request.EasilyDefended()),
Filip Kujawaf947cb42022-11-21 10:00:30 -0800363 })
Alex Perry81f96ba2022-03-13 18:26:19 -0700364 if err != nil {
365 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to insert notes: %v", err))
366 return
367 }
368
369 var response SubmitNotesResponseT
370 builder := flatbuffers.NewBuilder(10)
371 builder.Finish((&response).Pack(builder))
372 w.Write(builder.FinishedBytes())
373}
374
Emily Markova1abe9782023-03-11 19:45:38 -0800375func ConvertActionsToStat(submitActions *submit_actions.SubmitActions) (db.Stats2023, error) {
376 overall_time := int64(0)
377 cycles := int64(0)
378 picked_up := false
379 lastPlacedTime := int64(0)
380 stat := db.Stats2023{TeamNumber: string(submitActions.TeamNumber()), MatchNumber: submitActions.MatchNumber(), SetNumber: submitActions.SetNumber(), CompLevel: string(submitActions.CompLevel()),
381 StartingQuadrant: 0, LowCubesAuto: 0, MiddleCubesAuto: 0, HighCubesAuto: 0, CubesDroppedAuto: 0,
382 LowConesAuto: 0, MiddleConesAuto: 0, HighConesAuto: 0, ConesDroppedAuto: 0, LowCubes: 0, MiddleCubes: 0, HighCubes: 0,
383 CubesDropped: 0, LowCones: 0, MiddleCones: 0, HighCones: 0, ConesDropped: 0, AvgCycle: 0, CollectedBy: string(submitActions.CollectedBy()),
384 }
385 // Loop over all actions.
386 for i := 0; i < submitActions.ActionsListLength(); i++ {
387 var action submit_actions.Action
388 if !submitActions.ActionsList(&action, i) {
389 return db.Stats2023{}, errors.New(fmt.Sprintf("Failed to parse submit_actions.Action"))
390 }
391 actionTable := new(flatbuffers.Table)
392 action_type := action.ActionTakenType()
393 if !action.ActionTaken(actionTable) {
394 return db.Stats2023{}, errors.New(fmt.Sprint("Failed to parse sub-action or sub-action was missing"))
395 }
396 if action_type == submit_actions.ActionTypeStartMatchAction {
397 var startMatchAction submit_actions.StartMatchAction
398 startMatchAction.Init(actionTable.Bytes, actionTable.Pos)
399 stat.StartingQuadrant = startMatchAction.Position()
Emily Markova46a69bf2023-03-22 20:45:52 -0700400 } else if action_type == submit_actions.ActionTypeAutoBalanceAction {
401 var autoBalanceAction submit_actions.AutoBalanceAction
402 autoBalanceAction.Init(actionTable.Bytes, actionTable.Pos)
403 if autoBalanceAction.Docked() {
404 stat.DockedAuto = true
405 }
406 if autoBalanceAction.Engaged() {
407 stat.EngagedAuto = true
408 }
Emily Markova63c63f62023-03-29 20:57:35 -0700409 if autoBalanceAction.BalanceAttempt() {
410 stat.BalanceAttemptAuto = true
411 }
Emily Markova1abe9782023-03-11 19:45:38 -0800412 } else if action_type == submit_actions.ActionTypePickupObjectAction {
413 var pick_up_action submit_actions.PickupObjectAction
414 pick_up_action.Init(actionTable.Bytes, actionTable.Pos)
415 if picked_up == true {
416 object := pick_up_action.ObjectType().String()
417 auto := pick_up_action.Auto()
418 if object == "kCube" && auto == false {
419 stat.CubesDropped += 1
420 } else if object == "kCube" && auto == true {
421 stat.CubesDroppedAuto += 1
422 } else if object == "kCone" && auto == false {
423 stat.ConesDropped += 1
424 } else if object == "kCube" && auto == true {
425 stat.ConesDroppedAuto += 1
426 }
427 } else {
428 picked_up = true
429 }
430 } else if action_type == submit_actions.ActionTypePlaceObjectAction {
431 var place_action submit_actions.PlaceObjectAction
432 place_action.Init(actionTable.Bytes, actionTable.Pos)
433 if !picked_up {
434 return db.Stats2023{}, errors.New(fmt.Sprintf("Got PlaceObjectAction without corresponding PickupObjectAction"))
435 }
436 object := place_action.ObjectType()
437 level := place_action.ScoreLevel()
438 auto := place_action.Auto()
439 if object == 0 && level == 0 && auto == true {
440 stat.LowCubesAuto += 1
441 } else if object == 0 && level == 0 && auto == false {
442 stat.LowCubes += 1
443 } else if object == 0 && level == 1 && auto == true {
444 stat.MiddleCubesAuto += 1
445 } else if object == 0 && level == 1 && auto == false {
446 stat.MiddleCubes += 1
447 } else if object == 0 && level == 2 && auto == true {
448 stat.HighCubesAuto += 1
449 } else if object == 0 && level == 2 && auto == false {
450 stat.HighCubes += 1
451 } else if object == 1 && level == 0 && auto == true {
452 stat.LowConesAuto += 1
453 } else if object == 1 && level == 0 && auto == false {
454 stat.LowCones += 1
455 } else if object == 1 && level == 1 && auto == true {
456 stat.MiddleConesAuto += 1
457 } else if object == 1 && level == 1 && auto == false {
458 stat.MiddleCones += 1
459 } else if object == 1 && level == 2 && auto == true {
460 stat.HighConesAuto += 1
461 } else if object == 1 && level == 2 && auto == false {
462 stat.HighCones += 1
463 } else {
464 return db.Stats2023{}, errors.New(fmt.Sprintf("Got unknown ObjectType/ScoreLevel/Auto combination"))
465 }
466 picked_up = false
467 if lastPlacedTime != int64(0) {
468 // If this is not the first time we place,
469 // start counting cycle time. We define cycle
470 // time as the time between placements.
471 overall_time += int64(action.Timestamp()) - lastPlacedTime
472 cycles += 1
473 }
474 lastPlacedTime = int64(action.Timestamp())
Emily Markova46a69bf2023-03-22 20:45:52 -0700475 } else if action_type == submit_actions.ActionTypeEndMatchAction {
476 var endMatchAction submit_actions.EndMatchAction
477 endMatchAction.Init(actionTable.Bytes, actionTable.Pos)
478 if endMatchAction.Docked() {
479 stat.Docked = true
480 }
481 if endMatchAction.Engaged() {
482 stat.Engaged = true
483 }
Emily Markova63c63f62023-03-29 20:57:35 -0700484 if endMatchAction.BalanceAttempt() {
485 stat.BalanceAttempt = true
486 }
Emily Markova1abe9782023-03-11 19:45:38 -0800487 }
488 }
489 if cycles != 0 {
Philipp Schrader8c878a22023-03-20 22:36:38 -0700490 stat.AvgCycle = overall_time / cycles
Emily Markova1abe9782023-03-11 19:45:38 -0800491 } else {
492 stat.AvgCycle = 0
493 }
494 return stat, nil
495}
496
Emily Markova290147d2023-03-03 22:40:06 -0800497// Handles a Request2023DataScouting request.
498type request2023DataScoutingHandler struct {
499 db Database
500}
501
502func (handler request2023DataScoutingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
503 requestBytes, err := io.ReadAll(req.Body)
504 if err != nil {
505 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
506 return
507 }
508
509 _, success := parseRequest(w, requestBytes, "Request2023DataScouting", request_2023_data_scouting.GetRootAsRequest2023DataScouting)
510 if !success {
511 return
512 }
513
514 stats, err := handler.db.ReturnStats2023()
515 if err != nil {
516 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to query database: ", err))
517 return
518 }
519
520 var response Request2023DataScoutingResponseT
521 for _, stat := range stats {
522 response.StatsList = append(response.StatsList, &request_2023_data_scouting_response.Stats2023T{
Emily Markova63c63f62023-03-29 20:57:35 -0700523 TeamNumber: stat.TeamNumber,
524 MatchNumber: stat.MatchNumber,
525 SetNumber: stat.SetNumber,
526 CompLevel: stat.CompLevel,
527 StartingQuadrant: stat.StartingQuadrant,
528 LowCubesAuto: stat.LowCubesAuto,
529 MiddleCubesAuto: stat.MiddleCubesAuto,
530 HighCubesAuto: stat.HighCubesAuto,
531 CubesDroppedAuto: stat.CubesDroppedAuto,
532 LowConesAuto: stat.LowConesAuto,
533 MiddleConesAuto: stat.MiddleConesAuto,
534 HighConesAuto: stat.HighConesAuto,
535 ConesDroppedAuto: stat.ConesDroppedAuto,
536 LowCubes: stat.LowCubes,
537 MiddleCubes: stat.MiddleCubes,
538 HighCubes: stat.HighCubes,
539 CubesDropped: stat.CubesDropped,
540 LowCones: stat.LowCones,
541 MiddleCones: stat.MiddleCones,
542 HighCones: stat.HighCones,
543 ConesDropped: stat.ConesDropped,
544 AvgCycle: stat.AvgCycle,
545 DockedAuto: stat.DockedAuto,
546 EngagedAuto: stat.EngagedAuto,
547 BalanceAttemptAuto: stat.BalanceAttemptAuto,
548 Docked: stat.Docked,
549 Engaged: stat.Engaged,
550 BalanceAttempt: stat.BalanceAttempt,
551 CollectedBy: stat.CollectedBy,
Emily Markova290147d2023-03-03 22:40:06 -0800552 })
553 }
554
555 builder := flatbuffers.NewBuilder(50 * 1024)
556 builder.Finish((&response).Pack(builder))
557 w.Write(builder.FinishedBytes())
558}
559
Alex Perry81f96ba2022-03-13 18:26:19 -0700560type requestNotesForTeamHandler struct {
561 db Database
562}
563
564func (handler requestNotesForTeamHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
565 requestBytes, err := io.ReadAll(req.Body)
566 if err != nil {
567 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
568 return
569 }
570
Philipp Schraderb7e75932022-03-26 16:18:34 -0700571 request, success := parseRequest(w, requestBytes, "RequestNotesForTeam", request_notes_for_team.GetRootAsRequestNotesForTeam)
Alex Perry81f96ba2022-03-13 18:26:19 -0700572 if !success {
573 return
574 }
575
Philipp Schradereecb8962022-06-01 21:02:42 -0700576 notes, err := handler.db.QueryNotes(request.Team())
Alex Perry81f96ba2022-03-13 18:26:19 -0700577 if err != nil {
578 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to query notes: %v", err))
579 return
580 }
581
582 var response RequestNotesForTeamResponseT
Philipp Schradereecb8962022-06-01 21:02:42 -0700583 for _, data := range notes {
Alex Perry81f96ba2022-03-13 18:26:19 -0700584 response.Notes = append(response.Notes, &request_notes_for_team_response.NoteT{data})
585 }
586
587 builder := flatbuffers.NewBuilder(1024)
588 builder.Finish((&response).Pack(builder))
589 w.Write(builder.FinishedBytes())
590}
591
Milo Lin1d59f0c2022-06-22 20:30:58 -0700592type requestShiftScheduleHandler struct {
593 db Database
594}
595
596func (handler requestShiftScheduleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
597 requestBytes, err := io.ReadAll(req.Body)
598 if err != nil {
599 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
600 return
601 }
602
603 _, success := parseRequest(w, requestBytes, "RequestShiftSchedule", request_shift_schedule.GetRootAsRequestShiftSchedule)
604 if !success {
605 return
606 }
607
608 shiftData, err := handler.db.ReturnAllShifts()
609 if err != nil {
610 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to query shift schedule: %v", err))
611 return
612 }
613
614 var response RequestShiftScheduleResponseT
615 for _, shifts := range shiftData {
616 response.ShiftSchedule = append(response.ShiftSchedule, &request_shift_schedule_response.MatchAssignmentT{
617 MatchNumber: shifts.MatchNumber,
618 R1scouter: shifts.R1scouter,
619 R2scouter: shifts.R2scouter,
620 R3scouter: shifts.R3scouter,
621 B1scouter: shifts.B1scouter,
622 B2scouter: shifts.B2scouter,
623 B3scouter: shifts.B3scouter,
624 })
625 }
626
627 builder := flatbuffers.NewBuilder(1024)
628 builder.Finish((&response).Pack(builder))
629 w.Write(builder.FinishedBytes())
630}
631
632type submitShiftScheduleHandler struct {
633 db Database
634}
635
636func (handler submitShiftScheduleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
637 // Get the username of the person submitting the data.
638 username := parseUsername(req)
639
640 requestBytes, err := io.ReadAll(req.Body)
641 if err != nil {
642 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
643 return
644 }
645
646 request, success := parseRequest[SubmitShiftSchedule](w, requestBytes, "SubmitShiftSchedule", submit_shift_schedule.GetRootAsSubmitShiftSchedule)
647 if !success {
648 return
649 }
650
651 log.Println("Got shift schedule from", username)
652 shift_schedule_length := request.ShiftScheduleLength()
653 for i := 0; i < shift_schedule_length; i++ {
654 var match_assignment submit_shift_schedule.MatchAssignment
655 request.ShiftSchedule(&match_assignment, i)
656 current_shift := db.Shift{
657 MatchNumber: match_assignment.MatchNumber(),
658 R1scouter: string(match_assignment.R1scouter()),
659 R2scouter: string(match_assignment.R2scouter()),
660 R3scouter: string(match_assignment.R3scouter()),
661 B1scouter: string(match_assignment.B1scouter()),
662 B2scouter: string(match_assignment.B2scouter()),
663 B3scouter: string(match_assignment.B3scouter()),
664 }
665 err = handler.db.AddToShift(current_shift)
666 if err != nil {
667 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to submit shift schedule: ", err))
668 return
669 }
670 }
671
672 builder := flatbuffers.NewBuilder(50 * 1024)
673 builder.Finish((&SubmitShiftScheduleResponseT{}).Pack(builder))
674 w.Write(builder.FinishedBytes())
675}
676
Filip Kujawa210a03b2022-11-24 14:41:11 -0800677type SubmitDriverRankingHandler struct {
678 db Database
679}
680
681func (handler SubmitDriverRankingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
682 requestBytes, err := io.ReadAll(req.Body)
683 if err != nil {
684 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
685 return
686 }
687
688 request, success := parseRequest(w, requestBytes, "SubmitDriverRanking", submit_driver_ranking.GetRootAsSubmitDriverRanking)
689 if !success {
690 return
691 }
692
693 err = handler.db.AddDriverRanking(db.DriverRankingData{
694 MatchNumber: request.MatchNumber(),
695 Rank1: request.Rank1(),
696 Rank2: request.Rank2(),
697 Rank3: request.Rank3(),
698 })
699
700 if err != nil {
701 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to insert driver ranking: %v", err))
702 return
703 }
704
705 var response SubmitDriverRankingResponseT
706 builder := flatbuffers.NewBuilder(10)
707 builder.Finish((&response).Pack(builder))
708 w.Write(builder.FinishedBytes())
709}
710
Filip Kujawaf882e022022-12-14 13:14:08 -0800711type requestAllNotesHandler struct {
712 db Database
713}
714
715func (handler requestAllNotesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
716 requestBytes, err := io.ReadAll(req.Body)
717 if err != nil {
718 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
719 return
720 }
721
722 _, success := parseRequest(w, requestBytes, "RequestAllNotes", request_all_notes.GetRootAsRequestAllNotes)
723 if !success {
724 return
725 }
726
727 notes, err := handler.db.ReturnAllNotes()
728 if err != nil {
729 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to query database: ", err))
730 return
731 }
732
733 var response RequestAllNotesResponseT
734 for _, note := range notes {
735 response.NoteList = append(response.NoteList, &request_all_notes_response.NoteT{
Filip Kujawa7ddd5652023-03-07 19:56:15 -0800736 Team: note.TeamNumber,
737 Notes: note.Notes,
738 GoodDriving: note.GoodDriving,
739 BadDriving: note.BadDriving,
Filip Kujawa11dc4c92023-04-13 08:55:43 -0700740 SolidPlacing: note.SolidPlacing,
Filip Kujawa7ddd5652023-03-07 19:56:15 -0800741 SketchyPlacing: note.SketchyPlacing,
742 GoodDefense: note.GoodDefense,
743 BadDefense: note.BadDefense,
744 EasilyDefended: note.EasilyDefended,
Filip Kujawaf882e022022-12-14 13:14:08 -0800745 })
746 }
747
748 builder := flatbuffers.NewBuilder(50 * 1024)
749 builder.Finish((&response).Pack(builder))
750 w.Write(builder.FinishedBytes())
751}
752
753type requestAllDriverRankingsHandler struct {
754 db Database
755}
756
757func (handler requestAllDriverRankingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
758 requestBytes, err := io.ReadAll(req.Body)
759 if err != nil {
760 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
761 return
762 }
763
764 _, success := parseRequest(w, requestBytes, "RequestAllDriverRankings", request_all_driver_rankings.GetRootAsRequestAllDriverRankings)
765 if !success {
766 return
767 }
768
769 rankings, err := handler.db.ReturnAllDriverRankings()
770 if err != nil {
771 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to query database: ", err))
772 return
773 }
774
775 var response RequestAllDriverRankingsResponseT
776 for _, ranking := range rankings {
777 response.DriverRankingList = append(response.DriverRankingList, &request_all_driver_rankings_response.RankingT{
778 MatchNumber: ranking.MatchNumber,
779 Rank1: ranking.Rank1,
780 Rank2: ranking.Rank2,
781 Rank3: ranking.Rank3,
782 })
783 }
784
785 builder := flatbuffers.NewBuilder(50 * 1024)
786 builder.Finish((&response).Pack(builder))
787 w.Write(builder.FinishedBytes())
788}
789
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800790type submitActionsHandler struct {
791 db Database
792}
793
794func (handler submitActionsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
795 // Get the username of the person submitting the data.
796 username := parseUsername(req)
797
798 requestBytes, err := io.ReadAll(req.Body)
799 if err != nil {
800 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
801 return
802 }
803
804 request, success := parseRequest(w, requestBytes, "SubmitActions", submit_actions.GetRootAsSubmitActions)
805 if !success {
806 return
807 }
808
809 log.Println("Got actions for match", request.MatchNumber(), "team", request.TeamNumber(), "from", username)
810
811 for i := 0; i < request.ActionsListLength(); i++ {
812
813 var action Action
814 request.ActionsList(&action, i)
815
816 dbAction := db.Action{
817 TeamNumber: string(request.TeamNumber()),
818 MatchNumber: request.MatchNumber(),
819 SetNumber: request.SetNumber(),
820 CompLevel: string(request.CompLevel()),
821 //TODO: Serialize CompletedAction
822 CompletedAction: []byte{},
823 Timestamp: action.Timestamp(),
824 CollectedBy: username,
825 }
826
827 // Do some error checking.
828 if action.Timestamp() < 0 {
829 respondWithError(w, http.StatusBadRequest, fmt.Sprint(
830 "Invalid timestamp field value of ", action.Timestamp()))
831 return
832 }
833
834 err = handler.db.AddAction(dbAction)
835 if err != nil {
836 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to add action to database: ", err))
837 return
838 }
839 }
840
841 builder := flatbuffers.NewBuilder(50 * 1024)
842 builder.Finish((&SubmitActionsResponseT{}).Pack(builder))
843 w.Write(builder.FinishedBytes())
844}
845
Philipp Schrader43c730b2023-02-26 20:27:44 -0800846func HandleRequests(db Database, scoutingServer server.ScoutingServer) {
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800847 scoutingServer.HandleFunc("/requests", unknown)
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800848 scoutingServer.Handle("/requests/request/all_matches", requestAllMatchesHandler{db})
Filip Kujawaf882e022022-12-14 13:14:08 -0800849 scoutingServer.Handle("/requests/request/all_notes", requestAllNotesHandler{db})
850 scoutingServer.Handle("/requests/request/all_driver_rankings", requestAllDriverRankingsHandler{db})
Emily Markova290147d2023-03-03 22:40:06 -0800851 scoutingServer.Handle("/requests/request/2023_data_scouting", request2023DataScoutingHandler{db})
Alex Perry81f96ba2022-03-13 18:26:19 -0700852 scoutingServer.Handle("/requests/submit/submit_notes", submitNoteScoutingHandler{db})
853 scoutingServer.Handle("/requests/request/notes_for_team", requestNotesForTeamHandler{db})
Milo Lin1d59f0c2022-06-22 20:30:58 -0700854 scoutingServer.Handle("/requests/submit/shift_schedule", submitShiftScheduleHandler{db})
855 scoutingServer.Handle("/requests/request/shift_schedule", requestShiftScheduleHandler{db})
Filip Kujawa210a03b2022-11-24 14:41:11 -0800856 scoutingServer.Handle("/requests/submit/submit_driver_ranking", SubmitDriverRankingHandler{db})
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800857 scoutingServer.Handle("/requests/submit/submit_actions", submitActionsHandler{db})
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800858}