blob: b1d84b74d5c2628d7a39b6153a8313f899f0613d [file] [log] [blame]
Philipp Schrader817cce32022-03-26 15:00:00 -07001import {
2 Component,
3 ElementRef,
4 EventEmitter,
5 Input,
6 OnInit,
7 Output,
8 ViewChild,
9} from '@angular/core';
Ravago Jones2813c032022-03-16 23:44:11 -070010import {FormsModule} from '@angular/forms';
James Kuszmauldac091f2022-03-22 09:35:06 -070011import {Builder, ByteBuffer} from 'flatbuffers';
Philipp Schradere5d13942024-03-17 15:44:35 -070012import {ErrorResponse} from '@org_frc971/scouting/webserver/requests/messages/error_response_generated';
Philipp Schrader817cce32022-03-26 15:00:00 -070013import {
Filip Kujawa0ef334c2023-02-20 19:42:45 -080014 StartMatchAction,
Emily Markovadcadcb62024-02-03 13:07:17 -080015 ScoreType,
16 StageType,
17 Submit2024Actions,
Filip Kujawa0b4b1e52023-04-15 14:05:40 -070018 MobilityAction,
Emily Markovadcadcb62024-02-03 13:07:17 -080019 PenaltyAction,
20 PickupNoteAction,
21 PlaceNoteAction,
Filip Kujawa0ef334c2023-02-20 19:42:45 -080022 RobotDeathAction,
23 EndMatchAction,
24 ActionType,
25 Action,
Philipp Schradere5d13942024-03-17 15:44:35 -070026} from '@org_frc971/scouting/webserver/requests/messages/submit_2024_actions_generated';
27import {Match} from '@org_frc971/scouting/webserver/requests/messages/request_all_matches_response_generated';
28import {MatchListRequestor} from '@org_frc971/scouting/www/rpc';
Philipp Schradere2e27ff2024-02-25 22:08:55 -080029import * as pako from 'pako';
Philipp Schrader8b8ed672022-03-05 18:08:50 -080030
Philipp Schrader817cce32022-03-26 15:00:00 -070031type Section =
32 | 'Team Selection'
Filip Kujawa0ef334c2023-02-20 19:42:45 -080033 | 'Init'
34 | 'Pickup'
35 | 'Place'
36 | 'Endgame'
37 | 'Dead'
Philipp Schrader817cce32022-03-26 15:00:00 -070038 | 'Review and Submit'
Philipp Schradere2e27ff2024-02-25 22:08:55 -080039 | 'QR Code'
Philipp Schrader817cce32022-03-26 15:00:00 -070040 | 'Success';
Philipp Schrader80587432022-03-05 15:41:22 -080041
Philipp Schrader8aeb14f2022-04-08 21:23:18 -070042// TODO(phil): Deduplicate with match_list.component.ts.
43const COMP_LEVELS = ['qm', 'ef', 'qf', 'sf', 'f'] as const;
44type CompLevel = typeof COMP_LEVELS[number];
45
46// TODO(phil): Deduplicate with match_list.component.ts.
47const COMP_LEVEL_LABELS: Record<CompLevel, string> = {
48 qm: 'Qualifications',
49 ef: 'Eighth Finals',
50 qf: 'Quarter Finals',
51 sf: 'Semi Finals',
52 f: 'Finals',
53};
54
Philipp Schradere2e27ff2024-02-25 22:08:55 -080055// The maximum number of bytes per QR code. The user can adjust this value to
56// make the QR code contain less information, but easier to scan.
57const QR_CODE_PIECE_SIZES = [150, 300, 450, 600, 750, 900];
58
59// The default index into QR_CODE_PIECE_SIZES.
60const DEFAULT_QR_CODE_PIECE_SIZE_INDEX = QR_CODE_PIECE_SIZES.indexOf(750);
61
Filip Kujawa0ef334c2023-02-20 19:42:45 -080062type ActionT =
63 | {
64 type: 'startMatchAction';
65 timestamp?: number;
66 position: number;
67 }
68 | {
Filip Kujawa0b4b1e52023-04-15 14:05:40 -070069 type: 'mobilityAction';
70 timestamp?: number;
71 mobility: boolean;
72 }
73 | {
Emily Markovadcadcb62024-02-03 13:07:17 -080074 type: 'pickupNoteAction';
Filip Kujawa4413a592023-03-01 10:54:34 -080075 timestamp?: number;
Filip Kujawa0ef334c2023-02-20 19:42:45 -080076 auto?: boolean;
77 }
78 | {
Emily Markovadcadcb62024-02-03 13:07:17 -080079 type: 'placeNoteAction';
Filip Kujawa0ef334c2023-02-20 19:42:45 -080080 timestamp?: number;
Emily Markovadcadcb62024-02-03 13:07:17 -080081 scoreType: ScoreType;
Filip Kujawa0ef334c2023-02-20 19:42:45 -080082 auto?: boolean;
83 }
84 | {
85 type: 'robotDeathAction';
86 timestamp?: number;
Emily Markova040123c2024-02-27 09:48:37 -080087 robotDead: boolean;
Filip Kujawa0ef334c2023-02-20 19:42:45 -080088 }
89 | {
Emily Markovadcadcb62024-02-03 13:07:17 -080090 type: 'penaltyAction';
91 timestamp?: number;
92 penalties: number;
93 }
94 | {
Filip Kujawa0ef334c2023-02-20 19:42:45 -080095 type: 'endMatchAction';
Emily Markovadcadcb62024-02-03 13:07:17 -080096 stageType: StageType;
97 trapNote: boolean;
Emily Markova6079e2f2024-02-17 13:17:24 -080098 spotlight: boolean;
Filip Kujawa0ef334c2023-02-20 19:42:45 -080099 timestamp?: number;
100 }
101 | {
102 // This is not a action that is submitted,
103 // It is used for undoing purposes.
104 type: 'endAutoPhase';
105 timestamp?: number;
Emily Markovadcadcb62024-02-03 13:07:17 -0800106 }
107 | {
108 // This is not a action that is submitted,
109 // It is used for undoing purposes.
110 type: 'endTeleopPhase';
111 timestamp?: number;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800112 };
emilym38d08ba2022-10-22 15:25:01 -0700113
Philipp Schrader23993e82022-03-18 18:54:00 -0700114@Component({
115 selector: 'app-entry',
116 templateUrl: './entry.ng.html',
Philipp Schrader175a93c2023-02-19 13:13:40 -0800117 styleUrls: ['../app/common.css', './entry.component.css'],
Philipp Schrader23993e82022-03-18 18:54:00 -0700118})
Philipp Schrader75021f52023-04-09 21:14:13 -0700119export class EntryComponent implements OnInit {
Philipp Schrader36df73a2022-03-17 23:27:24 -0700120 // Re-export the type here so that we can use it in the `[value]` attribute
121 // of radio buttons.
Philipp Schrader8aeb14f2022-04-08 21:23:18 -0700122 readonly COMP_LEVELS = COMP_LEVELS;
123 readonly COMP_LEVEL_LABELS = COMP_LEVEL_LABELS;
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800124 readonly QR_CODE_PIECE_SIZES = QR_CODE_PIECE_SIZES;
Emily Markovadcadcb62024-02-03 13:07:17 -0800125 readonly ScoreType = ScoreType;
Emily Markova6079e2f2024-02-17 13:17:24 -0800126 readonly StageType = StageType;
Philipp Schrader36df73a2022-03-17 23:27:24 -0700127
Ravago Jones2813c032022-03-16 23:44:11 -0700128 section: Section = 'Team Selection';
Ravago Jones2813c032022-03-16 23:44:11 -0700129 @Input() matchNumber: number = 1;
Emily Markovae68b7632023-12-30 14:17:55 -0800130 @Input() teamNumber: string = '1';
Philipp Schrader30b4a682022-04-16 14:36:17 -0700131 @Input() setNumber: number = 1;
Philipp Schrader8aeb14f2022-04-08 21:23:18 -0700132 @Input() compLevel: CompLevel = 'qm';
Philipp Schrader75021f52023-04-09 21:14:13 -0700133 @Input() skipTeamSelection = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800134
Philipp Schrader63198402024-03-16 14:19:02 -0700135 @ViewChild('header') header: ElementRef;
136
Philipp Schrader8702b782023-04-15 17:33:37 -0700137 matchList: Match[] = [];
138
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800139 actionList: ActionT[] = [];
Philipp Schrader8702b782023-04-15 17:33:37 -0700140 progressMessage: string = '';
Ravago Jones2813c032022-03-16 23:44:11 -0700141 errorMessage: string = '';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800142 autoPhase: boolean = true;
Filip Kujawab73e94c2023-04-19 09:33:14 -0700143 mobilityCompleted: boolean = false;
Evelyn Yangc8036b12023-10-11 21:14:46 -0700144 selectedValue = 0;
145 nextTeamNumber = '';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800146
Philipp Schradere1498852023-04-15 18:06:45 -0700147 preScouting: boolean = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800148 matchStartTimestamp: number = 0;
Emily Markovadcadcb62024-02-03 13:07:17 -0800149 penalties: number = 0;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800150
Philipp Schrader8702b782023-04-15 17:33:37 -0700151 teamSelectionIsValid = false;
152
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800153 // When the user chooses to generate QR codes, we convert the flatbuffer into
154 // a long string. Since we frequently have more data than we can display in a
155 // single QR code, we break the data into multiple QR codes. The data for
156 // each QR code ("pieces") is stored in the `qrCodeValuePieces` list below.
157 // The `qrCodeValueIndex` keeps track of which QR code we're currently
158 // displaying.
159 qrCodeValuePieceSize = QR_CODE_PIECE_SIZES[DEFAULT_QR_CODE_PIECE_SIZE_INDEX];
160 qrCodeValuePieces: string[] = [];
161 qrCodeValueIndex: number = 0;
162
Philipp Schrader8702b782023-04-15 17:33:37 -0700163 constructor(private readonly matchListRequestor: MatchListRequestor) {}
164
Philipp Schrader75021f52023-04-09 21:14:13 -0700165 ngOnInit() {
166 // When the user navigated from the match list, we can skip the team
167 // selection. I.e. we trust that the user clicked the correct button.
168 this.section = this.skipTeamSelection ? 'Init' : 'Team Selection';
Evelyn Yangc8036b12023-10-11 21:14:46 -0700169 this.fetchMatchList();
170 }
Philipp Schrader8702b782023-04-15 17:33:37 -0700171
Evelyn Yangc8036b12023-10-11 21:14:46 -0700172 goToNextTeam() {
173 this.ngOnInit();
174 this.teamNumber = this.nextTeamNumber;
175 this.nextTeamNumber = '';
Philipp Schrader8702b782023-04-15 17:33:37 -0700176 }
177
178 async fetchMatchList() {
179 this.progressMessage = 'Fetching match list. Please be patient.';
180 this.errorMessage = '';
181
182 try {
183 this.matchList = await this.matchListRequestor.fetchMatchList();
184 this.progressMessage = 'Successfully fetched match list.';
185 } catch (e) {
186 this.errorMessage = e;
187 this.progressMessage = '';
188 }
189 }
190
191 // This gets called when the user changes something on the Init screen.
192 // It makes sure that the user can't click "Next" until the information is
Philipp Schradere1498852023-04-15 18:06:45 -0700193 // valid, or this is for pre-scouting.
Philipp Schrader8702b782023-04-15 17:33:37 -0700194 updateTeamSelectionValidity(): void {
Philipp Schradere1498852023-04-15 18:06:45 -0700195 this.teamSelectionIsValid = this.preScouting || this.matchIsInMatchList();
Philipp Schrader8702b782023-04-15 17:33:37 -0700196 }
197
198 matchIsInMatchList(): boolean {
199 // If the user deletes the content of the teamNumber field, the value here
200 // is undefined. Guard against that.
201 if (this.teamNumber == null) {
202 return false;
203 }
Philipp Schrader8702b782023-04-15 17:33:37 -0700204
205 for (const match of this.matchList) {
206 if (
207 this.matchNumber == match.matchNumber() &&
208 this.setNumber == match.setNumber() &&
209 this.compLevel == match.compLevel() &&
Evelyn Yangc8036b12023-10-11 21:14:46 -0700210 (this.teamNumber === match.r1() ||
211 this.teamNumber === match.r2() ||
212 this.teamNumber === match.r3() ||
213 this.teamNumber === match.b1() ||
214 this.teamNumber === match.b2() ||
215 this.teamNumber === match.b3())
Philipp Schrader8702b782023-04-15 17:33:37 -0700216 ) {
217 return true;
218 }
219 }
220 return false;
Philipp Schrader75021f52023-04-09 21:14:13 -0700221 }
222
Emily Markovadcadcb62024-02-03 13:07:17 -0800223 addPenalty(): void {
224 this.penalties += 1;
225 }
226
227 removePenalty(): void {
228 if (this.penalties > 0) {
229 this.penalties -= 1;
230 }
231 }
232
233 addPenalties(): void {
234 this.addAction({type: 'penaltyAction', penalties: this.penalties});
235 }
236
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800237 addAction(action: ActionT): void {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800238 if (action.type == 'startMatchAction') {
239 // Unix nanosecond timestamp.
240 this.matchStartTimestamp = Date.now() * 1e6;
241 action.timestamp = 0;
242 } else {
243 // Unix nanosecond timestamp relative to match start.
244 action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp;
245 }
246
Emily Markovadcadcb62024-02-03 13:07:17 -0800247 if (action.type == 'endMatchAction') {
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800248 // endMatchAction occurs at the same time as penaltyAction so add to its
249 // timestamp to make it unique.
Emily Markovadcadcb62024-02-03 13:07:17 -0800250 action.timestamp += 1;
251 }
252
Filip Kujawab73e94c2023-04-19 09:33:14 -0700253 if (action.type == 'mobilityAction') {
254 this.mobilityCompleted = true;
255 }
256
Emily Markovadcadcb62024-02-03 13:07:17 -0800257 if (action.type == 'pickupNoteAction' || action.type == 'placeNoteAction') {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800258 action.auto = this.autoPhase;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800259 }
260 this.actionList.push(action);
261 }
262
263 undoLastAction() {
264 if (this.actionList.length > 0) {
265 let lastAction = this.actionList.pop();
266 switch (lastAction?.type) {
267 case 'endAutoPhase':
268 this.autoPhase = true;
Emily Markovadcadcb62024-02-03 13:07:17 -0800269 this.section = 'Pickup';
270 case 'pickupNoteAction':
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800271 this.section = 'Pickup';
272 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800273 case 'endTeleopPhase':
274 this.section = 'Pickup';
275 break;
276 case 'placeNoteAction':
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800277 this.section = 'Place';
278 break;
279 case 'endMatchAction':
Emily Markovadcadcb62024-02-03 13:07:17 -0800280 this.section = 'Endgame';
281 case 'mobilityAction':
282 this.mobilityCompleted = false;
283 break;
284 case 'startMatchAction':
285 this.section = 'Init';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800286 break;
Filip Kujawa9f56d0e2023-03-03 19:44:43 -0800287 case 'robotDeathAction':
288 // TODO(FILIP): Return user to the screen they
289 // clicked dead robot on. Pickup is fine for now but
290 // might cause confusion.
291 this.section = 'Pickup';
292 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800293 default:
294 break;
295 }
296 }
297 }
298
Emily Markovadcadcb62024-02-03 13:07:17 -0800299 stringifyScoreType(scoreType: ScoreType): String {
300 return ScoreType[scoreType];
Emily Markovaf4b06a22023-05-10 17:44:09 -0700301 }
302
Emily Markovadcadcb62024-02-03 13:07:17 -0800303 stringifyStageType(stageType: StageType): String {
304 return StageType[stageType];
Emily Markovaf4b06a22023-05-10 17:44:09 -0700305 }
306
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800307 changeSectionTo(target: Section) {
Philipp Schrader8702b782023-04-15 17:33:37 -0700308 // Clear the messages since they won't be relevant in the next section.
309 this.errorMessage = '';
310 this.progressMessage = '';
311
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800312 // For the QR code screen, we need to make the value to encode available.
313 if (target == 'QR Code') {
314 this.updateQrCodeValuePieceSize();
315 }
316
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800317 this.section = target;
318 }
Philipp Schrader80587432022-03-05 15:41:22 -0800319
Ravago Jones2813c032022-03-16 23:44:11 -0700320 private scrollToTop() {
321 this.header.nativeElement.scrollIntoView();
322 }
323
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800324 createActionsBuffer() {
James Kuszmauldac091f2022-03-22 09:35:06 -0700325 const builder = new Builder();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800326 const actionOffsets: number[] = [];
327
328 for (const action of this.actionList) {
329 let actionOffset: number | undefined;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800330
331 switch (action.type) {
332 case 'startMatchAction':
333 const startMatchActionOffset =
334 StartMatchAction.createStartMatchAction(builder, action.position);
335 actionOffset = Action.createAction(
336 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700337 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800338 ActionType.StartMatchAction,
339 startMatchActionOffset
340 );
341 break;
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700342 case 'mobilityAction':
343 const mobilityActionOffset = MobilityAction.createMobilityAction(
344 builder,
345 action.mobility
346 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800347 actionOffset = Action.createAction(
348 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700349 BigInt(action.timestamp || 0),
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700350 ActionType.MobilityAction,
351 mobilityActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800352 );
353 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800354 case 'penaltyAction':
355 const penaltyActionOffset = PenaltyAction.createPenaltyAction(
356 builder,
357 action.penalties
358 );
Filip Kujawa4c286442023-03-03 10:41:22 -0800359 actionOffset = Action.createAction(
360 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700361 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800362 ActionType.PenaltyAction,
363 penaltyActionOffset
Filip Kujawa4c286442023-03-03 10:41:22 -0800364 );
365 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800366 case 'pickupNoteAction':
367 const pickupNoteActionOffset =
368 PickupNoteAction.createPickupNoteAction(
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700369 builder,
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700370 action.auto || false
371 );
372 actionOffset = Action.createAction(
373 builder,
374 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800375 ActionType.PickupNoteAction,
376 pickupNoteActionOffset
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700377 );
378 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800379 case 'placeNoteAction':
380 const placeNoteActionOffset = PlaceNoteAction.createPlaceNoteAction(
381 builder,
382 action.scoreType,
383 action.auto || false
384 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800385 actionOffset = Action.createAction(
386 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700387 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800388 ActionType.PlaceNoteAction,
389 placeNoteActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800390 );
391 break;
392
393 case 'robotDeathAction':
394 const robotDeathActionOffset =
Emily Markova040123c2024-02-27 09:48:37 -0800395 RobotDeathAction.createRobotDeathAction(builder, action.robotDead);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800396 actionOffset = Action.createAction(
397 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700398 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800399 ActionType.RobotDeathAction,
400 robotDeathActionOffset
401 );
402 break;
403
404 case 'endMatchAction':
405 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
406 builder,
Emily Markovadcadcb62024-02-03 13:07:17 -0800407 action.stageType,
Emily Markova6079e2f2024-02-17 13:17:24 -0800408 action.trapNote,
409 action.spotlight
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800410 );
411 actionOffset = Action.createAction(
412 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700413 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800414 ActionType.EndMatchAction,
415 endMatchActionOffset
416 );
417 break;
418
419 case 'endAutoPhase':
420 // Not important action.
421 break;
422
Emily Markovadcadcb62024-02-03 13:07:17 -0800423 case 'endTeleopPhase':
424 // Not important action.
425 break;
426
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800427 default:
428 throw new Error(`Unknown action type`);
429 }
430
431 if (actionOffset !== undefined) {
432 actionOffsets.push(actionOffset);
433 }
434 }
Emily Markovae68b7632023-12-30 14:17:55 -0800435 const teamNumberFb = builder.createString(this.teamNumber);
Philipp Schradere859e6e2023-03-22 19:59:51 -0700436 const compLevelFb = builder.createString(this.compLevel);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800437
Emily Markovadcadcb62024-02-03 13:07:17 -0800438 const actionsVector = Submit2024Actions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700439 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800440 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700441 );
Emily Markovadcadcb62024-02-03 13:07:17 -0800442 Submit2024Actions.startSubmit2024Actions(builder);
443 Submit2024Actions.addTeamNumber(builder, teamNumberFb);
444 Submit2024Actions.addMatchNumber(builder, this.matchNumber);
445 Submit2024Actions.addSetNumber(builder, this.setNumber);
446 Submit2024Actions.addCompLevel(builder, compLevelFb);
447 Submit2024Actions.addActionsList(builder, actionsVector);
448 Submit2024Actions.addPreScouting(builder, this.preScouting);
449 builder.finish(Submit2024Actions.endSubmit2024Actions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700450
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800451 return builder.asUint8Array();
452 }
453
454 // Same as createActionsBuffer, but encoded as Base64. It's also split into
455 // a number of pieces so that each piece is roughly limited to
456 // `qrCodeValuePieceSize` bytes.
457 createBase64ActionsBuffers(): string[] {
458 const originalBuffer = this.createActionsBuffer();
459 const deflatedData = pako.deflate(originalBuffer, {level: 9});
460
461 const pieceSize = this.qrCodeValuePieceSize;
462 const fullValue = btoa(String.fromCharCode(...deflatedData));
463 const numPieces = Math.ceil(fullValue.length / pieceSize);
464
465 let splitData: string[] = [];
466 for (let i = 0; i < numPieces; i++) {
467 const splitPiece = fullValue.slice(i * pieceSize, (i + 1) * pieceSize);
468 splitData.push(`${i}_${numPieces}_${pieceSize}_${splitPiece}`);
469 }
470 return splitData;
471 }
472
473 setQrCodeValueIndex(index: number) {
474 this.qrCodeValueIndex = Math.max(
475 0,
476 Math.min(index, this.qrCodeValuePieces.length - 1)
477 );
478 }
479
480 updateQrCodeValuePieceSize() {
481 this.qrCodeValuePieces = this.createBase64ActionsBuffers();
482 this.qrCodeValueIndex = 0;
483 }
484
485 async submit2024Actions() {
Emily Markovadcadcb62024-02-03 13:07:17 -0800486 const res = await fetch('/requests/submit/submit_2024_actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700487 method: 'POST',
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800488 body: this.createActionsBuffer(),
Philipp Schrader817cce32022-03-26 15:00:00 -0700489 });
Ravago Jones2813c032022-03-16 23:44:11 -0700490
491 if (res.ok) {
492 // We successfully submitted the data. Report success.
493 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800494 this.actionList = [];
Evelyn Yangc8036b12023-10-11 21:14:46 -0700495
Philipp Schrader63198402024-03-16 14:19:02 -0700496 // Keep track of the position of the last robot, use to figure out what
497 // the next robot in the same position is.
Evelyn Yangc8036b12023-10-11 21:14:46 -0700498 let lastTeamPos = '0';
499 for (const match of this.matchList) {
500 if (
501 this.matchNumber === match.matchNumber() &&
502 this.setNumber === match.setNumber() &&
503 this.compLevel === match.compLevel()
504 ) {
505 this.teamNumber = this.teamNumber;
506 if (this.teamNumber == match.r1()) {
507 lastTeamPos = 'r1';
508 } else if (this.teamNumber == match.r2()) {
509 lastTeamPos = 'r2';
510 } else if (this.teamNumber == match.r3()) {
511 lastTeamPos = 'r3';
512 } else if (this.teamNumber == match.b1()) {
513 lastTeamPos = 'b1';
514 } else if (this.teamNumber == match.b2()) {
515 lastTeamPos = 'b2';
516 } else if (this.teamNumber == match.b3()) {
517 lastTeamPos = 'b3';
518 } else {
519 console.log('Position of scouted team not found.');
520 }
521 break;
522 }
523 }
524 if (lastTeamPos != '0') {
525 this.matchNumber += 1;
526 for (const match of this.matchList) {
527 if (
528 this.matchNumber == match.matchNumber() &&
529 this.setNumber == match.setNumber() &&
530 this.compLevel == match.compLevel()
531 ) {
532 if (lastTeamPos == 'r1') {
533 this.nextTeamNumber = match.r1();
534 } else if (lastTeamPos == 'r2') {
535 this.nextTeamNumber = match.r2();
536 } else if (lastTeamPos == 'r3') {
537 this.nextTeamNumber = match.r3();
538 } else if (lastTeamPos == 'b1') {
539 this.nextTeamNumber = match.b1();
540 } else if (lastTeamPos == 'b2') {
541 this.nextTeamNumber = match.b2();
542 } else if (lastTeamPos == 'b3') {
543 this.nextTeamNumber = match.b3();
544 } else {
545 console.log('Position of last team not found.');
546 }
547 break;
548 }
549 }
550 } else {
551 console.log('Last team position not found.');
552 }
553 this.matchList = [];
554 this.progressMessage = '';
555 this.errorMessage = '';
556 this.autoPhase = true;
557 this.actionList = [];
558 this.mobilityCompleted = false;
559 this.preScouting = false;
560 this.matchStartTimestamp = 0;
561 this.selectedValue = 0;
Ravago Jones2813c032022-03-16 23:44:11 -0700562 } else {
563 const resBuffer = await res.arrayBuffer();
564 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700565 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700566
567 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700568 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800569 }
Ravago Jones2813c032022-03-16 23:44:11 -0700570 }
Philipp Schrader80587432022-03-05 15:41:22 -0800571}