Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 1 | import {Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core'; |
| 2 | import {FormsModule} from '@angular/forms'; |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 3 | import {Builder, ByteBuffer} from 'flatbuffers'; |
| 4 | import {ErrorResponse} from 'org_frc971/scouting/webserver/requests/messages/error_response_generated'; |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 5 | import {ClimbLevel, SubmitDataScouting} from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_generated'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 6 | import {SubmitDataScoutingResponse} from 'org_frc971/scouting/webserver/requests/messages/submit_data_scouting_response_generated'; |
Philipp Schrader | 8b8ed67 | 2022-03-05 18:08:50 -0800 | [diff] [blame] | 7 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 8 | type Section = 'Team Selection'|'Auto'|'TeleOp'|'Climb'|'Other'| |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 9 | 'Review and Submit'|'Success'; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 10 | |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 11 | @Component({ |
| 12 | selector: 'app-entry', |
| 13 | templateUrl: './entry.ng.html', |
| 14 | styleUrls: ['../common.css', './entry.component.css'] |
| 15 | }) |
| 16 | export class EntryComponent { |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 17 | // Re-export the type here so that we can use it in the `[value]` attribute |
| 18 | // of radio buttons. |
| 19 | readonly ClimbLevel = ClimbLevel; |
| 20 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 21 | section: Section = 'Team Selection'; |
| 22 | @Output() switchTabsEvent = new EventEmitter<string>(); |
| 23 | @Input() matchNumber: number = 1; |
| 24 | @Input() teamNumber: number = 1; |
| 25 | autoUpperShotsMade: number = 0; |
| 26 | autoLowerShotsMade: number = 0; |
| 27 | autoShotsMissed: number = 0; |
| 28 | teleUpperShotsMade: number = 0; |
| 29 | teleLowerShotsMade: number = 0; |
| 30 | teleShotsMissed: number = 0; |
| 31 | defensePlayedOnScore: number = 0; |
| 32 | defensePlayedScore: number = 0; |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 33 | level: ClimbLevel = ClimbLevel.NoAttempt; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 34 | ball1: boolean = false; |
| 35 | ball2: boolean = false; |
| 36 | ball3: boolean = false; |
| 37 | ball4: boolean = false; |
| 38 | ball5: boolean = false; |
| 39 | quadrant: number = 1; |
| 40 | errorMessage: string = ''; |
| 41 | noShow: boolean = false; |
| 42 | neverMoved: boolean = false; |
| 43 | batteryDied: boolean = false; |
| 44 | mechanicallyBroke: boolean = false; |
| 45 | lostComs: boolean = false; |
Philipp Schrader | 9bcbc33 | 2022-03-18 19:06:04 -0700 | [diff] [blame] | 46 | comment: string = ''; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 47 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 48 | @ViewChild('header') header: ElementRef; |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 49 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 50 | nextSection() { |
| 51 | if (this.section === 'Team Selection') { |
| 52 | this.section = 'Auto'; |
| 53 | } else if (this.section === 'Auto') { |
| 54 | this.section = 'TeleOp'; |
| 55 | } else if (this.section === 'TeleOp') { |
| 56 | this.section = 'Climb'; |
| 57 | } else if (this.section === 'Climb') { |
| 58 | this.section = 'Other'; |
| 59 | } else if (this.section === 'Other') { |
| 60 | this.section = 'Review and Submit'; |
| 61 | } else if (this.section === 'Review and Submit') { |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 62 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 63 | this.submitDataScouting(); |
| 64 | return; |
| 65 | } else if (this.section === 'Success') { |
Philipp Schrader | 23993e8 | 2022-03-18 18:54:00 -0700 | [diff] [blame] | 66 | this.switchTabsEvent.emit('MatchList'); |
| 67 | return; |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 68 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 69 | // Scroll back to the top so that we can be sure the user sees the |
| 70 | // entire next screen. Otherwise it's easy to overlook input fields. |
| 71 | this.scrollToTop(); |
| 72 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 73 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 74 | prevSection() { |
| 75 | if (this.section === 'Auto') { |
| 76 | this.section = 'Team Selection'; |
| 77 | } else if (this.section === 'TeleOp') { |
| 78 | this.section = 'Auto'; |
| 79 | } else if (this.section === 'Climb') { |
| 80 | this.section = 'TeleOp'; |
| 81 | } else if (this.section === 'Other') { |
| 82 | this.section = 'Climb'; |
| 83 | } else if (this.section === 'Review and Submit') { |
| 84 | this.section = 'Other'; |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 85 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 86 | // Scroll back to the top so that we can be sure the user sees the |
| 87 | // entire previous screen. Otherwise it's easy to overlook input |
| 88 | // fields. |
| 89 | this.scrollToTop(); |
| 90 | } |
Philipp Schrader | 6b2e950 | 2022-03-15 23:42:56 -0700 | [diff] [blame] | 91 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 92 | private scrollToTop() { |
| 93 | this.header.nativeElement.scrollIntoView(); |
| 94 | } |
| 95 | |
| 96 | async submitDataScouting() { |
| 97 | this.errorMessage = ''; |
| 98 | |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 99 | const builder = new Builder(); |
Philipp Schrader | 70569f7 | 2022-03-18 19:15:05 -0700 | [diff] [blame^] | 100 | const comment = builder.createString(this.comment); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 101 | SubmitDataScouting.startSubmitDataScouting(builder); |
| 102 | SubmitDataScouting.addTeam(builder, this.teamNumber); |
| 103 | SubmitDataScouting.addMatch(builder, this.matchNumber); |
| 104 | SubmitDataScouting.addMissedShotsAuto(builder, this.autoShotsMissed); |
| 105 | SubmitDataScouting.addUpperGoalAuto(builder, this.autoUpperShotsMade); |
| 106 | SubmitDataScouting.addLowerGoalAuto(builder, this.autoLowerShotsMade); |
| 107 | SubmitDataScouting.addMissedShotsTele(builder, this.teleShotsMissed); |
| 108 | SubmitDataScouting.addUpperGoalTele(builder, this.teleUpperShotsMade); |
| 109 | SubmitDataScouting.addLowerGoalTele(builder, this.teleLowerShotsMade); |
| 110 | SubmitDataScouting.addDefenseRating(builder, this.defensePlayedScore); |
Philipp Schrader | 70569f7 | 2022-03-18 19:15:05 -0700 | [diff] [blame^] | 111 | SubmitDataScouting.addDefenseReceivedRating(builder, this.defensePlayedOnScore); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 112 | SubmitDataScouting.addAutoBall1(builder, this.ball1); |
| 113 | SubmitDataScouting.addAutoBall2(builder, this.ball2); |
| 114 | SubmitDataScouting.addAutoBall3(builder, this.ball3); |
| 115 | SubmitDataScouting.addAutoBall4(builder, this.ball4); |
| 116 | SubmitDataScouting.addAutoBall5(builder, this.ball5); |
| 117 | SubmitDataScouting.addStartingQuadrant(builder, this.quadrant); |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 118 | SubmitDataScouting.addClimbLevel(builder, this.level); |
Philipp Schrader | 70569f7 | 2022-03-18 19:15:05 -0700 | [diff] [blame^] | 119 | SubmitDataScouting.addComment(builder, comment); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 120 | builder.finish(SubmitDataScouting.endSubmitDataScouting(builder)); |
| 121 | |
| 122 | const buffer = builder.asUint8Array(); |
| 123 | const res = await fetch( |
| 124 | '/requests/submit/data_scouting', {method: 'POST', body: buffer}); |
| 125 | |
| 126 | if (res.ok) { |
| 127 | // We successfully submitted the data. Report success. |
| 128 | this.section = 'Success'; |
| 129 | } else { |
| 130 | const resBuffer = await res.arrayBuffer(); |
| 131 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 132 | const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer); |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 133 | |
| 134 | const errorMessage = parsedResponse.errorMessage(); |
| 135 | this.errorMessage = |
| 136 | `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
Alex Perry | bb3d206 | 2022-03-05 18:14:33 -0800 | [diff] [blame] | 137 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 138 | } |
Philipp Schrader | 8058743 | 2022-03-05 15:41:22 -0800 | [diff] [blame] | 139 | } |