Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 1 | import { Component, OnInit } from '@angular/core'; |
| 2 | |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 3 | import * as flatbuffer_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder'; |
| 4 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
| 5 | import * as error_response from 'org_frc971/scouting/webserver/requests/messages/error_response_generated'; |
| 6 | import * as submit_data_scouting_response from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated'; |
| 7 | import * as submit_data_scouting from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated'; |
| 8 | import SubmitDataScouting = submit_data_scouting.scouting.webserver.requests.SubmitDataScouting; |
| 9 | import SubmitDataScoutingResponse = submit_data_scouting_response.scouting.webserver.requests.SubmitDataScoutingResponse; |
| 10 | import ErrorResponse = error_response.scouting.webserver.requests.ErrorResponse; |
| 11 | |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 12 | type Section = 'Team Selection'|'Auto'|'TeleOp'|'Climb'|'Defense'|'Review and Submit'|'Home' |
Philipp Schrader | 5990fd3 | 2022-03-15 21:49:58 -0700 | [diff] [blame^] | 13 | type Level = 'NoAttempt'|'Failed'|'FailedWithPlentyOfTime'|'Low'|'Medium'|'High'|'Transversal' |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 14 | |
| 15 | @Component({ |
| 16 | selector: 'app-entry', |
| 17 | templateUrl: './entry.ng.html', |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 18 | styleUrls: ['../common.css', './entry.component.css'] |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 19 | }) |
| 20 | export class EntryComponent { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 21 | section: Section = 'Team Selection'; |
| 22 | matchNumber: number = 1 |
| 23 | teamNumber: number = 1 |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 24 | autoUpperShotsMade: number = 0; |
| 25 | autoLowerShotsMade: number = 0; |
| 26 | autoShotsMissed: number = 0; |
| 27 | teleUpperShotsMade: number = 0; |
| 28 | teleLowerShotsMade: number = 0; |
| 29 | teleShotsMissed: number = 0; |
Yash Chainani | 43a8102 | 2022-03-12 16:46:47 -0800 | [diff] [blame] | 30 | defensePlayedOnScore: number = 0; |
| 31 | defensePlayedScore: number = 0; |
Philipp Schrader | 5990fd3 | 2022-03-15 21:49:58 -0700 | [diff] [blame^] | 32 | level: Level = 'NoAttempt'; |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 33 | errorMessage: string = ''; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 34 | |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 35 | nextSection() { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 36 | if (this.section === 'Team Selection') { |
| 37 | this.section = 'Auto'; |
| 38 | } else if (this.section === 'Auto') { |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 39 | 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 Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 47 | this.submitDataScouting(); |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 51 | prevSection() { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 52 | if (this.section === 'Auto') { |
| 53 | this.section = 'Team Selection'; |
| 54 | } else if (this.section === 'TeleOp') { |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 55 | 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 Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 65 | 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 Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 99 | } |