blob: 3db5f723c641010757b192b84be25b4c4f727d66 [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 nextSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080062 if (this.section === 'Team Selection') {
63 this.section = 'Auto';
64 } else if (this.section === 'Auto') {
Philipp Schrader80587432022-03-05 15:41:22 -080065 this.section = 'TeleOp';
66 } else if (this.section === 'TeleOp') {
67 this.section = 'Climb';
68 } else if (this.section === 'Climb') {
69 this.section = 'Defense';
70 } else if (this.section === 'Defense') {
71 this.section = 'Review and Submit';
72 } else if (this.section === 'Review and Submit') {
Philipp Schrader8b8ed672022-03-05 18:08:50 -080073 this.submitDataScouting();
Philipp Schrader80587432022-03-05 15:41:22 -080074 }
75 }
76
Alex Perrybb3d2062022-03-05 18:14:33 -080077 prevSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080078 if (this.section === 'Auto') {
79 this.section = 'Team Selection';
80 } else if (this.section === 'TeleOp') {
Alex Perrybb3d2062022-03-05 18:14:33 -080081 this.section = 'Auto';
82 } else if (this.section === 'Climb') {
83 this.section = 'TeleOp';
84 } else if (this.section === 'Defense') {
85 this.section = 'Climb';
86 } else if (this.section === 'Review and Submit') {
87 this.section = 'Defense';
88 }
89 }
90
Philipp Schrader8b8ed672022-03-05 18:08:50 -080091 async submitDataScouting() {
92 const builder = new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder;
93 SubmitDataScouting.startSubmitDataScouting(builder);
94 SubmitDataScouting.addTeam(builder, this.teamNumber);
95 SubmitDataScouting.addMatch(builder, this.matchNumber);
96 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
97 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
98 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
99 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
100 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
101 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
102 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
103 // TODO(phil): Add support for defensePlayedOnScore.
104 // TODO(phil): Fix the Climbing score.
105 SubmitDataScouting.addClimbing(builder, 1);
106 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
107
108 const buffer = builder.asUint8Array();
109 const res = await fetch(
110 '/requests/submit/data_scouting', {method: 'POST', body: buffer});
111
112 if (res.ok) {
113 // We successfully submitted the data. Go back to Home.
114 this.section = 'Home';
115 } else {
116 const resBuffer = await res.arrayBuffer();
117 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
118 const parsedResponse = ErrorResponse.getRootAsErrorResponse(
119 fbBuffer as unknown as flatbuffers.ByteBuffer);
120
121 const errorMessage = parsedResponse.errorMessage();
122 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
123 }
124 }
Philipp Schrader80587432022-03-05 15:41:22 -0800125}