blob: aaf23dd44ae96e9a7e2cd4ac77246ed204dcb137 [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';
Philipp Schrader8b8ed672022-03-05 18:08:50 -080034 errorMessage: string = '';
Philipp Schradere279e1a2022-03-15 22:20:10 -070035 noShow: boolean = false;
36 neverMoved: boolean = false;
37 batteryDied: boolean = false;
38 mechanicallyBroke: boolean = false;
39 lostComs: boolean = false;
Philipp Schrader80587432022-03-05 15:41:22 -080040
Philipp Schrader6b2e9502022-03-15 23:42:56 -070041 @ViewChild("header") header: ElementRef;
42
Philipp Schrader80587432022-03-05 15:41:22 -080043 nextSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080044 if (this.section === 'Team Selection') {
45 this.section = 'Auto';
46 } else if (this.section === 'Auto') {
Philipp Schrader80587432022-03-05 15:41:22 -080047 this.section = 'TeleOp';
48 } else if (this.section === 'TeleOp') {
49 this.section = 'Climb';
50 } else if (this.section === 'Climb') {
Philipp Schradere279e1a2022-03-15 22:20:10 -070051 this.section = 'Other';
52 } else if (this.section === 'Other') {
Philipp Schrader80587432022-03-05 15:41:22 -080053 this.section = 'Review and Submit';
54 } else if (this.section === 'Review and Submit') {
Philipp Schrader8b8ed672022-03-05 18:08:50 -080055 this.submitDataScouting();
Philipp Schrader6b2e9502022-03-15 23:42:56 -070056 return;
Philipp Schrader80587432022-03-05 15:41:22 -080057 }
Philipp Schrader6b2e9502022-03-15 23:42:56 -070058 // Scroll back to the top so that we can be sure the user sees the
59 // entire next screen. Otherwise it's easy to overlook input fields.
60 this.scrollToTop();
Philipp Schrader80587432022-03-05 15:41:22 -080061 }
62
Alex Perrybb3d2062022-03-05 18:14:33 -080063 prevSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080064 if (this.section === 'Auto') {
65 this.section = 'Team Selection';
66 } else if (this.section === 'TeleOp') {
Alex Perrybb3d2062022-03-05 18:14:33 -080067 this.section = 'Auto';
68 } else if (this.section === 'Climb') {
69 this.section = 'TeleOp';
Philipp Schradere279e1a2022-03-15 22:20:10 -070070 } else if (this.section === 'Other') {
Alex Perrybb3d2062022-03-05 18:14:33 -080071 this.section = 'Climb';
72 } else if (this.section === 'Review and Submit') {
Philipp Schradere279e1a2022-03-15 22:20:10 -070073 this.section = 'Other';
Alex Perrybb3d2062022-03-05 18:14:33 -080074 }
Philipp Schrader6b2e9502022-03-15 23:42:56 -070075 // Scroll back to the top so that we can be sure the user sees the
76 // entire previous screen. Otherwise it's easy to overlook input
77 // fields.
78 this.scrollToTop();
79 }
80
81 private scrollToTop() {
82 this.header.nativeElement.scrollIntoView();
Alex Perrybb3d2062022-03-05 18:14:33 -080083 }
84
Philipp Schrader8b8ed672022-03-05 18:08:50 -080085 async submitDataScouting() {
86 const builder = new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder;
87 SubmitDataScouting.startSubmitDataScouting(builder);
88 SubmitDataScouting.addTeam(builder, this.teamNumber);
89 SubmitDataScouting.addMatch(builder, this.matchNumber);
90 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
91 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
92 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
93 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
94 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
95 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
96 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
97 // TODO(phil): Add support for defensePlayedOnScore.
98 // TODO(phil): Fix the Climbing score.
99 SubmitDataScouting.addClimbing(builder, 1);
100 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
101
102 const buffer = builder.asUint8Array();
103 const res = await fetch(
104 '/requests/submit/data_scouting', {method: 'POST', body: buffer});
105
106 if (res.ok) {
107 // We successfully submitted the data. Go back to Home.
108 this.section = 'Home';
109 } else {
110 const resBuffer = await res.arrayBuffer();
111 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
112 const parsedResponse = ErrorResponse.getRootAsErrorResponse(
113 fbBuffer as unknown as flatbuffers.ByteBuffer);
114
115 const errorMessage = parsedResponse.errorMessage();
116 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
117 }
118 }
Philipp Schrader80587432022-03-05 15:41:22 -0800119}