blob: 82819d87bc5a394fddf92327ad281ec52d3ecd14 [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';
Philipp Schrader36df73a2022-03-17 23:27:24 -07005import {ClimbLevel, 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';
Philipp Schrader80587432022-03-05 15:41:22 -080010
Philipp Schrader23993e82022-03-18 18:54:00 -070011@Component({
12 selector: 'app-entry',
13 templateUrl: './entry.ng.html',
14 styleUrls: ['../common.css', './entry.component.css']
15})
16export class EntryComponent {
Philipp Schrader36df73a2022-03-17 23:27:24 -070017 // Re-export the type here so that we can use it in the `[value]` attribute
18 // of radio buttons.
19 readonly ClimbLevel = ClimbLevel;
20
Ravago Jones2813c032022-03-16 23:44:11 -070021 section: Section = 'Team Selection';
22 @Output() switchTabsEvent = new EventEmitter<string>();
23 @Input() matchNumber: number = 1;
24 @Input() teamNumber: number = 1;
25 autoUpperShotsMade: number = 0;
26 autoLowerShotsMade: number = 0;
27 autoShotsMissed: number = 0;
28 teleUpperShotsMade: number = 0;
29 teleLowerShotsMade: number = 0;
30 teleShotsMissed: number = 0;
31 defensePlayedOnScore: number = 0;
32 defensePlayedScore: number = 0;
Philipp Schrader36df73a2022-03-17 23:27:24 -070033 level: ClimbLevel = ClimbLevel.NoAttempt;
Ravago Jones2813c032022-03-16 23:44:11 -070034 ball1: boolean = false;
35 ball2: boolean = false;
36 ball3: boolean = false;
37 ball4: boolean = false;
38 ball5: boolean = false;
39 quadrant: number = 1;
40 errorMessage: string = '';
41 noShow: boolean = false;
42 neverMoved: boolean = false;
43 batteryDied: boolean = false;
44 mechanicallyBroke: boolean = false;
45 lostComs: boolean = false;
Philipp Schrader9bcbc332022-03-18 19:06:04 -070046 comment: string = '';
Philipp Schrader80587432022-03-05 15:41:22 -080047
Ravago Jones2813c032022-03-16 23:44:11 -070048 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -070049
Ravago Jones2813c032022-03-16 23:44:11 -070050 nextSection() {
51 if (this.section === 'Team Selection') {
52 this.section = 'Auto';
53 } else if (this.section === 'Auto') {
54 this.section = 'TeleOp';
55 } else if (this.section === 'TeleOp') {
56 this.section = 'Climb';
57 } else if (this.section === 'Climb') {
58 this.section = 'Other';
59 } else if (this.section === 'Other') {
60 this.section = 'Review and Submit';
61 } else if (this.section === 'Review and Submit') {
Philipp Schrader36df73a2022-03-17 23:27:24 -070062
Ravago Jones2813c032022-03-16 23:44:11 -070063 this.submitDataScouting();
64 return;
65 } else if (this.section === 'Success') {
Philipp Schrader23993e82022-03-18 18:54:00 -070066 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
James Kuszmauldac091f2022-03-22 09:35:06 -070099 const builder = new Builder();
Ravago Jones2813c032022-03-16 23:44:11 -0700100 SubmitDataScouting.startSubmitDataScouting(builder);
101 SubmitDataScouting.addTeam(builder, this.teamNumber);
102 SubmitDataScouting.addMatch(builder, this.matchNumber);
103 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
104 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
105 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
106 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
107 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
108 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
109 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
110 SubmitDataScouting.addAutoBall1(builder, this.ball1);
111 SubmitDataScouting.addAutoBall2(builder, this.ball2);
112 SubmitDataScouting.addAutoBall3(builder, this.ball3);
113 SubmitDataScouting.addAutoBall4(builder, this.ball4);
114 SubmitDataScouting.addAutoBall5(builder, this.ball5);
115 SubmitDataScouting.addStartingQuadrant(builder, this.quadrant);
Philipp Schrader9bcbc332022-03-18 19:06:04 -0700116 // TODO(ishan): Add support for the comment.
Ravago Jones2813c032022-03-16 23:44:11 -0700117 // TODO(phil): Add support for defensePlayedOnScore.
Philipp Schrader36df73a2022-03-17 23:27:24 -0700118 SubmitDataScouting.addClimbLevel(builder, this.level);
Ravago Jones2813c032022-03-16 23:44:11 -0700119 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
120
121 const buffer = builder.asUint8Array();
122 const res = await fetch(
123 '/requests/submit/data_scouting', {method: 'POST', body: buffer});
124
125 if (res.ok) {
126 // We successfully submitted the data. Report success.
127 this.section = 'Success';
128 } else {
129 const resBuffer = await res.arrayBuffer();
130 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700131 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700132
133 const errorMessage = parsedResponse.errorMessage();
134 this.errorMessage =
135 `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800136 }
Ravago Jones2813c032022-03-16 23:44:11 -0700137 }
Philipp Schrader80587432022-03-05 15:41:22 -0800138}