scouting: Convert actions to stats when submitted

Now that actions are submitted, we need to convert them into stats.
This patch does that by calling the correct conversion function and
then inserting the result into the database.

I had to adjust the scouting_test slightly because the behaviour
changed slightly. The test doesn't clear the database between test
cases. So the one test case submitting scout data means it can't be
scouted again. The button is now disabled.

Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: Ib3e68103c8064c61e628711c9fe375919372e18c
diff --git a/scouting/webserver/requests/requests.go b/scouting/webserver/requests/requests.go
index 1f4fcd1..8293fa2 100644
--- a/scouting/webserver/requests/requests.go
+++ b/scouting/webserver/requests/requests.go
@@ -382,7 +382,7 @@
 		TeamNumber:  string(submitActions.TeamNumber()), MatchNumber: submitActions.MatchNumber(), SetNumber: submitActions.SetNumber(), CompLevel: string(submitActions.CompLevel()),
 		StartingQuadrant: 0, LowCubesAuto: 0, MiddleCubesAuto: 0, HighCubesAuto: 0, CubesDroppedAuto: 0,
 		LowConesAuto: 0, MiddleConesAuto: 0, HighConesAuto: 0, ConesDroppedAuto: 0, LowCubes: 0, MiddleCubes: 0, HighCubes: 0,
-		CubesDropped: 0, LowCones: 0, MiddleCones: 0, HighCones: 0, ConesDropped: 0, SuperchargedPieces: 0, AvgCycle: 0, CollectedBy: string(submitActions.CollectedBy()),
+		CubesDropped: 0, LowCones: 0, MiddleCones: 0, HighCones: 0, ConesDropped: 0, SuperchargedPieces: 0, AvgCycle: 0, CollectedBy: "",
 	}
 	// Loop over all actions.
 	for i := 0; i < submitActions.ActionsListLength(); i++ {
@@ -852,6 +852,20 @@
 		}
 	}
 
+	stats, err := ConvertActionsToStat(request)
+	if err != nil {
+		respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to convert actions to stats: ", err))
+		return
+	}
+
+	stats.CollectedBy = username
+
+	err = handler.db.AddToStats2023(stats)
+	if err != nil {
+		respondWithError(w, http.StatusInternalServerError, fmt.Sprint("Failed to submit stats: ", stats, ": ", err))
+		return
+	}
+
 	builder := flatbuffers.NewBuilder(50 * 1024)
 	builder.Finish((&SubmitActionsResponseT{}).Pack(builder))
 	w.Write(builder.FinishedBytes())