blob: 09b1a8bd7397ad2fd40f1c6771ea15c7f57c9e64 [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;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800142
Philipp Schradere1498852023-04-15 18:06:45 -0700143 preScouting: boolean = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800144 matchStartTimestamp: number = 0;
Emily Markovadcadcb62024-02-03 13:07:17 -0800145 penalties: number = 0;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800146
Philipp Schrader8702b782023-04-15 17:33:37 -0700147 teamSelectionIsValid = false;
148
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800149 // When the user chooses to generate QR codes, we convert the flatbuffer into
150 // a long string. Since we frequently have more data than we can display in a
151 // single QR code, we break the data into multiple QR codes. The data for
152 // each QR code ("pieces") is stored in the `qrCodeValuePieces` list below.
153 // The `qrCodeValueIndex` keeps track of which QR code we're currently
154 // displaying.
155 qrCodeValuePieceSize = QR_CODE_PIECE_SIZES[DEFAULT_QR_CODE_PIECE_SIZE_INDEX];
156 qrCodeValuePieces: string[] = [];
157 qrCodeValueIndex: number = 0;
158
Philipp Schrader8702b782023-04-15 17:33:37 -0700159 constructor(private readonly matchListRequestor: MatchListRequestor) {}
160
Philipp Schrader75021f52023-04-09 21:14:13 -0700161 ngOnInit() {
162 // When the user navigated from the match list, we can skip the team
163 // selection. I.e. we trust that the user clicked the correct button.
164 this.section = this.skipTeamSelection ? 'Init' : 'Team Selection';
Philipp Schrader8702b782023-04-15 17:33:37 -0700165
166 if (this.section == 'Team Selection') {
167 this.fetchMatchList();
168 }
169 }
170
171 async fetchMatchList() {
172 this.progressMessage = 'Fetching match list. Please be patient.';
173 this.errorMessage = '';
174
175 try {
176 this.matchList = await this.matchListRequestor.fetchMatchList();
177 this.progressMessage = 'Successfully fetched match list.';
178 } catch (e) {
179 this.errorMessage = e;
180 this.progressMessage = '';
181 }
182 }
183
184 // This gets called when the user changes something on the Init screen.
185 // It makes sure that the user can't click "Next" until the information is
Philipp Schradere1498852023-04-15 18:06:45 -0700186 // valid, or this is for pre-scouting.
Philipp Schrader8702b782023-04-15 17:33:37 -0700187 updateTeamSelectionValidity(): void {
Philipp Schradere1498852023-04-15 18:06:45 -0700188 this.teamSelectionIsValid = this.preScouting || this.matchIsInMatchList();
Philipp Schrader8702b782023-04-15 17:33:37 -0700189 }
190
191 matchIsInMatchList(): boolean {
192 // If the user deletes the content of the teamNumber field, the value here
193 // is undefined. Guard against that.
194 if (this.teamNumber == null) {
195 return false;
196 }
Emily Markovae68b7632023-12-30 14:17:55 -0800197 const teamNumber = this.teamNumber;
Philipp Schrader8702b782023-04-15 17:33:37 -0700198
199 for (const match of this.matchList) {
200 if (
201 this.matchNumber == match.matchNumber() &&
202 this.setNumber == match.setNumber() &&
203 this.compLevel == match.compLevel() &&
204 (teamNumber === match.r1() ||
205 teamNumber === match.r2() ||
206 teamNumber === match.r3() ||
207 teamNumber === match.b1() ||
208 teamNumber === match.b2() ||
209 teamNumber === match.b3())
210 ) {
211 return true;
212 }
213 }
214 return false;
Philipp Schrader75021f52023-04-09 21:14:13 -0700215 }
216
Emily Markovadcadcb62024-02-03 13:07:17 -0800217 addPenalty(): void {
218 this.penalties += 1;
219 }
220
221 removePenalty(): void {
222 if (this.penalties > 0) {
223 this.penalties -= 1;
224 }
225 }
226
227 addPenalties(): void {
228 this.addAction({type: 'penaltyAction', penalties: this.penalties});
229 }
230
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800231 addAction(action: ActionT): void {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800232 if (action.type == 'startMatchAction') {
233 // Unix nanosecond timestamp.
234 this.matchStartTimestamp = Date.now() * 1e6;
235 action.timestamp = 0;
236 } else {
237 // Unix nanosecond timestamp relative to match start.
238 action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp;
239 }
240
Emily Markovadcadcb62024-02-03 13:07:17 -0800241 if (action.type == 'endMatchAction') {
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800242 // endMatchAction occurs at the same time as penaltyAction so add to its
243 // timestamp to make it unique.
Emily Markovadcadcb62024-02-03 13:07:17 -0800244 action.timestamp += 1;
245 }
246
Filip Kujawab73e94c2023-04-19 09:33:14 -0700247 if (action.type == 'mobilityAction') {
248 this.mobilityCompleted = true;
249 }
250
Emily Markovadcadcb62024-02-03 13:07:17 -0800251 if (action.type == 'pickupNoteAction' || action.type == 'placeNoteAction') {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800252 action.auto = this.autoPhase;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800253 }
254 this.actionList.push(action);
255 }
256
257 undoLastAction() {
258 if (this.actionList.length > 0) {
259 let lastAction = this.actionList.pop();
260 switch (lastAction?.type) {
261 case 'endAutoPhase':
262 this.autoPhase = true;
Emily Markovadcadcb62024-02-03 13:07:17 -0800263 this.section = 'Pickup';
264 case 'pickupNoteAction':
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800265 this.section = 'Pickup';
266 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800267 case 'endTeleopPhase':
268 this.section = 'Pickup';
269 break;
270 case 'placeNoteAction':
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800271 this.section = 'Place';
272 break;
273 case 'endMatchAction':
Emily Markovadcadcb62024-02-03 13:07:17 -0800274 this.section = 'Endgame';
275 case 'mobilityAction':
276 this.mobilityCompleted = false;
277 break;
278 case 'startMatchAction':
279 this.section = 'Init';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800280 break;
Filip Kujawa9f56d0e2023-03-03 19:44:43 -0800281 case 'robotDeathAction':
282 // TODO(FILIP): Return user to the screen they
283 // clicked dead robot on. Pickup is fine for now but
284 // might cause confusion.
285 this.section = 'Pickup';
286 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800287 default:
288 break;
289 }
290 }
291 }
292
Emily Markovadcadcb62024-02-03 13:07:17 -0800293 stringifyScoreType(scoreType: ScoreType): String {
294 return ScoreType[scoreType];
Emily Markovaf4b06a22023-05-10 17:44:09 -0700295 }
296
Emily Markovadcadcb62024-02-03 13:07:17 -0800297 stringifyStageType(stageType: StageType): String {
298 return StageType[stageType];
Emily Markovaf4b06a22023-05-10 17:44:09 -0700299 }
300
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800301 changeSectionTo(target: Section) {
Philipp Schrader8702b782023-04-15 17:33:37 -0700302 // Clear the messages since they won't be relevant in the next section.
303 this.errorMessage = '';
304 this.progressMessage = '';
305
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800306 // For the QR code screen, we need to make the value to encode available.
307 if (target == 'QR Code') {
308 this.updateQrCodeValuePieceSize();
309 }
310
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800311 this.section = target;
312 }
Philipp Schrader80587432022-03-05 15:41:22 -0800313
Ravago Jones2813c032022-03-16 23:44:11 -0700314 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700315
Ravago Jones2813c032022-03-16 23:44:11 -0700316 private scrollToTop() {
317 this.header.nativeElement.scrollIntoView();
318 }
319
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800320 createActionsBuffer() {
James Kuszmauldac091f2022-03-22 09:35:06 -0700321 const builder = new Builder();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800322 const actionOffsets: number[] = [];
323
324 for (const action of this.actionList) {
325 let actionOffset: number | undefined;
326 console.log(action.type);
327
328 switch (action.type) {
329 case 'startMatchAction':
330 const startMatchActionOffset =
331 StartMatchAction.createStartMatchAction(builder, action.position);
332 actionOffset = Action.createAction(
333 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700334 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800335 ActionType.StartMatchAction,
336 startMatchActionOffset
337 );
338 break;
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700339 case 'mobilityAction':
340 const mobilityActionOffset = MobilityAction.createMobilityAction(
341 builder,
342 action.mobility
343 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800344 actionOffset = Action.createAction(
345 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700346 BigInt(action.timestamp || 0),
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700347 ActionType.MobilityAction,
348 mobilityActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800349 );
350 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800351 case 'penaltyAction':
352 const penaltyActionOffset = PenaltyAction.createPenaltyAction(
353 builder,
354 action.penalties
355 );
Filip Kujawa4c286442023-03-03 10:41:22 -0800356 actionOffset = Action.createAction(
357 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700358 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800359 ActionType.PenaltyAction,
360 penaltyActionOffset
Filip Kujawa4c286442023-03-03 10:41:22 -0800361 );
362 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800363 case 'pickupNoteAction':
364 const pickupNoteActionOffset =
365 PickupNoteAction.createPickupNoteAction(
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700366 builder,
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700367 action.auto || false
368 );
369 actionOffset = Action.createAction(
370 builder,
371 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800372 ActionType.PickupNoteAction,
373 pickupNoteActionOffset
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700374 );
375 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800376 case 'placeNoteAction':
377 const placeNoteActionOffset = PlaceNoteAction.createPlaceNoteAction(
378 builder,
379 action.scoreType,
380 action.auto || false
381 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800382 actionOffset = Action.createAction(
383 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700384 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800385 ActionType.PlaceNoteAction,
386 placeNoteActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800387 );
388 break;
389
390 case 'robotDeathAction':
391 const robotDeathActionOffset =
Emily Markova040123c2024-02-27 09:48:37 -0800392 RobotDeathAction.createRobotDeathAction(builder, action.robotDead);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800393 actionOffset = Action.createAction(
394 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700395 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800396 ActionType.RobotDeathAction,
397 robotDeathActionOffset
398 );
399 break;
400
401 case 'endMatchAction':
402 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
403 builder,
Emily Markovadcadcb62024-02-03 13:07:17 -0800404 action.stageType,
Emily Markova6079e2f2024-02-17 13:17:24 -0800405 action.trapNote,
406 action.spotlight
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800407 );
408 actionOffset = Action.createAction(
409 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700410 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800411 ActionType.EndMatchAction,
412 endMatchActionOffset
413 );
414 break;
415
416 case 'endAutoPhase':
417 // Not important action.
418 break;
419
Emily Markovadcadcb62024-02-03 13:07:17 -0800420 case 'endTeleopPhase':
421 // Not important action.
422 break;
423
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800424 default:
425 throw new Error(`Unknown action type`);
426 }
427
428 if (actionOffset !== undefined) {
429 actionOffsets.push(actionOffset);
430 }
431 }
Emily Markovae68b7632023-12-30 14:17:55 -0800432 const teamNumberFb = builder.createString(this.teamNumber);
Philipp Schradere859e6e2023-03-22 19:59:51 -0700433 const compLevelFb = builder.createString(this.compLevel);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800434
Emily Markovadcadcb62024-02-03 13:07:17 -0800435 const actionsVector = Submit2024Actions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700436 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800437 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700438 );
Emily Markovadcadcb62024-02-03 13:07:17 -0800439 Submit2024Actions.startSubmit2024Actions(builder);
440 Submit2024Actions.addTeamNumber(builder, teamNumberFb);
441 Submit2024Actions.addMatchNumber(builder, this.matchNumber);
442 Submit2024Actions.addSetNumber(builder, this.setNumber);
443 Submit2024Actions.addCompLevel(builder, compLevelFb);
444 Submit2024Actions.addActionsList(builder, actionsVector);
445 Submit2024Actions.addPreScouting(builder, this.preScouting);
446 builder.finish(Submit2024Actions.endSubmit2024Actions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700447
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800448 return builder.asUint8Array();
449 }
450
451 // Same as createActionsBuffer, but encoded as Base64. It's also split into
452 // a number of pieces so that each piece is roughly limited to
453 // `qrCodeValuePieceSize` bytes.
454 createBase64ActionsBuffers(): string[] {
455 const originalBuffer = this.createActionsBuffer();
456 const deflatedData = pako.deflate(originalBuffer, {level: 9});
457
458 const pieceSize = this.qrCodeValuePieceSize;
459 const fullValue = btoa(String.fromCharCode(...deflatedData));
460 const numPieces = Math.ceil(fullValue.length / pieceSize);
461
462 let splitData: string[] = [];
463 for (let i = 0; i < numPieces; i++) {
464 const splitPiece = fullValue.slice(i * pieceSize, (i + 1) * pieceSize);
465 splitData.push(`${i}_${numPieces}_${pieceSize}_${splitPiece}`);
466 }
467 return splitData;
468 }
469
470 setQrCodeValueIndex(index: number) {
471 this.qrCodeValueIndex = Math.max(
472 0,
473 Math.min(index, this.qrCodeValuePieces.length - 1)
474 );
475 }
476
477 updateQrCodeValuePieceSize() {
478 this.qrCodeValuePieces = this.createBase64ActionsBuffers();
479 this.qrCodeValueIndex = 0;
480 }
481
482 async submit2024Actions() {
Emily Markovadcadcb62024-02-03 13:07:17 -0800483 const res = await fetch('/requests/submit/submit_2024_actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700484 method: 'POST',
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800485 body: this.createActionsBuffer(),
Philipp Schrader817cce32022-03-26 15:00:00 -0700486 });
Ravago Jones2813c032022-03-16 23:44:11 -0700487
488 if (res.ok) {
489 // We successfully submitted the data. Report success.
490 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800491 this.actionList = [];
Ravago Jones2813c032022-03-16 23:44:11 -0700492 } else {
493 const resBuffer = await res.arrayBuffer();
494 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700495 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700496
497 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700498 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800499 }
Ravago Jones2813c032022-03-16 23:44:11 -0700500 }
Philipp Schrader80587432022-03-05 15:41:22 -0800501}