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. |
Evelyn Yang | c4fad06 | 2024-03-20 20:19:50 -0700 | [diff] [blame] | 10 | type Section = 'TeamSelection' | 'TeamSelectionAdd' | 'Data'; |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 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']; |
Evelyn Yang | c4fad06 | 2024-03-20 20:19:50 -0700 | [diff] [blame] | 22 | added_teams: string[] = ['974', '975', '976']; |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 23 | |
| 24 | match_number: number = 1; |
| 25 | |
| 26 | errorMessage = ''; |
| 27 | |
| 28 | setTeamNumbers() { |
| 29 | this.section = 'Data'; |
| 30 | } |
| 31 | |
Evelyn Yang | c4fad06 | 2024-03-20 20:19:50 -0700 | [diff] [blame] | 32 | addTeamNumbers() { |
| 33 | for (let i = 0; i < this.added_teams.length; i++) { |
| 34 | this.team_ranking.push(this.added_teams[i]); |
| 35 | } |
| 36 | this.added_teams.splice(0, this.added_teams.length); |
| 37 | this.added_teams = ['974', '975', '976']; |
| 38 | this.section = 'Data'; |
| 39 | } |
| 40 | |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 41 | rankUp(index: number) { |
| 42 | if (index > 0) { |
| 43 | this.changeRank(index, index - 1); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | rankDown(index: number) { |
Evelyn Yang | c4fad06 | 2024-03-20 20:19:50 -0700 | [diff] [blame] | 48 | if (this.team_ranking.length == 6) { |
| 49 | if (index < 5) { |
| 50 | this.changeRank(index, index + 1); |
| 51 | } |
| 52 | } else { |
| 53 | if (index < 2) { |
| 54 | this.changeRank(index, index + 1); |
| 55 | } |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | // Change the rank of a team in team_ranking. |
| 60 | // Move the the team at index 'fromIndex' |
| 61 | // to the index 'toIndex'. |
| 62 | // Ex. Moving the rank 2 (index 1) team to rank1 (index 0) |
| 63 | // would be changeRank(1, 0) |
| 64 | |
| 65 | changeRank(fromIndex: number, toIndex: number) { |
| 66 | var element = this.team_ranking[fromIndex]; |
| 67 | this.team_ranking.splice(fromIndex, 1); |
| 68 | this.team_ranking.splice(toIndex, 0, element); |
| 69 | } |
| 70 | |
| 71 | editTeams() { |
| 72 | this.section = 'TeamSelection'; |
| 73 | } |
| 74 | |
Evelyn Yang | c4fad06 | 2024-03-20 20:19:50 -0700 | [diff] [blame] | 75 | addTeams() { |
| 76 | this.section = 'TeamSelectionAdd'; |
| 77 | } |
| 78 | |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 79 | async submitData() { |
| 80 | const builder = new Builder(); |
Evelyn Yang | c4fad06 | 2024-03-20 20:19:50 -0700 | [diff] [blame] | 81 | if (this.team_ranking.length == 3) { |
| 82 | const teamRanking1 = builder.createString(this.team_ranking[0]); |
| 83 | const teamRanking2 = builder.createString(this.team_ranking[1]); |
| 84 | const teamRanking3 = builder.createString(this.team_ranking[2]); |
| 85 | builder.finish( |
| 86 | SubmitDriverRanking.createSubmitDriverRanking( |
| 87 | builder, |
| 88 | this.match_number, |
| 89 | teamRanking1, |
| 90 | teamRanking2, |
| 91 | teamRanking3 |
| 92 | ) |
| 93 | ); |
| 94 | } else { |
| 95 | const teamRanking1 = builder.createString(this.team_ranking[0]); |
| 96 | const teamRanking2 = builder.createString(this.team_ranking[1]); |
| 97 | const teamRanking3 = builder.createString(this.team_ranking[2]); |
| 98 | const teamRanking4 = builder.createString(this.team_ranking[3]); |
| 99 | const teamRanking5 = builder.createString(this.team_ranking[4]); |
| 100 | const teamRanking6 = builder.createString(this.team_ranking[5]); |
| 101 | // Submits the ranking 4 times to prevent data loss |
| 102 | // since driver ranking compares three teams at a time. |
| 103 | builder.finish( |
| 104 | SubmitDriverRanking.createSubmitDriverRanking( |
| 105 | builder, |
| 106 | this.match_number, |
| 107 | teamRanking1, |
| 108 | teamRanking2, |
| 109 | teamRanking3 |
| 110 | ) |
| 111 | ); |
| 112 | builder.finish( |
| 113 | SubmitDriverRanking.createSubmitDriverRanking( |
| 114 | builder, |
| 115 | this.match_number, |
| 116 | teamRanking4, |
| 117 | teamRanking5, |
| 118 | teamRanking6 |
| 119 | ) |
| 120 | ); |
| 121 | builder.finish( |
| 122 | SubmitDriverRanking.createSubmitDriverRanking( |
| 123 | builder, |
| 124 | this.match_number, |
| 125 | teamRanking3, |
| 126 | teamRanking4, |
| 127 | teamRanking5 |
| 128 | ) |
| 129 | ); |
| 130 | builder.finish( |
| 131 | SubmitDriverRanking.createSubmitDriverRanking( |
| 132 | builder, |
| 133 | this.match_number, |
| 134 | teamRanking1, |
| 135 | teamRanking2, |
| 136 | teamRanking6 |
| 137 | ) |
| 138 | ); |
| 139 | } |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 140 | const buffer = builder.asUint8Array(); |
| 141 | const res = await fetch('/requests/submit/submit_driver_ranking', { |
| 142 | method: 'POST', |
| 143 | body: buffer, |
| 144 | }); |
| 145 | |
| 146 | if (!res.ok) { |
| 147 | const resBuffer = await res.arrayBuffer(); |
| 148 | const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer)); |
| 149 | const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer); |
| 150 | |
| 151 | const errorMessage = parsedResponse.errorMessage(); |
| 152 | this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`; |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | // Increment the match number. |
| 157 | this.match_number = this.match_number + 1; |
| 158 | |
| 159 | // Reset Data. |
| 160 | this.section = 'TeamSelection'; |
Emily Markova | e68b763 | 2023-12-30 14:17:55 -0800 | [diff] [blame] | 161 | this.team_ranking = ['971', '972', '973']; |
Filip Kujawa | 210a03b | 2022-11-24 14:41:11 -0800 | [diff] [blame] | 162 | } |
| 163 | } |