blob: d850949aedd20f5d15bb59e2895d517a429f8fd0 [file] [log] [blame]
Ravago Jones2813c032022-03-16 23:44:11 -07001import {Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
2import {FormsModule} from '@angular/forms';
Philipp Schrader8b8ed672022-03-05 18:08:50 -08003import * as flatbuffer_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
4import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
5import * as error_response from 'org_frc971/scouting/webserver/requests/messages/error_response_generated';
Philipp Schrader8b8ed672022-03-05 18:08:50 -08006import * as submit_data_scouting from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated';
Ravago Jones2813c032022-03-16 23:44:11 -07007import * as submit_data_scouting_response from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated';
8
Philipp Schrader8b8ed672022-03-05 18:08:50 -08009import SubmitDataScouting = submit_data_scouting.scouting.webserver.requests.SubmitDataScouting;
10import SubmitDataScoutingResponse = submit_data_scouting_response.scouting.webserver.requests.SubmitDataScoutingResponse;
11import ErrorResponse = error_response.scouting.webserver.requests.ErrorResponse;
12
Ravago Jones2813c032022-03-16 23:44:11 -070013type Section = 'Team Selection'|'Auto'|'TeleOp'|'Climb'|'Other'|
14 'Review and Submit'|'Success'
15type Level = 'NoAttempt'|'Failed'|'FailedWithPlentyOfTime'|'Low'|'Medium'|
16 'High'|'Transversal'
Philipp Schrader80587432022-03-05 15:41:22 -080017
Ravago Jones2813c032022-03-16 23:44:11 -070018 @Component({
19 selector: 'app-entry',
20 templateUrl: './entry.ng.html',
21 styleUrls: ['../common.css', './entry.component.css']
22 }) export class EntryComponent {
23 section: Section = 'Team Selection';
24 @Output() switchTabsEvent = new EventEmitter<string>();
25 @Input() matchNumber: number = 1;
26 @Input() teamNumber: number = 1;
27 autoUpperShotsMade: number = 0;
28 autoLowerShotsMade: number = 0;
29 autoShotsMissed: number = 0;
30 teleUpperShotsMade: number = 0;
31 teleLowerShotsMade: number = 0;
32 teleShotsMissed: number = 0;
33 defensePlayedOnScore: number = 0;
34 defensePlayedScore: number = 0;
35 level: Level = 'NoAttempt';
36 ball1: boolean = false;
37 ball2: boolean = false;
38 ball3: boolean = false;
39 ball4: boolean = false;
40 ball5: boolean = false;
41 quadrant: number = 1;
42 errorMessage: string = '';
43 noShow: boolean = false;
44 neverMoved: boolean = false;
45 batteryDied: boolean = false;
46 mechanicallyBroke: boolean = false;
47 lostComs: boolean = false;
Philipp Schrader80587432022-03-05 15:41:22 -080048
Ravago Jones2813c032022-03-16 23:44:11 -070049 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -070050
Ravago Jones2813c032022-03-16 23:44:11 -070051 nextSection() {
52 if (this.section === 'Team Selection') {
53 this.section = 'Auto';
54 } else if (this.section === 'Auto') {
55 this.section = 'TeleOp';
56 } else if (this.section === 'TeleOp') {
57 this.section = 'Climb';
58 } else if (this.section === 'Climb') {
59 this.section = 'Other';
60 } else if (this.section === 'Other') {
61 this.section = 'Review and Submit';
62 } else if (this.section === 'Review and Submit') {
63 this.submitDataScouting();
64 return;
65 } else if (this.section === 'Success') {
66 this.switchTabsEvent.emit('MatchList');
67 return;
Philipp Schrader80587432022-03-05 15:41:22 -080068 }
Ravago Jones2813c032022-03-16 23:44:11 -070069 // Scroll back to the top so that we can be sure the user sees the
70 // entire next screen. Otherwise it's easy to overlook input fields.
71 this.scrollToTop();
72 }
Philipp Schrader80587432022-03-05 15:41:22 -080073
Ravago Jones2813c032022-03-16 23:44:11 -070074 prevSection() {
75 if (this.section === 'Auto') {
76 this.section = 'Team Selection';
77 } else if (this.section === 'TeleOp') {
78 this.section = 'Auto';
79 } else if (this.section === 'Climb') {
80 this.section = 'TeleOp';
81 } else if (this.section === 'Other') {
82 this.section = 'Climb';
83 } else if (this.section === 'Review and Submit') {
84 this.section = 'Other';
Philipp Schrader6b2e9502022-03-15 23:42:56 -070085 }
Ravago Jones2813c032022-03-16 23:44:11 -070086 // Scroll back to the top so that we can be sure the user sees the
87 // entire previous screen. Otherwise it's easy to overlook input
88 // fields.
89 this.scrollToTop();
90 }
Philipp Schrader6b2e9502022-03-15 23:42:56 -070091
Ravago Jones2813c032022-03-16 23:44:11 -070092 private scrollToTop() {
93 this.header.nativeElement.scrollIntoView();
94 }
95
96 async submitDataScouting() {
97 this.errorMessage = '';
98
99 const builder =
100 new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder;
101 SubmitDataScouting.startSubmitDataScouting(builder);
102 SubmitDataScouting.addTeam(builder, this.teamNumber);
103 SubmitDataScouting.addMatch(builder, this.matchNumber);
104 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
105 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
106 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
107 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
108 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
109 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
110 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
111 SubmitDataScouting.addAutoBall1(builder, this.ball1);
112 SubmitDataScouting.addAutoBall2(builder, this.ball2);
113 SubmitDataScouting.addAutoBall3(builder, this.ball3);
114 SubmitDataScouting.addAutoBall4(builder, this.ball4);
115 SubmitDataScouting.addAutoBall5(builder, this.ball5);
116 SubmitDataScouting.addStartingQuadrant(builder, this.quadrant);
117
118 // TODO(phil): Add support for defensePlayedOnScore.
119 // TODO(phil): Fix the Climbing score.
120 SubmitDataScouting.addClimbing(builder, 1);
121 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
122
123 const buffer = builder.asUint8Array();
124 const res = await fetch(
125 '/requests/submit/data_scouting', {method: 'POST', body: buffer});
126
127 if (res.ok) {
128 // We successfully submitted the data. Report success.
129 this.section = 'Success';
130 } else {
131 const resBuffer = await res.arrayBuffer();
132 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
133 const parsedResponse = ErrorResponse.getRootAsErrorResponse(
134 fbBuffer as unknown as flatbuffers.ByteBuffer);
135
136 const errorMessage = parsedResponse.errorMessage();
137 this.errorMessage =
138 `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800139 }
Ravago Jones2813c032022-03-16 23:44:11 -0700140 }
Philipp Schrader80587432022-03-05 15:41:22 -0800141}