blob: 290e2fb137e06be5d187a805029b0ca4718ec752 [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
Philipp Schradere1498852023-04-15 18:06:45 -0700132 preScouting: boolean = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800133 matchStartTimestamp: number = 0;
134
Philipp Schrader8702b782023-04-15 17:33:37 -0700135 teamSelectionIsValid = false;
136
137 constructor(private readonly matchListRequestor: MatchListRequestor) {}
138
Philipp Schrader75021f52023-04-09 21:14:13 -0700139 ngOnInit() {
140 // When the user navigated from the match list, we can skip the team
141 // selection. I.e. we trust that the user clicked the correct button.
142 this.section = this.skipTeamSelection ? 'Init' : 'Team Selection';
Philipp Schrader8702b782023-04-15 17:33:37 -0700143
144 if (this.section == 'Team Selection') {
145 this.fetchMatchList();
146 }
147 }
148
149 async fetchMatchList() {
150 this.progressMessage = 'Fetching match list. Please be patient.';
151 this.errorMessage = '';
152
153 try {
154 this.matchList = await this.matchListRequestor.fetchMatchList();
155 this.progressMessage = 'Successfully fetched match list.';
156 } catch (e) {
157 this.errorMessage = e;
158 this.progressMessage = '';
159 }
160 }
161
162 // This gets called when the user changes something on the Init screen.
163 // It makes sure that the user can't click "Next" until the information is
Philipp Schradere1498852023-04-15 18:06:45 -0700164 // valid, or this is for pre-scouting.
Philipp Schrader8702b782023-04-15 17:33:37 -0700165 updateTeamSelectionValidity(): void {
Philipp Schradere1498852023-04-15 18:06:45 -0700166 this.teamSelectionIsValid = this.preScouting || this.matchIsInMatchList();
Philipp Schrader8702b782023-04-15 17:33:37 -0700167 }
168
169 matchIsInMatchList(): boolean {
170 // If the user deletes the content of the teamNumber field, the value here
171 // is undefined. Guard against that.
172 if (this.teamNumber == null) {
173 return false;
174 }
175 const teamNumber = this.teamNumber.toString();
176
177 for (const match of this.matchList) {
178 if (
179 this.matchNumber == match.matchNumber() &&
180 this.setNumber == match.setNumber() &&
181 this.compLevel == match.compLevel() &&
182 (teamNumber === match.r1() ||
183 teamNumber === match.r2() ||
184 teamNumber === match.r3() ||
185 teamNumber === match.b1() ||
186 teamNumber === match.b2() ||
187 teamNumber === match.b3())
188 ) {
189 return true;
190 }
191 }
192 return false;
Philipp Schrader75021f52023-04-09 21:14:13 -0700193 }
194
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800195 addAction(action: ActionT): void {
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800196 if (action.type == 'startMatchAction') {
197 // Unix nanosecond timestamp.
198 this.matchStartTimestamp = Date.now() * 1e6;
199 action.timestamp = 0;
200 } else {
201 // Unix nanosecond timestamp relative to match start.
202 action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp;
203 }
204
205 if (
206 action.type == 'pickupObjectAction' ||
207 action.type == 'placeObjectAction'
208 ) {
209 action.auto = this.autoPhase;
210 if (action.type == 'pickupObjectAction') {
211 this.lastObject = action.objectType;
212 } else if (action.type == 'placeObjectAction') {
213 action.objectType = this.lastObject;
214 }
215 }
216 this.actionList.push(action);
217 }
218
219 undoLastAction() {
220 if (this.actionList.length > 0) {
221 let lastAction = this.actionList.pop();
222 switch (lastAction?.type) {
223 case 'endAutoPhase':
224 this.autoPhase = true;
225 case 'pickupObjectAction':
226 this.section = 'Pickup';
227 break;
228 case 'placeObjectAction':
229 this.section = 'Place';
230 break;
231 case 'endMatchAction':
232 this.section = 'Pickup';
233 break;
Filip Kujawa9f56d0e2023-03-03 19:44:43 -0800234 case 'robotDeathAction':
235 // TODO(FILIP): Return user to the screen they
236 // clicked dead robot on. Pickup is fine for now but
237 // might cause confusion.
238 this.section = 'Pickup';
239 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800240 default:
241 break;
242 }
243 }
244 }
245
246 changeSectionTo(target: Section) {
Philipp Schrader8702b782023-04-15 17:33:37 -0700247 // Clear the messages since they won't be relevant in the next section.
248 this.errorMessage = '';
249 this.progressMessage = '';
250
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800251 this.section = target;
252 }
Philipp Schrader80587432022-03-05 15:41:22 -0800253
Ravago Jones2813c032022-03-16 23:44:11 -0700254 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700255
Ravago Jones2813c032022-03-16 23:44:11 -0700256 private scrollToTop() {
257 this.header.nativeElement.scrollIntoView();
258 }
259
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800260 async submitActions() {
James Kuszmauldac091f2022-03-22 09:35:06 -0700261 const builder = new Builder();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800262 const actionOffsets: number[] = [];
263
264 for (const action of this.actionList) {
265 let actionOffset: number | undefined;
266 console.log(action.type);
267
268 switch (action.type) {
269 case 'startMatchAction':
270 const startMatchActionOffset =
271 StartMatchAction.createStartMatchAction(builder, action.position);
272 actionOffset = Action.createAction(
273 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700274 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800275 ActionType.StartMatchAction,
276 startMatchActionOffset
277 );
278 break;
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700279 case 'mobilityAction':
280 const mobilityActionOffset = MobilityAction.createMobilityAction(
281 builder,
282 action.mobility
283 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800284 actionOffset = Action.createAction(
285 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700286 BigInt(action.timestamp || 0),
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700287 ActionType.MobilityAction,
288 mobilityActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800289 );
290 break;
Filip Kujawa4c286442023-03-03 10:41:22 -0800291 case 'autoBalanceAction':
292 const autoBalanceActionOffset =
293 AutoBalanceAction.createAutoBalanceAction(
294 builder,
295 action.docked,
Emily Markova63c63f62023-03-29 20:57:35 -0700296 action.engaged,
297 action.balanceAttempt
Filip Kujawa4c286442023-03-03 10:41:22 -0800298 );
299 actionOffset = Action.createAction(
300 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700301 BigInt(action.timestamp || 0),
Filip Kujawa4c286442023-03-03 10:41:22 -0800302 ActionType.AutoBalanceAction,
303 autoBalanceActionOffset
304 );
305 break;
306
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700307 case 'pickupObjectAction':
308 const pickupObjectActionOffset =
309 PickupObjectAction.createPickupObjectAction(
310 builder,
311 action.objectType,
312 action.auto || false
313 );
314 actionOffset = Action.createAction(
315 builder,
316 BigInt(action.timestamp || 0),
317 ActionType.PickupObjectAction,
318 pickupObjectActionOffset
319 );
320 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800321 case 'placeObjectAction':
322 const placeObjectActionOffset =
323 PlaceObjectAction.createPlaceObjectAction(
324 builder,
325 action.objectType,
326 action.scoreLevel,
327 action.auto || false
328 );
329 actionOffset = Action.createAction(
330 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700331 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800332 ActionType.PlaceObjectAction,
333 placeObjectActionOffset
334 );
335 break;
336
337 case 'robotDeathAction':
338 const robotDeathActionOffset =
339 RobotDeathAction.createRobotDeathAction(builder, action.robotOn);
340 actionOffset = Action.createAction(
341 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700342 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800343 ActionType.RobotDeathAction,
344 robotDeathActionOffset
345 );
346 break;
347
348 case 'endMatchAction':
349 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
350 builder,
351 action.docked,
Emily Markova63c63f62023-03-29 20:57:35 -0700352 action.engaged,
353 action.balanceAttempt
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800354 );
355 actionOffset = Action.createAction(
356 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700357 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800358 ActionType.EndMatchAction,
359 endMatchActionOffset
360 );
361 break;
362
363 case 'endAutoPhase':
364 // Not important action.
365 break;
366
367 default:
368 throw new Error(`Unknown action type`);
369 }
370
371 if (actionOffset !== undefined) {
372 actionOffsets.push(actionOffset);
373 }
374 }
Philipp Schradere859e6e2023-03-22 19:59:51 -0700375 const teamNumberFb = builder.createString(this.teamNumber.toString());
376 const compLevelFb = builder.createString(this.compLevel);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800377
378 const actionsVector = SubmitActions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700379 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800380 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700381 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800382 SubmitActions.startSubmitActions(builder);
Philipp Schradere859e6e2023-03-22 19:59:51 -0700383 SubmitActions.addTeamNumber(builder, teamNumberFb);
384 SubmitActions.addMatchNumber(builder, this.matchNumber);
385 SubmitActions.addSetNumber(builder, this.setNumber);
386 SubmitActions.addCompLevel(builder, compLevelFb);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800387 SubmitActions.addActionsList(builder, actionsVector);
Philipp Schradere1498852023-04-15 18:06:45 -0700388 SubmitActions.addPreScouting(builder, this.preScouting);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800389 builder.finish(SubmitActions.endSubmitActions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700390
391 const buffer = builder.asUint8Array();
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800392 const res = await fetch('/requests/submit/submit_actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700393 method: 'POST',
394 body: buffer,
395 });
Ravago Jones2813c032022-03-16 23:44:11 -0700396
397 if (res.ok) {
398 // We successfully submitted the data. Report success.
399 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800400 this.actionList = [];
Ravago Jones2813c032022-03-16 23:44:11 -0700401 } else {
402 const resBuffer = await res.arrayBuffer();
403 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700404 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700405
406 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700407 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800408 }
Ravago Jones2813c032022-03-16 23:44:11 -0700409 }
Philipp Schrader80587432022-03-05 15:41:22 -0800410}