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