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 | d7efa2b | 2023-02-17 21:15:13 -0800 | [diff] [blame] | 12 | import {ErrorResponse} from '../../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, |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 26 | } from '../../webserver/requests/messages/submit_2024_actions_generated'; |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 27 | import {Match} from '../../webserver/requests/messages/request_all_matches_response_generated'; |
Philipp Schrader | ba072d9 | 2024-02-21 17:00:37 -0800 | [diff] [blame] | 28 | import {MatchListRequestor} from '../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; |
| 44 | type CompLevel = typeof COMP_LEVELS[number]; |
| 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 | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 135 | matchList: Match[] = []; |
| 136 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 137 | actionList: ActionT[] = []; |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 138 | progressMessage: string = ''; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 139 | errorMessage: string = ''; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 140 | autoPhase: boolean = true; |
Filip Kujawa | b73e94c | 2023-04-19 09:33:14 -0700 | [diff] [blame] | 141 | mobilityCompleted: boolean = false; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 142 | |
Philipp Schrader | e149885 | 2023-04-15 18:06:45 -0700 | [diff] [blame] | 143 | preScouting: boolean = false; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 144 | matchStartTimestamp: number = 0; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 145 | penalties: number = 0; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 146 | |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 147 | teamSelectionIsValid = false; |
| 148 | |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame^] | 149 | // When the user chooses to generate QR codes, we convert the flatbuffer into |
| 150 | // a long string. Since we frequently have more data than we can display in a |
| 151 | // single QR code, we break the data into multiple QR codes. The data for |
| 152 | // each QR code ("pieces") is stored in the `qrCodeValuePieces` list below. |
| 153 | // The `qrCodeValueIndex` keeps track of which QR code we're currently |
| 154 | // displaying. |
| 155 | qrCodeValuePieceSize = QR_CODE_PIECE_SIZES[DEFAULT_QR_CODE_PIECE_SIZE_INDEX]; |
| 156 | qrCodeValuePieces: string[] = []; |
| 157 | qrCodeValueIndex: number = 0; |
| 158 | |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 159 | constructor(private readonly matchListRequestor: MatchListRequestor) {} |
| 160 | |
Philipp Schrader | 75021f5 | 2023-04-09 21:14:13 -0700 | [diff] [blame] | 161 | ngOnInit() { |
| 162 | // When the user navigated from the match list, we can skip the team |
| 163 | // selection. I.e. we trust that the user clicked the correct button. |
| 164 | this.section = this.skipTeamSelection ? 'Init' : 'Team Selection'; |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 165 | |
| 166 | if (this.section == 'Team Selection') { |
| 167 | this.fetchMatchList(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | async fetchMatchList() { |
| 172 | this.progressMessage = 'Fetching match list. Please be patient.'; |
| 173 | this.errorMessage = ''; |
| 174 | |
| 175 | try { |
| 176 | this.matchList = await this.matchListRequestor.fetchMatchList(); |
| 177 | this.progressMessage = 'Successfully fetched match list.'; |
| 178 | } catch (e) { |
| 179 | this.errorMessage = e; |
| 180 | this.progressMessage = ''; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // This gets called when the user changes something on the Init screen. |
| 185 | // 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] | 186 | // valid, or this is for pre-scouting. |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 187 | updateTeamSelectionValidity(): void { |
Philipp Schrader | e149885 | 2023-04-15 18:06:45 -0700 | [diff] [blame] | 188 | this.teamSelectionIsValid = this.preScouting || this.matchIsInMatchList(); |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | matchIsInMatchList(): boolean { |
| 192 | // If the user deletes the content of the teamNumber field, the value here |
| 193 | // is undefined. Guard against that. |
| 194 | if (this.teamNumber == null) { |
| 195 | return false; |
| 196 | } |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 197 | const teamNumber = this.teamNumber; |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 198 | |
| 199 | for (const match of this.matchList) { |
| 200 | if ( |
| 201 | this.matchNumber == match.matchNumber() && |
| 202 | this.setNumber == match.setNumber() && |
| 203 | this.compLevel == match.compLevel() && |
| 204 | (teamNumber === match.r1() || |
| 205 | teamNumber === match.r2() || |
| 206 | teamNumber === match.r3() || |
| 207 | teamNumber === match.b1() || |
| 208 | teamNumber === match.b2() || |
| 209 | teamNumber === match.b3()) |
| 210 | ) { |
| 211 | return true; |
| 212 | } |
| 213 | } |
| 214 | return false; |
Philipp Schrader | 75021f5 | 2023-04-09 21:14:13 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 217 | addPenalty(): void { |
| 218 | this.penalties += 1; |
| 219 | } |
| 220 | |
| 221 | removePenalty(): void { |
| 222 | if (this.penalties > 0) { |
| 223 | this.penalties -= 1; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | addPenalties(): void { |
| 228 | this.addAction({type: 'penaltyAction', penalties: this.penalties}); |
| 229 | } |
| 230 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 231 | addAction(action: ActionT): void { |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 232 | if (action.type == 'startMatchAction') { |
| 233 | // Unix nanosecond timestamp. |
| 234 | this.matchStartTimestamp = Date.now() * 1e6; |
| 235 | action.timestamp = 0; |
| 236 | } else { |
| 237 | // Unix nanosecond timestamp relative to match start. |
| 238 | action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp; |
| 239 | } |
| 240 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 241 | if (action.type == 'endMatchAction') { |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame^] | 242 | // endMatchAction occurs at the same time as penaltyAction so add to its |
| 243 | // timestamp to make it unique. |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 244 | action.timestamp += 1; |
| 245 | } |
| 246 | |
Filip Kujawa | b73e94c | 2023-04-19 09:33:14 -0700 | [diff] [blame] | 247 | if (action.type == 'mobilityAction') { |
| 248 | this.mobilityCompleted = true; |
| 249 | } |
| 250 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 251 | if (action.type == 'pickupNoteAction' || action.type == 'placeNoteAction') { |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 252 | action.auto = this.autoPhase; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 253 | } |
| 254 | this.actionList.push(action); |
| 255 | } |
| 256 | |
| 257 | undoLastAction() { |
| 258 | if (this.actionList.length > 0) { |
| 259 | let lastAction = this.actionList.pop(); |
| 260 | switch (lastAction?.type) { |
| 261 | case 'endAutoPhase': |
| 262 | this.autoPhase = true; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 263 | this.section = 'Pickup'; |
| 264 | case 'pickupNoteAction': |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 265 | this.section = 'Pickup'; |
| 266 | break; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 267 | case 'endTeleopPhase': |
| 268 | this.section = 'Pickup'; |
| 269 | break; |
| 270 | case 'placeNoteAction': |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 271 | this.section = 'Place'; |
| 272 | break; |
| 273 | case 'endMatchAction': |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 274 | this.section = 'Endgame'; |
| 275 | case 'mobilityAction': |
| 276 | this.mobilityCompleted = false; |
| 277 | break; |
| 278 | case 'startMatchAction': |
| 279 | this.section = 'Init'; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 280 | break; |
Filip Kujawa | 9f56d0e | 2023-03-03 19:44:43 -0800 | [diff] [blame] | 281 | case 'robotDeathAction': |
| 282 | // TODO(FILIP): Return user to the screen they |
| 283 | // clicked dead robot on. Pickup is fine for now but |
| 284 | // might cause confusion. |
| 285 | this.section = 'Pickup'; |
| 286 | break; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 287 | default: |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 293 | stringifyScoreType(scoreType: ScoreType): String { |
| 294 | return ScoreType[scoreType]; |
Emily Markova | f4b06a2 | 2023-05-10 17:44:09 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 297 | stringifyStageType(stageType: StageType): String { |
| 298 | return StageType[stageType]; |
Emily Markova | f4b06a2 | 2023-05-10 17:44:09 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 301 | changeSectionTo(target: Section) { |
Philipp Schrader | 8702b78 | 2023-04-15 17:33:37 -0700 | [diff] [blame] | 302 | // Clear the messages since they won't be relevant in the next section. |
| 303 | this.errorMessage = ''; |
| 304 | this.progressMessage = ''; |
| 305 | |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame^] | 306 | // For the QR code screen, we need to make the value to encode available. |
| 307 | if (target == 'QR Code') { |
| 308 | this.updateQrCodeValuePieceSize(); |
| 309 | } |
| 310 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 311 | this.section = target; |
| 312 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 313 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 314 | @ViewChild('header') header: ElementRef; |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 315 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 316 | private scrollToTop() { |
| 317 | this.header.nativeElement.scrollIntoView(); |
| 318 | } |
| 319 | |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame^] | 320 | createActionsBuffer() { |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 321 | const builder = new Builder(); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 322 | const actionOffsets: number[] = []; |
| 323 | |
| 324 | for (const action of this.actionList) { |
| 325 | let actionOffset: number | undefined; |
| 326 | console.log(action.type); |
| 327 | |
| 328 | switch (action.type) { |
| 329 | case 'startMatchAction': |
| 330 | const startMatchActionOffset = |
| 331 | StartMatchAction.createStartMatchAction(builder, action.position); |
| 332 | actionOffset = Action.createAction( |
| 333 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 334 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 335 | ActionType.StartMatchAction, |
| 336 | startMatchActionOffset |
| 337 | ); |
| 338 | break; |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 339 | case 'mobilityAction': |
| 340 | const mobilityActionOffset = MobilityAction.createMobilityAction( |
| 341 | builder, |
| 342 | action.mobility |
| 343 | ); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 344 | actionOffset = Action.createAction( |
| 345 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 346 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 347 | ActionType.MobilityAction, |
| 348 | mobilityActionOffset |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 349 | ); |
| 350 | break; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 351 | case 'penaltyAction': |
| 352 | const penaltyActionOffset = PenaltyAction.createPenaltyAction( |
| 353 | builder, |
| 354 | action.penalties |
| 355 | ); |
Filip Kujawa | 4c28644 | 2023-03-03 10:41:22 -0800 | [diff] [blame] | 356 | actionOffset = Action.createAction( |
| 357 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 358 | BigInt(action.timestamp || 0), |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 359 | ActionType.PenaltyAction, |
| 360 | penaltyActionOffset |
Filip Kujawa | 4c28644 | 2023-03-03 10:41:22 -0800 | [diff] [blame] | 361 | ); |
| 362 | break; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 363 | case 'pickupNoteAction': |
| 364 | const pickupNoteActionOffset = |
| 365 | PickupNoteAction.createPickupNoteAction( |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 366 | builder, |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 367 | action.auto || false |
| 368 | ); |
| 369 | actionOffset = Action.createAction( |
| 370 | builder, |
| 371 | BigInt(action.timestamp || 0), |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 372 | ActionType.PickupNoteAction, |
| 373 | pickupNoteActionOffset |
Filip Kujawa | 0b4b1e5 | 2023-04-15 14:05:40 -0700 | [diff] [blame] | 374 | ); |
| 375 | break; |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 376 | case 'placeNoteAction': |
| 377 | const placeNoteActionOffset = PlaceNoteAction.createPlaceNoteAction( |
| 378 | builder, |
| 379 | action.scoreType, |
| 380 | action.auto || false |
| 381 | ); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 382 | actionOffset = Action.createAction( |
| 383 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 384 | BigInt(action.timestamp || 0), |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 385 | ActionType.PlaceNoteAction, |
| 386 | placeNoteActionOffset |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 387 | ); |
| 388 | break; |
| 389 | |
| 390 | case 'robotDeathAction': |
| 391 | const robotDeathActionOffset = |
Emily Markova | 040123c | 2024-02-27 09:48:37 -0800 | [diff] [blame] | 392 | RobotDeathAction.createRobotDeathAction(builder, action.robotDead); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 393 | actionOffset = Action.createAction( |
| 394 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 395 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 396 | ActionType.RobotDeathAction, |
| 397 | robotDeathActionOffset |
| 398 | ); |
| 399 | break; |
| 400 | |
| 401 | case 'endMatchAction': |
| 402 | const endMatchActionOffset = EndMatchAction.createEndMatchAction( |
| 403 | builder, |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 404 | action.stageType, |
Emily Markova | 6079e2f | 2024-02-17 13:17:24 -0800 | [diff] [blame] | 405 | action.trapNote, |
| 406 | action.spotlight |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 407 | ); |
| 408 | actionOffset = Action.createAction( |
| 409 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 410 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 411 | ActionType.EndMatchAction, |
| 412 | endMatchActionOffset |
| 413 | ); |
| 414 | break; |
| 415 | |
| 416 | case 'endAutoPhase': |
| 417 | // Not important action. |
| 418 | break; |
| 419 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 420 | case 'endTeleopPhase': |
| 421 | // Not important action. |
| 422 | break; |
| 423 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 424 | default: |
| 425 | throw new Error(`Unknown action type`); |
| 426 | } |
| 427 | |
| 428 | if (actionOffset !== undefined) { |
| 429 | actionOffsets.push(actionOffset); |
| 430 | } |
| 431 | } |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 432 | const teamNumberFb = builder.createString(this.teamNumber); |
Philipp Schrader | e859e6e | 2023-03-22 19:59:51 -0700 | [diff] [blame] | 433 | const compLevelFb = builder.createString(this.compLevel); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 434 | |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 435 | const actionsVector = Submit2024Actions.createActionsListVector( |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 436 | builder, |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 437 | actionOffsets |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 438 | ); |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 439 | Submit2024Actions.startSubmit2024Actions(builder); |
| 440 | Submit2024Actions.addTeamNumber(builder, teamNumberFb); |
| 441 | Submit2024Actions.addMatchNumber(builder, this.matchNumber); |
| 442 | Submit2024Actions.addSetNumber(builder, this.setNumber); |
| 443 | Submit2024Actions.addCompLevel(builder, compLevelFb); |
| 444 | Submit2024Actions.addActionsList(builder, actionsVector); |
| 445 | Submit2024Actions.addPreScouting(builder, this.preScouting); |
| 446 | builder.finish(Submit2024Actions.endSubmit2024Actions(builder)); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 447 | |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame^] | 448 | return builder.asUint8Array(); |
| 449 | } |
| 450 | |
| 451 | // Same as createActionsBuffer, but encoded as Base64. It's also split into |
| 452 | // a number of pieces so that each piece is roughly limited to |
| 453 | // `qrCodeValuePieceSize` bytes. |
| 454 | createBase64ActionsBuffers(): string[] { |
| 455 | const originalBuffer = this.createActionsBuffer(); |
| 456 | const deflatedData = pako.deflate(originalBuffer, {level: 9}); |
| 457 | |
| 458 | const pieceSize = this.qrCodeValuePieceSize; |
| 459 | const fullValue = btoa(String.fromCharCode(...deflatedData)); |
| 460 | const numPieces = Math.ceil(fullValue.length / pieceSize); |
| 461 | |
| 462 | let splitData: string[] = []; |
| 463 | for (let i = 0; i < numPieces; i++) { |
| 464 | const splitPiece = fullValue.slice(i * pieceSize, (i + 1) * pieceSize); |
| 465 | splitData.push(`${i}_${numPieces}_${pieceSize}_${splitPiece}`); |
| 466 | } |
| 467 | return splitData; |
| 468 | } |
| 469 | |
| 470 | setQrCodeValueIndex(index: number) { |
| 471 | this.qrCodeValueIndex = Math.max( |
| 472 | 0, |
| 473 | Math.min(index, this.qrCodeValuePieces.length - 1) |
| 474 | ); |
| 475 | } |
| 476 | |
| 477 | updateQrCodeValuePieceSize() { |
| 478 | this.qrCodeValuePieces = this.createBase64ActionsBuffers(); |
| 479 | this.qrCodeValueIndex = 0; |
| 480 | } |
| 481 | |
| 482 | async submit2024Actions() { |
Emily Markova | dcadcb6 | 2024-02-03 13:07:17 -0800 | [diff] [blame] | 483 | const res = await fetch('/requests/submit/submit_2024_actions', { |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 484 | method: 'POST', |
Philipp Schrader | e2e27ff | 2024-02-25 22:08:55 -0800 | [diff] [blame^] | 485 | body: this.createActionsBuffer(), |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 486 | }); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 487 | |
| 488 | if (res.ok) { |
| 489 | // We successfully submitted the data. Report success. |
| 490 | this.section = 'Success'; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 491 | this.actionList = []; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 492 | } else { |
| 493 | const resBuffer = await res.arrayBuffer(); |
| 494 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 495 | const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 496 | |
| 497 | const errorMessage = parsedResponse.errorMessage(); |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 498 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 499 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 500 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 501 | } |