blob: d83f709c6621f0d3576715cfc2e730fd5cad0d12 [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'
Philipp Schrader5990fd32022-03-15 21:49:58 -070013type Level = 'NoAttempt'|'Failed'|'FailedWithPlentyOfTime'|'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 Schrader5990fd32022-03-15 21:49:58 -070032 level: Level = 'NoAttempt';
Philipp Schrader8b8ed672022-03-05 18:08:50 -080033 errorMessage: string = '';
Philipp Schrader80587432022-03-05 15:41:22 -080034
Philipp Schrader80587432022-03-05 15:41:22 -080035 nextSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080036 if (this.section === 'Team Selection') {
37 this.section = 'Auto';
38 } else if (this.section === 'Auto') {
Philipp Schrader80587432022-03-05 15:41:22 -080039 this.section = 'TeleOp';
40 } else if (this.section === 'TeleOp') {
41 this.section = 'Climb';
42 } else if (this.section === 'Climb') {
43 this.section = 'Defense';
44 } else if (this.section === 'Defense') {
45 this.section = 'Review and Submit';
46 } else if (this.section === 'Review and Submit') {
Philipp Schrader8b8ed672022-03-05 18:08:50 -080047 this.submitDataScouting();
Philipp Schrader80587432022-03-05 15:41:22 -080048 }
49 }
50
Alex Perrybb3d2062022-03-05 18:14:33 -080051 prevSection() {
Philipp Schrader93ade042022-03-05 17:16:10 -080052 if (this.section === 'Auto') {
53 this.section = 'Team Selection';
54 } else if (this.section === 'TeleOp') {
Alex Perrybb3d2062022-03-05 18:14:33 -080055 this.section = 'Auto';
56 } else if (this.section === 'Climb') {
57 this.section = 'TeleOp';
58 } else if (this.section === 'Defense') {
59 this.section = 'Climb';
60 } else if (this.section === 'Review and Submit') {
61 this.section = 'Defense';
62 }
63 }
64
Philipp Schrader8b8ed672022-03-05 18:08:50 -080065 async submitDataScouting() {
66 const builder = new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder;
67 SubmitDataScouting.startSubmitDataScouting(builder);
68 SubmitDataScouting.addTeam(builder, this.teamNumber);
69 SubmitDataScouting.addMatch(builder, this.matchNumber);
70 SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed);
71 SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade);
72 SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade);
73 SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed);
74 SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade);
75 SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade);
76 SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore);
77 // TODO(phil): Add support for defensePlayedOnScore.
78 // TODO(phil): Fix the Climbing score.
79 SubmitDataScouting.addClimbing(builder, 1);
80 builder.finish(SubmitDataScouting.endSubmitDataScouting(builder));
81
82 const buffer = builder.asUint8Array();
83 const res = await fetch(
84 '/requests/submit/data_scouting', {method: 'POST', body: buffer});
85
86 if (res.ok) {
87 // We successfully submitted the data. Go back to Home.
88 this.section = 'Home';
89 } else {
90 const resBuffer = await res.arrayBuffer();
91 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
92 const parsedResponse = ErrorResponse.getRootAsErrorResponse(
93 fbBuffer as unknown as flatbuffers.ByteBuffer);
94
95 const errorMessage = parsedResponse.errorMessage();
96 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
97 }
98 }
Philipp Schrader80587432022-03-05 15:41:22 -080099}