blob: 09f62c2aff25bdd5d159d3f316031656a1800f1a [file] [log] [blame]
Filip Kujawa210a03b2022-11-24 14:41:11 -08001import {Component, OnInit} from '@angular/core';
2import {Builder, ByteBuffer} from 'flatbuffers';
Philipp Schraderd7efa2b2023-02-17 21:15:13 -08003import {SubmitDriverRanking} from '../../webserver/requests/messages/submit_driver_ranking_generated';
4import {ErrorResponse} from '../../webserver/requests/messages/error_response_generated';
Filip Kujawa210a03b2022-11-24 14:41:11 -08005
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.
10type Section = 'TeamSelection' | 'Data';
11
12@Component({
13 selector: 'app-driver-ranking',
14 templateUrl: './driver_ranking.ng.html',
Philipp Schrader175a93c2023-02-19 13:13:40 -080015 styleUrls: ['../app/common.css', './driver_ranking.component.css'],
Filip Kujawa210a03b2022-11-24 14:41:11 -080016})
17export class DriverRankingComponent {
18 section: Section = 'TeamSelection';
19
20 // Stores the team keys and rank (order of the array).
Emily Markovae68b7632023-12-30 14:17:55 -080021 team_ranking: string[] = ['971', '972', '973'];
Filip Kujawa210a03b2022-11-24 14:41:11 -080022
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 Markovae68b7632023-12-30 14:17:55 -080061 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 Kujawa210a03b2022-11-24 14:41:11 -080064 builder.finish(
65 SubmitDriverRanking.createSubmitDriverRanking(
66 builder,
67 this.match_number,
Emily Markovae68b7632023-12-30 14:17:55 -080068 teamRanking1,
69 teamRanking2,
70 teamRanking3
Filip Kujawa210a03b2022-11-24 14:41:11 -080071 )
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 Markovae68b7632023-12-30 14:17:55 -080094 this.team_ranking = ['971', '972', '973'];
Filip Kujawa210a03b2022-11-24 14:41:11 -080095 }
96}