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