blob: 5a93251b537876b56569b335b5255a2b3d5eecb4 [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;
Emily Markovae68b7632023-12-30 14:17:55 -0800118 @Input() teamNumber: string = '1';
Philipp Schrader30b4a682022-04-16 14:36:17 -0700119 @Input() setNumber: number = 1;
Philipp Schrader8aeb14f2022-04-08 21:23:18 -0700120 @Input() compLevel: CompLevel = 'qm';
Philipp Schrader75021f52023-04-09 21:14:13 -0700121 @Input() skipTeamSelection = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800122
Philipp Schrader8702b782023-04-15 17:33:37 -0700123 matchList: Match[] = [];
124
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800125 actionList: ActionT[] = [];
Philipp Schrader8702b782023-04-15 17:33:37 -0700126 progressMessage: string = '';
Ravago Jones2813c032022-03-16 23:44:11 -0700127 errorMessage: string = '';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800128 autoPhase: boolean = true;
Filip Kujawab73e94c2023-04-19 09:33:14 -0700129 mobilityCompleted: boolean = false;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800130 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 }
Emily Markovae68b7632023-12-30 14:17:55 -0800175 const teamNumber = this.teamNumber;
Philipp Schrader8702b782023-04-15 17:33:37 -0700176
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
Filip Kujawab73e94c2023-04-19 09:33:14 -0700205 if (action.type == 'mobilityAction') {
206 this.mobilityCompleted = true;
207 }
208
Filip Kujawae4402712023-04-19 09:45:10 -0700209 if (action.type == 'autoBalanceAction') {
210 // Timestamp is a unique index in the database so
211 // adding one makes sure it dosen't overlap with the
212 // start teleop action that is added at the same time.
213 action.timestamp += 1;
214 }
215
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800216 if (
217 action.type == 'pickupObjectAction' ||
218 action.type == 'placeObjectAction'
219 ) {
220 action.auto = this.autoPhase;
221 if (action.type == 'pickupObjectAction') {
222 this.lastObject = action.objectType;
223 } else if (action.type == 'placeObjectAction') {
224 action.objectType = this.lastObject;
225 }
226 }
227 this.actionList.push(action);
228 }
229
230 undoLastAction() {
231 if (this.actionList.length > 0) {
232 let lastAction = this.actionList.pop();
233 switch (lastAction?.type) {
234 case 'endAutoPhase':
235 this.autoPhase = true;
236 case 'pickupObjectAction':
237 this.section = 'Pickup';
238 break;
239 case 'placeObjectAction':
240 this.section = 'Place';
241 break;
242 case 'endMatchAction':
243 this.section = 'Pickup';
244 break;
Filip Kujawa9f56d0e2023-03-03 19:44:43 -0800245 case 'robotDeathAction':
246 // TODO(FILIP): Return user to the screen they
247 // clicked dead robot on. Pickup is fine for now but
248 // might cause confusion.
249 this.section = 'Pickup';
250 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800251 default:
252 break;
253 }
254 }
255 }
256
Emily Markovaf4b06a22023-05-10 17:44:09 -0700257 stringifyObjectType(objectType: ObjectType): String {
258 return ObjectType[objectType];
259 }
260
261 stringifyScoreLevel(scoreLevel: ScoreLevel): String {
262 return ScoreLevel[scoreLevel];
263 }
264
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800265 changeSectionTo(target: Section) {
Philipp Schrader8702b782023-04-15 17:33:37 -0700266 // Clear the messages since they won't be relevant in the next section.
267 this.errorMessage = '';
268 this.progressMessage = '';
269
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800270 this.section = target;
271 }
Philipp Schrader80587432022-03-05 15:41:22 -0800272
Ravago Jones2813c032022-03-16 23:44:11 -0700273 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700274
Ravago Jones2813c032022-03-16 23:44:11 -0700275 private scrollToTop() {
276 this.header.nativeElement.scrollIntoView();
277 }
278
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800279 async submitActions() {
James Kuszmauldac091f2022-03-22 09:35:06 -0700280 const builder = new Builder();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800281 const actionOffsets: number[] = [];
282
283 for (const action of this.actionList) {
284 let actionOffset: number | undefined;
285 console.log(action.type);
286
287 switch (action.type) {
288 case 'startMatchAction':
289 const startMatchActionOffset =
290 StartMatchAction.createStartMatchAction(builder, action.position);
291 actionOffset = Action.createAction(
292 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700293 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800294 ActionType.StartMatchAction,
295 startMatchActionOffset
296 );
297 break;
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700298 case 'mobilityAction':
299 const mobilityActionOffset = MobilityAction.createMobilityAction(
300 builder,
301 action.mobility
302 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800303 actionOffset = Action.createAction(
304 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700305 BigInt(action.timestamp || 0),
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700306 ActionType.MobilityAction,
307 mobilityActionOffset
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800308 );
309 break;
Filip Kujawa4c286442023-03-03 10:41:22 -0800310 case 'autoBalanceAction':
311 const autoBalanceActionOffset =
312 AutoBalanceAction.createAutoBalanceAction(
313 builder,
314 action.docked,
Emily Markova63c63f62023-03-29 20:57:35 -0700315 action.engaged,
316 action.balanceAttempt
Filip Kujawa4c286442023-03-03 10:41:22 -0800317 );
318 actionOffset = Action.createAction(
319 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700320 BigInt(action.timestamp || 0),
Filip Kujawa4c286442023-03-03 10:41:22 -0800321 ActionType.AutoBalanceAction,
322 autoBalanceActionOffset
323 );
324 break;
325
Filip Kujawa0b4b1e52023-04-15 14:05:40 -0700326 case 'pickupObjectAction':
327 const pickupObjectActionOffset =
328 PickupObjectAction.createPickupObjectAction(
329 builder,
330 action.objectType,
331 action.auto || false
332 );
333 actionOffset = Action.createAction(
334 builder,
335 BigInt(action.timestamp || 0),
336 ActionType.PickupObjectAction,
337 pickupObjectActionOffset
338 );
339 break;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800340 case 'placeObjectAction':
341 const placeObjectActionOffset =
342 PlaceObjectAction.createPlaceObjectAction(
343 builder,
344 action.objectType,
345 action.scoreLevel,
346 action.auto || false
347 );
348 actionOffset = Action.createAction(
349 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700350 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800351 ActionType.PlaceObjectAction,
352 placeObjectActionOffset
353 );
354 break;
355
356 case 'robotDeathAction':
357 const robotDeathActionOffset =
358 RobotDeathAction.createRobotDeathAction(builder, action.robotOn);
359 actionOffset = Action.createAction(
360 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700361 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800362 ActionType.RobotDeathAction,
363 robotDeathActionOffset
364 );
365 break;
366
367 case 'endMatchAction':
368 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
369 builder,
370 action.docked,
Emily Markova63c63f62023-03-29 20:57:35 -0700371 action.engaged,
372 action.balanceAttempt
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800373 );
374 actionOffset = Action.createAction(
375 builder,
Philipp Schrader8c878a22023-03-20 22:36:38 -0700376 BigInt(action.timestamp || 0),
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800377 ActionType.EndMatchAction,
378 endMatchActionOffset
379 );
380 break;
381
382 case 'endAutoPhase':
383 // Not important action.
384 break;
385
386 default:
387 throw new Error(`Unknown action type`);
388 }
389
390 if (actionOffset !== undefined) {
391 actionOffsets.push(actionOffset);
392 }
393 }
Emily Markovae68b7632023-12-30 14:17:55 -0800394 const teamNumberFb = builder.createString(this.teamNumber);
Philipp Schradere859e6e2023-03-22 19:59:51 -0700395 const compLevelFb = builder.createString(this.compLevel);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800396
397 const actionsVector = SubmitActions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700398 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800399 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700400 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800401 SubmitActions.startSubmitActions(builder);
Philipp Schradere859e6e2023-03-22 19:59:51 -0700402 SubmitActions.addTeamNumber(builder, teamNumberFb);
403 SubmitActions.addMatchNumber(builder, this.matchNumber);
404 SubmitActions.addSetNumber(builder, this.setNumber);
405 SubmitActions.addCompLevel(builder, compLevelFb);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800406 SubmitActions.addActionsList(builder, actionsVector);
Philipp Schradere1498852023-04-15 18:06:45 -0700407 SubmitActions.addPreScouting(builder, this.preScouting);
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800408 builder.finish(SubmitActions.endSubmitActions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700409
410 const buffer = builder.asUint8Array();
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800411 const res = await fetch('/requests/submit/submit_actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700412 method: 'POST',
413 body: buffer,
414 });
Ravago Jones2813c032022-03-16 23:44:11 -0700415
416 if (res.ok) {
417 // We successfully submitted the data. Report success.
418 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800419 this.actionList = [];
Ravago Jones2813c032022-03-16 23:44:11 -0700420 } else {
421 const resBuffer = await res.arrayBuffer();
422 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700423 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700424
425 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700426 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800427 }
Ravago Jones2813c032022-03-16 23:44:11 -0700428 }
Philipp Schrader80587432022-03-05 15:41:22 -0800429}