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