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