Skip to content

Commit 9b9cdf4

Browse files
authored
Merge pull request #55 from codersforcauses/issue-42-Add_Group_API_fields_for_honour_po
Issue 42 add group api fields for honour po
2 parents 9a6c32e + 6e13c85 commit 9b9cdf4

3 files changed

Lines changed: 116 additions & 93 deletions

File tree

client/src/components/ui/Leaderboard/List.tsx

Lines changed: 110 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,150 @@
11
import { useEffect, useState } from "react";
22

3+
const groupNames: String[] = ["", "Group A", "Group B", "Group C"];
4+
35
export default function List({ data = [] }) {
46
// Mock data for development (with four teams per group)
57
const mockData = [
68
// Group A teams
79
{
8-
groupRank: 1,
9-
teamName: "Drone Rangers",
10-
honourPoints: 350,
11-
regularPoints: 420,
12-
missionTime: "01:45:22",
13-
group: "Group A",
14-
qualified: true,
10+
team_name: "Drone Rangers",
11+
honor_points: 350,
12+
regular_points: 420,
13+
total_time_seconds: 154,
14+
group: 1,
1515
},
1616
{
17-
groupRank: 2,
18-
teamName: "RoboPilots",
19-
honourPoints: 290,
20-
regularPoints: 365,
21-
missionTime: "02:05:33",
22-
group: "Group A",
23-
qualified: true,
17+
team_name: "RoboPilots",
18+
honor_points: 290,
19+
regular_points: 365,
20+
total_time_seconds: 234,
21+
group: 1,
2422
},
2523
{
26-
groupRank: 3,
27-
teamName: "Airborne Coders",
28-
honourPoints: 210,
29-
regularPoints: 280,
30-
missionTime: "02:15:40",
31-
group: "Group A",
32-
qualified: false,
24+
team_name: "Airborne Coders",
25+
honor_points: 210,
26+
regular_points: 280,
27+
total_time_seconds: 163,
28+
group: 1,
3329
},
3430
{
35-
groupRank: 4,
36-
teamName: "Flying Algorithms",
37-
honourPoints: 180,
38-
regularPoints: 240,
39-
missionTime: "02:30:15",
40-
group: "Group A",
41-
qualified: false,
31+
team_name: "Flying Algorithms",
32+
honor_points: 180,
33+
regular_points: 240,
34+
total_time_seconds: 274,
35+
group: 1,
4236
},
4337

4438
// Group B teams
4539
{
46-
groupRank: 1,
47-
teamName: "Sky Navigators",
48-
honourPoints: 320,
49-
regularPoints: 380,
50-
missionTime: "01:52:10",
51-
group: "Group B",
52-
qualified: true,
40+
team_name: "Sky Navigators",
41+
honor_points: 320,
42+
regular_points: 380,
43+
total_time_seconds: 198,
44+
group: 2,
5345
},
5446
{
55-
groupRank: 2,
56-
teamName: "Circuit Flyers",
57-
honourPoints: 250,
58-
regularPoints: 310,
59-
missionTime: "02:18:20",
60-
group: "Group B",
61-
qualified: true,
47+
team_name: "Circuit Flyers",
48+
honor_points: 250,
49+
regular_points: 310,
50+
total_time_seconds: 193,
51+
group: 2,
6252
},
6353
{
64-
groupRank: 3,
65-
teamName: "Byte Squadron",
66-
honourPoints: 220,
67-
regularPoints: 290,
68-
missionTime: "02:25:17",
69-
group: "Group B",
70-
qualified: false,
54+
team_name: "Byte Squadron",
55+
honor_points: 220,
56+
regular_points: 290,
57+
total_time_seconds: 198,
58+
group: 2,
7159
},
7260
{
73-
groupRank: 4,
74-
teamName: "Quantum Wings",
75-
honourPoints: 190,
76-
regularPoints: 250,
77-
missionTime: "02:35:45",
78-
group: "Group B",
79-
qualified: false,
61+
team_name: "Quantum Wings",
62+
honor_points: 190,
63+
regular_points: 250,
64+
total_time_seconds: 183,
65+
group: 2,
8066
},
8167

8268
// Group C teams
8369
{
84-
groupRank: 1,
85-
teamName: "Aerial Mechanics",
86-
honourPoints: 270,
87-
regularPoints: 340,
88-
missionTime: "02:11:45",
89-
group: "Group C",
90-
qualified: true,
70+
team_name: "Aerial Mechanics",
71+
honor_points: 270,
72+
regular_points: 340,
73+
total_time_seconds: 348,
74+
group: 3,
9175
},
9276
{
93-
groupRank: 2,
94-
teamName: "PropCoders",
95-
honourPoints: 240,
96-
regularPoints: 300,
97-
missionTime: "02:20:30",
98-
group: "Group C",
99-
qualified: true,
77+
team_name: "PropCoders",
78+
honor_points: 240,
79+
regular_points: 300,
80+
total_time_seconds: 314,
81+
group: 3,
10082
},
10183
{
102-
groupRank: 3,
103-
teamName: "Binary Flyers",
104-
honourPoints: 200,
105-
regularPoints: 270,
106-
missionTime: "02:28:55",
107-
group: "Group C",
108-
qualified: false,
84+
team_name: "Binary Flyers",
85+
honor_points: 200,
86+
regular_points: 270,
87+
total_time_seconds: 109,
88+
group: 3,
10989
},
11090
{
111-
groupRank: 4,
112-
teamName: "Aerodynamic Devs",
113-
honourPoints: 170,
114-
regularPoints: 230,
115-
missionTime: "02:38:20",
116-
group: "Group C",
117-
qualified: false,
91+
team_name: "Aerodynamic Devs",
92+
honor_points: 170,
93+
regular_points: 230,
94+
total_time_seconds: 54,
95+
group: 3,
11896
},
11997
];
12098

99+
// TODO: Fetch real data from api/match/group
100+
121101
// Use provided data or fallback to mock data
122-
const teamsData = data.length > 0 ? data : mockData;
102+
const rawData = data.length > 0 ? data : mockData;
103+
104+
// Get unique groups
105+
const groupIds = [...new Set(rawData.map((item) => item.group))];
106+
107+
function processData(rawData: Array<any>) {
108+
let teamsData: Array<any> = [];
109+
110+
// For each group
111+
for (let i = 0; i < groupIds.length; i++) {
112+
// Get all the teams in that group
113+
let groupData = rawData.filter((team) => {
114+
return team.group === groupIds[i];
115+
});
116+
117+
// Sort them descending by points
118+
groupData.sort((teamA, teamB) => {
119+
return teamB.regular_points - teamA.regular_points;
120+
// TODO: Implement sorting by honor points in case of draw, then by time to finish
121+
});
122+
123+
// Add each of those teams into the teamData with calculated fields
124+
for (let j = 0; j < groupData.length; j++) {
125+
let team = groupData[j];
126+
team.group = groupNames[team.group];
127+
team.group_rank = j + 1; // TODO: Make resistant to ties
128+
team.qualified = team.group_rank <= 2; // TODO: Make resistant to ties
129+
team.mission_time =
130+
"" +
131+
Math.floor(team.total_time_seconds / 60) +
132+
":" +
133+
(team.total_time_seconds % 60);
134+
teamsData.push(team);
135+
}
136+
}
137+
return teamsData;
138+
}
139+
140+
// Add calculated fields group_rank and qualified to the data
141+
const teamsData = processData(rawData);
123142

124143
// Get unique groups for dropdown
125144
const groups = [...new Set(teamsData.map((item) => item.group))];
126145

127146
// Initialize selectedGroup with the first group instead of "All Groups"
128-
const [selectedGroup, setSelectedGroup] = useState(groups[0] || "Group A");
147+
const [selectedGroup, setSelectedGroup] = useState(groups[0] || 1);
129148

130149
// Filter data for the selected group only
131150
const displayData = teamsData.filter((item) => item.group === selectedGroup);
@@ -181,12 +200,12 @@ export default function List({ data = [] }) {
181200
}`}
182201
>
183202
<td className="body-lg px-6 py-4 font-bold italic">
184-
{row.groupRank}
203+
{row.group_rank}
185204
</td>
186-
<td className="px-6 py-4">{row.teamName}</td>
187-
<td className="px-6 py-4">{row.honourPoints}</td>
188-
<td className="px-6 py-4">{row.regularPoints}</td>
189-
<td className="px-6 py-4">{row.missionTime}</td>
205+
<td className="px-6 py-4">{row.team_name}</td>
206+
<td className="px-6 py-4">{row.honor_points}</td>
207+
<td className="px-6 py-4">{row.regular_points}</td>
208+
<td className="px-6 py-4">{row.mission_time}</td>
190209
<td className="px-6 py-4">
191210
{row.qualified ? (
192211
<span className="font-medium text-green-600">

server/api/match/serializers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ class GroupSerializer(serializers.Serializer):
2020
group = serializers.IntegerField()
2121
team = serializers.IntegerField()
2222
team_name = serializers.CharField(max_length=64)
23-
total_points = serializers.IntegerField()
23+
regular_points = serializers.IntegerField()
24+
honor_points = serializers.IntegerField()
25+
total_time_seconds = serializers.IntegerField()

server/api/match/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ def get_queryset(self):
2424
result = MatchResult.objects.values("team", "team__team_name", "match__group_id")\
2525
.annotate(team_name=F("team__team_name"))\
2626
.annotate(group=F("match__group_id"))\
27-
.annotate(total_points=Sum('point'))
27+
.annotate(honor_points=Sum('honor_point'))\
28+
.annotate(regular_points=Sum('point'))\
29+
.annotate(total_time_seconds=Sum('completed_time_second'))
2830
return result

0 commit comments

Comments
 (0)