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/messages/submit_actions.fbs b/scouting/webserver/requests/messages/submit_actions.fbs
index d435913..869c2f0 100644
--- a/scouting/webserver/requests/messages/submit_actions.fbs
+++ b/scouting/webserver/requests/messages/submit_actions.fbs
@@ -68,8 +68,11 @@
     set_number:int (id: 2);
     comp_level:string (id: 3);
     actions_list:[Action] (id:4);
-    //TODO: delete this field
-    collected_by:string (id: 5);
+
+    // Do not use this field. The information is collected by the webserver,
+    // not the web page.
+    collected_by:string (id: 5, deprecated);
+
     // If this is for pre-scouting, then the server should accept this
     // submission. I.e. checking that the match information exists in the match
     // list should be skipped.
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())
diff --git a/scouting/webserver/requests/requests_test.go b/scouting/webserver/requests/requests_test.go
index 5561829..d68dd61 100644
--- a/scouting/webserver/requests/requests_test.go
+++ b/scouting/webserver/requests/requests_test.go
@@ -292,7 +292,6 @@
 		MatchNumber: 3,
 		SetNumber:   1,
 		CompLevel:   "quals",
-		CollectedBy: "katie",
 		ActionsList: []*submit_actions.ActionT{
 			{
 				ActionTaken: &submit_actions.ActionTypeT{
@@ -429,7 +428,7 @@
 		MiddleCones: 0, HighCones: 1, ConesDropped: 0, SuperchargedPieces: 1,
 		AvgCycle: 950, Mobility: true, DockedAuto: true, EngagedAuto: true,
 		BalanceAttemptAuto: false, Docked: true, Engaged: false,
-		BalanceAttempt: true, CollectedBy: "katie",
+		BalanceAttempt: true, CollectedBy: "",
 	}
 
 	if expected != response {
@@ -884,10 +883,28 @@
 		},
 	}
 
+	expectedStats := []db.Stats2023{
+		db.Stats2023{
+			PreScouting: true,
+			TeamNumber:  "1234", MatchNumber: 4, SetNumber: 1,
+			CompLevel: "qual", StartingQuadrant: 0, LowCubesAuto: 0,
+			MiddleCubesAuto: 0, HighCubesAuto: 0, CubesDroppedAuto: 0,
+			LowConesAuto: 0, MiddleConesAuto: 0, HighConesAuto: 0,
+			ConesDroppedAuto: 0, LowCubes: 1, MiddleCubes: 0,
+			HighCubes: 0, CubesDropped: 0, LowCones: 0,
+			MiddleCones: 0, HighCones: 0, ConesDropped: 0, SuperchargedPieces: 0,
+			AvgCycle: 0, Mobility: false, DockedAuto: false, EngagedAuto: false,
+			BalanceAttemptAuto: false, Docked: false, Engaged: false,
+			BalanceAttempt: false, CollectedBy: "debug_cli",
+		},
+	}
+
 	if !reflect.DeepEqual(expectedActions, database.actions) {
 		t.Fatal("Expected ", expectedActions, ", but got:", database.actions)
 	}
-
+	if !reflect.DeepEqual(expectedStats, database.stats2023) {
+		t.Fatal("Expected ", expectedStats, ", but got:", database.stats2023)
+	}
 }
 
 // A mocked database we can use for testing. Add functionality to this as