blob: c90e3d04630d9165bcb2d6de93858b0fcb8c4911 [file] [log] [blame]
Philipp Schrader817cce32022-03-26 15:00:00 -07001import {
2 Component,
3 ElementRef,
4 EventEmitter,
5 Input,
6 OnInit,
7 Output,
8 ViewChild,
9} from '@angular/core';
Ravago Jones2813c032022-03-16 23:44:11 -070010import {FormsModule} from '@angular/forms';
James Kuszmauldac091f2022-03-22 09:35:06 -070011import {Builder, ByteBuffer} from 'flatbuffers';
12import {ErrorResponse} from 'org_frc971/scouting/webserver/requests/messages/error_response_generated';
Philipp Schrader817cce32022-03-26 15:00:00 -070013import {
14 ClimbLevel,
15 SubmitDataScouting,
16} from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated';
Alex Perrybb901052022-03-23 19:46:15 -070017import {SubmitDataScoutingResponse} from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated';
Philipp Schrader8b8ed672022-03-05 18:08:50 -080018
Philipp Schrader817cce32022-03-26 15:00:00 -070019type Section =
20 | 'Team Selection'
21 | 'Auto'
22 | 'TeleOp'
23 | 'Climb'
24 | 'Other'
25 | 'Review and Submit'
26 | 'Success';
Philipp Schrader80587432022-03-05 15:41:22 -080027
Philipp Schrader23993e82022-03-18 18:54:00 -070028@Component({
29 selector: 'app-entry',
30 templateUrl: './entry.ng.html',
Philipp Schrader817cce32022-03-26 15:00:00 -070031 styleUrls: ['../common.css', './entry.component.css'],
Philipp Schrader23993e82022-03-18 18:54:00 -070032})
33export class EntryComponent {
Philipp Schrader36df73a2022-03-17 23:27:24 -070034 // Re-export the type here so that we can use it in the `[value]` attribute
35 // of radio buttons.
36 readonly ClimbLevel = ClimbLevel;
37
Ravago Jones2813c032022-03-16 23:44:11 -070038 section: Section = 'Team Selection';
39 @Output() switchTabsEvent = new EventEmitter<string>();
40 @Input() matchNumber: number = 1;
41 @Input() teamNumber: number = 1;
42 autoUpperShotsMade: number = 0;
43 autoLowerShotsMade: number = 0;
44 autoShotsMissed: number = 0;
45 teleUpperShotsMade: number = 0;
46 teleLowerShotsMade: number = 0;
47 teleShotsMissed: number = 0;
48 defensePlayedOnScore: number = 0;
49 defensePlayedScore: number = 0;
Philipp Schrader36df73a2022-03-17 23:27:24 -070050 level: ClimbLevel = ClimbLevel.NoAttempt;
Ravago Jones2813c032022-03-16 23:44:11 -070051 ball1: boolean = false;
52 ball2: boolean = false;
53 ball3: boolean = false;
54 ball4: boolean = false;
55 ball5: boolean = false;
56 quadrant: number = 1;
57 errorMessage: string = '';
58 noShow: boolean = false;
59 neverMoved: boolean = false;
60 batteryDied: boolean = false;
61 mechanicallyBroke: boolean = false;
62 lostComs: boolean = false;
Philipp Schrader9bcbc332022-03-18 19:06:04 -070063 comment: string = '';
Philipp Schrader80587432022-03-05 15:41:22 -080064
Ravago Jones2813c032022-03-16 23:44:11 -070065 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -070066
Ravago Jones2813c032022-03-16 23:44:11 -070067 nextSection() {
68 if (this.section === 'Team Selection') {
69 this.section = 'Auto';
70 } else if (this.section === 'Auto') {
71 this.section = 'TeleOp';
72 } else if (this.section === 'TeleOp') {
73 this.section = 'Climb';
74 } else if (this.section === 'Climb') {
75 this.section = 'Other';
76 } else if (this.section === 'Other') {
77 this.section = 'Review and Submit';
78 } else if (this.section === 'Review and Submit') {
79 this.submitDataScouting();
80 return;
81 } else if (this.section === 'Success') {
Philipp Schrader23993e82022-03-18 18:54:00 -070082 this.switchTabsEvent.emit('MatchList');
83 return;
Philipp Schrader80587432022-03-05 15:41:22 -080084 }
Ravago Jones2813c032022-03-16 23:44:11 -070085 // Scroll back to the top so that we can be sure the user sees the
86 // entire next screen. Otherwise it's easy to overlook input fields.
87 this.scrollToTop();
88 }
Philipp Schrader80587432022-03-05 15:41:22 -080089
Ravago Jones2813c032022-03-16 23:44:11 -070090 prevSection() {
91 if (this.section === 'Auto') {
92 this.section = 'Team Selection';
93 } else if (this.section === 'TeleOp') {
94 this.section = 'Auto';
95 } else if (this.section === 'Climb') {
96 this.section = 'TeleOp';
97 } else if (this.section === 'Other') {
98 this.section = 'Climb';
99 } else if (this.section === 'Review and Submit') {
100 this.section = 'Other';
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700101 }
Ravago Jones2813c032022-03-16 23:44:11 -0700102 // Scroll back to the top so that we can be sure the user sees the
103 // entire previous screen. Otherwise it's easy to overlook input
104 // fields.
105 this.scrollToTop();
106 }
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700107
Ravago Jones2813c032022-03-16 23:44:11 -0700108 private scrollToTop() {
109 this.header.nativeElement.scrollIntoView();
110 }
111
112 async submitDataScouting() {
113 this.errorMessage = '';
114
James Kuszmauldac091f2022-03-22 09:35:06 -0700115 const builder = new Builder();
Philipp Schrader70569f72022-03-18 19:15:05 -0700116 const comment = builder.createString(this.comment);
Ravago Jones2813c032022-03-16 23:44:11 -0700117 SubmitDataScouting.startSubmitDataScouting(builder);
118 SubmitDataScouting.addTeam(builder, this.teamNumber);
119 SubmitDataScouting.addMatch(builder, this.matchNumber);
120 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
121 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
122 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
123 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
124 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
125 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
126 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
Philipp Schrader817cce32022-03-26 15:00:00 -0700127 SubmitDataScouting.addDefenseReceivedRating(
128 builder,
129 this.defensePlayedOnScore
130 );
Ravago Jones2813c032022-03-16 23:44:11 -0700131 SubmitDataScouting.addAutoBall1(builder, this.ball1);
132 SubmitDataScouting.addAutoBall2(builder, this.ball2);
133 SubmitDataScouting.addAutoBall3(builder, this.ball3);
134 SubmitDataScouting.addAutoBall4(builder, this.ball4);
135 SubmitDataScouting.addAutoBall5(builder, this.ball5);
136 SubmitDataScouting.addStartingQuadrant(builder, this.quadrant);
Philipp Schrader36df73a2022-03-17 23:27:24 -0700137 SubmitDataScouting.addClimbLevel(builder, this.level);
Philipp Schrader70569f72022-03-18 19:15:05 -0700138 SubmitDataScouting.addComment(builder, comment);
Ravago Jones2813c032022-03-16 23:44:11 -0700139 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
140
141 const buffer = builder.asUint8Array();
Philipp Schrader817cce32022-03-26 15:00:00 -0700142 const res = await fetch('/requests/submit/data_scouting', {
143 method: 'POST',
144 body: buffer,
145 });
Ravago Jones2813c032022-03-16 23:44:11 -0700146
147 if (res.ok) {
148 // We successfully submitted the data. Report success.
149 this.section = 'Success';
150 } else {
151 const resBuffer = await res.arrayBuffer();
152 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700153 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700154
155 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700156 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800157 }
Ravago Jones2813c032022-03-16 23:44:11 -0700158 }
Philipp Schrader80587432022-03-05 15:41:22 -0800159}