blob: d067c3e6909bce80a149313426513c8f61645820 [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';
James Kuszmauldac091f2022-03-22 09:35:06 -07003import {Builder, ByteBuffer} from 'flatbuffers';
4import {ErrorResponse} from 'org_frc971/scouting/webserver/requests/messages/error_response_generated';
James Kuszmauldac091f2022-03-22 09:35:06 -07005import {SubmitDataScouting} from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated';
Alex Perrybb901052022-03-23 19:46:15 -07006import {SubmitDataScoutingResponse} from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated';
Philipp Schrader8b8ed672022-03-05 18:08:50 -08007
Ravago Jones2813c032022-03-16 23:44:11 -07008type Section = 'Team Selection'|'Auto'|'TeleOp'|'Climb'|'Other'|
Philipp Schrader23993e82022-03-18 18:54:00 -07009 'Review and Submit'|'Success';
Ravago Jones2813c032022-03-16 23:44:11 -070010type Level = 'NoAttempt'|'Failed'|'FailedWithPlentyOfTime'|'Low'|'Medium'|
Philipp Schrader23993e82022-03-18 18:54:00 -070011 'High'|'Transversal';
Philipp Schrader80587432022-03-05 15:41:22 -080012
Philipp Schrader23993e82022-03-18 18:54:00 -070013@Component({
14 selector: 'app-entry',
15 templateUrl: './entry.ng.html',
16 styleUrls: ['../common.css', './entry.component.css']
17})
18export class EntryComponent {
Ravago Jones2813c032022-03-16 23:44:11 -070019 section: Section = 'Team Selection';
20 @Output() switchTabsEvent = new EventEmitter<string>();
21 @Input() matchNumber: number = 1;
22 @Input() teamNumber: number = 1;
23 autoUpperShotsMade: number = 0;
24 autoLowerShotsMade: number = 0;
25 autoShotsMissed: number = 0;
26 teleUpperShotsMade: number = 0;
27 teleLowerShotsMade: number = 0;
28 teleShotsMissed: number = 0;
29 defensePlayedOnScore: number = 0;
30 defensePlayedScore: number = 0;
31 level: Level = 'NoAttempt';
32 ball1: boolean = false;
33 ball2: boolean = false;
34 ball3: boolean = false;
35 ball4: boolean = false;
36 ball5: boolean = false;
37 quadrant: number = 1;
38 errorMessage: string = '';
39 noShow: boolean = false;
40 neverMoved: boolean = false;
41 batteryDied: boolean = false;
42 mechanicallyBroke: boolean = false;
43 lostComs: boolean = false;
Philipp Schrader80587432022-03-05 15:41:22 -080044
Ravago Jones2813c032022-03-16 23:44:11 -070045 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -070046
Ravago Jones2813c032022-03-16 23:44:11 -070047 nextSection() {
48 if (this.section === 'Team Selection') {
49 this.section = 'Auto';
50 } else if (this.section === 'Auto') {
51 this.section = 'TeleOp';
52 } else if (this.section === 'TeleOp') {
53 this.section = 'Climb';
54 } else if (this.section === 'Climb') {
55 this.section = 'Other';
56 } else if (this.section === 'Other') {
57 this.section = 'Review and Submit';
58 } else if (this.section === 'Review and Submit') {
59 this.submitDataScouting();
60 return;
61 } else if (this.section === 'Success') {
Philipp Schrader23993e82022-03-18 18:54:00 -070062 this.switchTabsEvent.emit('MatchList');
63 return;
Philipp Schrader80587432022-03-05 15:41:22 -080064 }
Ravago Jones2813c032022-03-16 23:44:11 -070065 // Scroll back to the top so that we can be sure the user sees the
66 // entire next screen. Otherwise it's easy to overlook input fields.
67 this.scrollToTop();
68 }
Philipp Schrader80587432022-03-05 15:41:22 -080069
Ravago Jones2813c032022-03-16 23:44:11 -070070 prevSection() {
71 if (this.section === 'Auto') {
72 this.section = 'Team Selection';
73 } else if (this.section === 'TeleOp') {
74 this.section = 'Auto';
75 } else if (this.section === 'Climb') {
76 this.section = 'TeleOp';
77 } else if (this.section === 'Other') {
78 this.section = 'Climb';
79 } else if (this.section === 'Review and Submit') {
80 this.section = 'Other';
Philipp Schrader6b2e9502022-03-15 23:42:56 -070081 }
Ravago Jones2813c032022-03-16 23:44:11 -070082 // Scroll back to the top so that we can be sure the user sees the
83 // entire previous screen. Otherwise it's easy to overlook input
84 // fields.
85 this.scrollToTop();
86 }
Philipp Schrader6b2e9502022-03-15 23:42:56 -070087
Ravago Jones2813c032022-03-16 23:44:11 -070088 private scrollToTop() {
89 this.header.nativeElement.scrollIntoView();
90 }
91
92 async submitDataScouting() {
93 this.errorMessage = '';
94
James Kuszmauldac091f2022-03-22 09:35:06 -070095 const builder = new Builder();
Ravago Jones2813c032022-03-16 23:44:11 -070096 SubmitDataScouting.startSubmitDataScouting(builder);
97 SubmitDataScouting.addTeam(builder, this.teamNumber);
98 SubmitDataScouting.addMatch(builder, this.matchNumber);
99 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
100 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
101 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
102 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
103 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
104 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
105 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
106 SubmitDataScouting.addAutoBall1(builder, this.ball1);
107 SubmitDataScouting.addAutoBall2(builder, this.ball2);
108 SubmitDataScouting.addAutoBall3(builder, this.ball3);
109 SubmitDataScouting.addAutoBall4(builder, this.ball4);
110 SubmitDataScouting.addAutoBall5(builder, this.ball5);
111 SubmitDataScouting.addStartingQuadrant(builder, this.quadrant);
112
113 // TODO(phil): Add support for defensePlayedOnScore.
114 // TODO(phil): Fix the Climbing score.
115 SubmitDataScouting.addClimbing(builder, 1);
116 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
117
118 const buffer = builder.asUint8Array();
119 const res = await fetch(
120 '/requests/submit/data_scouting', {method: 'POST', body: buffer});
121
122 if (res.ok) {
123 // We successfully submitted the data. Report success.
124 this.section = 'Success';
125 } else {
126 const resBuffer = await res.arrayBuffer();
127 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700128 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700129
130 const errorMessage = parsedResponse.errorMessage();
131 this.errorMessage =
132 `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800133 }
Ravago Jones2813c032022-03-16 23:44:11 -0700134 }
Philipp Schrader80587432022-03-05 15:41:22 -0800135}