Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 1 | import { |
| 2 | Component, |
| 3 | ElementRef, |
| 4 | EventEmitter, |
| 5 | Input, |
| 6 | OnInit, |
| 7 | Output, |
| 8 | ViewChild, |
| 9 | } from '@angular/core'; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 10 | import {FormsModule} from '@angular/forms'; |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 11 | import {Builder, ByteBuffer} from 'flatbuffers'; |
| 12 | import {ErrorResponse} from 'org_frc971/scouting/webserver/requests/messages/error_response_generated'; |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 13 | import { |
| 14 | ClimbLevel, |
| 15 | SubmitDataScouting, |
| 16 | } from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 17 | import {SubmitDataScoutingResponse} from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated'; |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 18 | |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 19 | type Section = |
| 20 | | 'Team Selection' |
| 21 | | 'Auto' |
| 22 | | 'TeleOp' |
| 23 | | 'Climb' |
| 24 | | 'Other' |
| 25 | | 'Review and Submit' |
| 26 | | 'Success'; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 27 | |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 28 | // TODO(phil): Deduplicate with match_list.component.ts. |
| 29 | const COMP_LEVELS = ['qm', 'ef', 'qf', 'sf', 'f'] as const; |
| 30 | type CompLevel = typeof COMP_LEVELS[number]; |
| 31 | |
| 32 | // TODO(phil): Deduplicate with match_list.component.ts. |
| 33 | const COMP_LEVEL_LABELS: Record<CompLevel, string> = { |
| 34 | qm: 'Qualifications', |
| 35 | ef: 'Eighth Finals', |
| 36 | qf: 'Quarter Finals', |
| 37 | sf: 'Semi Finals', |
| 38 | f: 'Finals', |
| 39 | }; |
| 40 | |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 41 | @Component({ |
| 42 | selector: 'app-entry', |
| 43 | templateUrl: './entry.ng.html', |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 44 | styleUrls: ['../common.css', './entry.component.css'], |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 45 | }) |
| 46 | export class EntryComponent { |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 47 | // Re-export the type here so that we can use it in the `[value]` attribute |
| 48 | // of radio buttons. |
| 49 | readonly ClimbLevel = ClimbLevel; |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 50 | readonly COMP_LEVELS = COMP_LEVELS; |
| 51 | readonly COMP_LEVEL_LABELS = COMP_LEVEL_LABELS; |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 52 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 53 | section: Section = 'Team Selection'; |
| 54 | @Output() switchTabsEvent = new EventEmitter<string>(); |
| 55 | @Input() matchNumber: number = 1; |
| 56 | @Input() teamNumber: number = 1; |
Philipp Schrader | 30b4a68 | 2022-04-16 14:36:17 -0700 | [diff] [blame^] | 57 | @Input() setNumber: number = 1; |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 58 | @Input() compLevel: CompLevel = 'qm'; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 59 | autoUpperShotsMade: number = 0; |
| 60 | autoLowerShotsMade: number = 0; |
| 61 | autoShotsMissed: number = 0; |
| 62 | teleUpperShotsMade: number = 0; |
| 63 | teleLowerShotsMade: number = 0; |
| 64 | teleShotsMissed: number = 0; |
| 65 | defensePlayedOnScore: number = 0; |
| 66 | defensePlayedScore: number = 0; |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 67 | level: ClimbLevel = ClimbLevel.NoAttempt; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 68 | ball1: boolean = false; |
| 69 | ball2: boolean = false; |
| 70 | ball3: boolean = false; |
| 71 | ball4: boolean = false; |
| 72 | ball5: boolean = false; |
| 73 | quadrant: number = 1; |
| 74 | errorMessage: string = ''; |
| 75 | noShow: boolean = false; |
| 76 | neverMoved: boolean = false; |
| 77 | batteryDied: boolean = false; |
| 78 | mechanicallyBroke: boolean = false; |
| 79 | lostComs: boolean = false; |
Philipp Schrader | 9bcbc33 | 2022-03-18 19:06:04 -0700 | [diff] [blame] | 80 | comment: string = ''; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 81 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 82 | @ViewChild('header') header: ElementRef; |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 83 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 84 | nextSection() { |
| 85 | if (this.section === 'Team Selection') { |
| 86 | this.section = 'Auto'; |
| 87 | } else if (this.section === 'Auto') { |
| 88 | this.section = 'TeleOp'; |
| 89 | } else if (this.section === 'TeleOp') { |
| 90 | this.section = 'Climb'; |
| 91 | } else if (this.section === 'Climb') { |
| 92 | this.section = 'Other'; |
| 93 | } else if (this.section === 'Other') { |
| 94 | this.section = 'Review and Submit'; |
| 95 | } else if (this.section === 'Review and Submit') { |
| 96 | this.submitDataScouting(); |
| 97 | return; |
| 98 | } else if (this.section === 'Success') { |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 99 | this.switchTabsEvent.emit('MatchList'); |
| 100 | return; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 101 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 102 | // Scroll back to the top so that we can be sure the user sees the |
| 103 | // entire next screen. Otherwise it's easy to overlook input fields. |
| 104 | this.scrollToTop(); |
| 105 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 106 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 107 | prevSection() { |
| 108 | if (this.section === 'Auto') { |
| 109 | this.section = 'Team Selection'; |
| 110 | } else if (this.section === 'TeleOp') { |
| 111 | this.section = 'Auto'; |
| 112 | } else if (this.section === 'Climb') { |
| 113 | this.section = 'TeleOp'; |
| 114 | } else if (this.section === 'Other') { |
| 115 | this.section = 'Climb'; |
| 116 | } else if (this.section === 'Review and Submit') { |
| 117 | this.section = 'Other'; |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 118 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 119 | // Scroll back to the top so that we can be sure the user sees the |
| 120 | // entire previous screen. Otherwise it's easy to overlook input |
| 121 | // fields. |
| 122 | this.scrollToTop(); |
| 123 | } |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 124 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 125 | private scrollToTop() { |
| 126 | this.header.nativeElement.scrollIntoView(); |
| 127 | } |
| 128 | |
| 129 | async submitDataScouting() { |
| 130 | this.errorMessage = ''; |
| 131 | |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 132 | const builder = new Builder(); |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 133 | const compLevel = builder.createString(this.compLevel); |
Philipp Schrader | 70569f7 | 2022-03-18 19:15:05 -0700 | [diff] [blame] | 134 | const comment = builder.createString(this.comment); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 135 | SubmitDataScouting.startSubmitDataScouting(builder); |
| 136 | SubmitDataScouting.addTeam(builder, this.teamNumber); |
| 137 | SubmitDataScouting.addMatch(builder, this.matchNumber); |
Philipp Schrader | 30b4a68 | 2022-04-16 14:36:17 -0700 | [diff] [blame^] | 138 | SubmitDataScouting.addSetNumber(builder, this.setNumber); |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 139 | SubmitDataScouting.addCompLevel(builder, compLevel); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 140 | SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed); |
| 141 | SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade); |
| 142 | SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade); |
| 143 | SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed); |
| 144 | SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade); |
| 145 | SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade); |
| 146 | SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore); |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 147 | SubmitDataScouting.addDefenseReceivedRating( |
| 148 | builder, |
| 149 | this.defensePlayedOnScore |
| 150 | ); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 151 | SubmitDataScouting.addAutoBall1(builder, this.ball1); |
| 152 | SubmitDataScouting.addAutoBall2(builder, this.ball2); |
| 153 | SubmitDataScouting.addAutoBall3(builder, this.ball3); |
| 154 | SubmitDataScouting.addAutoBall4(builder, this.ball4); |
| 155 | SubmitDataScouting.addAutoBall5(builder, this.ball5); |
| 156 | SubmitDataScouting.addStartingQuadrant(builder, this.quadrant); |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 157 | SubmitDataScouting.addClimbLevel(builder, this.level); |
Philipp Schrader | 70569f7 | 2022-03-18 19:15:05 -0700 | [diff] [blame] | 158 | SubmitDataScouting.addComment(builder, comment); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 159 | builder.finish(SubmitDataScouting.endSubmitDataScouting(builder)); |
| 160 | |
| 161 | const buffer = builder.asUint8Array(); |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 162 | const res = await fetch('/requests/submit/data_scouting', { |
| 163 | method: 'POST', |
| 164 | body: buffer, |
| 165 | }); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 166 | |
| 167 | if (res.ok) { |
| 168 | // We successfully submitted the data. Report success. |
| 169 | this.section = 'Success'; |
| 170 | } else { |
| 171 | const resBuffer = await res.arrayBuffer(); |
| 172 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 173 | const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 174 | |
| 175 | const errorMessage = parsedResponse.errorMessage(); |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 176 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 177 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 178 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 179 | } |