Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 1 | import {Component, OnInit} from '@angular/core'; |
| 2 | import {Builder, ByteBuffer} from 'flatbuffers'; |
Philipp Schrader | e5d1394 | 2024-03-17 15:44:35 -0700 | [diff] [blame^] | 3 | import {SubmitDriverRanking} from '@org_frc971/scouting/webserver/requests/messages/submit_driver_ranking_generated'; |
| 4 | import {ErrorResponse} from '@org_frc971/scouting/webserver/requests/messages/error_response_generated'; |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 5 | |
| 6 | // TeamSelection: Display form to input which |
| 7 | // teams to rank and the match number. |
| 8 | // Data: Display the ranking interface where |
| 9 | // the scout can reorder teams and submit data. |
| 10 | type Section = 'TeamSelection' | 'Data'; |
| 11 | |
| 12 | @Component({ |
| 13 | selector: 'app-driver-ranking', |
| 14 | templateUrl: './driver_ranking.ng.html', |
Philipp Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 15 | styleUrls: ['../app/common.css', './driver_ranking.component.css'], |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 16 | }) |
| 17 | export class DriverRankingComponent { |
| 18 | section: Section = 'TeamSelection'; |
| 19 | |
| 20 | // Stores the team keys and rank (order of the array). |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 21 | team_ranking: string[] = ['971', '972', '973']; |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 22 | |
| 23 | match_number: number = 1; |
| 24 | |
| 25 | errorMessage = ''; |
| 26 | |
| 27 | setTeamNumbers() { |
| 28 | this.section = 'Data'; |
| 29 | } |
| 30 | |
| 31 | rankUp(index: number) { |
| 32 | if (index > 0) { |
| 33 | this.changeRank(index, index - 1); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | rankDown(index: number) { |
| 38 | if (index < 2) { |
| 39 | this.changeRank(index, index + 1); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Change the rank of a team in team_ranking. |
| 44 | // Move the the team at index 'fromIndex' |
| 45 | // to the index 'toIndex'. |
| 46 | // Ex. Moving the rank 2 (index 1) team to rank1 (index 0) |
| 47 | // would be changeRank(1, 0) |
| 48 | |
| 49 | changeRank(fromIndex: number, toIndex: number) { |
| 50 | var element = this.team_ranking[fromIndex]; |
| 51 | this.team_ranking.splice(fromIndex, 1); |
| 52 | this.team_ranking.splice(toIndex, 0, element); |
| 53 | } |
| 54 | |
| 55 | editTeams() { |
| 56 | this.section = 'TeamSelection'; |
| 57 | } |
| 58 | |
| 59 | async submitData() { |
| 60 | const builder = new Builder(); |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 61 | const teamRanking1 = builder.createString(this.team_ranking[0]); |
| 62 | const teamRanking2 = builder.createString(this.team_ranking[1]); |
| 63 | const teamRanking3 = builder.createString(this.team_ranking[2]); |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 64 | builder.finish( |
| 65 | SubmitDriverRanking.createSubmitDriverRanking( |
| 66 | builder, |
| 67 | this.match_number, |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 68 | teamRanking1, |
| 69 | teamRanking2, |
| 70 | teamRanking3 |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 71 | ) |
| 72 | ); |
| 73 | const buffer = builder.asUint8Array(); |
| 74 | const res = await fetch('/requests/submit/submit_driver_ranking', { |
| 75 | method: 'POST', |
| 76 | body: buffer, |
| 77 | }); |
| 78 | |
| 79 | if (!res.ok) { |
| 80 | const resBuffer = await res.arrayBuffer(); |
| 81 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
| 82 | const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer); |
| 83 | |
| 84 | const errorMessage = parsedResponse.errorMessage(); |
| 85 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // Increment the match number. |
| 90 | this.match_number = this.match_number + 1; |
| 91 | |
| 92 | // Reset Data. |
| 93 | this.section = 'TeamSelection'; |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 94 | this.team_ranking = ['971', '972', '973']; |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 95 | } |
| 96 | } |