blob: 56ee1225fe2ed4a3778caf9b0ad315afbab0158a [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 ObjectType,
15 ScoreLevel,
16 SubmitActions,
17 StartMatchAction,
Filip Kujawa0b4b1e52023-04-15 14:05:40 -070018 MobilityAction,
Filip Kujawa4413a592023-03-01 10:54:34 -080019 AutoBalanceAction,
Filip Kujawa0ef334c2023-02-20 19:42:45 -080020 PickupObjectAction,
21 PlaceObjectAction,
22 RobotDeathAction,
23 EndMatchAction,
24 ActionType,
25 Action,
26} from '../../webserver/requests/messages/submit_actions_generated';
Philipp Schrader8702b782023-04-15 17:33:37 -070027import {Match} from '../../webserver/requests/messages/request_all_matches_response_generated';
28import {MatchListRequestor} from '@org_frc971/scouting/www/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 | {
Filip Kujawa4413a592023-03-01 10:54:34 -080065 type: 'autoBalanceAction';
66 timestamp?: number;
67 docked: boolean;
68 engaged: boolean;
Emily Markova63c63f62023-03-29 20:57:35 -070069 balanceAttempt: boolean;
Filip Kujawa4413a592023-03-01 10:54:34 -080070 }
71 | {
Filip Kujawa0ef334c2023-02-20 19:42:45 -080072 type: 'pickupObjectAction';
73 timestamp?: number;
74 objectType: ObjectType;
75 auto?: boolean;
76 }
77 | {
78 type: 'placeObjectAction';
79 timestamp?: number;
80 objectType?: ObjectType;
81 scoreLevel: ScoreLevel;
82 auto?: boolean;
83 }
84 | {
85 type: 'robotDeathAction';
86 timestamp?: number;
87 robotOn: boolean;
88 }
89 | {
90 type: 'endMatchAction';
91 docked: boolean;
92 engaged: boolean;
Emily Markova63c63f62023-03-29 20:57:35 -070093 balanceAttempt: boolean;
Filip Kujawa0ef334c2023-02-20 19:42:45 -080094 timestamp?: number;
95 }
96 | {
97 // This is not a action that is submitted,
98 // It is used for undoing purposes.
99 type: 'endAutoPhase';
100 timestamp?: number;
101 };
emilym38d08ba2022-10-22 15:25:01 -0700102
Philipp Schrader23993e82022-03-18 18:54:00 -0700103@Component({
104 selector: 'app-entry',
105 templateUrl: './entry.ng.html',
Philipp Schrader175a93c2023-02-19 13:13:40 -0800106 styleUrls: ['../app/common.css', './entry.component.css'],
Philipp Schrader23993e82022-03-18 18:54:00 -0700107})
Philipp Schrader75021f52023-04-09 21:14:13 -0700108export class EntryComponent implements OnInit {
Philipp Schrader36df73a2022-03-17 23:27:24 -0700109 // Re-export the type here so that we can use it in the `[value]` attribute
110 // of radio buttons.
Philipp Schrader8aeb14f2022-04-08 21:23:18 -0700111 readonly COMP_LEVELS = COMP_LEVELS;
112 readonly COMP_LEVEL_LABELS = COMP_LEVEL_LABELS;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800113 readonly ObjectType = ObjectType;
114 readonly ScoreLevel = ScoreLevel;
Philipp Schrader36df73a2022-03-17 23:27:24 -0700115
Ravago Jones2813c032022-03-16 23:44:11 -0700116 section: Section = 'Team Selection';
Ravago Jones2813c032022-03-16 23:44:11 -0700117 @Input() matchNumber: number = 1;
Philipp Schrader8702b782023-04-15 17:33:37 -0700118 // TODO(phil): Change the type of teamNumber to a string.
Ravago Jones2813c032022-03-16 23:44:11 -0700119 @Input() teamNumber: number = 1;
Philipp Schrader30b4a682022-04-16 14:36:17 -0700120 @Input() setNumber: number = 1;
Philipp Schrader8aeb14f2022-04-08 21:23:18 -0700121 @Input() compLevel: CompLevel = 'qm';
Philipp Schrader75021f52023-04-09 21:14:13 -0700122 @Input() skipTeamSelection = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800123
Philipp Schrader8702b782023-04-15 17:33:37 -0700124 matchList: Match[] = [];
125
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800126 actionList: ActionT[] = [];
Philipp Schrader8702b782023-04-15 17:33:37 -0700127 progressMessage: string = '';
Ravago Jones2813c032022-03-16 23:44:11 -0700128 errorMessage: string = '';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800129 autoPhase: boolean = true;
130 lastObject: ObjectType = null;
131
132 matchStartTimestamp: number = 0;
133
Philipp Schrader8702b782023-04-15 17:33:37 -0700134 teamSelectionIsValid = false;
135
136 constructor(private readonly matchListRequestor: MatchListRequestor) {}
137
Philipp Schrader75021f52023-04-09 21:14:13 -0700138 ngOnInit() {
139 // When the user navigated from the match list, we can skip the team
140 // selection. I.e. we trust that the user clicked the correct button.
141 this.section = this.skipTeamSelection ? 'Init' : 'Team Selection';
Philipp Schrader8702b782023-04-15 17:33:37 -0700142
143 if (this.section == 'Team Selection') {
144 this.fetchMatchList();
145 }
146 }
147
148 async fetchMatchList() {
149 this.progressMessage = 'Fetching match list. Please be patient.';
150 this.errorMessage = '';
151
152 try {
153 this.matchList = await this.matchListRequestor.fetchMatchList();
154 this.progressMessage = 'Successfully fetched match list.';
155 } catch (e) {
156 this.errorMessage = e;
157 this.progressMessage = '';
158 }
159 }
160
161 // This gets called when the user changes something on the Init screen.
162 // It makes sure that the user can't click "Next" until the information is
163 // valid.
164 updateTeamSelectionValidity(): void {
165 this.teamSelectionIsValid = this.matchIsInMatchList();
166 }
167
168 matchIsInMatchList(): boolean {
169 // If the user deletes the content of the teamNumber field, the value here
170 // is undefined. Guard against that.
171 if (this.teamNumber == null) {
172 return false;
173 }
174 const teamNumber = this.teamNumber.toString();
175
176 for (const match of this.matchList) {
177 if (
178 this.matchNumber == match.matchNumber() &&
179 this.setNumber == match.setNumber() &&
180 this.compLevel == match.compLevel() &&
181 (teamNumber === match.r1() ||
182 teamNumber === match.r2() ||
183 teamNumber === match.r3() ||
184 teamNumber === match.b1() ||
185 teamNumber === match.b2() ||
186 teamNumber === match.b3())
187 ) {
188 return true;
189 }
190 }
191 return false;
Philipp Schrader75021f52023-04-09 21:14:13 -0700192 }
193
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800194 addAction(action: ActionT): void {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800195 if (action.type == 'startMatchAction') {
196 // Unix nanosecond timestamp.
197 this.matchStartTimestamp = Date.now() * 1e6;
198 action.timestamp = 0;
199 } else {
200 // Unix nanosecond timestamp relative to match start.
201 action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp;
202 }
203
204 if (
205 action.type == 'pickupObjectAction' ||
206 action.type == 'placeObjectAction'
207 ) {
208 action.auto = this.autoPhase;
209 if (action.type == 'pickupObjectAction') {
210 this.lastObject = action.objectType;
211 } else if (action.type == 'placeObjectAction') {
212 action.objectType = this.lastObject;
213 }
214 }
215 this.actionList.push(action);
216 }
217
218 undoLastAction() {
219 if (this.actionList.length > 0) {
220 let lastAction = this.actionList.pop();
221 switch (lastAction?.type) {
222 case 'endAutoPhase':
223 this.autoPhase = true;
224 case 'pickupObjectAction':
225 this.section = 'Pickup';
226 break;
227 case 'placeObjectAction':
228 this.section = 'Place';
229 break;
230 case 'endMatchAction':
231 this.section = 'Pickup';
232 break;
Filip Kujawa9f56d0e2023-03-03 19:44:43 -0800233 case 'robotDeathAction':
234 // TODO(FILIP): Return user to the screen they
235 // clicked dead robot on. Pickup is fine for now but
236 // might cause confusion.
237 this.section = 'Pickup';
238 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800239 default:
240 break;
241 }
242 }
243 }
244
245 changeSectionTo(target: Section) {
Philipp Schrader8702b782023-04-15 17:33:37 -0700246 // Clear the messages since they won't be relevant in the next section.
247 this.errorMessage = '';
248 this.progressMessage = '';
249
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800250 this.section = target;
251 }
Philipp Schrader80587432022-03-05 15:41:22 -0800252
Ravago Jones2813c032022-03-16 23:44:11 -0700253 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700254
Ravago Jones2813c032022-03-16 23:44:11 -0700255 private scrollToTop() {
256 this.header.nativeElement.scrollIntoView();
257 }
258
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800259 async submitActions() {
James Kuszmauldac091f2022-03-22 09:35:06 -0700260 const builder = new Builder();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800261 const actionOffsets: number[] = [];
262
263 for (const action of this.actionList) {
264 let actionOffset: number | undefined;
265 console.log(action.type);
266
267 switch (action.type) {
268 case 'startMatchAction':
269 const startMatchActionOffset =
270 StartMatchAction.createStartMatchAction(builder, action.position);
271 actionOffset = Action.createAction(
272 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700273 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800274 ActionType.StartMatchAction,
275 startMatchActionOffset
276 );
277 break;
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700278 case 'mobilityAction':
279 const mobilityActionOffset = MobilityAction.createMobilityAction(
280 builder,
281 action.mobility
282 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800283 actionOffset = Action.createAction(
284 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700285 BigInt(action.timestamp || 0),
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700286 ActionType.MobilityAction,
287 mobilityActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800288 );
289 break;
Filip Kujawa4c286442023-03-03 10:41:22 -0800290 case 'autoBalanceAction':
291 const autoBalanceActionOffset =
292 AutoBalanceAction.createAutoBalanceAction(
293 builder,
294 action.docked,
Emily Markova63c63f62023-03-29 20:57:35 -0700295 action.engaged,
296 action.balanceAttempt
Filip Kujawa4c286442023-03-03 10:41:22 -0800297 );
298 actionOffset = Action.createAction(
299 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700300 BigInt(action.timestamp || 0),
Filip Kujawa4c286442023-03-03 10:41:22 -0800301 ActionType.AutoBalanceAction,
302 autoBalanceActionOffset
303 );
304 break;
305
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700306 case 'pickupObjectAction':
307 const pickupObjectActionOffset =
308 PickupObjectAction.createPickupObjectAction(
309 builder,
310 action.objectType,
311 action.auto || false
312 );
313 actionOffset = Action.createAction(
314 builder,
315 BigInt(action.timestamp || 0),
316 ActionType.PickupObjectAction,
317 pickupObjectActionOffset
318 );
319 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800320 case 'placeObjectAction':
321 const placeObjectActionOffset =
322 PlaceObjectAction.createPlaceObjectAction(
323 builder,
324 action.objectType,
325 action.scoreLevel,
326 action.auto || false
327 );
328 actionOffset = Action.createAction(
329 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700330 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800331 ActionType.PlaceObjectAction,
332 placeObjectActionOffset
333 );
334 break;
335
336 case 'robotDeathAction':
337 const robotDeathActionOffset =
338 RobotDeathAction.createRobotDeathAction(builder, action.robotOn);
339 actionOffset = Action.createAction(
340 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700341 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800342 ActionType.RobotDeathAction,
343 robotDeathActionOffset
344 );
345 break;
346
347 case 'endMatchAction':
348 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
349 builder,
350 action.docked,
Emily Markova63c63f62023-03-29 20:57:35 -0700351 action.engaged,
352 action.balanceAttempt
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800353 );
354 actionOffset = Action.createAction(
355 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700356 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800357 ActionType.EndMatchAction,
358 endMatchActionOffset
359 );
360 break;
361
362 case 'endAutoPhase':
363 // Not important action.
364 break;
365
366 default:
367 throw new Error(`Unknown action type`);
368 }
369
370 if (actionOffset !== undefined) {
371 actionOffsets.push(actionOffset);
372 }
373 }
Philipp Schradere859e6e2023-03-22 19:59:51 -0700374 const teamNumberFb = builder.createString(this.teamNumber.toString());
375 const compLevelFb = builder.createString(this.compLevel);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800376
377 const actionsVector = SubmitActions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700378 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800379 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700380 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800381 SubmitActions.startSubmitActions(builder);
Philipp Schradere859e6e2023-03-22 19:59:51 -0700382 SubmitActions.addTeamNumber(builder, teamNumberFb);
383 SubmitActions.addMatchNumber(builder, this.matchNumber);
384 SubmitActions.addSetNumber(builder, this.setNumber);
385 SubmitActions.addCompLevel(builder, compLevelFb);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800386 SubmitActions.addActionsList(builder, actionsVector);
387 builder.finish(SubmitActions.endSubmitActions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700388
389 const buffer = builder.asUint8Array();
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800390 const res = await fetch('/requests/submit/submit_actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700391 method: 'POST',
392 body: buffer,
393 });
Ravago Jones2813c032022-03-16 23:44:11 -0700394
395 if (res.ok) {
396 // We successfully submitted the data. Report success.
397 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800398 this.actionList = [];
Ravago Jones2813c032022-03-16 23:44:11 -0700399 } else {
400 const resBuffer = await res.arrayBuffer();
401 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700402 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700403
404 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700405 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800406 }
Ravago Jones2813c032022-03-16 23:44:11 -0700407 }
Philipp Schrader80587432022-03-05 15:41:22 -0800408}