blob: d35818d0649bec297d4dbd1445f8b2fdd78ca6f6 [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'|
Philipp Schrader23993e82022-03-18 18:54:00 -070014 'Review and Submit'|'Success';
Ravago Jones2813c032022-03-16 23:44:11 -070015type Level = 'NoAttempt'|'Failed'|'FailedWithPlentyOfTime'|'Low'|'Medium'|
Philipp Schrader23993e82022-03-18 18:54:00 -070016 'High'|'Transversal';
Philipp Schrader80587432022-03-05 15:41:22 -080017
Philipp Schrader23993e82022-03-18 18:54:00 -070018@Component({
19 selector: 'app-entry',
20 templateUrl: './entry.ng.html',
21 styleUrls: ['../common.css', './entry.component.css']
22})
23export class EntryComponent {
Ravago Jones2813c032022-03-16 23:44:11 -070024 section: Section = 'Team Selection';
25 @Output() switchTabsEvent = new EventEmitter<string>();
26 @Input() matchNumber: number = 1;
27 @Input() teamNumber: number = 1;
28 autoUpperShotsMade: number = 0;
29 autoLowerShotsMade: number = 0;
30 autoShotsMissed: number = 0;
31 teleUpperShotsMade: number = 0;
32 teleLowerShotsMade: number = 0;
33 teleShotsMissed: number = 0;
34 defensePlayedOnScore: number = 0;
35 defensePlayedScore: number = 0;
36 level: Level = 'NoAttempt';
37 ball1: boolean = false;
38 ball2: boolean = false;
39 ball3: boolean = false;
40 ball4: boolean = false;
41 ball5: boolean = false;
42 quadrant: number = 1;
43 errorMessage: string = '';
44 noShow: boolean = false;
45 neverMoved: boolean = false;
46 batteryDied: boolean = false;
47 mechanicallyBroke: boolean = false;
48 lostComs: boolean = false;
Philipp Schrader80587432022-03-05 15:41:22 -080049
Ravago Jones2813c032022-03-16 23:44:11 -070050 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -070051
Ravago Jones2813c032022-03-16 23:44:11 -070052 nextSection() {
53 if (this.section === 'Team Selection') {
54 this.section = 'Auto';
55 } else if (this.section === 'Auto') {
56 this.section = 'TeleOp';
57 } else if (this.section === 'TeleOp') {
58 this.section = 'Climb';
59 } else if (this.section === 'Climb') {
60 this.section = 'Other';
61 } else if (this.section === 'Other') {
62 this.section = 'Review and Submit';
63 } else if (this.section === 'Review and Submit') {
64 this.submitDataScouting();
65 return;
66 } else if (this.section === 'Success') {
Philipp Schrader23993e82022-03-18 18:54:00 -070067 this.switchTabsEvent.emit('MatchList');
68 return;
Philipp Schrader80587432022-03-05 15:41:22 -080069 }
Ravago Jones2813c032022-03-16 23:44:11 -070070 // Scroll back to the top so that we can be sure the user sees the
71 // entire next screen. Otherwise it's easy to overlook input fields.
72 this.scrollToTop();
73 }
Philipp Schrader80587432022-03-05 15:41:22 -080074
Ravago Jones2813c032022-03-16 23:44:11 -070075 prevSection() {
76 if (this.section === 'Auto') {
77 this.section = 'Team Selection';
78 } else if (this.section === 'TeleOp') {
79 this.section = 'Auto';
80 } else if (this.section === 'Climb') {
81 this.section = 'TeleOp';
82 } else if (this.section === 'Other') {
83 this.section = 'Climb';
84 } else if (this.section === 'Review and Submit') {
85 this.section = 'Other';
Philipp Schrader6b2e9502022-03-15 23:42:56 -070086 }
Ravago Jones2813c032022-03-16 23:44:11 -070087 // Scroll back to the top so that we can be sure the user sees the
88 // entire previous screen. Otherwise it's easy to overlook input
89 // fields.
90 this.scrollToTop();
91 }
Philipp Schrader6b2e9502022-03-15 23:42:56 -070092
Ravago Jones2813c032022-03-16 23:44:11 -070093 private scrollToTop() {
94 this.header.nativeElement.scrollIntoView();
95 }
96
97 async submitDataScouting() {
98 this.errorMessage = '';
99
100 const builder =
101 new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder;
102 SubmitDataScouting.startSubmitDataScouting(builder);
103 SubmitDataScouting.addTeam(builder, this.teamNumber);
104 SubmitDataScouting.addMatch(builder, this.matchNumber);
105 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
106 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
107 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
108 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
109 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
110 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
111 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
112 SubmitDataScouting.addAutoBall1(builder, this.ball1);
113 SubmitDataScouting.addAutoBall2(builder, this.ball2);
114 SubmitDataScouting.addAutoBall3(builder, this.ball3);
115 SubmitDataScouting.addAutoBall4(builder, this.ball4);
116 SubmitDataScouting.addAutoBall5(builder, this.ball5);
117 SubmitDataScouting.addStartingQuadrant(builder, this.quadrant);
118
119 // TODO(phil): Add support for defensePlayedOnScore.
120 // TODO(phil): Fix the Climbing score.
121 SubmitDataScouting.addClimbing(builder, 1);
122 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
123
124 const buffer = builder.asUint8Array();
125 const res = await fetch(
126 '/requests/submit/data_scouting', {method: 'POST', body: buffer});
127
128 if (res.ok) {
129 // We successfully submitted the data. Report success.
130 this.section = 'Success';
131 } else {
132 const resBuffer = await res.arrayBuffer();
133 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
134 const parsedResponse = ErrorResponse.getRootAsErrorResponse(
135 fbBuffer as unknown as flatbuffers.ByteBuffer);
136
137 const errorMessage = parsedResponse.errorMessage();
138 this.errorMessage =
139 `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800140 }
Ravago Jones2813c032022-03-16 23:44:11 -0700141 }
Philipp Schrader80587432022-03-05 15:41:22 -0800142}