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 | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 13 | type Level = 'Low'|'Medium'|'High'|'Transversal' |
| 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 | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 32 | level: Level; |
| 33 | proper: boolean = false; |
| 34 | climbed: boolean = false; |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 35 | errorMessage: string = ''; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 36 | |
| 37 | toggleProper() { |
| 38 | this.proper = !this.proper; |
| 39 | } |
| 40 | |
| 41 | setLow() { |
| 42 | this.level = 'Low'; |
| 43 | } |
| 44 | |
| 45 | setMedium() { |
| 46 | this.level = 'Medium'; |
| 47 | } |
| 48 | |
| 49 | setHigh() { |
| 50 | this.level = 'High'; |
| 51 | } |
| 52 | |
| 53 | setTransversal() { |
| 54 | this.level = 'Transversal'; |
| 55 | } |
| 56 | |
| 57 | defensePlayedOnSlider(event) { |
| 58 | this.defensePlayedOnScore = event.target.value; |
| 59 | } |
| 60 | |
| 61 | defensePlayedSlider(event) { |
| 62 | this.defensePlayedScore = event.target.value; |
| 63 | } |
| 64 | |
| 65 | setClimbedTrue() { |
| 66 | this.climbed = true; |
| 67 | } |
| 68 | |
| 69 | setClimbedFalse() { |
| 70 | this.climbed = false; |
| 71 | } |
| 72 | |
| 73 | nextSection() { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 74 | if (this.section === 'Team Selection') { |
| 75 | this.section = 'Auto'; |
| 76 | } else if (this.section === 'Auto') { |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 77 | this.section = 'TeleOp'; |
| 78 | } else if (this.section === 'TeleOp') { |
| 79 | this.section = 'Climb'; |
| 80 | } else if (this.section === 'Climb') { |
| 81 | this.section = 'Defense'; |
| 82 | } else if (this.section === 'Defense') { |
| 83 | this.section = 'Review and Submit'; |
| 84 | } else if (this.section === 'Review and Submit') { |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 85 | this.submitDataScouting(); |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 89 | prevSection() { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 90 | if (this.section === 'Auto') { |
| 91 | this.section = 'Team Selection'; |
| 92 | } else if (this.section === 'TeleOp') { |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 93 | this.section = 'Auto'; |
| 94 | } else if (this.section === 'Climb') { |
| 95 | this.section = 'TeleOp'; |
| 96 | } else if (this.section === 'Defense') { |
| 97 | this.section = 'Climb'; |
| 98 | } else if (this.section === 'Review and Submit') { |
| 99 | this.section = 'Defense'; |
| 100 | } |
| 101 | } |
| 102 | |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 103 | async submitDataScouting() { |
| 104 | const builder = new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder; |
| 105 | SubmitDataScouting.startSubmitDataScouting(builder); |
| 106 | SubmitDataScouting.addTeam(builder, this.teamNumber); |
| 107 | SubmitDataScouting.addMatch(builder, this.matchNumber); |
| 108 | SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed); |
| 109 | SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade); |
| 110 | SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade); |
| 111 | SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed); |
| 112 | SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade); |
| 113 | SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade); |
| 114 | SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore); |
| 115 | // TODO(phil): Add support for defensePlayedOnScore. |
| 116 | // TODO(phil): Fix the Climbing score. |
| 117 | SubmitDataScouting.addClimbing(builder, 1); |
| 118 | builder.finish(SubmitDataScouting.endSubmitDataScouting(builder)); |
| 119 | |
| 120 | const buffer = builder.asUint8Array(); |
| 121 | const res = await fetch( |
| 122 | '/requests/submit/data_scouting', {method: 'POST', body: buffer}); |
| 123 | |
| 124 | if (res.ok) { |
| 125 | // We successfully submitted the data. Go back to Home. |
| 126 | this.section = 'Home'; |
| 127 | } else { |
| 128 | const resBuffer = await res.arrayBuffer(); |
| 129 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
| 130 | const parsedResponse = ErrorResponse.getRootAsErrorResponse( |
| 131 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
| 132 | |
| 133 | const errorMessage = parsedResponse.errorMessage(); |
| 134 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
| 135 | } |
| 136 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 137 | } |