Skip to content

Commit 25c4c8f

Browse files
CopilotAdez017
andcommitted
Implement label-based leaderboard points calculation
Co-authored-by: Adez017 <[email protected]>
1 parent ac627ec commit 25c4c8f

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/lib/statsProvider.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ interface PRDetails {
4848
mergedAt: string;
4949
repoName: string;
5050
number: number;
51+
points: number;
5152
}
5253

5354
interface Contributor {
@@ -75,6 +76,7 @@ interface PullRequestItem {
7576
title?: string;
7677
html_url?: string;
7778
number?: number;
79+
labels?: Array<{ name: string }>;
7880
}
7981

8082
// Enhanced contributor type for internal processing (stores all PRs)
@@ -96,6 +98,31 @@ const MAX_CONCURRENT_REQUESTS = 8;
9698
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes cache
9799
const MAX_PAGES_PER_REPO = 20;
98100

101+
// Function to calculate points based on PR labels
102+
const calculatePointsForPR = (labels?: Array<{ name: string }>): number => {
103+
if (!labels || labels.length === 0) {
104+
return 0; // No points if no labels
105+
}
106+
107+
const labelNames = labels.map(label => label.name.toLowerCase());
108+
109+
// Check if PR has the "recode" label
110+
if (!labelNames.includes('recode')) {
111+
return 0; // No points if "recode" label is missing
112+
}
113+
114+
// Check for level labels and assign points accordingly
115+
if (labelNames.includes('level 1')) {
116+
return 10;
117+
} else if (labelNames.includes('level 2')) {
118+
return 30;
119+
} else if (labelNames.includes('level 3')) {
120+
return 50;
121+
}
122+
123+
return 0; // No points if no level label
124+
};
125+
99126
// Time filter utility functions
100127
const getTimeFilterDate = (filter: TimeFilter): Date | null => {
101128
const now = new Date();
@@ -163,11 +190,14 @@ export function CommunityStatsProvider({ children }: CommunityStatsProviderProps
163190
isPRInTimeRange(pr.mergedAt, currentTimeFilter)
164191
);
165192

193+
// Calculate total points from all filtered PRs
194+
const totalPoints = filteredPRs.reduce((sum, pr) => sum + pr.points, 0);
195+
166196
return {
167197
username: contributor.username,
168198
avatar: contributor.avatar,
169199
profile: contributor.profile,
170-
points: filteredPRs.length * POINTS_PER_PR,
200+
points: totalPoints,
171201
prs: filteredPRs.length,
172202
prDetails: filteredPRs, // For backward compatibility, though we'll use the new function
173203
};
@@ -319,6 +349,9 @@ export function CommunityStatsProvider({ children }: CommunityStatsProviderProps
319349
}
320350
const contributor = contributorMap.get(username)!;
321351

352+
// Calculate points for this PR based on labels
353+
const prPoints = calculatePointsForPR(pr.labels);
354+
322355
// Add detailed PR information to the full list
323356
if (pr.title && pr.html_url && pr.merged_at && pr.number) {
324357
contributor.allPRDetails.push({
@@ -327,6 +360,7 @@ export function CommunityStatsProvider({ children }: CommunityStatsProviderProps
327360
mergedAt: pr.merged_at,
328361
repoName,
329362
number: pr.number,
363+
points: prPoints,
330364
});
331365
}
332366
});

0 commit comments

Comments
 (0)