blob: f61d901b0ad39b26d99b9e34090d1e4e40d7198f [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).
21 team_ranking: number[] = [971, 972, 973];
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();
61 builder.finish(
62 SubmitDriverRanking.createSubmitDriverRanking(
63 builder,
64 this.match_number,
65 this.team_ranking[0],
66 this.team_ranking[1],
67 this.team_ranking[2]
68 )
69 );
70 const buffer = builder.asUint8Array();
71 const res = await fetch('/requests/submit/submit_driver_ranking', {
72 method: 'POST',
73 body: buffer,
74 });
75
76 if (!res.ok) {
77 const resBuffer = await res.arrayBuffer();
78 const fbBuffer = new ByteBuffer(new Uint8Array(resBuffer));
79 const parsedResponse = ErrorResponse.getRootAsErrorResponse(fbBuffer);
80
81 const errorMessage = parsedResponse.errorMessage();
82 this.errorMessage = `Received ${res.status} ${res.statusText}: "${errorMessage}"`;
83 return;
84 }
85
86 // Increment the match number.
87 this.match_number = this.match_number + 1;
88
89 // Reset Data.
90 this.section = 'TeamSelection';
91 this.team_ranking = [971, 972, 973];
92 }
93}