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