blob: 6ecc95dd4d77d374711d658af022d8ec7cf19e74 [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 Kujawa4413a592023-03-01 10:54:34 -080018 AutoBalanceAction,
Filip Kujawa0ef334c2023-02-20 19:42:45 -080019 PickupObjectAction,
20 PlaceObjectAction,
21 RobotDeathAction,
22 EndMatchAction,
23 ActionType,
24 Action,
25} from '../../webserver/requests/messages/submit_actions_generated';
Philipp Schrader8b8ed672022-03-05 18:08:50 -080026
Philipp Schrader817cce32022-03-26 15:00:00 -070027type Section =
28 | 'Team Selection'
Filip Kujawa0ef334c2023-02-20 19:42:45 -080029 | 'Init'
30 | 'Pickup'
31 | 'Place'
32 | 'Endgame'
33 | 'Dead'
Philipp Schrader817cce32022-03-26 15:00:00 -070034 | 'Review and Submit'
35 | 'Success';
Philipp Schrader80587432022-03-05 15:41:22 -080036
Philipp Schrader8aeb14f2022-04-08 21:23:18 -070037// TODO(phil): Deduplicate with match_list.component.ts.
38const COMP_LEVELS = ['qm', 'ef', 'qf', 'sf', 'f'] as const;
39type CompLevel = typeof COMP_LEVELS[number];
40
41// TODO(phil): Deduplicate with match_list.component.ts.
42const COMP_LEVEL_LABELS: Record<CompLevel, string> = {
43 qm: 'Qualifications',
44 ef: 'Eighth Finals',
45 qf: 'Quarter Finals',
46 sf: 'Semi Finals',
47 f: 'Finals',
48};
49
Filip Kujawa0ef334c2023-02-20 19:42:45 -080050type ActionT =
51 | {
52 type: 'startMatchAction';
53 timestamp?: number;
54 position: number;
55 }
56 | {
Filip Kujawa4413a592023-03-01 10:54:34 -080057 type: 'autoBalanceAction';
58 timestamp?: number;
59 docked: boolean;
60 engaged: boolean;
61 }
62 | {
Filip Kujawa0ef334c2023-02-20 19:42:45 -080063 type: 'pickupObjectAction';
64 timestamp?: number;
65 objectType: ObjectType;
66 auto?: boolean;
67 }
68 | {
69 type: 'placeObjectAction';
70 timestamp?: number;
71 objectType?: ObjectType;
72 scoreLevel: ScoreLevel;
73 auto?: boolean;
74 }
75 | {
76 type: 'robotDeathAction';
77 timestamp?: number;
78 robotOn: boolean;
79 }
80 | {
81 type: 'endMatchAction';
82 docked: boolean;
83 engaged: boolean;
84 timestamp?: number;
85 }
86 | {
87 // This is not a action that is submitted,
88 // It is used for undoing purposes.
89 type: 'endAutoPhase';
90 timestamp?: number;
91 };
emilym38d08ba2022-10-22 15:25:01 -070092
Philipp Schrader23993e82022-03-18 18:54:00 -070093@Component({
94 selector: 'app-entry',
95 templateUrl: './entry.ng.html',
Philipp Schrader175a93c2023-02-19 13:13:40 -080096 styleUrls: ['../app/common.css', './entry.component.css'],
Philipp Schrader23993e82022-03-18 18:54:00 -070097})
98export class EntryComponent {
Philipp Schrader36df73a2022-03-17 23:27:24 -070099 // Re-export the type here so that we can use it in the `[value]` attribute
100 // of radio buttons.
Philipp Schrader8aeb14f2022-04-08 21:23:18 -0700101 readonly COMP_LEVELS = COMP_LEVELS;
102 readonly COMP_LEVEL_LABELS = COMP_LEVEL_LABELS;
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800103 readonly ObjectType = ObjectType;
104 readonly ScoreLevel = ScoreLevel;
Philipp Schrader36df73a2022-03-17 23:27:24 -0700105
Ravago Jones2813c032022-03-16 23:44:11 -0700106 section: Section = 'Team Selection';
107 @Output() switchTabsEvent = new EventEmitter<string>();
108 @Input() matchNumber: number = 1;
109 @Input() teamNumber: number = 1;
Philipp Schrader30b4a682022-04-16 14:36:17 -0700110 @Input() setNumber: number = 1;
Philipp Schrader8aeb14f2022-04-08 21:23:18 -0700111 @Input() compLevel: CompLevel = 'qm';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800112
113 actionList: ActionT[] = [];
Ravago Jones2813c032022-03-16 23:44:11 -0700114 errorMessage: string = '';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800115 autoPhase: boolean = true;
116 lastObject: ObjectType = null;
117
118 matchStartTimestamp: number = 0;
119
120 addAction(action: ActionT): void {
121 action.timestamp = Math.floor(Date.now() / 1000);
122 if (action.type == 'startMatchAction') {
123 // Unix nanosecond timestamp.
124 this.matchStartTimestamp = Date.now() * 1e6;
125 action.timestamp = 0;
126 } else {
127 // Unix nanosecond timestamp relative to match start.
128 action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp;
129 }
130
131 if (
132 action.type == 'pickupObjectAction' ||
133 action.type == 'placeObjectAction'
134 ) {
135 action.auto = this.autoPhase;
136 if (action.type == 'pickupObjectAction') {
137 this.lastObject = action.objectType;
138 } else if (action.type == 'placeObjectAction') {
139 action.objectType = this.lastObject;
140 }
141 }
142 this.actionList.push(action);
143 }
144
145 undoLastAction() {
146 if (this.actionList.length > 0) {
147 let lastAction = this.actionList.pop();
148 switch (lastAction?.type) {
149 case 'endAutoPhase':
150 this.autoPhase = true;
151 case 'pickupObjectAction':
152 this.section = 'Pickup';
153 break;
154 case 'placeObjectAction':
155 this.section = 'Place';
156 break;
157 case 'endMatchAction':
158 this.section = 'Pickup';
159 break;
160 default:
161 break;
162 }
163 }
164 }
165
166 changeSectionTo(target: Section) {
167 this.section = target;
168 }
Philipp Schrader80587432022-03-05 15:41:22 -0800169
Ravago Jones2813c032022-03-16 23:44:11 -0700170 @ViewChild('header') header: ElementRef;
Philipp Schrader6b2e9502022-03-15 23:42:56 -0700171
Ravago Jones2813c032022-03-16 23:44:11 -0700172 private scrollToTop() {
173 this.header.nativeElement.scrollIntoView();
174 }
175
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800176 async submitActions() {
James Kuszmauldac091f2022-03-22 09:35:06 -0700177 const builder = new Builder();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800178 const actionOffsets: number[] = [];
179
180 for (const action of this.actionList) {
181 let actionOffset: number | undefined;
182 console.log(action.type);
183
184 switch (action.type) {
185 case 'startMatchAction':
186 const startMatchActionOffset =
187 StartMatchAction.createStartMatchAction(builder, action.position);
188 actionOffset = Action.createAction(
189 builder,
190 action.timestamp || 0,
191 ActionType.StartMatchAction,
192 startMatchActionOffset
193 );
194 break;
195
196 case 'pickupObjectAction':
197 const pickupObjectActionOffset =
198 PickupObjectAction.createPickupObjectAction(
199 builder,
200 action.objectType,
201 action.auto || false
202 );
203 actionOffset = Action.createAction(
204 builder,
205 action.timestamp || 0,
206 ActionType.PickupObjectAction,
207 pickupObjectActionOffset
208 );
209 break;
210
Filip Kujawa4c286442023-03-03 10:41:22 -0800211 case 'autoBalanceAction':
212 const autoBalanceActionOffset =
213 AutoBalanceAction.createAutoBalanceAction(
214 builder,
215 action.docked,
216 action.engaged
217 );
218 actionOffset = Action.createAction(
219 builder,
220 action.timestamp || 0,
221 ActionType.AutoBalanceAction,
222 autoBalanceActionOffset
223 );
224 break;
225
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800226 case 'placeObjectAction':
227 const placeObjectActionOffset =
228 PlaceObjectAction.createPlaceObjectAction(
229 builder,
230 action.objectType,
231 action.scoreLevel,
232 action.auto || false
233 );
234 actionOffset = Action.createAction(
235 builder,
236 action.timestamp || 0,
237 ActionType.PlaceObjectAction,
238 placeObjectActionOffset
239 );
240 break;
241
242 case 'robotDeathAction':
243 const robotDeathActionOffset =
244 RobotDeathAction.createRobotDeathAction(builder, action.robotOn);
245 actionOffset = Action.createAction(
246 builder,
247 action.timestamp || 0,
248 ActionType.RobotDeathAction,
249 robotDeathActionOffset
250 );
251 break;
252
253 case 'endMatchAction':
254 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
255 builder,
256 action.docked,
257 action.engaged
258 );
259 actionOffset = Action.createAction(
260 builder,
261 action.timestamp || 0,
262 ActionType.EndMatchAction,
263 endMatchActionOffset
264 );
265 break;
266
267 case 'endAutoPhase':
268 // Not important action.
269 break;
270
271 default:
272 throw new Error(`Unknown action type`);
273 }
274
275 if (actionOffset !== undefined) {
276 actionOffsets.push(actionOffset);
277 }
278 }
279
280 const actionsVector = SubmitActions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700281 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800282 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700283 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800284 SubmitActions.startSubmitActions(builder);
285 SubmitActions.addActionsList(builder, actionsVector);
286 builder.finish(SubmitActions.endSubmitActions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700287
288 const buffer = builder.asUint8Array();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800289 const res = await fetch('/requests/submit/actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700290 method: 'POST',
291 body: buffer,
292 });
Ravago Jones2813c032022-03-16 23:44:11 -0700293
294 if (res.ok) {
295 // We successfully submitted the data. Report success.
296 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800297 this.actionList = [];
Ravago Jones2813c032022-03-16 23:44:11 -0700298 } else {
299 const resBuffer = await res.arrayBuffer();
300 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700301 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700302
303 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700304 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800305 }
Ravago Jones2813c032022-03-16 23:44:11 -0700306 }
Philipp Schrader80587432022-03-05 15:41:22 -0800307}