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 | e5d1394 | 2024-03-17 15:44:35 -0700 | [diff] [blame] | 3 | import {ErrorResponse} from '@org_frc971/scouting/webserver/requests/messages/error_response_generated'; |
| 4 | import {RequestNotesForTeam} from '@org_frc971/scouting/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 | e5d1394 | 2024-03-17 15:44:35 -0700 | [diff] [blame] | 8 | } from '@org_frc971/scouting/webserver/requests/messages/request_notes_for_team_response_generated'; |
| 9 | import {SubmitNotes} from '@org_frc971/scouting/webserver/requests/messages/submit_notes_generated'; |
| 10 | import {SubmitNotesResponse} from '@org_frc971/scouting/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 | |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 36 | const COMP_LEVELS = ['qm', 'ef', 'qf', 'sf', 'f'] as const; |
| 37 | type CompLevel = typeof COMP_LEVELS[number]; |
| 38 | |
| 39 | // TODO(phil): Deduplicate with match_list.component.ts. |
| 40 | const COMP_LEVEL_LABELS: Record<CompLevel, string> = { |
| 41 | qm: 'Qualifications', |
| 42 | ef: 'Eighth Finals', |
| 43 | qf: 'Quarter Finals', |
| 44 | sf: 'Semi Finals', |
| 45 | f: 'Finals', |
| 46 | }; |
| 47 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 48 | // Every keyword checkbox corresponds to a boolean. |
| 49 | // If the boolean is True, the checkbox is selected |
| 50 | // and the note scout saw that the robot being scouted |
| 51 | // displayed said property (ex. Driving really well -> goodDriving) |
| 52 | interface Keywords { |
| 53 | goodDriving: boolean; |
| 54 | badDriving: boolean; |
Filip Kujawa | 11dc4c9 | 2023-04-13 08:55:43 -0700 | [diff] [blame] | 55 | solidPlacing: boolean; |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 56 | sketchyPlacing: boolean; |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 57 | goodDefense: boolean; |
| 58 | badDefense: boolean; |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 59 | easilyDefended: boolean; |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 60 | noShow: boolean; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 63 | interface Input { |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 64 | teamNumber: string; |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 65 | notesData: string; |
| 66 | keywordsData: Keywords; |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 67 | matchNumber: number; |
| 68 | setNumber: number; |
| 69 | compLevel: string; |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | const KEYWORD_CHECKBOX_LABELS = { |
| 73 | goodDriving: 'Good Driving', |
| 74 | badDriving: 'Bad Driving', |
Emily Markova | a287ac5 | 2024-03-07 19:28:58 -0800 | [diff] [blame] | 75 | solidPlacing: 'Solid Shooting', |
| 76 | sketchyPlacing: 'Sketchy Shooting', |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 77 | goodDefense: 'Good Defense', |
| 78 | badDefense: 'Bad Defense', |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 79 | easilyDefended: 'Easily Defended', |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 80 | noShow: 'No Show', |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 81 | } as const; |
| 82 | |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 83 | @Component({ |
| 84 | selector: 'frc971-notes', |
| 85 | templateUrl: './notes.ng.html', |
Philipp Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 86 | styleUrls: ['../app/common.css', './notes.component.css'], |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 87 | }) |
| 88 | export class Notes { |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 89 | // Re-export KEYWORD_CHECKBOX_LABELS so that we can |
| 90 | // use it in the checkbox properties. |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 91 | readonly COMP_LEVELS = COMP_LEVELS; |
| 92 | readonly COMP_LEVEL_LABELS = COMP_LEVEL_LABELS; |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 93 | readonly KEYWORD_CHECKBOX_LABELS = KEYWORD_CHECKBOX_LABELS; |
| 94 | |
| 95 | // Necessary in order to iterate the keys of KEYWORD_CHECKBOX_LABELS. |
| 96 | Object = Object; |
| 97 | |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 98 | section: Section = 'TeamSelection'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 99 | |
| 100 | errorMessage = ''; |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 101 | teamNumber: string = '1'; |
| 102 | matchNumber: number = 1; |
| 103 | setNumber: number = 1; |
| 104 | compLevel: CompLevel = 'qm'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 105 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 106 | // Data inputted by user is stored in this array. |
| 107 | // Includes the team number, notes, and keyword selection. |
| 108 | newData: Input[] = []; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 109 | |
Filip Kujawa | 7dd4995 | 2022-12-02 12:09:13 -0800 | [diff] [blame] | 110 | // Keyboard shortcuts to switch between text areas. |
| 111 | // Listens for Ctrl + number and focuses on the |
| 112 | // corresponding textbox. |
| 113 | // More Info: https://angular.io/api/core/HostListener |
| 114 | |
| 115 | @HostListener('window:keyup', ['$event']) |
| 116 | onEvent(event: KeyboardEvent) { |
| 117 | if (event.ctrlKey) { |
| 118 | if (event.code.includes('Digit')) { |
| 119 | this.handleFocus(event.key); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | handleFocus(digit: string) { |
| 125 | let textArea = <HTMLInputElement>( |
| 126 | document.getElementById('text-input-' + digit) |
| 127 | ); |
| 128 | if (textArea != null) { |
| 129 | textArea.focus(); |
| 130 | } |
| 131 | } |
| 132 | |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 133 | setTeamData() { |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 134 | let data: Input = { |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 135 | teamNumber: this.teamNumber, |
| 136 | notesData: |
| 137 | 'Match ' + |
| 138 | this.matchNumber + |
| 139 | ' Set ' + |
| 140 | this.setNumber + |
| 141 | ' ' + |
| 142 | COMP_LEVEL_LABELS[this.compLevel] + |
| 143 | ' \nAuto: \nTeleop: \nEndgame: ', |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 144 | keywordsData: { |
| 145 | goodDriving: false, |
| 146 | badDriving: false, |
Filip Kujawa | 11dc4c9 | 2023-04-13 08:55:43 -0700 | [diff] [blame] | 147 | solidPlacing: false, |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 148 | sketchyPlacing: false, |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 149 | goodDefense: false, |
| 150 | badDefense: false, |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 151 | easilyDefended: false, |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 152 | noShow: false, |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 153 | }, |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 154 | matchNumber: this.matchNumber, |
| 155 | setNumber: this.setNumber, |
| 156 | compLevel: this.compLevel, |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 157 | }; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 158 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 159 | this.newData.push(data); |
| 160 | this.section = 'Data'; |
| 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 | removeTeam(index: number) { |
| 164 | this.newData.splice(index, 1); |
| 165 | if (this.newData.length == 0) { |
| 166 | this.section = 'TeamSelection'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 167 | } else { |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 168 | this.section = 'Data'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 172 | addTeam() { |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 173 | this.section = 'TeamSelection'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 174 | } |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 175 | |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 176 | async submitData() { |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 177 | for (let i = 0; i < this.newData.length; i++) { |
| 178 | const builder = new Builder(); |
| 179 | const dataFb = builder.createString(this.newData[i].notesData); |
Filip Kujawa | ba55bb3 | 2022-12-02 14:08:32 -0800 | [diff] [blame] | 180 | |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 181 | const teamNumber = builder.createString(this.newData[i].teamNumber); |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 182 | const compLevel = builder.createString(this.newData[i].compLevel); |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 183 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 184 | builder.finish( |
| 185 | SubmitNotes.createSubmitNotes( |
| 186 | builder, |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 187 | teamNumber, |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 188 | dataFb, |
| 189 | this.newData[i].keywordsData.goodDriving, |
| 190 | this.newData[i].keywordsData.badDriving, |
Filip Kujawa | 11dc4c9 | 2023-04-13 08:55:43 -0700 | [diff] [blame] | 191 | this.newData[i].keywordsData.solidPlacing, |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 192 | this.newData[i].keywordsData.sketchyPlacing, |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 193 | this.newData[i].keywordsData.goodDefense, |
Filip Kujawa | 7ddd565 | 2023-03-07 19:56:15 -0800 | [diff] [blame] | 194 | this.newData[i].keywordsData.badDefense, |
Emily Markova | cf893f4 | 2024-03-13 19:03:10 -0700 | [diff] [blame^] | 195 | this.newData[i].keywordsData.easilyDefended, |
| 196 | this.newData[i].keywordsData.noShow, |
| 197 | this.newData[i].matchNumber, |
| 198 | this.newData[i].setNumber, |
| 199 | compLevel |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 200 | ) |
| 201 | ); |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 202 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 203 | const buffer = builder.asUint8Array(); |
| 204 | const res = await fetch('/requests/submit/submit_notes', { |
| 205 | method: 'POST', |
| 206 | body: buffer, |
| 207 | }); |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 208 | |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 209 | if (!res.ok) { |
| 210 | const resBuffer = await res.arrayBuffer(); |
| 211 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
| 212 | const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer); |
| 213 | const errorMessage = parsedResponse.errorMessage(); |
| 214 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
| 215 | } |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 216 | } |
Filip Kujawa | f947cb4 | 2022-11-21 10:00:30 -0800 | [diff] [blame] | 217 | |
| 218 | this.newData = []; |
| 219 | this.errorMessage = ''; |
| 220 | this.section = 'TeamSelection'; |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 221 | } |
Philipp Schrader | 02db74b | 2023-02-17 20:36:58 -0800 | [diff] [blame] | 222 | |
| 223 | labelToId(label: String): String { |
| 224 | return label.replaceAll(' ', '_').toLowerCase(); |
| 225 | } |
Alex Perry | bb90105 | 2022-03-23 19:46:15 -0700 | [diff] [blame] | 226 | } |