blob: 4c94670966f0a33b18db8f4ee568c40c6bcb8a95 [file] [log] [blame]
Philipp Schrader80587432022-03-05 15:41:22 -08001import { Component, OnInit } from '@angular/core';
2
Philipp Schrader8b8ed672022-03-05 18:08:50 -08003import * as flatbuffer_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
4import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
5import * as error_response from 'org_frc971/scouting/webserver/requests/messages/error_response_generated';
6import * as submit_data_scouting_response from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated';
7import * as submit_data_scouting from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated';
8import SubmitDataScouting = submit_data_scouting.scouting.webserver.requests.SubmitDataScouting;
9import SubmitDataScoutingResponse = submit_data_scouting_response.scouting.webserver.requests.SubmitDataScoutingResponse;
10import ErrorResponse = error_response.scouting.webserver.requests.ErrorResponse;
11
Philipp Schrader93ade042022-03-05 17:16:10 -080012type Section = 'Team Selection'|'Auto'|'TeleOp'|'Climb'|'Defense'|'Review and Submit'|'Home'
Milo Linefef48f2022-03-12 15:18:56 -080013type Level = 'Failed'|'Low'|'Medium'|'High'|'Transversal'
Philipp Schrader80587432022-03-05 15:41:22 -080014
15@Component({
16 selector: 'app-entry',
17 templateUrl: './entry.ng.html',
Philipp Schrader72beced2022-03-07 05:29:52 -080018 styleUrls: ['../common.css', './entry.component.css']
Philipp Schrader80587432022-03-05 15:41:22 -080019})
20export class EntryComponent {
Philipp Schrader93ade042022-03-05 17:16:10 -080021 section: Section = 'Team Selection';
22 matchNumber: number = 1
23 teamNumber: number = 1
Philipp Schrader80587432022-03-05 15:41:22 -080024 autoUpperShotsMade: number = 0;
25 autoLowerShotsMade: number = 0;
26 autoShotsMissed: number = 0;
27 teleUpperShotsMade: number = 0;
28 teleLowerShotsMade: number = 0;
29 teleShotsMissed: number = 0;
Yash Chainani43a81022022-03-12 16:46:47 -080030 defensePlayedOnScore: number = 0;
31 defensePlayedScore: number = 0;
Philipp Schrader80587432022-03-05 15:41:22 -080032 level: Level;
33 proper: boolean = false;
34 climbed: boolean = false;
Philipp Schrader8b8ed672022-03-05 18:08:50 -080035 errorMessage: string = '';
Philipp Schrader80587432022-03-05 15:41:22 -080036
37 toggleProper() {
38 this.proper = !this.proper;
39 }
40
Milo Linefef48f2022-03-12 15:18:56 -080041 setFailed() {
42 this.level = 'Failed';
43 }
44
Philipp Schrader80587432022-03-05 15:41:22 -080045 setLow() {
46 this.level = 'Low';
47 }
48
49 setMedium() {
50 this.level = 'Medium';
51 }
52
53 setHigh() {
54 this.level = 'High';
55 }
56
57 setTransversal() {
58 this.level = 'Transversal';
59 }
60
Philipp Schrader80587432022-03-05 15:41:22 -080061 setClimbedTrue() {
62 this.climbed = true;
63 }
64
65 setClimbedFalse() {
66 this.climbed = false;
67 }
68
69 nextSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080070 if (this.section === 'Team Selection') {
71 this.section = 'Auto';
72 } else if (this.section === 'Auto') {
Philipp Schrader80587432022-03-05 15:41:22 -080073 this.section = 'TeleOp';
74 } else if (this.section === 'TeleOp') {
75 this.section = 'Climb';
76 } else if (this.section === 'Climb') {
77 this.section = 'Defense';
78 } else if (this.section === 'Defense') {
79 this.section = 'Review and Submit';
80 } else if (this.section === 'Review and Submit') {
Philipp Schrader8b8ed672022-03-05 18:08:50 -080081 this.submitDataScouting();
Philipp Schrader80587432022-03-05 15:41:22 -080082 }
83 }
84
Alex Perrybb3d2062022-03-05 18:14:33 -080085 prevSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080086 if (this.section === 'Auto') {
87 this.section = 'Team Selection';
88 } else if (this.section === 'TeleOp') {
Alex Perrybb3d2062022-03-05 18:14:33 -080089 this.section = 'Auto';
90 } else if (this.section === 'Climb') {
91 this.section = 'TeleOp';
92 } else if (this.section === 'Defense') {
93 this.section = 'Climb';
94 } else if (this.section === 'Review and Submit') {
95 this.section = 'Defense';
96 }
97 }
98
Philipp Schrader8b8ed672022-03-05 18:08:50 -080099 async submitDataScouting() {
100 const builder = new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder;
101 SubmitDataScouting.startSubmitDataScouting(builder);
102 SubmitDataScouting.addTeam(builder, this.teamNumber);
103 SubmitDataScouting.addMatch(builder, this.matchNumber);
104 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
105 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
106 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
107 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
108 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
109 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
110 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
111 // TODO(phil): Add support for defensePlayedOnScore.
112 // TODO(phil): Fix the Climbing score.
113 SubmitDataScouting.addClimbing(builder, 1);
114 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
115
116 const buffer = builder.asUint8Array();
117 const res = await fetch(
118 '/requests/submit/data_scouting', {method: 'POST', body: buffer});
119
120 if (res.ok) {
121 // We successfully submitted the data. Go back to Home.
122 this.section = 'Home';
123 } else {
124 const resBuffer = await res.arrayBuffer();
125 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
126 const parsedResponse = ErrorResponse.getRootAsErrorResponse(
127 fbBuffer as unknown as flatbuffers.ByteBuffer);
128
129 const errorMessage = parsedResponse.errorMessage();
130 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
131 }
132 }
Philipp Schrader80587432022-03-05 15:41:22 -0800133}