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'; |
Philipp Schrader | e5d1394 | 2024-03-17 15:44:35 -0700 | [diff] [blame] | 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 { |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 14 | StartMatchAction, |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 15 | ScoreType, |
| 16 | StageType, |
| 17 | Submit2024Actions, |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 18 | MobilityAction, |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 19 | PenaltyAction, |
| 20 | PickupNoteAction, |
| 21 | PlaceNoteAction, |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 22 | RobotDeathAction, |
| 23 | EndMatchAction, |
| 24 | ActionType, |
| 25 | Action, |
Philipp Schrader | e5d1394 | 2024-03-17 15:44:35 -0700 | [diff] [blame] | 26 | } from '@org_frc971/scouting/webserver/requests/messages/submit_2024_actions_generated'; |
| 27 | import {Match} from '@org_frc971/scouting/webserver/requests/messages/request_all_matches_response_generated'; |
| 28 | import {MatchListRequestor} from '@org_frc971/scouting/www/rpc'; |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 29 | import * as pako from 'pako'; |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 30 | |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 31 | type Section = |
| 32 | | 'Team Selection' |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 33 | | 'Init' |
| 34 | | 'Pickup' |
| 35 | | 'Place' |
| 36 | | 'Endgame' |
| 37 | | 'Dead' |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 38 | | 'Review and Submit' |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 39 | | 'QR Code' |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 40 | | 'Success'; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 41 | |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 42 | // TODO(phil): Deduplicate with match_list.component.ts. |
| 43 | const COMP_LEVELS = ['qm', 'ef', 'qf', 'sf', 'f'] as const; |
Philipp Schrader | ba315da | 2024-03-17 16:16:50 -0700 | [diff] [blame] | 44 | export type CompLevel = typeof COMP_LEVELS[number]; |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 45 | |
| 46 | // TODO(phil): Deduplicate with match_list.component.ts. |
| 47 | const COMP_LEVEL_LABELS: Record<CompLevel, string> = { |
| 48 | qm: 'Qualifications', |
| 49 | ef: 'Eighth Finals', |
| 50 | qf: 'Quarter Finals', |
| 51 | sf: 'Semi Finals', |
| 52 | f: 'Finals', |
| 53 | }; |
| 54 | |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 55 | // The maximum number of bytes per QR code. The user can adjust this value to |
| 56 | // make the QR code contain less information, but easier to scan. |
| 57 | const QR_CODE_PIECE_SIZES = [150, 300, 450, 600, 750, 900]; |
| 58 | |
| 59 | // The default index into QR_CODE_PIECE_SIZES. |
| 60 | const DEFAULT_QR_CODE_PIECE_SIZE_INDEX = QR_CODE_PIECE_SIZES.indexOf(750); |
| 61 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 62 | type ActionT = |
| 63 | | { |
| 64 | type: 'startMatchAction'; |
| 65 | timestamp?: number; |
| 66 | position: number; |
| 67 | } |
| 68 | | { |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 69 | type: 'mobilityAction'; |
| 70 | timestamp?: number; |
| 71 | mobility: boolean; |
| 72 | } |
| 73 | | { |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 74 | type: 'pickupNoteAction'; |
Filip Kujawa | 4413a59 | 2023-03-01 10:54:34 -0800 | [diff] [blame] | 75 | timestamp?: number; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 76 | auto?: boolean; |
| 77 | } |
| 78 | | { |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 79 | type: 'placeNoteAction'; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 80 | timestamp?: number; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 81 | scoreType: ScoreType; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 82 | auto?: boolean; |
| 83 | } |
| 84 | | { |
| 85 | type: 'robotDeathAction'; |
| 86 | timestamp?: number; |
Emily Markova | 040123c | 2024-02-27 09:48:37 -0800 | [diff] [blame] | 87 | robotDead: boolean; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 88 | } |
| 89 | | { |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 90 | type: 'penaltyAction'; |
| 91 | timestamp?: number; |
| 92 | penalties: number; |
| 93 | } |
| 94 | | { |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 95 | type: 'endMatchAction'; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 96 | stageType: StageType; |
| 97 | trapNote: boolean; |
Emily Markova | 6079e2f | 2024-02-17 13:17:24 -0800 | [diff] [blame] | 98 | spotlight: boolean; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 99 | timestamp?: number; |
| 100 | } |
| 101 | | { |
| 102 | // This is not a action that is submitted, |
| 103 | // It is used for undoing purposes. |
| 104 | type: 'endAutoPhase'; |
| 105 | timestamp?: number; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 106 | } |
| 107 | | { |
| 108 | // This is not a action that is submitted, |
| 109 | // It is used for undoing purposes. |
| 110 | type: 'endTeleopPhase'; |
| 111 | timestamp?: number; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 112 | }; |
emilym | 38d08ba | 2022-10-22 15:25:01 -0700 | [diff] [blame] | 113 | |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 114 | @Component({ |
| 115 | selector: 'app-entry', |
| 116 | templateUrl: './entry.ng.html', |
Philipp Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 117 | styleUrls: ['../app/common.css', './entry.component.css'], |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 118 | }) |
Philipp Schrader | 75021f5 | 2023-04-09 21:14:13 -0700 | [diff] [blame] | 119 | export class EntryComponent implements OnInit { |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 120 | // Re-export the type here so that we can use it in the `[value]` attribute |
| 121 | // of radio buttons. |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 122 | readonly COMP_LEVELS = COMP_LEVELS; |
| 123 | readonly COMP_LEVEL_LABELS = COMP_LEVEL_LABELS; |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 124 | readonly QR_CODE_PIECE_SIZES = QR_CODE_PIECE_SIZES; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 125 | readonly ScoreType = ScoreType; |
Emily Markova | 6079e2f | 2024-02-17 13:17:24 -0800 | [diff] [blame] | 126 | readonly StageType = StageType; |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 127 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 128 | section: Section = 'Team Selection'; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 129 | @Input() matchNumber: number = 1; |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 130 | @Input() teamNumber: string = '1'; |
Philipp Schrader | 30b4a68 | 2022-04-16 14:36:17 -0700 | [diff] [blame] | 131 | @Input() setNumber: number = 1; |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 132 | @Input() compLevel: CompLevel = 'qm'; |
Philipp Schrader | 75021f5 | 2023-04-09 21:14:13 -0700 | [diff] [blame] | 133 | @Input() skipTeamSelection = false; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 134 | |
Philipp Schrader | 6319840 | 2024-03-16 14:19:02 -0700 | [diff] [blame] | 135 | @ViewChild('header') header: ElementRef; |
| 136 | |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 137 | matchList: Match[] = []; |
| 138 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 139 | actionList: ActionT[] = []; |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 140 | progressMessage: string = ''; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 141 | errorMessage: string = ''; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 142 | autoPhase: boolean = true; |
Filip Kujawa | b73e94c | 2023-04-19 09:33:14 -0700 | [diff] [blame] | 143 | mobilityCompleted: boolean = false; |
Philipp Schrader | ba315da | 2024-03-17 16:16:50 -0700 | [diff] [blame] | 144 | // TODO(phil): Come up with a better name here. |
Evelyn Yang | c8036b1 | 2023-10-11 21:14:46 -0700 | [diff] [blame] | 145 | selectedValue = 0; |
Philipp Schrader | ba315da | 2024-03-17 16:16:50 -0700 | [diff] [blame] | 146 | endGameAction: StageType = StageType.kMISSING; |
| 147 | noteIsTrapped: boolean = false; |
| 148 | endGameSpotlight: boolean = false; |
| 149 | |
Evelyn Yang | c8036b1 | 2023-10-11 21:14:46 -0700 | [diff] [blame] | 150 | nextTeamNumber = ''; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 151 | |
Philipp Schrader | e149885 | 2023-04-15 18:06:45 -0700 | [diff] [blame] | 152 | preScouting: boolean = false; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 153 | matchStartTimestamp: number = 0; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 154 | penalties: number = 0; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 155 | |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 156 | teamSelectionIsValid = false; |
| 157 | |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 158 | // When the user chooses to generate QR codes, we convert the flatbuffer into |
| 159 | // a long string. Since we frequently have more data than we can display in a |
| 160 | // single QR code, we break the data into multiple QR codes. The data for |
| 161 | // each QR code ("pieces") is stored in the `qrCodeValuePieces` list below. |
| 162 | // The `qrCodeValueIndex` keeps track of which QR code we're currently |
| 163 | // displaying. |
| 164 | qrCodeValuePieceSize = QR_CODE_PIECE_SIZES[DEFAULT_QR_CODE_PIECE_SIZE_INDEX]; |
| 165 | qrCodeValuePieces: string[] = []; |
| 166 | qrCodeValueIndex: number = 0; |
| 167 | |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 168 | constructor(private readonly matchListRequestor: MatchListRequestor) {} |
| 169 | |
Philipp Schrader | 75021f5 | 2023-04-09 21:14:13 -0700 | [diff] [blame] | 170 | ngOnInit() { |
| 171 | // When the user navigated from the match list, we can skip the team |
| 172 | // selection. I.e. we trust that the user clicked the correct button. |
| 173 | this.section = this.skipTeamSelection ? 'Init' : 'Team Selection'; |
Evelyn Yang | c8036b1 | 2023-10-11 21:14:46 -0700 | [diff] [blame] | 174 | this.fetchMatchList(); |
| 175 | } |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 176 | |
Evelyn Yang | c8036b1 | 2023-10-11 21:14:46 -0700 | [diff] [blame] | 177 | goToNextTeam() { |
| 178 | this.ngOnInit(); |
| 179 | this.teamNumber = this.nextTeamNumber; |
| 180 | this.nextTeamNumber = ''; |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | async fetchMatchList() { |
| 184 | this.progressMessage = 'Fetching match list. Please be patient.'; |
| 185 | this.errorMessage = ''; |
| 186 | |
| 187 | try { |
| 188 | this.matchList = await this.matchListRequestor.fetchMatchList(); |
| 189 | this.progressMessage = 'Successfully fetched match list.'; |
| 190 | } catch (e) { |
| 191 | this.errorMessage = e; |
| 192 | this.progressMessage = ''; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // This gets called when the user changes something on the Init screen. |
| 197 | // It makes sure that the user can't click "Next" until the information is |
Philipp Schrader | e149885 | 2023-04-15 18:06:45 -0700 | [diff] [blame] | 198 | // valid, or this is for pre-scouting. |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 199 | updateTeamSelectionValidity(): void { |
Philipp Schrader | e149885 | 2023-04-15 18:06:45 -0700 | [diff] [blame] | 200 | this.teamSelectionIsValid = this.preScouting || this.matchIsInMatchList(); |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | matchIsInMatchList(): boolean { |
| 204 | // If the user deletes the content of the teamNumber field, the value here |
| 205 | // is undefined. Guard against that. |
| 206 | if (this.teamNumber == null) { |
| 207 | return false; |
| 208 | } |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 209 | |
| 210 | for (const match of this.matchList) { |
| 211 | if ( |
| 212 | this.matchNumber == match.matchNumber() && |
| 213 | this.setNumber == match.setNumber() && |
| 214 | this.compLevel == match.compLevel() && |
Evelyn Yang | c8036b1 | 2023-10-11 21:14:46 -0700 | [diff] [blame] | 215 | (this.teamNumber === match.r1() || |
| 216 | this.teamNumber === match.r2() || |
| 217 | this.teamNumber === match.r3() || |
| 218 | this.teamNumber === match.b1() || |
| 219 | this.teamNumber === match.b2() || |
| 220 | this.teamNumber === match.b3()) |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 221 | ) { |
| 222 | return true; |
| 223 | } |
| 224 | } |
| 225 | return false; |
Philipp Schrader | 75021f5 | 2023-04-09 21:14:13 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 228 | addPenalty(): void { |
| 229 | this.penalties += 1; |
| 230 | } |
| 231 | |
| 232 | removePenalty(): void { |
| 233 | if (this.penalties > 0) { |
| 234 | this.penalties -= 1; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | addPenalties(): void { |
| 239 | this.addAction({type: 'penaltyAction', penalties: this.penalties}); |
| 240 | } |
| 241 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 242 | addAction(action: ActionT): void { |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 243 | if (action.type == 'startMatchAction') { |
| 244 | // Unix nanosecond timestamp. |
| 245 | this.matchStartTimestamp = Date.now() * 1e6; |
| 246 | action.timestamp = 0; |
| 247 | } else { |
| 248 | // Unix nanosecond timestamp relative to match start. |
| 249 | action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp; |
| 250 | } |
| 251 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 252 | if (action.type == 'endMatchAction') { |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 253 | // endMatchAction occurs at the same time as penaltyAction so add to its |
| 254 | // timestamp to make it unique. |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 255 | action.timestamp += 1; |
| 256 | } |
| 257 | |
Filip Kujawa | b73e94c | 2023-04-19 09:33:14 -0700 | [diff] [blame] | 258 | if (action.type == 'mobilityAction') { |
| 259 | this.mobilityCompleted = true; |
| 260 | } |
| 261 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 262 | if (action.type == 'pickupNoteAction' || action.type == 'placeNoteAction') { |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 263 | action.auto = this.autoPhase; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 264 | } |
| 265 | this.actionList.push(action); |
| 266 | } |
| 267 | |
| 268 | undoLastAction() { |
| 269 | if (this.actionList.length > 0) { |
| 270 | let lastAction = this.actionList.pop(); |
| 271 | switch (lastAction?.type) { |
| 272 | case 'endAutoPhase': |
| 273 | this.autoPhase = true; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 274 | this.section = 'Pickup'; |
| 275 | case 'pickupNoteAction': |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 276 | this.section = 'Pickup'; |
| 277 | break; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 278 | case 'endTeleopPhase': |
| 279 | this.section = 'Pickup'; |
| 280 | break; |
| 281 | case 'placeNoteAction': |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 282 | this.section = 'Place'; |
| 283 | break; |
| 284 | case 'endMatchAction': |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 285 | this.section = 'Endgame'; |
| 286 | case 'mobilityAction': |
| 287 | this.mobilityCompleted = false; |
| 288 | break; |
| 289 | case 'startMatchAction': |
| 290 | this.section = 'Init'; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 291 | break; |
Filip Kujawa | 9f56d0e | 2023-03-03 19:44:43 -0800 | [diff] [blame] | 292 | case 'robotDeathAction': |
| 293 | // TODO(FILIP): Return user to the screen they |
| 294 | // clicked dead robot on. Pickup is fine for now but |
| 295 | // might cause confusion. |
| 296 | this.section = 'Pickup'; |
| 297 | break; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 298 | default: |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 304 | stringifyScoreType(scoreType: ScoreType): String { |
| 305 | return ScoreType[scoreType]; |
Emily Markova | f4b06a2 | 2023-05-10 17:44:09 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 308 | stringifyStageType(stageType: StageType): String { |
| 309 | return StageType[stageType]; |
Emily Markova | f4b06a2 | 2023-05-10 17:44:09 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 312 | changeSectionTo(target: Section) { |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 313 | // Clear the messages since they won't be relevant in the next section. |
| 314 | this.errorMessage = ''; |
| 315 | this.progressMessage = ''; |
| 316 | |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 317 | // For the QR code screen, we need to make the value to encode available. |
| 318 | if (target == 'QR Code') { |
| 319 | this.updateQrCodeValuePieceSize(); |
| 320 | } |
| 321 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 322 | this.section = target; |
| 323 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 324 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 325 | private scrollToTop() { |
| 326 | this.header.nativeElement.scrollIntoView(); |
| 327 | } |
| 328 | |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 329 | createActionsBuffer() { |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 330 | const builder = new Builder(); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 331 | const actionOffsets: number[] = []; |
| 332 | |
| 333 | for (const action of this.actionList) { |
| 334 | let actionOffset: number | undefined; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 335 | |
| 336 | switch (action.type) { |
| 337 | case 'startMatchAction': |
| 338 | const startMatchActionOffset = |
| 339 | StartMatchAction.createStartMatchAction(builder, action.position); |
| 340 | actionOffset = Action.createAction( |
| 341 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 342 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 343 | ActionType.StartMatchAction, |
| 344 | startMatchActionOffset |
| 345 | ); |
| 346 | break; |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 347 | case 'mobilityAction': |
| 348 | const mobilityActionOffset = MobilityAction.createMobilityAction( |
| 349 | builder, |
| 350 | action.mobility |
| 351 | ); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 352 | actionOffset = Action.createAction( |
| 353 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 354 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 355 | ActionType.MobilityAction, |
| 356 | mobilityActionOffset |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 357 | ); |
| 358 | break; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 359 | case 'penaltyAction': |
| 360 | const penaltyActionOffset = PenaltyAction.createPenaltyAction( |
| 361 | builder, |
| 362 | action.penalties |
| 363 | ); |
Filip Kujawa | 4c28644 | 2023-03-03 10:41:22 -0800 | [diff] [blame] | 364 | actionOffset = Action.createAction( |
| 365 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 366 | BigInt(action.timestamp || 0), |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 367 | ActionType.PenaltyAction, |
| 368 | penaltyActionOffset |
Filip Kujawa | 4c28644 | 2023-03-03 10:41:22 -0800 | [diff] [blame] | 369 | ); |
| 370 | break; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 371 | case 'pickupNoteAction': |
| 372 | const pickupNoteActionOffset = |
| 373 | PickupNoteAction.createPickupNoteAction( |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 374 | builder, |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 375 | action.auto || false |
| 376 | ); |
| 377 | actionOffset = Action.createAction( |
| 378 | builder, |
| 379 | BigInt(action.timestamp || 0), |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 380 | ActionType.PickupNoteAction, |
| 381 | pickupNoteActionOffset |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 382 | ); |
| 383 | break; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 384 | case 'placeNoteAction': |
| 385 | const placeNoteActionOffset = PlaceNoteAction.createPlaceNoteAction( |
| 386 | builder, |
| 387 | action.scoreType, |
| 388 | action.auto || false |
| 389 | ); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 390 | actionOffset = Action.createAction( |
| 391 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 392 | BigInt(action.timestamp || 0), |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 393 | ActionType.PlaceNoteAction, |
| 394 | placeNoteActionOffset |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 395 | ); |
| 396 | break; |
| 397 | |
| 398 | case 'robotDeathAction': |
| 399 | const robotDeathActionOffset = |
Emily Markova | 040123c | 2024-02-27 09:48:37 -0800 | [diff] [blame] | 400 | RobotDeathAction.createRobotDeathAction(builder, action.robotDead); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 401 | actionOffset = Action.createAction( |
| 402 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 403 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 404 | ActionType.RobotDeathAction, |
| 405 | robotDeathActionOffset |
| 406 | ); |
| 407 | break; |
| 408 | |
| 409 | case 'endMatchAction': |
| 410 | const endMatchActionOffset = EndMatchAction.createEndMatchAction( |
| 411 | builder, |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 412 | action.stageType, |
Emily Markova | 6079e2f | 2024-02-17 13:17:24 -0800 | [diff] [blame] | 413 | action.trapNote, |
| 414 | action.spotlight |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 415 | ); |
| 416 | actionOffset = Action.createAction( |
| 417 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 418 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 419 | ActionType.EndMatchAction, |
| 420 | endMatchActionOffset |
| 421 | ); |
| 422 | break; |
| 423 | |
| 424 | case 'endAutoPhase': |
| 425 | // Not important action. |
| 426 | break; |
| 427 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 428 | case 'endTeleopPhase': |
| 429 | // Not important action. |
| 430 | break; |
| 431 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 432 | default: |
| 433 | throw new Error(`Unknown action type`); |
| 434 | } |
| 435 | |
| 436 | if (actionOffset !== undefined) { |
| 437 | actionOffsets.push(actionOffset); |
| 438 | } |
| 439 | } |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 440 | const teamNumberFb = builder.createString(this.teamNumber); |
Philipp Schrader | e859e6e | 2023-03-22 19:59:51 -0700 | [diff] [blame] | 441 | const compLevelFb = builder.createString(this.compLevel); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 442 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 443 | const actionsVector = Submit2024Actions.createActionsListVector( |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 444 | builder, |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 445 | actionOffsets |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 446 | ); |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 447 | Submit2024Actions.startSubmit2024Actions(builder); |
| 448 | Submit2024Actions.addTeamNumber(builder, teamNumberFb); |
| 449 | Submit2024Actions.addMatchNumber(builder, this.matchNumber); |
| 450 | Submit2024Actions.addSetNumber(builder, this.setNumber); |
| 451 | Submit2024Actions.addCompLevel(builder, compLevelFb); |
| 452 | Submit2024Actions.addActionsList(builder, actionsVector); |
| 453 | Submit2024Actions.addPreScouting(builder, this.preScouting); |
| 454 | builder.finish(Submit2024Actions.endSubmit2024Actions(builder)); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 455 | |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 456 | return builder.asUint8Array(); |
| 457 | } |
| 458 | |
| 459 | // Same as createActionsBuffer, but encoded as Base64. It's also split into |
| 460 | // a number of pieces so that each piece is roughly limited to |
| 461 | // `qrCodeValuePieceSize` bytes. |
| 462 | createBase64ActionsBuffers(): string[] { |
| 463 | const originalBuffer = this.createActionsBuffer(); |
| 464 | const deflatedData = pako.deflate(originalBuffer, {level: 9}); |
| 465 | |
| 466 | const pieceSize = this.qrCodeValuePieceSize; |
| 467 | const fullValue = btoa(String.fromCharCode(...deflatedData)); |
| 468 | const numPieces = Math.ceil(fullValue.length / pieceSize); |
| 469 | |
| 470 | let splitData: string[] = []; |
| 471 | for (let i = 0; i < numPieces; i++) { |
| 472 | const splitPiece = fullValue.slice(i * pieceSize, (i + 1) * pieceSize); |
| 473 | splitData.push(`${i}_${numPieces}_${pieceSize}_${splitPiece}`); |
| 474 | } |
| 475 | return splitData; |
| 476 | } |
| 477 | |
| 478 | setQrCodeValueIndex(index: number) { |
| 479 | this.qrCodeValueIndex = Math.max( |
| 480 | 0, |
| 481 | Math.min(index, this.qrCodeValuePieces.length - 1) |
| 482 | ); |
| 483 | } |
| 484 | |
| 485 | updateQrCodeValuePieceSize() { |
| 486 | this.qrCodeValuePieces = this.createBase64ActionsBuffers(); |
| 487 | this.qrCodeValueIndex = 0; |
| 488 | } |
| 489 | |
| 490 | async submit2024Actions() { |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 491 | const res = await fetch('/requests/submit/submit_2024_actions', { |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 492 | method: 'POST', |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame] | 493 | body: this.createActionsBuffer(), |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 494 | }); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 495 | |
| 496 | if (res.ok) { |
| 497 | // We successfully submitted the data. Report success. |
| 498 | this.section = 'Success'; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 499 | this.actionList = []; |
Evelyn Yang | c8036b1 | 2023-10-11 21:14:46 -0700 | [diff] [blame] | 500 | |
Philipp Schrader | 6319840 | 2024-03-16 14:19:02 -0700 | [diff] [blame] | 501 | // Keep track of the position of the last robot, use to figure out what |
| 502 | // the next robot in the same position is. |
Evelyn Yang | c8036b1 | 2023-10-11 21:14:46 -0700 | [diff] [blame] | 503 | let lastTeamPos = '0'; |
| 504 | for (const match of this.matchList) { |
| 505 | if ( |
| 506 | this.matchNumber === match.matchNumber() && |
| 507 | this.setNumber === match.setNumber() && |
| 508 | this.compLevel === match.compLevel() |
| 509 | ) { |
| 510 | this.teamNumber = this.teamNumber; |
| 511 | if (this.teamNumber == match.r1()) { |
| 512 | lastTeamPos = 'r1'; |
| 513 | } else if (this.teamNumber == match.r2()) { |
| 514 | lastTeamPos = 'r2'; |
| 515 | } else if (this.teamNumber == match.r3()) { |
| 516 | lastTeamPos = 'r3'; |
| 517 | } else if (this.teamNumber == match.b1()) { |
| 518 | lastTeamPos = 'b1'; |
| 519 | } else if (this.teamNumber == match.b2()) { |
| 520 | lastTeamPos = 'b2'; |
| 521 | } else if (this.teamNumber == match.b3()) { |
| 522 | lastTeamPos = 'b3'; |
| 523 | } else { |
| 524 | console.log('Position of scouted team not found.'); |
| 525 | } |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | if (lastTeamPos != '0') { |
| 530 | this.matchNumber += 1; |
| 531 | for (const match of this.matchList) { |
| 532 | if ( |
| 533 | this.matchNumber == match.matchNumber() && |
| 534 | this.setNumber == match.setNumber() && |
| 535 | this.compLevel == match.compLevel() |
| 536 | ) { |
| 537 | if (lastTeamPos == 'r1') { |
| 538 | this.nextTeamNumber = match.r1(); |
| 539 | } else if (lastTeamPos == 'r2') { |
| 540 | this.nextTeamNumber = match.r2(); |
| 541 | } else if (lastTeamPos == 'r3') { |
| 542 | this.nextTeamNumber = match.r3(); |
| 543 | } else if (lastTeamPos == 'b1') { |
| 544 | this.nextTeamNumber = match.b1(); |
| 545 | } else if (lastTeamPos == 'b2') { |
| 546 | this.nextTeamNumber = match.b2(); |
| 547 | } else if (lastTeamPos == 'b3') { |
| 548 | this.nextTeamNumber = match.b3(); |
| 549 | } else { |
| 550 | console.log('Position of last team not found.'); |
| 551 | } |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | } else { |
| 556 | console.log('Last team position not found.'); |
| 557 | } |
| 558 | this.matchList = []; |
| 559 | this.progressMessage = ''; |
| 560 | this.errorMessage = ''; |
| 561 | this.autoPhase = true; |
| 562 | this.actionList = []; |
| 563 | this.mobilityCompleted = false; |
| 564 | this.preScouting = false; |
| 565 | this.matchStartTimestamp = 0; |
| 566 | this.selectedValue = 0; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 567 | } else { |
| 568 | const resBuffer = await res.arrayBuffer(); |
| 569 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 570 | const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 571 | |
| 572 | const errorMessage = parsedResponse.errorMessage(); |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 573 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 574 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 575 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 576 | } |