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