blob: 25bb6b7f7975c95f55110edbb6a3182caad0f93f [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,
Filip Kujawa7a045e72023-04-13 08:41:09 -0700383 CubesDropped: 0, LowCones: 0, MiddleCones: 0, HighCones: 0, ConesDropped: 0, SuperchargedPieces: 0, AvgCycle: 0, CollectedBy: string(submitActions.CollectedBy()),
Emily Markova1abe9782023-03-11 19:45:38 -0800384 }
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()
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700400 } else if action_type == submit_actions.ActionTypeMobilityAction {
401 var mobilityAction submit_actions.MobilityAction
402 mobilityAction.Init(actionTable.Bytes, actionTable.Pos)
403 if mobilityAction.Mobility() {
404 stat.Mobility = true
405 }
406
Emily Markova46a69bf2023-03-22 20:45:52 -0700407 } else if action_type == submit_actions.ActionTypeAutoBalanceAction {
408 var autoBalanceAction submit_actions.AutoBalanceAction
409 autoBalanceAction.Init(actionTable.Bytes, actionTable.Pos)
410 if autoBalanceAction.Docked() {
411 stat.DockedAuto = true
412 }
413 if autoBalanceAction.Engaged() {
414 stat.EngagedAuto = true
415 }
Emily Markova63c63f62023-03-29 20:57:35 -0700416 if autoBalanceAction.BalanceAttempt() {
417 stat.BalanceAttemptAuto = true
418 }
Emily Markova1abe9782023-03-11 19:45:38 -0800419 } else if action_type == submit_actions.ActionTypePickupObjectAction {
420 var pick_up_action submit_actions.PickupObjectAction
421 pick_up_action.Init(actionTable.Bytes, actionTable.Pos)
422 if picked_up == true {
423 object := pick_up_action.ObjectType().String()
424 auto := pick_up_action.Auto()
425 if object == "kCube" && auto == false {
426 stat.CubesDropped += 1
427 } else if object == "kCube" && auto == true {
428 stat.CubesDroppedAuto += 1
429 } else if object == "kCone" && auto == false {
430 stat.ConesDropped += 1
431 } else if object == "kCube" && auto == true {
432 stat.ConesDroppedAuto += 1
433 }
434 } else {
435 picked_up = true
436 }
437 } else if action_type == submit_actions.ActionTypePlaceObjectAction {
438 var place_action submit_actions.PlaceObjectAction
439 place_action.Init(actionTable.Bytes, actionTable.Pos)
440 if !picked_up {
441 return db.Stats2023{}, errors.New(fmt.Sprintf("Got PlaceObjectAction without corresponding PickupObjectAction"))
442 }
443 object := place_action.ObjectType()
444 level := place_action.ScoreLevel()
445 auto := place_action.Auto()
446 if object == 0 && level == 0 && auto == true {
447 stat.LowCubesAuto += 1
448 } else if object == 0 && level == 0 && auto == false {
449 stat.LowCubes += 1
450 } else if object == 0 && level == 1 && auto == true {
451 stat.MiddleCubesAuto += 1
452 } else if object == 0 && level == 1 && auto == false {
453 stat.MiddleCubes += 1
454 } else if object == 0 && level == 2 && auto == true {
455 stat.HighCubesAuto += 1
456 } else if object == 0 && level == 2 && auto == false {
457 stat.HighCubes += 1
458 } else if object == 1 && level == 0 && auto == true {
459 stat.LowConesAuto += 1
460 } else if object == 1 && level == 0 && auto == false {
461 stat.LowCones += 1
462 } else if object == 1 && level == 1 && auto == true {
463 stat.MiddleConesAuto += 1
464 } else if object == 1 && level == 1 && auto == false {
465 stat.MiddleCones += 1
466 } else if object == 1 && level == 2 && auto == true {
467 stat.HighConesAuto += 1
468 } else if object == 1 && level == 2 && auto == false {
469 stat.HighCones += 1
Filip Kujawa7a045e72023-04-13 08:41:09 -0700470 } else if level == 3 {
471 stat.SuperchargedPieces += 1
Emily Markova1abe9782023-03-11 19:45:38 -0800472 } else {
473 return db.Stats2023{}, errors.New(fmt.Sprintf("Got unknown ObjectType/ScoreLevel/Auto combination"))
474 }
475 picked_up = false
476 if lastPlacedTime != int64(0) {
477 // If this is not the first time we place,
478 // start counting cycle time. We define cycle
479 // time as the time between placements.
480 overall_time += int64(action.Timestamp()) - lastPlacedTime
481 cycles += 1
482 }
483 lastPlacedTime = int64(action.Timestamp())
Emily Markova46a69bf2023-03-22 20:45:52 -0700484 } else if action_type == submit_actions.ActionTypeEndMatchAction {
485 var endMatchAction submit_actions.EndMatchAction
486 endMatchAction.Init(actionTable.Bytes, actionTable.Pos)
487 if endMatchAction.Docked() {
488 stat.Docked = true
489 }
490 if endMatchAction.Engaged() {
491 stat.Engaged = true
492 }
Emily Markova63c63f62023-03-29 20:57:35 -0700493 if endMatchAction.BalanceAttempt() {
494 stat.BalanceAttempt = true
495 }
Emily Markova1abe9782023-03-11 19:45:38 -0800496 }
497 }
498 if cycles != 0 {
Philipp Schrader8c878a22023-03-20 22:36:38 -0700499 stat.AvgCycle = overall_time / cycles
Emily Markova1abe9782023-03-11 19:45:38 -0800500 } else {
501 stat.AvgCycle = 0
502 }
503 return stat, nil
504}
505
Emily Markova290147d2023-03-03 22:40:06 -0800506// Handles a Request2023DataScouting request.
507type request2023DataScoutingHandler struct {
508 db Database
509}
510
511func (handler request2023DataScoutingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
512 requestBytes, err := io.ReadAll(req.Body)
513 if err != nil {
514 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
515 return
516 }
517
518 _, success := parseRequest(w, requestBytes, "Request2023DataScouting", request_2023_data_scouting.GetRootAsRequest2023DataScouting)
519 if !success {
520 return
521 }
522
523 stats, err := handler.db.ReturnStats2023()
524 if err != nil {
525 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to query database: ", err))
526 return
527 }
528
529 var response Request2023DataScoutingResponseT
530 for _, stat := range stats {
531 response.StatsList = append(response.StatsList, &request_2023_data_scouting_response.Stats2023T{
Emily Markova63c63f62023-03-29 20:57:35 -0700532 TeamNumber: stat.TeamNumber,
533 MatchNumber: stat.MatchNumber,
534 SetNumber: stat.SetNumber,
535 CompLevel: stat.CompLevel,
536 StartingQuadrant: stat.StartingQuadrant,
537 LowCubesAuto: stat.LowCubesAuto,
538 MiddleCubesAuto: stat.MiddleCubesAuto,
539 HighCubesAuto: stat.HighCubesAuto,
540 CubesDroppedAuto: stat.CubesDroppedAuto,
541 LowConesAuto: stat.LowConesAuto,
542 MiddleConesAuto: stat.MiddleConesAuto,
543 HighConesAuto: stat.HighConesAuto,
544 ConesDroppedAuto: stat.ConesDroppedAuto,
545 LowCubes: stat.LowCubes,
546 MiddleCubes: stat.MiddleCubes,
547 HighCubes: stat.HighCubes,
548 CubesDropped: stat.CubesDropped,
549 LowCones: stat.LowCones,
550 MiddleCones: stat.MiddleCones,
551 HighCones: stat.HighCones,
552 ConesDropped: stat.ConesDropped,
Filip Kujawa7a045e72023-04-13 08:41:09 -0700553 SuperchargedPieces: stat.SuperchargedPieces,
Emily Markova63c63f62023-03-29 20:57:35 -0700554 AvgCycle: stat.AvgCycle,
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700555 Mobility: stat.Mobility,
Emily Markova63c63f62023-03-29 20:57:35 -0700556 DockedAuto: stat.DockedAuto,
557 EngagedAuto: stat.EngagedAuto,
558 BalanceAttemptAuto: stat.BalanceAttemptAuto,
559 Docked: stat.Docked,
560 Engaged: stat.Engaged,
561 BalanceAttempt: stat.BalanceAttempt,
562 CollectedBy: stat.CollectedBy,
Emily Markova290147d2023-03-03 22:40:06 -0800563 })
564 }
565
566 builder := flatbuffers.NewBuilder(50 * 1024)
567 builder.Finish((&response).Pack(builder))
568 w.Write(builder.FinishedBytes())
569}
570
Alex Perry81f96ba2022-03-13 18:26:19 -0700571type requestNotesForTeamHandler struct {
572 db Database
573}
574
575func (handler requestNotesForTeamHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
576 requestBytes, err := io.ReadAll(req.Body)
577 if err != nil {
578 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
579 return
580 }
581
Philipp Schraderb7e75932022-03-26 16:18:34 -0700582 request, success := parseRequest(w, requestBytes, "RequestNotesForTeam", request_notes_for_team.GetRootAsRequestNotesForTeam)
Alex Perry81f96ba2022-03-13 18:26:19 -0700583 if !success {
584 return
585 }
586
Philipp Schradereecb8962022-06-01 21:02:42 -0700587 notes, err := handler.db.QueryNotes(request.Team())
Alex Perry81f96ba2022-03-13 18:26:19 -0700588 if err != nil {
589 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to query notes: %v", err))
590 return
591 }
592
593 var response RequestNotesForTeamResponseT
Philipp Schradereecb8962022-06-01 21:02:42 -0700594 for _, data := range notes {
Alex Perry81f96ba2022-03-13 18:26:19 -0700595 response.Notes = append(response.Notes, &request_notes_for_team_response.NoteT{data})
596 }
597
598 builder := flatbuffers.NewBuilder(1024)
599 builder.Finish((&response).Pack(builder))
600 w.Write(builder.FinishedBytes())
601}
602
Milo Lin1d59f0c2022-06-22 20:30:58 -0700603type requestShiftScheduleHandler struct {
604 db Database
605}
606
607func (handler requestShiftScheduleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
608 requestBytes, err := io.ReadAll(req.Body)
609 if err != nil {
610 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
611 return
612 }
613
614 _, success := parseRequest(w, requestBytes, "RequestShiftSchedule", request_shift_schedule.GetRootAsRequestShiftSchedule)
615 if !success {
616 return
617 }
618
619 shiftData, err := handler.db.ReturnAllShifts()
620 if err != nil {
621 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to query shift schedule: %v", err))
622 return
623 }
624
625 var response RequestShiftScheduleResponseT
626 for _, shifts := range shiftData {
627 response.ShiftSchedule = append(response.ShiftSchedule, &request_shift_schedule_response.MatchAssignmentT{
628 MatchNumber: shifts.MatchNumber,
629 R1scouter: shifts.R1scouter,
630 R2scouter: shifts.R2scouter,
631 R3scouter: shifts.R3scouter,
632 B1scouter: shifts.B1scouter,
633 B2scouter: shifts.B2scouter,
634 B3scouter: shifts.B3scouter,
635 })
636 }
637
638 builder := flatbuffers.NewBuilder(1024)
639 builder.Finish((&response).Pack(builder))
640 w.Write(builder.FinishedBytes())
641}
642
643type submitShiftScheduleHandler struct {
644 db Database
645}
646
647func (handler submitShiftScheduleHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
648 // Get the username of the person submitting the data.
649 username := parseUsername(req)
650
651 requestBytes, err := io.ReadAll(req.Body)
652 if err != nil {
653 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
654 return
655 }
656
657 request, success := parseRequest[SubmitShiftSchedule](w, requestBytes, "SubmitShiftSchedule", submit_shift_schedule.GetRootAsSubmitShiftSchedule)
658 if !success {
659 return
660 }
661
662 log.Println("Got shift schedule from", username)
663 shift_schedule_length := request.ShiftScheduleLength()
664 for i := 0; i < shift_schedule_length; i++ {
665 var match_assignment submit_shift_schedule.MatchAssignment
666 request.ShiftSchedule(&match_assignment, i)
667 current_shift := db.Shift{
668 MatchNumber: match_assignment.MatchNumber(),
669 R1scouter: string(match_assignment.R1scouter()),
670 R2scouter: string(match_assignment.R2scouter()),
671 R3scouter: string(match_assignment.R3scouter()),
672 B1scouter: string(match_assignment.B1scouter()),
673 B2scouter: string(match_assignment.B2scouter()),
674 B3scouter: string(match_assignment.B3scouter()),
675 }
676 err = handler.db.AddToShift(current_shift)
677 if err != nil {
678 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to submit shift schedule: ", err))
679 return
680 }
681 }
682
683 builder := flatbuffers.NewBuilder(50 * 1024)
684 builder.Finish((&SubmitShiftScheduleResponseT{}).Pack(builder))
685 w.Write(builder.FinishedBytes())
686}
687
Filip Kujawa210a03b2022-11-24 14:41:11 -0800688type SubmitDriverRankingHandler struct {
689 db Database
690}
691
692func (handler SubmitDriverRankingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
693 requestBytes, err := io.ReadAll(req.Body)
694 if err != nil {
695 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
696 return
697 }
698
699 request, success := parseRequest(w, requestBytes, "SubmitDriverRanking", submit_driver_ranking.GetRootAsSubmitDriverRanking)
700 if !success {
701 return
702 }
703
704 err = handler.db.AddDriverRanking(db.DriverRankingData{
705 MatchNumber: request.MatchNumber(),
706 Rank1: request.Rank1(),
707 Rank2: request.Rank2(),
708 Rank3: request.Rank3(),
709 })
710
711 if err != nil {
712 respondWithError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to insert driver ranking: %v", err))
713 return
714 }
715
716 var response SubmitDriverRankingResponseT
717 builder := flatbuffers.NewBuilder(10)
718 builder.Finish((&response).Pack(builder))
719 w.Write(builder.FinishedBytes())
720}
721
Filip Kujawaf882e022022-12-14 13:14:08 -0800722type requestAllNotesHandler struct {
723 db Database
724}
725
726func (handler requestAllNotesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
727 requestBytes, err := io.ReadAll(req.Body)
728 if err != nil {
729 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
730 return
731 }
732
733 _, success := parseRequest(w, requestBytes, "RequestAllNotes", request_all_notes.GetRootAsRequestAllNotes)
734 if !success {
735 return
736 }
737
738 notes, err := handler.db.ReturnAllNotes()
739 if err != nil {
740 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to query database: ", err))
741 return
742 }
743
744 var response RequestAllNotesResponseT
745 for _, note := range notes {
746 response.NoteList = append(response.NoteList, &request_all_notes_response.NoteT{
Filip Kujawa7ddd5652023-03-07 19:56:15 -0800747 Team: note.TeamNumber,
748 Notes: note.Notes,
749 GoodDriving: note.GoodDriving,
750 BadDriving: note.BadDriving,
Filip Kujawa11dc4c92023-04-13 08:55:43 -0700751 SolidPlacing: note.SolidPlacing,
Filip Kujawa7ddd5652023-03-07 19:56:15 -0800752 SketchyPlacing: note.SketchyPlacing,
753 GoodDefense: note.GoodDefense,
754 BadDefense: note.BadDefense,
755 EasilyDefended: note.EasilyDefended,
Filip Kujawaf882e022022-12-14 13:14:08 -0800756 })
757 }
758
759 builder := flatbuffers.NewBuilder(50 * 1024)
760 builder.Finish((&response).Pack(builder))
761 w.Write(builder.FinishedBytes())
762}
763
764type requestAllDriverRankingsHandler struct {
765 db Database
766}
767
768func (handler requestAllDriverRankingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
769 requestBytes, err := io.ReadAll(req.Body)
770 if err != nil {
771 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
772 return
773 }
774
775 _, success := parseRequest(w, requestBytes, "RequestAllDriverRankings", request_all_driver_rankings.GetRootAsRequestAllDriverRankings)
776 if !success {
777 return
778 }
779
780 rankings, err := handler.db.ReturnAllDriverRankings()
781 if err != nil {
782 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to query database: ", err))
783 return
784 }
785
786 var response RequestAllDriverRankingsResponseT
787 for _, ranking := range rankings {
788 response.DriverRankingList = append(response.DriverRankingList, &request_all_driver_rankings_response.RankingT{
789 MatchNumber: ranking.MatchNumber,
790 Rank1: ranking.Rank1,
791 Rank2: ranking.Rank2,
792 Rank3: ranking.Rank3,
793 })
794 }
795
796 builder := flatbuffers.NewBuilder(50 * 1024)
797 builder.Finish((&response).Pack(builder))
798 w.Write(builder.FinishedBytes())
799}
800
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800801type submitActionsHandler struct {
802 db Database
803}
804
805func (handler submitActionsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
806 // Get the username of the person submitting the data.
807 username := parseUsername(req)
808
809 requestBytes, err := io.ReadAll(req.Body)
810 if err != nil {
811 respondWithError(w, http.StatusBadRequest, fmt.Sprint("Failed to read request bytes:", err))
812 return
813 }
814
815 request, success := parseRequest(w, requestBytes, "SubmitActions", submit_actions.GetRootAsSubmitActions)
816 if !success {
817 return
818 }
819
820 log.Println("Got actions for match", request.MatchNumber(), "team", request.TeamNumber(), "from", username)
821
822 for i := 0; i < request.ActionsListLength(); i++ {
823
824 var action Action
825 request.ActionsList(&action, i)
826
827 dbAction := db.Action{
828 TeamNumber: string(request.TeamNumber()),
829 MatchNumber: request.MatchNumber(),
830 SetNumber: request.SetNumber(),
831 CompLevel: string(request.CompLevel()),
832 //TODO: Serialize CompletedAction
833 CompletedAction: []byte{},
834 Timestamp: action.Timestamp(),
835 CollectedBy: username,
836 }
837
838 // Do some error checking.
839 if action.Timestamp() < 0 {
840 respondWithError(w, http.StatusBadRequest, fmt.Sprint(
841 "Invalid timestamp field value of ", action.Timestamp()))
842 return
843 }
844
845 err = handler.db.AddAction(dbAction)
846 if err != nil {
847 respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to add action to database: ", err))
848 return
849 }
850 }
851
852 builder := flatbuffers.NewBuilder(50 * 1024)
853 builder.Finish((&SubmitActionsResponseT{}).Pack(builder))
854 w.Write(builder.FinishedBytes())
855}
856
Philipp Schrader43c730b2023-02-26 20:27:44 -0800857func HandleRequests(db Database, scoutingServer server.ScoutingServer) {
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800858 scoutingServer.HandleFunc("/requests", unknown)
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800859 scoutingServer.Handle("/requests/request/all_matches", requestAllMatchesHandler{db})
Filip Kujawaf882e022022-12-14 13:14:08 -0800860 scoutingServer.Handle("/requests/request/all_notes", requestAllNotesHandler{db})
861 scoutingServer.Handle("/requests/request/all_driver_rankings", requestAllDriverRankingsHandler{db})
Emily Markova290147d2023-03-03 22:40:06 -0800862 scoutingServer.Handle("/requests/request/2023_data_scouting", request2023DataScoutingHandler{db})
Alex Perry81f96ba2022-03-13 18:26:19 -0700863 scoutingServer.Handle("/requests/submit/submit_notes", submitNoteScoutingHandler{db})
864 scoutingServer.Handle("/requests/request/notes_for_team", requestNotesForTeamHandler{db})
Milo Lin1d59f0c2022-06-22 20:30:58 -0700865 scoutingServer.Handle("/requests/submit/shift_schedule", submitShiftScheduleHandler{db})
866 scoutingServer.Handle("/requests/request/shift_schedule", requestShiftScheduleHandler{db})
Filip Kujawa210a03b2022-11-24 14:41:11 -0800867 scoutingServer.Handle("/requests/submit/submit_driver_ranking", SubmitDriverRankingHandler{db})
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800868 scoutingServer.Handle("/requests/submit/submit_actions", submitActionsHandler{db})
Philipp Schradercdb5cfc2022-02-20 14:57:07 -0800869}