diff --git a/config.json.example b/config.json.example index f8548c1..075872a 100644 --- a/config.json.example +++ b/config.json.example @@ -38,5 +38,6 @@ "max": 20, // 20 requests in 15min time window "statusCode": 200 } - } + }, + "maxRewardTimePerSegmentInSeconds": 86400 // maximum time a user get rewarded in the leaderboard for a single segment } diff --git a/src/config.ts b/src/config.ts index 7a172cc..a37fb6c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -45,6 +45,7 @@ addDefaults(config, { }, userCounterURL: null, youtubeAPIKey: null, + maxRewardTimePerSegmentInSeconds: 86400, }); // Add defaults diff --git a/src/routes/getTopUsers.ts b/src/routes/getTopUsers.ts index cf069a1..c7a6141 100644 --- a/src/routes/getTopUsers.ts +++ b/src/routes/getTopUsers.ts @@ -5,6 +5,7 @@ import {Request, Response} from 'express'; const MILLISECONDS_IN_MINUTE = 60000; const getTopUsersWithCache = createMemoryCache(generateTopUsersStats, config.getTopUsersCacheTimeMinutes * MILLISECONDS_IN_MINUTE); +const maxRewardTimePerSegmentInSeconds = config.maxRewardTimePerSegmentInSeconds ?? 86400; function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boolean = false) { return new Promise((resolve) => { @@ -25,7 +26,7 @@ function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boolean = f } const rows = db.prepare('all', "SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount," + - "SUM((sponsorTimes.endTime - sponsorTimes.startTime) / 60 * sponsorTimes.views) as minutesSaved, " + + "SUM(((CASE WHEN endTime - startTime > " + maxRewardTimePerSegmentInSeconds + " THEN " + maxRewardTimePerSegmentInSeconds + " ELSE endTime - startTime END) / 60) * views) as minutesSaved," + "SUM(votes) as userVotes, " + additionalFields + "IFNULL(userNames.userName, sponsorTimes.userID) as userName FROM sponsorTimes LEFT JOIN userNames ON sponsorTimes.userID=userNames.userID " + diff --git a/src/types/config.model.ts b/src/types/config.model.ts index 88af3d2..110b210 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -36,6 +36,7 @@ export interface SBSConfig { minimumPrefix?: string; maximumPrefix?: string; redis?: redis.ClientOpts; + maxRewardTimePerSegmentInSeconds?: number; } export interface WebhookConfig {