Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 1 | import { |
| 2 | Component, |
| 3 | ElementRef, |
| 4 | EventEmitter, |
| 5 | Input, |
| 6 | OnInit, |
| 7 | Output, |
| 8 | ViewChild, |
| 9 | } from '@angular/core'; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 10 | import {FormsModule} from '@angular/forms'; |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 11 | import {Builder, ByteBuffer} from 'flatbuffers'; |
Philipp Schrader | d7efa2b | 2023-02-17 21:15:13 -0800 | [diff] [blame] | 12 | import {ErrorResponse} from '../../webserver/requests/messages/error_response_generated'; |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 13 | import { |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 14 | ObjectType, |
| 15 | ScoreLevel, |
| 16 | SubmitActions, |
| 17 | StartMatchAction, |
Filip Kujawa | 4413a59 | 2023-03-01 10:54:34 -0800 | [diff] [blame] | 18 | AutoBalanceAction, |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 19 | PickupObjectAction, |
| 20 | PlaceObjectAction, |
| 21 | RobotDeathAction, |
| 22 | EndMatchAction, |
| 23 | ActionType, |
| 24 | Action, |
| 25 | } from '../../webserver/requests/messages/submit_actions_generated'; |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 26 | |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 27 | type Section = |
| 28 | | 'Team Selection' |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 29 | | 'Init' |
| 30 | | 'Pickup' |
| 31 | | 'Place' |
| 32 | | 'Endgame' |
| 33 | | 'Dead' |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 34 | | 'Review and Submit' |
| 35 | | 'Success'; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 36 | |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 37 | // TODO(phil): Deduplicate with match_list.component.ts. |
| 38 | const COMP_LEVELS = ['qm', 'ef', 'qf', 'sf', 'f'] as const; |
| 39 | type CompLevel = typeof COMP_LEVELS[number]; |
| 40 | |
| 41 | // TODO(phil): Deduplicate with match_list.component.ts. |
| 42 | const 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 Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 50 | type ActionT = |
| 51 | | { |
| 52 | type: 'startMatchAction'; |
| 53 | timestamp?: number; |
| 54 | position: number; |
| 55 | } |
| 56 | | { |
Filip Kujawa | 4413a59 | 2023-03-01 10:54:34 -0800 | [diff] [blame] | 57 | type: 'autoBalanceAction'; |
| 58 | timestamp?: number; |
| 59 | docked: boolean; |
| 60 | engaged: boolean; |
Emily Markova | 63c63f6 | 2023-03-29 20:57:35 -0700 | [diff] [blame] | 61 | balanceAttempt: boolean; |
Filip Kujawa | 4413a59 | 2023-03-01 10:54:34 -0800 | [diff] [blame] | 62 | } |
| 63 | | { |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 64 | type: 'pickupObjectAction'; |
| 65 | timestamp?: number; |
| 66 | objectType: ObjectType; |
| 67 | auto?: boolean; |
| 68 | } |
| 69 | | { |
| 70 | type: 'placeObjectAction'; |
| 71 | timestamp?: number; |
| 72 | objectType?: ObjectType; |
| 73 | scoreLevel: ScoreLevel; |
| 74 | auto?: boolean; |
| 75 | } |
| 76 | | { |
| 77 | type: 'robotDeathAction'; |
| 78 | timestamp?: number; |
| 79 | robotOn: boolean; |
| 80 | } |
| 81 | | { |
| 82 | type: 'endMatchAction'; |
| 83 | docked: boolean; |
| 84 | engaged: boolean; |
Emily Markova | 63c63f6 | 2023-03-29 20:57:35 -0700 | [diff] [blame] | 85 | balanceAttempt: boolean; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 86 | timestamp?: number; |
| 87 | } |
| 88 | | { |
| 89 | // This is not a action that is submitted, |
| 90 | // It is used for undoing purposes. |
| 91 | type: 'endAutoPhase'; |
| 92 | timestamp?: number; |
| 93 | }; |
emilym | 38d08ba | 2022-10-22 15:25:01 -0700 | [diff] [blame] | 94 | |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 95 | @Component({ |
| 96 | selector: 'app-entry', |
| 97 | templateUrl: './entry.ng.html', |
Philipp Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 98 | styleUrls: ['../app/common.css', './entry.component.css'], |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 99 | }) |
| 100 | export class EntryComponent { |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 101 | // Re-export the type here so that we can use it in the `[value]` attribute |
| 102 | // of radio buttons. |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 103 | readonly COMP_LEVELS = COMP_LEVELS; |
| 104 | readonly COMP_LEVEL_LABELS = COMP_LEVEL_LABELS; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 105 | readonly ObjectType = ObjectType; |
| 106 | readonly ScoreLevel = ScoreLevel; |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 107 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 108 | section: Section = 'Team Selection'; |
| 109 | @Output() switchTabsEvent = new EventEmitter<string>(); |
| 110 | @Input() matchNumber: number = 1; |
| 111 | @Input() teamNumber: number = 1; |
Philipp Schrader | 30b4a68 | 2022-04-16 14:36:17 -0700 | [diff] [blame] | 112 | @Input() setNumber: number = 1; |
Philipp Schrader | 8aeb14f | 2022-04-08 21:23:18 -0700 | [diff] [blame] | 113 | @Input() compLevel: CompLevel = 'qm'; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 114 | |
| 115 | actionList: ActionT[] = []; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 116 | errorMessage: string = ''; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 117 | autoPhase: boolean = true; |
| 118 | lastObject: ObjectType = null; |
| 119 | |
| 120 | matchStartTimestamp: number = 0; |
| 121 | |
| 122 | addAction(action: ActionT): void { |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 123 | if (action.type == 'startMatchAction') { |
| 124 | // Unix nanosecond timestamp. |
| 125 | this.matchStartTimestamp = Date.now() * 1e6; |
| 126 | action.timestamp = 0; |
| 127 | } else { |
| 128 | // Unix nanosecond timestamp relative to match start. |
| 129 | action.timestamp = Date.now() * 1e6 - this.matchStartTimestamp; |
| 130 | } |
| 131 | |
| 132 | if ( |
| 133 | action.type == 'pickupObjectAction' || |
| 134 | action.type == 'placeObjectAction' |
| 135 | ) { |
| 136 | action.auto = this.autoPhase; |
| 137 | if (action.type == 'pickupObjectAction') { |
| 138 | this.lastObject = action.objectType; |
| 139 | } else if (action.type == 'placeObjectAction') { |
| 140 | action.objectType = this.lastObject; |
| 141 | } |
| 142 | } |
| 143 | this.actionList.push(action); |
| 144 | } |
| 145 | |
| 146 | undoLastAction() { |
| 147 | if (this.actionList.length > 0) { |
| 148 | let lastAction = this.actionList.pop(); |
| 149 | switch (lastAction?.type) { |
| 150 | case 'endAutoPhase': |
| 151 | this.autoPhase = true; |
| 152 | case 'pickupObjectAction': |
| 153 | this.section = 'Pickup'; |
| 154 | break; |
| 155 | case 'placeObjectAction': |
| 156 | this.section = 'Place'; |
| 157 | break; |
| 158 | case 'endMatchAction': |
| 159 | this.section = 'Pickup'; |
| 160 | break; |
Filip Kujawa | 9f56d0e | 2023-03-03 19:44:43 -0800 | [diff] [blame] | 161 | case 'robotDeathAction': |
| 162 | // TODO(FILIP): Return user to the screen they |
| 163 | // clicked dead robot on. Pickup is fine for now but |
| 164 | // might cause confusion. |
| 165 | this.section = 'Pickup'; |
| 166 | break; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 167 | default: |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | changeSectionTo(target: Section) { |
| 174 | this.section = target; |
| 175 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 176 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 177 | @ViewChild('header') header: ElementRef; |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 178 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 179 | private scrollToTop() { |
| 180 | this.header.nativeElement.scrollIntoView(); |
| 181 | } |
| 182 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 183 | async submitActions() { |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 184 | const builder = new Builder(); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 185 | const actionOffsets: number[] = []; |
| 186 | |
| 187 | for (const action of this.actionList) { |
| 188 | let actionOffset: number | undefined; |
| 189 | console.log(action.type); |
| 190 | |
| 191 | switch (action.type) { |
| 192 | case 'startMatchAction': |
| 193 | const startMatchActionOffset = |
| 194 | StartMatchAction.createStartMatchAction(builder, action.position); |
| 195 | actionOffset = Action.createAction( |
| 196 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 197 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 198 | ActionType.StartMatchAction, |
| 199 | startMatchActionOffset |
| 200 | ); |
| 201 | break; |
| 202 | |
| 203 | case 'pickupObjectAction': |
| 204 | const pickupObjectActionOffset = |
| 205 | PickupObjectAction.createPickupObjectAction( |
| 206 | builder, |
| 207 | action.objectType, |
| 208 | action.auto || false |
| 209 | ); |
| 210 | actionOffset = Action.createAction( |
| 211 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 212 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 213 | ActionType.PickupObjectAction, |
| 214 | pickupObjectActionOffset |
| 215 | ); |
| 216 | break; |
| 217 | |
Filip Kujawa | 4c28644 | 2023-03-03 10:41:22 -0800 | [diff] [blame] | 218 | case 'autoBalanceAction': |
| 219 | const autoBalanceActionOffset = |
| 220 | AutoBalanceAction.createAutoBalanceAction( |
| 221 | builder, |
| 222 | action.docked, |
Emily Markova | 63c63f6 | 2023-03-29 20:57:35 -0700 | [diff] [blame] | 223 | action.engaged, |
| 224 | action.balanceAttempt |
Filip Kujawa | 4c28644 | 2023-03-03 10:41:22 -0800 | [diff] [blame] | 225 | ); |
| 226 | actionOffset = Action.createAction( |
| 227 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 228 | BigInt(action.timestamp || 0), |
Filip Kujawa | 4c28644 | 2023-03-03 10:41:22 -0800 | [diff] [blame] | 229 | ActionType.AutoBalanceAction, |
| 230 | autoBalanceActionOffset |
| 231 | ); |
| 232 | break; |
| 233 | |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 234 | case 'placeObjectAction': |
| 235 | const placeObjectActionOffset = |
| 236 | PlaceObjectAction.createPlaceObjectAction( |
| 237 | builder, |
| 238 | action.objectType, |
| 239 | action.scoreLevel, |
| 240 | action.auto || false |
| 241 | ); |
| 242 | actionOffset = Action.createAction( |
| 243 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 244 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 245 | ActionType.PlaceObjectAction, |
| 246 | placeObjectActionOffset |
| 247 | ); |
| 248 | break; |
| 249 | |
| 250 | case 'robotDeathAction': |
| 251 | const robotDeathActionOffset = |
| 252 | RobotDeathAction.createRobotDeathAction(builder, action.robotOn); |
| 253 | actionOffset = Action.createAction( |
| 254 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 255 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 256 | ActionType.RobotDeathAction, |
| 257 | robotDeathActionOffset |
| 258 | ); |
| 259 | break; |
| 260 | |
| 261 | case 'endMatchAction': |
| 262 | const endMatchActionOffset = EndMatchAction.createEndMatchAction( |
| 263 | builder, |
| 264 | action.docked, |
Emily Markova | 63c63f6 | 2023-03-29 20:57:35 -0700 | [diff] [blame] | 265 | action.engaged, |
| 266 | action.balanceAttempt |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 267 | ); |
| 268 | actionOffset = Action.createAction( |
| 269 | builder, |
Philipp Schrader | 8c878a2 | 2023-03-20 22:36:38 -0700 | [diff] [blame] | 270 | BigInt(action.timestamp || 0), |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 271 | ActionType.EndMatchAction, |
| 272 | endMatchActionOffset |
| 273 | ); |
| 274 | break; |
| 275 | |
| 276 | case 'endAutoPhase': |
| 277 | // Not important action. |
| 278 | break; |
| 279 | |
| 280 | default: |
| 281 | throw new Error(`Unknown action type`); |
| 282 | } |
| 283 | |
| 284 | if (actionOffset !== undefined) { |
| 285 | actionOffsets.push(actionOffset); |
| 286 | } |
| 287 | } |
Philipp Schrader | e859e6e | 2023-03-22 19:59:51 -0700 | [diff] [blame] | 288 | const teamNumberFb = builder.createString(this.teamNumber.toString()); |
| 289 | const compLevelFb = builder.createString(this.compLevel); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 290 | |
| 291 | const actionsVector = SubmitActions.createActionsListVector( |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 292 | builder, |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 293 | actionOffsets |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 294 | ); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 295 | SubmitActions.startSubmitActions(builder); |
Philipp Schrader | e859e6e | 2023-03-22 19:59:51 -0700 | [diff] [blame] | 296 | SubmitActions.addTeamNumber(builder, teamNumberFb); |
| 297 | SubmitActions.addMatchNumber(builder, this.matchNumber); |
| 298 | SubmitActions.addSetNumber(builder, this.setNumber); |
| 299 | SubmitActions.addCompLevel(builder, compLevelFb); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 300 | SubmitActions.addActionsList(builder, actionsVector); |
| 301 | builder.finish(SubmitActions.endSubmitActions(builder)); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 302 | |
| 303 | const buffer = builder.asUint8Array(); |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 304 | const res = await fetch('/requests/submit/actions', { |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 305 | method: 'POST', |
| 306 | body: buffer, |
| 307 | }); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 308 | |
| 309 | if (res.ok) { |
| 310 | // We successfully submitted the data. Report success. |
| 311 | this.section = 'Success'; |
Filip Kujawa | 0ef334c | 2023-02-20 19:42:45 -0800 | [diff] [blame] | 312 | this.actionList = []; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 313 | } else { |
| 314 | const resBuffer = await res.arrayBuffer(); |
| 315 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 316 | const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 317 | |
| 318 | const errorMessage = parsedResponse.errorMessage(); |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 319 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 320 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 321 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 322 | } |