Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 2 | import { FormsModule } from '@angular/forms'; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 3 | |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 4 | import * as flatbuffer_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder'; |
| 5 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
| 6 | import * as error_response from 'org_frc971/scouting/webserver/requests/messages/error_response_generated'; |
| 7 | import * as submit_data_scouting_response from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated'; |
| 8 | import * as submit_data_scouting from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated'; |
| 9 | import SubmitDataScouting = submit_data_scouting.scouting.webserver.requests.SubmitDataScouting; |
| 10 | import SubmitDataScoutingResponse = submit_data_scouting_response.scouting.webserver.requests.SubmitDataScoutingResponse; |
| 11 | import ErrorResponse = error_response.scouting.webserver.requests.ErrorResponse; |
| 12 | |
Philipp Schrader | a3c4e26 | 2022-03-17 09:04:12 -0700 | [diff] [blame] | 13 | type Section = 'Team Selection'|'Auto'|'TeleOp'|'Climb'|'Other'|'Review and Submit'|'Success' |
Philipp Schrader | 5990fd3 | 2022-03-15 21:49:58 -0700 | [diff] [blame] | 14 | type Level = 'NoAttempt'|'Failed'|'FailedWithPlentyOfTime'|'Low'|'Medium'|'High'|'Transversal' |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 15 | |
| 16 | @Component({ |
| 17 | selector: 'app-entry', |
| 18 | templateUrl: './entry.ng.html', |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 19 | styleUrls: ['../common.css', './entry.component.css'] |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 20 | }) |
| 21 | export class EntryComponent { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 22 | section: Section = 'Team Selection'; |
| 23 | matchNumber: number = 1 |
| 24 | teamNumber: number = 1 |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 25 | autoUpperShotsMade: number = 0; |
| 26 | autoLowerShotsMade: number = 0; |
| 27 | autoShotsMissed: number = 0; |
| 28 | teleUpperShotsMade: number = 0; |
| 29 | teleLowerShotsMade: number = 0; |
| 30 | teleShotsMissed: number = 0; |
Yash Chainani | 43a8102 | 2022-03-12 16:46:47 -0800 | [diff] [blame] | 31 | defensePlayedOnScore: number = 0; |
| 32 | defensePlayedScore: number = 0; |
Philipp Schrader | 5990fd3 | 2022-03-15 21:49:58 -0700 | [diff] [blame] | 33 | level: Level = 'NoAttempt'; |
Milo Lin | 905e78a | 2022-03-12 15:55:35 -0800 | [diff] [blame] | 34 | ball1: boolean = false; |
| 35 | ball2: boolean = false; |
| 36 | ball3: boolean = false; |
| 37 | ball4: boolean = false; |
| 38 | ball5: boolean = false; |
Philipp Schrader | e7c252d | 2022-03-17 21:13:47 -0700 | [diff] [blame] | 39 | quadrant: number = 1; |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 40 | errorMessage: string = ''; |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 41 | noShow: boolean = false; |
| 42 | neverMoved: boolean = false; |
| 43 | batteryDied: boolean = false; |
| 44 | mechanicallyBroke: boolean = false; |
| 45 | lostComs: boolean = false; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 46 | |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 47 | @ViewChild("header") header: ElementRef; |
| 48 | |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 49 | nextSection() { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 50 | if (this.section === 'Team Selection') { |
| 51 | this.section = 'Auto'; |
| 52 | } else if (this.section === 'Auto') { |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 53 | this.section = 'TeleOp'; |
| 54 | } else if (this.section === 'TeleOp') { |
| 55 | this.section = 'Climb'; |
| 56 | } else if (this.section === 'Climb') { |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 57 | this.section = 'Other'; |
| 58 | } else if (this.section === 'Other') { |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 59 | this.section = 'Review and Submit'; |
| 60 | } else if (this.section === 'Review and Submit') { |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 61 | this.submitDataScouting(); |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 62 | return; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 63 | } |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 64 | // Scroll back to the top so that we can be sure the user sees the |
| 65 | // entire next screen. Otherwise it's easy to overlook input fields. |
| 66 | this.scrollToTop(); |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 69 | prevSection() { |
Philipp Schrader | 93ade04 | 2022-03-05 17:16:10 -0800 | [diff] [blame] | 70 | if (this.section === 'Auto') { |
| 71 | this.section = 'Team Selection'; |
| 72 | } else if (this.section === 'TeleOp') { |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 73 | this.section = 'Auto'; |
| 74 | } else if (this.section === 'Climb') { |
| 75 | this.section = 'TeleOp'; |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 76 | } else if (this.section === 'Other') { |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 77 | this.section = 'Climb'; |
| 78 | } else if (this.section === 'Review and Submit') { |
Philipp Schrader | e279e1a | 2022-03-15 22:20:10 -0700 | [diff] [blame] | 79 | this.section = 'Other'; |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 80 | } |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 81 | // Scroll back to the top so that we can be sure the user sees the |
| 82 | // entire previous screen. Otherwise it's easy to overlook input |
| 83 | // fields. |
| 84 | this.scrollToTop(); |
| 85 | } |
| 86 | |
| 87 | private scrollToTop() { |
| 88 | this.header.nativeElement.scrollIntoView(); |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 91 | async submitDataScouting() { |
Philipp Schrader | 5f19001 | 2022-03-15 23:29:09 -0700 | [diff] [blame] | 92 | this.errorMessage = ''; |
| 93 | |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 94 | const builder = new flatbuffer_builder.Builder() as unknown as flatbuffers.Builder; |
| 95 | SubmitDataScouting.startSubmitDataScouting(builder); |
| 96 | SubmitDataScouting.addTeam(builder, this.teamNumber); |
| 97 | SubmitDataScouting.addMatch(builder, this.matchNumber); |
| 98 | SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed); |
| 99 | SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade); |
| 100 | SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade); |
| 101 | SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed); |
| 102 | SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade); |
| 103 | SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade); |
| 104 | SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore); |
Milo Lin | 905e78a | 2022-03-12 15:55:35 -0800 | [diff] [blame] | 105 | SubmitDataScouting.addAutoBall1(builder, this.ball1); |
| 106 | SubmitDataScouting.addAutoBall2(builder, this.ball2); |
| 107 | SubmitDataScouting.addAutoBall3(builder, this.ball3); |
| 108 | SubmitDataScouting.addAutoBall4(builder, this.ball4); |
| 109 | SubmitDataScouting.addAutoBall5(builder, this.ball5); |
Philipp Schrader | e7c252d | 2022-03-17 21:13:47 -0700 | [diff] [blame] | 110 | SubmitDataScouting.addStartingQuadrant(builder, this.quadrant); |
Milo Lin | 905e78a | 2022-03-12 15:55:35 -0800 | [diff] [blame] | 111 | |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 112 | // TODO(phil): Add support for defensePlayedOnScore. |
| 113 | // TODO(phil): Fix the Climbing score. |
| 114 | SubmitDataScouting.addClimbing(builder, 1); |
| 115 | builder.finish(SubmitDataScouting.endSubmitDataScouting(builder)); |
| 116 | |
| 117 | const buffer = builder.asUint8Array(); |
| 118 | const res = await fetch( |
| 119 | '/requests/submit/data_scouting', {method: 'POST', body: buffer}); |
| 120 | |
| 121 | if (res.ok) { |
Philipp Schrader | a3c4e26 | 2022-03-17 09:04:12 -0700 | [diff] [blame] | 122 | // We successfully submitted the data. Report success. |
| 123 | this.section = 'Success'; |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 124 | } else { |
| 125 | const resBuffer = await res.arrayBuffer(); |
| 126 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
| 127 | const parsedResponse = ErrorResponse.getRootAsErrorResponse( |
| 128 | fbBuffer as unknown as flatbuffers.ByteBuffer); |
| 129 | |
| 130 | const errorMessage = parsedResponse.errorMessage(); |
| 131 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
| 132 | } |
| 133 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 134 | } |