blob: 9a4caa680f65a34b4868569d73499c63de3fe577 [file] [log] [blame]
Philipp Schrader6b2e9502022-03-15 23:42:56 -07001import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
Philipp Schradere279e1a2022-03-15 22:20:10 -07002import { FormsModule } from '@angular/forms';
Philipp Schrader80587432022-03-05 15:41:22 -08003
Philipp Schrader8b8ed672022-03-05 18:08:50 -08004import * as flatbuffer_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
5import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
6import * as error_response from 'org_frc971/scouting/webserver/requests/messages/error_response_generated';
7import * as submit_data_scouting_response from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated';
8import * as submit_data_scouting from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated';
9import 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
Philipp Schradere279e1a2022-03-15 22:20:10 -070013type Section = 'Team Selection'|'Auto'|'TeleOp'|'Climb'|'Other'|'Review and Submit'|'Home'
Philipp Schrader5990fd32022-03-15 21:49:58 -070014type Level = 'NoAttempt'|'Failed'|'FailedWithPlentyOfTime'|'Low'|'Medium'|'High'|'Transversal'
Philipp Schrader80587432022-03-05 15:41:22 -080015
16@Component({
17 selector: 'app-entry',
18 templateUrl: './entry.ng.html',
Philipp Schrader72beced2022-03-07 05:29:52 -080019 styleUrls: ['../common.css', './entry.component.css']
Philipp Schrader80587432022-03-05 15:41:22 -080020})
21export class EntryComponent {
Philipp Schrader93ade042022-03-05 17:16:10 -080022 section: Section = 'Team Selection';
23 matchNumber: number = 1
24 teamNumber: number = 1
Philipp Schrader80587432022-03-05 15:41:22 -080025 autoUpperShotsMade: number = 0;
26 autoLowerShotsMade: number = 0;
27 autoShotsMissed: number = 0;
28 teleUpperShotsMade: number = 0;
29 teleLowerShotsMade: number = 0;
30 teleShotsMissed: number = 0;
Yash Chainani43a81022022-03-12 16:46:47 -080031 defensePlayedOnScore: number = 0;
32 defensePlayedScore: number = 0;
Philipp Schrader5990fd32022-03-15 21:49:58 -070033 level: Level = 'NoAttempt';
Milo Lin905e78a2022-03-12 15:55:35 -080034 ball1: boolean = false;
35 ball2: boolean = false;
36 ball3: boolean = false;
37 ball4: boolean = false;
38 ball5: boolean = false;
Philipp Schrader8b8ed672022-03-05 18:08:50 -080039 errorMessage: string = '';
Philipp Schradere279e1a2022-03-15 22:20:10 -070040 noShow: boolean = false;
41 neverMoved: boolean = false;
42 batteryDied: boolean = false;
43 mechanicallyBroke: boolean = false;
44 lostComs: boolean = false;
Philipp Schrader80587432022-03-05 15:41:22 -080045
Philipp Schrader6b2e9502022-03-15 23:42:56 -070046 @ViewChild("header") header: ElementRef;
47
Philipp Schrader80587432022-03-05 15:41:22 -080048 nextSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080049 if (this.section === 'Team Selection') {
50 this.section = 'Auto';
51 } else if (this.section === 'Auto') {
Philipp Schrader80587432022-03-05 15:41:22 -080052 this.section = 'TeleOp';
53 } else if (this.section === 'TeleOp') {
54 this.section = 'Climb';
55 } else if (this.section === 'Climb') {
Philipp Schradere279e1a2022-03-15 22:20:10 -070056 this.section = 'Other';
57 } else if (this.section === 'Other') {
Philipp Schrader80587432022-03-05 15:41:22 -080058 this.section = 'Review and Submit';
59 } else if (this.section === 'Review and Submit') {
Philipp Schrader8b8ed672022-03-05 18:08:50 -080060 this.submitDataScouting();
Philipp Schrader6b2e9502022-03-15 23:42:56 -070061 return;
Philipp Schrader80587432022-03-05 15:41:22 -080062 }
Philipp Schrader6b2e9502022-03-15 23:42:56 -070063 // Scroll back to the top so that we can be sure the user sees the
64 // entire next screen. Otherwise it's easy to overlook input fields.
65 this.scrollToTop();
Philipp Schrader80587432022-03-05 15:41:22 -080066 }
67
Alex Perrybb3d2062022-03-05 18:14:33 -080068 prevSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080069 if (this.section === 'Auto') {
70 this.section = 'Team Selection';
71 } else if (this.section === 'TeleOp') {
Alex Perrybb3d2062022-03-05 18:14:33 -080072 this.section = 'Auto';
73 } else if (this.section === 'Climb') {
74 this.section = 'TeleOp';
Philipp Schradere279e1a2022-03-15 22:20:10 -070075 } else if (this.section === 'Other') {
Alex Perrybb3d2062022-03-05 18:14:33 -080076 this.section = 'Climb';
77 } else if (this.section === 'Review and Submit') {
Philipp Schradere279e1a2022-03-15 22:20:10 -070078 this.section = 'Other';
Alex Perrybb3d2062022-03-05 18:14:33 -080079 }
Philipp Schrader6b2e9502022-03-15 23:42:56 -070080 // Scroll back to the top so that we can be sure the user sees the
81 // entire previous screen. Otherwise it's easy to overlook input
82 // fields.
83 this.scrollToTop();
84 }
85
86 private scrollToTop() {
87 this.header.nativeElement.scrollIntoView();
Alex Perrybb3d2062022-03-05 18:14:33 -080088 }
89
Philipp Schrader8b8ed672022-03-05 18:08:50 -080090 async submitDataScouting() {
91 const builder = new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder;
92 SubmitDataScouting.startSubmitDataScouting(builder);
93 SubmitDataScouting.addTeam(builder, this.teamNumber);
94 SubmitDataScouting.addMatch(builder, this.matchNumber);
95 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
96 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
97 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
98 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
99 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
100 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
101 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
Milo Lin905e78a2022-03-12 15:55:35 -0800102 SubmitDataScouting.addAutoBall1(builder, this.ball1);
103 SubmitDataScouting.addAutoBall2(builder, this.ball2);
104 SubmitDataScouting.addAutoBall3(builder, this.ball3);
105 SubmitDataScouting.addAutoBall4(builder, this.ball4);
106 SubmitDataScouting.addAutoBall5(builder, this.ball5);
107
Philipp Schrader8b8ed672022-03-05 18:08:50 -0800108 // TODO(phil): Add support for defensePlayedOnScore.
109 // TODO(phil): Fix the Climbing score.
110 SubmitDataScouting.addClimbing(builder, 1);
111 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
112
113 const buffer = builder.asUint8Array();
114 const res = await fetch(
115 '/requests/submit/data_scouting', {method: 'POST', body: buffer});
116
117 if (res.ok) {
118 // We successfully submitted the data. Go back to Home.
119 this.section = 'Home';
120 } else {
121 const resBuffer = await res.arrayBuffer();
122 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
123 const parsedResponse = ErrorResponse.getRootAsErrorResponse(
124 fbBuffer as unknown as flatbuffers.ByteBuffer);
125
126 const errorMessage = parsedResponse.errorMessage();
127 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
128 }
129 }
Philipp Schrader80587432022-03-05 15:41:22 -0800130}