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