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