Filip Kujawa | 7dd4995 | 2022-12-02 12:09:13 -0800 | [diff] [blame] | 1 | import {Component, HostListener} from '@angular/core'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 2 | import {Builder, ByteBuffer} from 'flatbuffers'; |
Philipp Schrader | d7efa2b | 2023-02-17 21:15:13 -0800 | [diff] [blame] | 3 | import {ErrorResponse} from '../../webserver/requests/messages/error_response_generated'; |
| 4 | import {RequestNotesForTeam} from '../../webserver/requests/messages/request_notes_for_team_generated'; |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 5 | import { |
| 6 | Note as NoteFb, |
| 7 | RequestNotesForTeamResponse, |
Philipp Schrader | d7efa2b | 2023-02-17 21:15:13 -0800 | [diff] [blame] | 8 | } from '../../webserver/requests/messages/request_notes_for_team_response_generated'; |
| 9 | import {SubmitNotes} from '../../webserver/requests/messages/submit_notes_generated'; |
| 10 | import {SubmitNotesResponse} from '../../webserver/requests/messages/submit_notes_response_generated'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 11 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 12 | /* |
| 13 | For new games, the keywords being used will likely need to be updated. |
| 14 | To update the keywords complete the following: |
| 15 | 1) Update the Keywords Interface and KEYWORD_CHECKBOX_LABELS in notes.component.ts |
| 16 | The keys of Keywords and KEYWORD_CHECKBOX_LABELS should match. |
| 17 | 2) In notes.component.ts, update the setTeamNumber() method with the new keywords. |
| 18 | 3) Add/Edit the new keywords in /scouting/webserver/requests/messages/submit_notes.fbs. |
| 19 | 4) In notes.component.ts, update the submitData() method with the newKeywords |
| 20 | so that it matches the updated flatbuffer |
| 21 | 5) In db.go, update the NotesData struct and the |
| 22 | AddNotes method with the new keywords |
| 23 | 6) In db_test.go update the TestNotes method so the test uses the keywords |
| 24 | 7) Update the submitNoteScoutingHandler in requests.go with the new keywords |
| 25 | 8) Finally, update the corresponding test in requests_test.go (TestSubmitNotes) |
| 26 | |
| 27 | Note: If you change the number of keywords you might need to |
| 28 | update how they are displayed in notes.ng.html |
| 29 | */ |
| 30 | |
| 31 | // TeamSelection: Display form to add a team to the teams being scouted. |
| 32 | // Data: Display the note textbox and keyword selection form |
| 33 | // for all the teams being scouted. |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 34 | type Section = 'TeamSelection' | 'Data'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 35 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 36 | // Every keyword checkbox corresponds to a boolean. |
| 37 | // If the boolean is True, the checkbox is selected |
| 38 | // and the note scout saw that the robot being scouted |
| 39 | // displayed said property (ex. Driving really well -> goodDriving) |
| 40 | interface Keywords { |
| 41 | goodDriving: boolean; |
| 42 | badDriving: boolean; |
Filip Kujawa | 6f7f0b3 | 2023-03-30 13:26:08 -0700 | [diff] [blame] | 43 | solidPickup: boolean; |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 44 | sketchyPlacing: boolean; |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 45 | goodDefense: boolean; |
| 46 | badDefense: boolean; |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 47 | easilyDefended: boolean; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 50 | interface Input { |
| 51 | teamNumber: number; |
| 52 | notesData: string; |
| 53 | keywordsData: Keywords; |
| 54 | } |
| 55 | |
| 56 | const KEYWORD_CHECKBOX_LABELS = { |
| 57 | goodDriving: 'Good Driving', |
| 58 | badDriving: 'Bad Driving', |
Filip Kujawa | 6f7f0b3 | 2023-03-30 13:26:08 -0700 | [diff] [blame] | 59 | solidPickup: 'Solid Pickup', |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 60 | sketchyPlacing: 'Sketchy Placing', |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 61 | goodDefense: 'Good Defense', |
| 62 | badDefense: 'Bad Defense', |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 63 | easilyDefended: 'Easily Defended', |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 64 | } as const; |
| 65 | |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 66 | @Component({ |
| 67 | selector: 'frc971-notes', |
| 68 | templateUrl: './notes.ng.html', |
Philipp Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 69 | styleUrls: ['../app/common.css', './notes.component.css'], |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 70 | }) |
| 71 | export class Notes { |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 72 | // Re-export KEYWORD_CHECKBOX_LABELS so that we can |
| 73 | // use it in the checkbox properties. |
| 74 | readonly KEYWORD_CHECKBOX_LABELS = KEYWORD_CHECKBOX_LABELS; |
| 75 | |
| 76 | // Necessary in order to iterate the keys of KEYWORD_CHECKBOX_LABELS. |
| 77 | Object = Object; |
| 78 | |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 79 | section: Section = 'TeamSelection'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 80 | |
| 81 | errorMessage = ''; |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 82 | teamNumberSelection: number = 971; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 83 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 84 | // Data inputted by user is stored in this array. |
| 85 | // Includes the team number, notes, and keyword selection. |
| 86 | newData: Input[] = []; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 87 | |
Filip Kujawa | 7dd4995 | 2022-12-02 12:09:13 -0800 | [diff] [blame] | 88 | // Keyboard shortcuts to switch between text areas. |
| 89 | // Listens for Ctrl + number and focuses on the |
| 90 | // corresponding textbox. |
| 91 | // More Info: https://angular.io/api/core/HostListener |
| 92 | |
| 93 | @HostListener('window:keyup', ['$event']) |
| 94 | onEvent(event: KeyboardEvent) { |
| 95 | if (event.ctrlKey) { |
| 96 | if (event.code.includes('Digit')) { |
| 97 | this.handleFocus(event.key); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | handleFocus(digit: string) { |
| 103 | let textArea = <HTMLInputElement>( |
| 104 | document.getElementById('text-input-' + digit) |
| 105 | ); |
| 106 | if (textArea != null) { |
| 107 | textArea.focus(); |
| 108 | } |
| 109 | } |
| 110 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 111 | setTeamNumber() { |
| 112 | let data: Input = { |
| 113 | teamNumber: this.teamNumberSelection, |
Filip Kujawa | 6534a58 | 2023-03-31 16:19:42 -0700 | [diff] [blame^] | 114 | notesData: 'Match: \nAuto: \nTeleop: \nEndgame: ', |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 115 | keywordsData: { |
| 116 | goodDriving: false, |
| 117 | badDriving: false, |
Filip Kujawa | 6f7f0b3 | 2023-03-30 13:26:08 -0700 | [diff] [blame] | 118 | solidPickup: false, |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 119 | sketchyPlacing: false, |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 120 | goodDefense: false, |
| 121 | badDefense: false, |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 122 | easilyDefended: false, |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 123 | }, |
| 124 | }; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 125 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 126 | this.newData.push(data); |
| 127 | this.section = 'Data'; |
| 128 | } |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 129 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 130 | removeTeam(index: number) { |
| 131 | this.newData.splice(index, 1); |
| 132 | if (this.newData.length == 0) { |
| 133 | this.section = 'TeamSelection'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 134 | } else { |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 135 | this.section = 'Data'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 139 | addTeam() { |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 140 | this.section = 'TeamSelection'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 141 | } |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 142 | |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 143 | async submitData() { |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 144 | for (let i = 0; i < this.newData.length; i++) { |
| 145 | const builder = new Builder(); |
| 146 | const dataFb = builder.createString(this.newData[i].notesData); |
Filip Kujawa | ba55bb3 | 2022-12-02 14:08:32 -0800 | [diff] [blame] | 147 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 148 | builder.finish( |
| 149 | SubmitNotes.createSubmitNotes( |
| 150 | builder, |
| 151 | this.newData[i].teamNumber, |
| 152 | dataFb, |
| 153 | this.newData[i].keywordsData.goodDriving, |
| 154 | this.newData[i].keywordsData.badDriving, |
Filip Kujawa | 6f7f0b3 | 2023-03-30 13:26:08 -0700 | [diff] [blame] | 155 | this.newData[i].keywordsData.solidPickup, |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 156 | this.newData[i].keywordsData.sketchyPlacing, |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 157 | this.newData[i].keywordsData.goodDefense, |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 158 | this.newData[i].keywordsData.badDefense, |
| 159 | this.newData[i].keywordsData.easilyDefended |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 160 | ) |
| 161 | ); |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 162 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 163 | const buffer = builder.asUint8Array(); |
| 164 | const res = await fetch('/requests/submit/submit_notes', { |
| 165 | method: 'POST', |
| 166 | body: buffer, |
| 167 | }); |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 168 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 169 | if (!res.ok) { |
| 170 | const resBuffer = await res.arrayBuffer(); |
| 171 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
| 172 | const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer); |
| 173 | const errorMessage = parsedResponse.errorMessage(); |
| 174 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
| 175 | } |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 176 | } |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 177 | |
| 178 | this.newData = []; |
| 179 | this.errorMessage = ''; |
| 180 | this.section = 'TeamSelection'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 181 | } |
Philipp Schrader | 02db74b | 2023-02-17 20:36:58 -0800 | [diff] [blame] | 182 | |
| 183 | labelToId(label: String): String { |
| 184 | return label.replaceAll(' ', '_').toLowerCase(); |
| 185 | } |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 186 | } |