blob: 6da8be8be9f339dba0dda0e42e9362cba3e67a91 [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 Schrader8b8ed672022-03-05 18:08:50 -080029
Philipp Schrader817cce32022-03-26 15:00:00 -070030type Section =
31 | 'Team Selection'
Filip Kujawa0ef334c2023-02-20 19:42:45 -080032 | 'Init'
33 | 'Pickup'
34 | 'Place'
35 | 'Endgame'
36 | 'Dead'
Philipp Schrader817cce32022-03-26 15:00:00 -070037 | 'Review and Submit'
38 | 'Success';
Philipp Schrader80587432022-03-05 15:41:22 -080039
Philipp Schrader8aeb14f2022-04-08 21:23:18 -070040// TODO(phil): Deduplicate with match_list.component.ts.
41const COMP_LEVELS = ['qm', 'ef', 'qf', 'sf', 'f'] as const;
42type CompLevel = typeof COMP_LEVELS[number];
43
44// TODO(phil): Deduplicate with match_list.component.ts.
45const COMP_LEVEL_LABELS: Record<CompLevel, string> = {
46 qm: 'Qualifications',
47 ef: 'Eighth Finals',
48 qf: 'Quarter Finals',
49 sf: 'Semi Finals',
50 f: 'Finals',
51};
52
Filip Kujawa0ef334c2023-02-20 19:42:45 -080053type ActionT =
54 | {
55 type: 'startMatchAction';
56 timestamp?: number;
57 position: number;
58 }
59 | {
Filip Kujawa0b4b1e52023-04-15 14:05:40 -070060 type: 'mobilityAction';
61 timestamp?: number;
62 mobility: boolean;
63 }
64 | {
Emily Markovadcadcb62024-02-03 13:07:17 -080065 type: 'pickupNoteAction';
Filip Kujawa4413a592023-03-01 10:54:34 -080066 timestamp?: number;
Filip Kujawa0ef334c2023-02-20 19:42:45 -080067 auto?: boolean;
68 }
69 | {
Emily Markovadcadcb62024-02-03 13:07:17 -080070 type: 'placeNoteAction';
Filip Kujawa0ef334c2023-02-20 19:42:45 -080071 timestamp?: number;
Emily Markovadcadcb62024-02-03 13:07:17 -080072 scoreType: ScoreType;
Filip Kujawa0ef334c2023-02-20 19:42:45 -080073 auto?: boolean;
74 }
75 | {
76 type: 'robotDeathAction';
77 timestamp?: number;
Emily Markova040123c2024-02-27 09:48:37 -080078 robotDead: boolean;
Filip Kujawa0ef334c2023-02-20 19:42:45 -080079 }
80 | {
Emily Markovadcadcb62024-02-03 13:07:17 -080081 type: 'penaltyAction';
82 timestamp?: number;
83 penalties: number;
84 }
85 | {
Filip Kujawa0ef334c2023-02-20 19:42:45 -080086 type: 'endMatchAction';
Emily Markovadcadcb62024-02-03 13:07:17 -080087 stageType: StageType;
88 trapNote: boolean;
Emily Markova6079e2f2024-02-17 13:17:24 -080089 spotlight: boolean;
Filip Kujawa0ef334c2023-02-20 19:42:45 -080090 timestamp?: number;
91 }
92 | {
93 // This is not a action that is submitted,
94 // It is used for undoing purposes.
95 type: 'endAutoPhase';
96 timestamp?: number;
Emily Markovadcadcb62024-02-03 13:07:17 -080097 }
98 | {
99 // This is not a action that is submitted,
100 // It is used for undoing purposes.
101 type: 'endTeleopPhase';
102 timestamp?: number;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800103 };
emilym38d08ba2022-10-22 15:25:01 -0700104
Philipp Schrader23993e82022-03-18 18:54:00 -0700105@Component({
106 selector: 'app-entry',
107 templateUrl: './entry.ng.html',
Philipp Schrader175a93c2023-02-19 13:13:40 -0800108 styleUrls: ['../app/common.css', './entry.component.css'],
Philipp Schrader23993e82022-03-18 18:54:00 -0700109})
Philipp Schrader75021f52023-04-09 21:14:13 -0700110export class EntryComponent implements OnInit {
Philipp Schrader36df73a2022-03-17 23:27:24 -0700111 // Re-export the type here so that we can use it in the `[value]` attribute
112 // of radio buttons.
Philipp Schrader8aeb14f2022-04-08 21:23:18 -0700113 readonly COMP_LEVELS = COMP_LEVELS;
114 readonly COMP_LEVEL_LABELS = COMP_LEVEL_LABELS;
Emily Markovadcadcb62024-02-03 13:07:17 -0800115 readonly ScoreType = ScoreType;
Emily Markova6079e2f2024-02-17 13:17:24 -0800116 readonly StageType = StageType;
Philipp Schrader36df73a2022-03-17 23:27:24 -0700117
Ravago Jones2813c032022-03-16 23:44:11 -0700118 section: Section = 'Team Selection';
Ravago Jones2813c032022-03-16 23:44:11 -0700119 @Input() matchNumber: number = 1;
Emily Markovae68b7632023-12-30 14:17:55 -0800120 @Input() teamNumber: string = '1';
Philipp Schrader30b4a682022-04-16 14:36:17 -0700121 @Input() setNumber: number = 1;
Philipp Schrader8aeb14f2022-04-08 21:23:18 -0700122 @Input() compLevel: CompLevel = 'qm';
Philipp Schrader75021f52023-04-09 21:14:13 -0700123 @Input() skipTeamSelection = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800124
Philipp Schrader8702b782023-04-15 17:33:37 -0700125 matchList: Match[] = [];
126
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800127 actionList: ActionT[] = [];
Philipp Schrader8702b782023-04-15 17:33:37 -0700128 progressMessage: string = '';
Ravago Jones2813c032022-03-16 23:44:11 -0700129 errorMessage: string = '';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800130 autoPhase: boolean = true;
Filip Kujawab73e94c2023-04-19 09:33:14 -0700131 mobilityCompleted: boolean = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800132
Philipp Schradere1498852023-04-15 18:06:45 -0700133 preScouting: boolean = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800134 matchStartTimestamp: number = 0;
Emily Markovadcadcb62024-02-03 13:07:17 -0800135 penalties: number = 0;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800136
Philipp Schrader8702b782023-04-15 17:33:37 -0700137 teamSelectionIsValid = false;
138
139 constructor(private readonly matchListRequestor: MatchListRequestor) {}
140
Philipp Schrader75021f52023-04-09 21:14:13 -0700141 ngOnInit() {
142 // When the user navigated from the match list, we can skip the team
143 // selection. I.e. we trust that the user clicked the correct button.
144 this.section = this.skipTeamSelection ? 'Init' : 'Team Selection';
Philipp Schrader8702b782023-04-15 17:33:37 -0700145
146 if (this.section == 'Team Selection') {
147 this.fetchMatchList();
148 }
149 }
150
151 async fetchMatchList() {
152 this.progressMessage = 'Fetching match list. Please be patient.';
153 this.errorMessage = '';
154
155 try {
156 this.matchList = await this.matchListRequestor.fetchMatchList();
157 this.progressMessage = 'Successfully fetched match list.';
158 } catch (e) {
159 this.errorMessage = e;
160 this.progressMessage = '';
161 }
162 }
163
164 // This gets called when the user changes something on the Init screen.
165 // It makes sure that the user can't click "Next" until the information is
Philipp Schradere1498852023-04-15 18:06:45 -0700166 // valid, or this is for pre-scouting.
Philipp Schrader8702b782023-04-15 17:33:37 -0700167 updateTeamSelectionValidity(): void {
Philipp Schradere1498852023-04-15 18:06:45 -0700168 this.teamSelectionIsValid = this.preScouting || this.matchIsInMatchList();
Philipp Schrader8702b782023-04-15 17:33:37 -0700169 }
170
171 matchIsInMatchList(): boolean {
172 // If the user deletes the content of the teamNumber field, the value here
173 // is undefined. Guard against that.
174 if (this.teamNumber == null) {
175 return false;
176 }
Emily Markovae68b7632023-12-30 14:17:55 -0800177 const teamNumber = this.teamNumber;
Philipp Schrader8702b782023-04-15 17:33:37 -0700178
179 for (const match of this.matchList) {
180 if (
181 this.matchNumber == match.matchNumber() &&
182 this.setNumber == match.setNumber() &&
183 this.compLevel == match.compLevel() &&
184 (teamNumber === match.r1() ||
185 teamNumber === match.r2() ||
186 teamNumber === match.r3() ||
187 teamNumber === match.b1() ||
188 teamNumber === match.b2() ||
189 teamNumber === match.b3())
190 ) {
191 return true;
192 }
193 }
194 return false;
Philipp Schrader75021f52023-04-09 21:14:13 -0700195 }
196
Emily Markovadcadcb62024-02-03 13:07:17 -0800197 addPenalty(): void {
198 this.penalties += 1;
199 }
200
201 removePenalty(): void {
202 if (this.penalties > 0) {
203 this.penalties -= 1;
204 }
205 }
206
207 addPenalties(): void {
208 this.addAction({type: 'penaltyAction', penalties: this.penalties});
209 }
210
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800211 addAction(action: ActionT): void {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800212 if (action.type == 'startMatchAction') {
213 // Unix nanosecond timestamp.
214 this.matchStartTimestamp = Date.now() * 1e6;
215 action.timestamp = 0;
216 } else {
217 // Unix nanosecond timestamp relative to match start.
218 action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp;
219 }
220
Emily Markovadcadcb62024-02-03 13:07:17 -0800221 if (action.type == 'endMatchAction') {
222 // endMatchAction occurs at the same time as penaltyAction so add to its timestamp to make it unique.
223 action.timestamp += 1;
224 }
225
Filip Kujawab73e94c2023-04-19 09:33:14 -0700226 if (action.type == 'mobilityAction') {
227 this.mobilityCompleted = true;
228 }
229
Emily Markovadcadcb62024-02-03 13:07:17 -0800230 if (action.type == 'pickupNoteAction' || action.type == 'placeNoteAction') {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800231 action.auto = this.autoPhase;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800232 }
233 this.actionList.push(action);
234 }
235
236 undoLastAction() {
237 if (this.actionList.length > 0) {
238 let lastAction = this.actionList.pop();
239 switch (lastAction?.type) {
240 case 'endAutoPhase':
241 this.autoPhase = true;
Emily Markovadcadcb62024-02-03 13:07:17 -0800242 this.section = 'Pickup';
243 case 'pickupNoteAction':
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800244 this.section = 'Pickup';
245 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800246 case 'endTeleopPhase':
247 this.section = 'Pickup';
248 break;
249 case 'placeNoteAction':
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800250 this.section = 'Place';
251 break;
252 case 'endMatchAction':
Emily Markovadcadcb62024-02-03 13:07:17 -0800253 this.section = 'Endgame';
254 case 'mobilityAction':
255 this.mobilityCompleted = false;
256 break;
257 case 'startMatchAction':
258 this.section = 'Init';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800259 break;
Filip Kujawa9f56d0e2023-03-03 19:44:43 -0800260 case 'robotDeathAction':
261 // TODO(FILIP): Return user to the screen they
262 // clicked dead robot on. Pickup is fine for now but
263 // might cause confusion.
264 this.section = 'Pickup';
265 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800266 default:
267 break;
268 }
269 }
270 }
271
Emily Markovadcadcb62024-02-03 13:07:17 -0800272 stringifyScoreType(scoreType: ScoreType): String {
273 return ScoreType[scoreType];
Emily Markovaf4b06a22023-05-10 17:44:09 -0700274 }
275
Emily Markovadcadcb62024-02-03 13:07:17 -0800276 stringifyStageType(stageType: StageType): String {
277 return StageType[stageType];
Emily Markovaf4b06a22023-05-10 17:44:09 -0700278 }
279
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800280 changeSectionTo(target: Section) {
Philipp Schrader8702b782023-04-15 17:33:37 -0700281 // Clear the messages since they won't be relevant in the next section.
282 this.errorMessage = '';
283 this.progressMessage = '';
284
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800285 this.section = target;
286 }
Philipp Schrader80587432022-03-05 15:41:22 -0800287
Ravago Jones2813c032022-03-16 23:44:11 -0700288 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700289
Ravago Jones2813c032022-03-16 23:44:11 -0700290 private scrollToTop() {
291 this.header.nativeElement.scrollIntoView();
292 }
293
Emily Markovadcadcb62024-02-03 13:07:17 -0800294 async submit2024Actions() {
James Kuszmauldac091f2022-03-22 09:35:06 -0700295 const builder = new Builder();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800296 const actionOffsets: number[] = [];
297
298 for (const action of this.actionList) {
299 let actionOffset: number | undefined;
300 console.log(action.type);
301
302 switch (action.type) {
303 case 'startMatchAction':
304 const startMatchActionOffset =
305 StartMatchAction.createStartMatchAction(builder, action.position);
306 actionOffset = Action.createAction(
307 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700308 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800309 ActionType.StartMatchAction,
310 startMatchActionOffset
311 );
312 break;
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700313 case 'mobilityAction':
314 const mobilityActionOffset = MobilityAction.createMobilityAction(
315 builder,
316 action.mobility
317 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800318 actionOffset = Action.createAction(
319 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700320 BigInt(action.timestamp || 0),
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700321 ActionType.MobilityAction,
322 mobilityActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800323 );
324 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800325 case 'penaltyAction':
326 const penaltyActionOffset = PenaltyAction.createPenaltyAction(
327 builder,
328 action.penalties
329 );
Filip Kujawa4c286442023-03-03 10:41:22 -0800330 actionOffset = Action.createAction(
331 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700332 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800333 ActionType.PenaltyAction,
334 penaltyActionOffset
Filip Kujawa4c286442023-03-03 10:41:22 -0800335 );
336 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800337 case 'pickupNoteAction':
338 const pickupNoteActionOffset =
339 PickupNoteAction.createPickupNoteAction(
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700340 builder,
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700341 action.auto || false
342 );
343 actionOffset = Action.createAction(
344 builder,
345 BigInt(action.timestamp || 0),
Emily Markovadcadcb62024-02-03 13:07:17 -0800346 ActionType.PickupNoteAction,
347 pickupNoteActionOffset
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700348 );
349 break;
Emily Markovadcadcb62024-02-03 13:07:17 -0800350 case 'placeNoteAction':
351 const placeNoteActionOffset = PlaceNoteAction.createPlaceNoteAction(
352 builder,
353 action.scoreType,
354 action.auto || false
355 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -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.PlaceNoteAction,
360 placeNoteActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800361 );
362 break;
363
364 case 'robotDeathAction':
365 const robotDeathActionOffset =
Emily Markova040123c2024-02-27 09:48:37 -0800366 RobotDeathAction.createRobotDeathAction(builder, action.robotDead);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800367 actionOffset = Action.createAction(
368 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700369 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800370 ActionType.RobotDeathAction,
371 robotDeathActionOffset
372 );
373 break;
374
375 case 'endMatchAction':
376 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
377 builder,
Emily Markovadcadcb62024-02-03 13:07:17 -0800378 action.stageType,
Emily Markova6079e2f2024-02-17 13:17:24 -0800379 action.trapNote,
380 action.spotlight
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800381 );
382 actionOffset = Action.createAction(
383 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700384 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800385 ActionType.EndMatchAction,
386 endMatchActionOffset
387 );
388 break;
389
390 case 'endAutoPhase':
391 // Not important action.
392 break;
393
Emily Markovadcadcb62024-02-03 13:07:17 -0800394 case 'endTeleopPhase':
395 // Not important action.
396 break;
397
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800398 default:
399 throw new Error(`Unknown action type`);
400 }
401
402 if (actionOffset !== undefined) {
403 actionOffsets.push(actionOffset);
404 }
405 }
Emily Markovae68b7632023-12-30 14:17:55 -0800406 const teamNumberFb = builder.createString(this.teamNumber);
Philipp Schradere859e6e2023-03-22 19:59:51 -0700407 const compLevelFb = builder.createString(this.compLevel);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800408
Emily Markovadcadcb62024-02-03 13:07:17 -0800409 const actionsVector = Submit2024Actions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700410 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800411 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700412 );
Emily Markovadcadcb62024-02-03 13:07:17 -0800413 Submit2024Actions.startSubmit2024Actions(builder);
414 Submit2024Actions.addTeamNumber(builder, teamNumberFb);
415 Submit2024Actions.addMatchNumber(builder, this.matchNumber);
416 Submit2024Actions.addSetNumber(builder, this.setNumber);
417 Submit2024Actions.addCompLevel(builder, compLevelFb);
418 Submit2024Actions.addActionsList(builder, actionsVector);
419 Submit2024Actions.addPreScouting(builder, this.preScouting);
420 builder.finish(Submit2024Actions.endSubmit2024Actions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700421
422 const buffer = builder.asUint8Array();
Emily Markovadcadcb62024-02-03 13:07:17 -0800423 const res = await fetch('/requests/submit/submit_2024_actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700424 method: 'POST',
425 body: buffer,
426 });
Ravago Jones2813c032022-03-16 23:44:11 -0700427
428 if (res.ok) {
429 // We successfully submitted the data. Report success.
430 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800431 this.actionList = [];
Ravago Jones2813c032022-03-16 23:44:11 -0700432 } else {
433 const resBuffer = await res.arrayBuffer();
434 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700435 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700436
437 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700438 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800439 }
Ravago Jones2813c032022-03-16 23:44:11 -0700440 }
Philipp Schrader80587432022-03-05 15:41:22 -0800441}