blob: 50d0cd9b33088a11334769542f7bb590e05b23cc [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;
Filip Kujawab73e94c2023-04-19 09:33:14 -0700130 mobilityCompleted: boolean = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800131 lastObject: ObjectType = null;
132
Philipp Schradere1498852023-04-15 18:06:45 -0700133 preScouting: boolean = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800134 matchStartTimestamp: number = 0;
135
Philipp Schrader8702b782023-04-15 17:33:37 -0700136 teamSelectionIsValid = false;
137
138 constructor(private readonly matchListRequestor: MatchListRequestor) {}
139
Philipp Schrader75021f52023-04-09 21:14:13 -0700140 ngOnInit() {
141 // When the user navigated from the match list, we can skip the team
142 // selection. I.e. we trust that the user clicked the correct button.
143 this.section = this.skipTeamSelection ? 'Init' : 'Team Selection';
Philipp Schrader8702b782023-04-15 17:33:37 -0700144
145 if (this.section == 'Team Selection') {
146 this.fetchMatchList();
147 }
148 }
149
150 async fetchMatchList() {
151 this.progressMessage = 'Fetching match list. Please be patient.';
152 this.errorMessage = '';
153
154 try {
155 this.matchList = await this.matchListRequestor.fetchMatchList();
156 this.progressMessage = 'Successfully fetched match list.';
157 } catch (e) {
158 this.errorMessage = e;
159 this.progressMessage = '';
160 }
161 }
162
163 // This gets called when the user changes something on the Init screen.
164 // It makes sure that the user can't click "Next" until the information is
Philipp Schradere1498852023-04-15 18:06:45 -0700165 // valid, or this is for pre-scouting.
Philipp Schrader8702b782023-04-15 17:33:37 -0700166 updateTeamSelectionValidity(): void {
Philipp Schradere1498852023-04-15 18:06:45 -0700167 this.teamSelectionIsValid = this.preScouting || this.matchIsInMatchList();
Philipp Schrader8702b782023-04-15 17:33:37 -0700168 }
169
170 matchIsInMatchList(): boolean {
171 // If the user deletes the content of the teamNumber field, the value here
172 // is undefined. Guard against that.
173 if (this.teamNumber == null) {
174 return false;
175 }
176 const teamNumber = this.teamNumber.toString();
177
178 for (const match of this.matchList) {
179 if (
180 this.matchNumber == match.matchNumber() &&
181 this.setNumber == match.setNumber() &&
182 this.compLevel == match.compLevel() &&
183 (teamNumber === match.r1() ||
184 teamNumber === match.r2() ||
185 teamNumber === match.r3() ||
186 teamNumber === match.b1() ||
187 teamNumber === match.b2() ||
188 teamNumber === match.b3())
189 ) {
190 return true;
191 }
192 }
193 return false;
Philipp Schrader75021f52023-04-09 21:14:13 -0700194 }
195
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800196 addAction(action: ActionT): void {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800197 if (action.type == 'startMatchAction') {
198 // Unix nanosecond timestamp.
199 this.matchStartTimestamp = Date.now() * 1e6;
200 action.timestamp = 0;
201 } else {
202 // Unix nanosecond timestamp relative to match start.
203 action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp;
204 }
205
Filip Kujawab73e94c2023-04-19 09:33:14 -0700206 if (action.type == 'mobilityAction') {
207 this.mobilityCompleted = true;
208 }
209
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800210 if (
211 action.type == 'pickupObjectAction' ||
212 action.type == 'placeObjectAction'
213 ) {
214 action.auto = this.autoPhase;
215 if (action.type == 'pickupObjectAction') {
216 this.lastObject = action.objectType;
217 } else if (action.type == 'placeObjectAction') {
218 action.objectType = this.lastObject;
219 }
220 }
221 this.actionList.push(action);
222 }
223
224 undoLastAction() {
225 if (this.actionList.length > 0) {
226 let lastAction = this.actionList.pop();
227 switch (lastAction?.type) {
228 case 'endAutoPhase':
229 this.autoPhase = true;
230 case 'pickupObjectAction':
231 this.section = 'Pickup';
232 break;
233 case 'placeObjectAction':
234 this.section = 'Place';
235 break;
236 case 'endMatchAction':
237 this.section = 'Pickup';
238 break;
Filip Kujawa9f56d0e2023-03-03 19:44:43 -0800239 case 'robotDeathAction':
240 // TODO(FILIP): Return user to the screen they
241 // clicked dead robot on. Pickup is fine for now but
242 // might cause confusion.
243 this.section = 'Pickup';
244 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800245 default:
246 break;
247 }
248 }
249 }
250
251 changeSectionTo(target: Section) {
Philipp Schrader8702b782023-04-15 17:33:37 -0700252 // Clear the messages since they won't be relevant in the next section.
253 this.errorMessage = '';
254 this.progressMessage = '';
255
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800256 this.section = target;
257 }
Philipp Schrader80587432022-03-05 15:41:22 -0800258
Ravago Jones2813c032022-03-16 23:44:11 -0700259 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700260
Ravago Jones2813c032022-03-16 23:44:11 -0700261 private scrollToTop() {
262 this.header.nativeElement.scrollIntoView();
263 }
264
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800265 async submitActions() {
James Kuszmauldac091f2022-03-22 09:35:06 -0700266 const builder = new Builder();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800267 const actionOffsets: number[] = [];
268
269 for (const action of this.actionList) {
270 let actionOffset: number | undefined;
271 console.log(action.type);
272
273 switch (action.type) {
274 case 'startMatchAction':
275 const startMatchActionOffset =
276 StartMatchAction.createStartMatchAction(builder, action.position);
277 actionOffset = Action.createAction(
278 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700279 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800280 ActionType.StartMatchAction,
281 startMatchActionOffset
282 );
283 break;
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700284 case 'mobilityAction':
285 const mobilityActionOffset = MobilityAction.createMobilityAction(
286 builder,
287 action.mobility
288 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800289 actionOffset = Action.createAction(
290 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700291 BigInt(action.timestamp || 0),
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700292 ActionType.MobilityAction,
293 mobilityActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800294 );
295 break;
Filip Kujawa4c286442023-03-03 10:41:22 -0800296 case 'autoBalanceAction':
297 const autoBalanceActionOffset =
298 AutoBalanceAction.createAutoBalanceAction(
299 builder,
300 action.docked,
Emily Markova63c63f62023-03-29 20:57:35 -0700301 action.engaged,
302 action.balanceAttempt
Filip Kujawa4c286442023-03-03 10:41:22 -0800303 );
304 actionOffset = Action.createAction(
305 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700306 BigInt(action.timestamp || 0),
Filip Kujawa4c286442023-03-03 10:41:22 -0800307 ActionType.AutoBalanceAction,
308 autoBalanceActionOffset
309 );
310 break;
311
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700312 case 'pickupObjectAction':
313 const pickupObjectActionOffset =
314 PickupObjectAction.createPickupObjectAction(
315 builder,
316 action.objectType,
317 action.auto || false
318 );
319 actionOffset = Action.createAction(
320 builder,
321 BigInt(action.timestamp || 0),
322 ActionType.PickupObjectAction,
323 pickupObjectActionOffset
324 );
325 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800326 case 'placeObjectAction':
327 const placeObjectActionOffset =
328 PlaceObjectAction.createPlaceObjectAction(
329 builder,
330 action.objectType,
331 action.scoreLevel,
332 action.auto || false
333 );
334 actionOffset = Action.createAction(
335 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700336 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800337 ActionType.PlaceObjectAction,
338 placeObjectActionOffset
339 );
340 break;
341
342 case 'robotDeathAction':
343 const robotDeathActionOffset =
344 RobotDeathAction.createRobotDeathAction(builder, action.robotOn);
345 actionOffset = Action.createAction(
346 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700347 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800348 ActionType.RobotDeathAction,
349 robotDeathActionOffset
350 );
351 break;
352
353 case 'endMatchAction':
354 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
355 builder,
356 action.docked,
Emily Markova63c63f62023-03-29 20:57:35 -0700357 action.engaged,
358 action.balanceAttempt
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800359 );
360 actionOffset = Action.createAction(
361 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700362 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800363 ActionType.EndMatchAction,
364 endMatchActionOffset
365 );
366 break;
367
368 case 'endAutoPhase':
369 // Not important action.
370 break;
371
372 default:
373 throw new Error(`Unknown action type`);
374 }
375
376 if (actionOffset !== undefined) {
377 actionOffsets.push(actionOffset);
378 }
379 }
Philipp Schradere859e6e2023-03-22 19:59:51 -0700380 const teamNumberFb = builder.createString(this.teamNumber.toString());
381 const compLevelFb = builder.createString(this.compLevel);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800382
383 const actionsVector = SubmitActions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700384 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800385 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700386 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800387 SubmitActions.startSubmitActions(builder);
Philipp Schradere859e6e2023-03-22 19:59:51 -0700388 SubmitActions.addTeamNumber(builder, teamNumberFb);
389 SubmitActions.addMatchNumber(builder, this.matchNumber);
390 SubmitActions.addSetNumber(builder, this.setNumber);
391 SubmitActions.addCompLevel(builder, compLevelFb);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800392 SubmitActions.addActionsList(builder, actionsVector);
Philipp Schradere1498852023-04-15 18:06:45 -0700393 SubmitActions.addPreScouting(builder, this.preScouting);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800394 builder.finish(SubmitActions.endSubmitActions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700395
396 const buffer = builder.asUint8Array();
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800397 const res = await fetch('/requests/submit/submit_actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700398 method: 'POST',
399 body: buffer,
400 });
Ravago Jones2813c032022-03-16 23:44:11 -0700401
402 if (res.ok) {
403 // We successfully submitted the data. Report success.
404 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800405 this.actionList = [];
Ravago Jones2813c032022-03-16 23:44:11 -0700406 } else {
407 const resBuffer = await res.arrayBuffer();
408 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700409 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700410
411 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700412 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800413 }
Ravago Jones2813c032022-03-16 23:44:11 -0700414 }
Philipp Schrader80587432022-03-05 15:41:22 -0800415}