blob: 8ede3a2edc9d89c589d2f560c12b3bde48fcd09b [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 Schraderd7efa2b2023-02-17 21:15:13 -080012import {ErrorResponse} from '../../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,
Emily Markovadcadcb62024-02-03 13:07:17 -080026} from '../../webserver/requests/messages/submit_2024_actions_generated';
Philipp Schrader8702b782023-04-15 17:33:37 -070027import {Match} from '../../webserver/requests/messages/request_all_matches_response_generated';
Philipp Schraderba072d92024-02-21 17:00:37 -080028import {MatchListRequestor} from '../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 Schrader8702b782023-04-15 17:33:37 -0700135 matchList: Match[] = [];
136
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800137 actionList: ActionT[] = [];
Philipp Schrader8702b782023-04-15 17:33:37 -0700138 progressMessage: string = '';
Ravago Jones2813c032022-03-16 23:44:11 -0700139 errorMessage: string = '';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800140 autoPhase: boolean = true;
Filip Kujawab73e94c2023-04-19 09:33:14 -0700141 mobilityCompleted: boolean = false;
Evelyn Yangc8036b12023-10-11 21:14:46 -0700142 selectedValue = 0;
143 nextTeamNumber = '';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800144
Philipp Schradere1498852023-04-15 18:06:45 -0700145 preScouting: boolean = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800146 matchStartTimestamp: number = 0;
Emily Markovadcadcb62024-02-03 13:07:17 -0800147 penalties: number = 0;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800148
Philipp Schrader8702b782023-04-15 17:33:37 -0700149 teamSelectionIsValid = false;
150
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800151 // When the user chooses to generate QR codes, we convert the flatbuffer into
152 // a long string. Since we frequently have more data than we can display in a
153 // single QR code, we break the data into multiple QR codes. The data for
154 // each QR code ("pieces") is stored in the `qrCodeValuePieces` list below.
155 // The `qrCodeValueIndex` keeps track of which QR code we're currently
156 // displaying.
157 qrCodeValuePieceSize = QR_CODE_PIECE_SIZES[DEFAULT_QR_CODE_PIECE_SIZE_INDEX];
158 qrCodeValuePieces: string[] = [];
159 qrCodeValueIndex: number = 0;
160
Philipp Schrader8702b782023-04-15 17:33:37 -0700161 constructor(private readonly matchListRequestor: MatchListRequestor) {}
162
Philipp Schrader75021f52023-04-09 21:14:13 -0700163 ngOnInit() {
164 // When the user navigated from the match list, we can skip the team
165 // selection. I.e. we trust that the user clicked the correct button.
166 this.section = this.skipTeamSelection ? 'Init' : 'Team Selection';
Evelyn Yangc8036b12023-10-11 21:14:46 -0700167 this.fetchMatchList();
168 }
Philipp Schrader8702b782023-04-15 17:33:37 -0700169
Evelyn Yangc8036b12023-10-11 21:14:46 -0700170 goToNextTeam() {
171 this.ngOnInit();
172 this.teamNumber = this.nextTeamNumber;
173 this.nextTeamNumber = '';
Philipp Schrader8702b782023-04-15 17:33:37 -0700174 }
175
176 async fetchMatchList() {
177 this.progressMessage = 'Fetching match list. Please be patient.';
178 this.errorMessage = '';
179
180 try {
181 this.matchList = await this.matchListRequestor.fetchMatchList();
182 this.progressMessage = 'Successfully fetched match list.';
183 } catch (e) {
184 this.errorMessage = e;
185 this.progressMessage = '';
186 }
187 }
188
189 // This gets called when the user changes something on the Init screen.
190 // It makes sure that the user can't click "Next" until the information is
Philipp Schradere1498852023-04-15 18:06:45 -0700191 // valid, or this is for pre-scouting.
Philipp Schrader8702b782023-04-15 17:33:37 -0700192 updateTeamSelectionValidity(): void {
Philipp Schradere1498852023-04-15 18:06:45 -0700193 this.teamSelectionIsValid = this.preScouting || this.matchIsInMatchList();
Philipp Schrader8702b782023-04-15 17:33:37 -0700194 }
195
196 matchIsInMatchList(): boolean {
197 // If the user deletes the content of the teamNumber field, the value here
198 // is undefined. Guard against that.
199 if (this.teamNumber == null) {
200 return false;
201 }
Philipp Schrader8702b782023-04-15 17:33:37 -0700202
203 for (const match of this.matchList) {
204 if (
205 this.matchNumber == match.matchNumber() &&
206 this.setNumber == match.setNumber() &&
207 this.compLevel == match.compLevel() &&
Evelyn Yangc8036b12023-10-11 21:14:46 -0700208 (this.teamNumber === match.r1() ||
209 this.teamNumber === match.r2() ||
210 this.teamNumber === match.r3() ||
211 this.teamNumber === match.b1() ||
212 this.teamNumber === match.b2() ||
213 this.teamNumber === match.b3())
Philipp Schrader8702b782023-04-15 17:33:37 -0700214 ) {
215 return true;
216 }
217 }
218 return false;
Philipp Schrader75021f52023-04-09 21:14:13 -0700219 }
220
Emily Markovadcadcb62024-02-03 13:07:17 -0800221 addPenalty(): void {
222 this.penalties += 1;
223 }
224
225 removePenalty(): void {
226 if (this.penalties > 0) {
227 this.penalties -= 1;
228 }
229 }
230
231 addPenalties(): void {
232 this.addAction({type: 'penaltyAction', penalties: this.penalties});
233 }
234
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800235 addAction(action: ActionT): void {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800236 if (action.type == 'startMatchAction') {
237 // Unix nanosecond timestamp.
238 this.matchStartTimestamp = Date.now() * 1e6;
239 action.timestamp = 0;
240 } else {
241 // Unix nanosecond timestamp relative to match start.
242 action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp;
243 }
244
Emily Markovadcadcb62024-02-03 13:07:17 -0800245 if (action.type == 'endMatchAction') {
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800246 // endMatchAction occurs at the same time as penaltyAction so add to its
247 // timestamp to make it unique.
Emily Markovadcadcb62024-02-03 13:07:17 -0800248 action.timestamp += 1;
249 }
250
Filip Kujawab73e94c2023-04-19 09:33:14 -0700251 if (action.type == 'mobilityAction') {
252 this.mobilityCompleted = true;
253 }
254
Emily Markovadcadcb62024-02-03 13:07:17 -0800255 if (action.type == 'pickupNoteAction' || action.type == 'placeNoteAction') {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800256 action.auto = this.autoPhase;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800257 }
258 this.actionList.push(action);
259 }
260
261 undoLastAction() {
262 if (this.actionList.length > 0) {
263 let lastAction = this.actionList.pop();
264 switch (lastAction?.type) {
265 case 'endAutoPhase':
266 this.autoPhase = true;
Emily Markovadcadcb62024-02-03 13:07:17 -0800267 this.section = 'Pickup';
268 case 'pickupNoteAction':
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800269 this.section = 'Pickup';
270 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800271 case 'endTeleopPhase':
272 this.section = 'Pickup';
273 break;
274 case 'placeNoteAction':
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800275 this.section = 'Place';
276 break;
277 case 'endMatchAction':
Emily Markovadcadcb62024-02-03 13:07:17 -0800278 this.section = 'Endgame';
279 case 'mobilityAction':
280 this.mobilityCompleted = false;
281 break;
282 case 'startMatchAction':
283 this.section = 'Init';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800284 break;
Filip Kujawa9f56d0e2023-03-03 19:44:43 -0800285 case 'robotDeathAction':
286 // TODO(FILIP): Return user to the screen they
287 // clicked dead robot on. Pickup is fine for now but
288 // might cause confusion.
289 this.section = 'Pickup';
290 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800291 default:
292 break;
293 }
294 }
295 }
296
Emily Markovadcadcb62024-02-03 13:07:17 -0800297 stringifyScoreType(scoreType: ScoreType): String {
298 return ScoreType[scoreType];
Emily Markovaf4b06a22023-05-10 17:44:09 -0700299 }
300
Emily Markovadcadcb62024-02-03 13:07:17 -0800301 stringifyStageType(stageType: StageType): String {
302 return StageType[stageType];
Emily Markovaf4b06a22023-05-10 17:44:09 -0700303 }
304
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800305 changeSectionTo(target: Section) {
Philipp Schrader8702b782023-04-15 17:33:37 -0700306 // Clear the messages since they won't be relevant in the next section.
307 this.errorMessage = '';
308 this.progressMessage = '';
309
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800310 // For the QR code screen, we need to make the value to encode available.
311 if (target == 'QR Code') {
312 this.updateQrCodeValuePieceSize();
313 }
314
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800315 this.section = target;
316 }
Philipp Schrader80587432022-03-05 15:41:22 -0800317
Ravago Jones2813c032022-03-16 23:44:11 -0700318 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700319
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;
330 console.log(action.type);
331
332 switch (action.type) {
333 case 'startMatchAction':
334 const startMatchActionOffset =
335 StartMatchAction.createStartMatchAction(builder, action.position);
336 actionOffset = Action.createAction(
337 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700338 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800339 ActionType.StartMatchAction,
340 startMatchActionOffset
341 );
342 break;
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700343 case 'mobilityAction':
344 const mobilityActionOffset = MobilityAction.createMobilityAction(
345 builder,
346 action.mobility
347 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800348 actionOffset = Action.createAction(
349 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700350 BigInt(action.timestamp || 0),
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700351 ActionType.MobilityAction,
352 mobilityActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800353 );
354 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800355 case 'penaltyAction':
356 const penaltyActionOffset = PenaltyAction.createPenaltyAction(
357 builder,
358 action.penalties
359 );
Filip Kujawa4c286442023-03-03 10:41:22 -0800360 actionOffset = Action.createAction(
361 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700362 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800363 ActionType.PenaltyAction,
364 penaltyActionOffset
Filip Kujawa4c286442023-03-03 10:41:22 -0800365 );
366 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800367 case 'pickupNoteAction':
368 const pickupNoteActionOffset =
369 PickupNoteAction.createPickupNoteAction(
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700370 builder,
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700371 action.auto || false
372 );
373 actionOffset = Action.createAction(
374 builder,
375 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800376 ActionType.PickupNoteAction,
377 pickupNoteActionOffset
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700378 );
379 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800380 case 'placeNoteAction':
381 const placeNoteActionOffset = PlaceNoteAction.createPlaceNoteAction(
382 builder,
383 action.scoreType,
384 action.auto || false
385 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800386 actionOffset = Action.createAction(
387 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700388 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800389 ActionType.PlaceNoteAction,
390 placeNoteActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800391 );
392 break;
393
394 case 'robotDeathAction':
395 const robotDeathActionOffset =
Emily Markova040123c2024-02-27 09:48:37 -0800396 RobotDeathAction.createRobotDeathAction(builder, action.robotDead);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800397 actionOffset = Action.createAction(
398 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700399 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800400 ActionType.RobotDeathAction,
401 robotDeathActionOffset
402 );
403 break;
404
405 case 'endMatchAction':
406 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
407 builder,
Emily Markovadcadcb62024-02-03 13:07:17 -0800408 action.stageType,
Emily Markova6079e2f2024-02-17 13:17:24 -0800409 action.trapNote,
410 action.spotlight
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800411 );
412 actionOffset = Action.createAction(
413 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700414 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800415 ActionType.EndMatchAction,
416 endMatchActionOffset
417 );
418 break;
419
420 case 'endAutoPhase':
421 // Not important action.
422 break;
423
Emily Markovadcadcb62024-02-03 13:07:17 -0800424 case 'endTeleopPhase':
425 // Not important action.
426 break;
427
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800428 default:
429 throw new Error(`Unknown action type`);
430 }
431
432 if (actionOffset !== undefined) {
433 actionOffsets.push(actionOffset);
434 }
435 }
Emily Markovae68b7632023-12-30 14:17:55 -0800436 const teamNumberFb = builder.createString(this.teamNumber);
Philipp Schradere859e6e2023-03-22 19:59:51 -0700437 const compLevelFb = builder.createString(this.compLevel);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800438
Emily Markovadcadcb62024-02-03 13:07:17 -0800439 const actionsVector = Submit2024Actions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700440 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800441 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700442 );
Emily Markovadcadcb62024-02-03 13:07:17 -0800443 Submit2024Actions.startSubmit2024Actions(builder);
444 Submit2024Actions.addTeamNumber(builder, teamNumberFb);
445 Submit2024Actions.addMatchNumber(builder, this.matchNumber);
446 Submit2024Actions.addSetNumber(builder, this.setNumber);
447 Submit2024Actions.addCompLevel(builder, compLevelFb);
448 Submit2024Actions.addActionsList(builder, actionsVector);
449 Submit2024Actions.addPreScouting(builder, this.preScouting);
450 builder.finish(Submit2024Actions.endSubmit2024Actions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700451
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800452 return builder.asUint8Array();
453 }
454
455 // Same as createActionsBuffer, but encoded as Base64. It's also split into
456 // a number of pieces so that each piece is roughly limited to
457 // `qrCodeValuePieceSize` bytes.
458 createBase64ActionsBuffers(): string[] {
459 const originalBuffer = this.createActionsBuffer();
460 const deflatedData = pako.deflate(originalBuffer, {level: 9});
461
462 const pieceSize = this.qrCodeValuePieceSize;
463 const fullValue = btoa(String.fromCharCode(...deflatedData));
464 const numPieces = Math.ceil(fullValue.length / pieceSize);
465
466 let splitData: string[] = [];
467 for (let i = 0; i < numPieces; i++) {
468 const splitPiece = fullValue.slice(i * pieceSize, (i + 1) * pieceSize);
469 splitData.push(`${i}_${numPieces}_${pieceSize}_${splitPiece}`);
470 }
471 return splitData;
472 }
473
474 setQrCodeValueIndex(index: number) {
475 this.qrCodeValueIndex = Math.max(
476 0,
477 Math.min(index, this.qrCodeValuePieces.length - 1)
478 );
479 }
480
481 updateQrCodeValuePieceSize() {
482 this.qrCodeValuePieces = this.createBase64ActionsBuffers();
483 this.qrCodeValueIndex = 0;
484 }
485
486 async submit2024Actions() {
Emily Markovadcadcb62024-02-03 13:07:17 -0800487 const res = await fetch('/requests/submit/submit_2024_actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700488 method: 'POST',
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800489 body: this.createActionsBuffer(),
Philipp Schrader817cce32022-03-26 15:00:00 -0700490 });
Ravago Jones2813c032022-03-16 23:44:11 -0700491
492 if (res.ok) {
493 // We successfully submitted the data. Report success.
494 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800495 this.actionList = [];
Evelyn Yangc8036b12023-10-11 21:14:46 -0700496
497 // Keep track of the position of the last robot, use to figure out what the next robot in the same position is.
498
499 let lastTeamPos = '0';
500 for (const match of this.matchList) {
501 if (
502 this.matchNumber === match.matchNumber() &&
503 this.setNumber === match.setNumber() &&
504 this.compLevel === match.compLevel()
505 ) {
506 this.teamNumber = this.teamNumber;
507 if (this.teamNumber == match.r1()) {
508 lastTeamPos = 'r1';
509 } else if (this.teamNumber == match.r2()) {
510 lastTeamPos = 'r2';
511 } else if (this.teamNumber == match.r3()) {
512 lastTeamPos = 'r3';
513 } else if (this.teamNumber == match.b1()) {
514 lastTeamPos = 'b1';
515 } else if (this.teamNumber == match.b2()) {
516 lastTeamPos = 'b2';
517 } else if (this.teamNumber == match.b3()) {
518 lastTeamPos = 'b3';
519 } else {
520 console.log('Position of scouted team not found.');
521 }
522 break;
523 }
524 }
525 if (lastTeamPos != '0') {
526 this.matchNumber += 1;
527 for (const match of this.matchList) {
528 if (
529 this.matchNumber == match.matchNumber() &&
530 this.setNumber == match.setNumber() &&
531 this.compLevel == match.compLevel()
532 ) {
533 if (lastTeamPos == 'r1') {
534 this.nextTeamNumber = match.r1();
535 } else if (lastTeamPos == 'r2') {
536 this.nextTeamNumber = match.r2();
537 } else if (lastTeamPos == 'r3') {
538 this.nextTeamNumber = match.r3();
539 } else if (lastTeamPos == 'b1') {
540 this.nextTeamNumber = match.b1();
541 } else if (lastTeamPos == 'b2') {
542 this.nextTeamNumber = match.b2();
543 } else if (lastTeamPos == 'b3') {
544 this.nextTeamNumber = match.b3();
545 } else {
546 console.log('Position of last team not found.');
547 }
548 break;
549 }
550 }
551 } else {
552 console.log('Last team position not found.');
553 }
554 this.matchList = [];
555 this.progressMessage = '';
556 this.errorMessage = '';
557 this.autoPhase = true;
558 this.actionList = [];
559 this.mobilityCompleted = false;
560 this.preScouting = false;
561 this.matchStartTimestamp = 0;
562 this.selectedValue = 0;
Ravago Jones2813c032022-03-16 23:44:11 -0700563 } else {
564 const resBuffer = await res.arrayBuffer();
565 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700566 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700567
568 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700569 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800570 }
Ravago Jones2813c032022-03-16 23:44:11 -0700571 }
Philipp Schrader80587432022-03-05 15:41:22 -0800572}