blob: 80f7e6a7be9e85396c56c6ad950cf465a62af2ec [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
211 case 'placeObjectAction':
212 const placeObjectActionOffset =
213 PlaceObjectAction.createPlaceObjectAction(
214 builder,
215 action.objectType,
216 action.scoreLevel,
217 action.auto || false
218 );
219 actionOffset = Action.createAction(
220 builder,
221 action.timestamp || 0,
222 ActionType.PlaceObjectAction,
223 placeObjectActionOffset
224 );
225 break;
226
227 case 'robotDeathAction':
228 const robotDeathActionOffset =
229 RobotDeathAction.createRobotDeathAction(builder, action.robotOn);
230 actionOffset = Action.createAction(
231 builder,
232 action.timestamp || 0,
233 ActionType.RobotDeathAction,
234 robotDeathActionOffset
235 );
236 break;
237
238 case 'endMatchAction':
239 const endMatchActionOffset = EndMatchAction.createEndMatchAction(
240 builder,
241 action.docked,
242 action.engaged
243 );
244 actionOffset = Action.createAction(
245 builder,
246 action.timestamp || 0,
247 ActionType.EndMatchAction,
248 endMatchActionOffset
249 );
250 break;
251
252 case 'endAutoPhase':
253 // Not important action.
254 break;
255
256 default:
257 throw new Error(`Unknown action type`);
258 }
259
260 if (actionOffset !== undefined) {
261 actionOffsets.push(actionOffset);
262 }
263 }
264
265 const actionsVector = SubmitActions.createActionsListVector(
Philipp Schrader817cce32022-03-26 15:00:00 -0700266 builder,
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800267 actionOffsets
Philipp Schrader817cce32022-03-26 15:00:00 -0700268 );
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800269 SubmitActions.startSubmitActions(builder);
270 SubmitActions.addActionsList(builder, actionsVector);
271 builder.finish(SubmitActions.endSubmitActions(builder));
Ravago Jones2813c032022-03-16 23:44:11 -0700272
273 const buffer = builder.asUint8Array();
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800274 const res = await fetch('/requests/submit/actions', {
Philipp Schrader817cce32022-03-26 15:00:00 -0700275 method: 'POST',
276 body: buffer,
277 });
Ravago Jones2813c032022-03-16 23:44:11 -0700278
279 if (res.ok) {
280 // We successfully submitted the data. Report success.
281 this.section = 'Success';
Filip Kujawa0ef334c2023-02-20 19:42:45 -0800282 this.actionList = [];
Ravago Jones2813c032022-03-16 23:44:11 -0700283 } else {
284 const resBuffer = await res.arrayBuffer();
285 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
James Kuszmauldac091f2022-03-22 09:35:06 -0700286 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
Ravago Jones2813c032022-03-16 23:44:11 -0700287
288 const errorMessage = parsedResponse.errorMessage();
Philipp Schrader817cce32022-03-26 15:00:00 -0700289 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
Alex Perrybb3d2062022-03-05 18:14:33 -0800290 }
Ravago Jones2813c032022-03-16 23:44:11 -0700291 }
Philipp Schrader80587432022-03-05 15:41:22 -0800292}