|
| 1 | +import { getRanking, RANKING_MAX, getBlocAtDate } from '../utils'; |
| 2 | +import { handleFixedPointScore } from '../handlers/points'; |
| 3 | +import { prisma } from './base'; |
| 4 | +import { CodamCoalitionRanking } from '@prisma/client'; |
| 5 | + |
| 6 | +const handleRankingBonusForDateTime = async function(ranking: CodamCoalitionRanking, atDateTime: Date = new Date()): Promise<void> { |
| 7 | + console.log(` - Applying bonus points for ranking ${ranking.name} at ${atDateTime.toISOString()}...`); |
| 8 | + |
| 9 | + ranking.last_bonus_run = atDateTime; |
| 10 | + await prisma.codamCoalitionRanking.update({ |
| 11 | + where: { |
| 12 | + type: ranking.type, |
| 13 | + }, |
| 14 | + data: { |
| 15 | + last_bonus_run: ranking.last_bonus_run, |
| 16 | + }, |
| 17 | + }); |
| 18 | + |
| 19 | + // Get top users in this ranking at the specified date and time |
| 20 | + const topUsers = await getRanking(prisma, ranking.type, atDateTime, RANKING_MAX); |
| 21 | + if (topUsers.length === 0) { |
| 22 | + console.log(` - No users found in ranking ${ranking.name}, skipping...`); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + // Calculate bonus points to award per hour |
| 27 | + const bonusPointsPerHour = Math.floor((ranking.bonus_points || 0) / 168); // 168 hours in a week |
| 28 | + |
| 29 | + if (bonusPointsPerHour <= 0) { |
| 30 | + console.warn(` - Bonus points to award this hour is 0 for ranking ${ranking.name}, skipping...`); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Get fixed type for ranking bonus |
| 35 | + const fixedType = await prisma.codamCoalitionFixedType.findUnique({ |
| 36 | + where: { |
| 37 | + type: 'ranking_bonus', |
| 38 | + }, |
| 39 | + }); |
| 40 | + if (!fixedType) { |
| 41 | + console.error(` - Fixed type "ranking_bonus" not found, cannot award ranking bonus points for ranking ${ranking.name}, skipping...`); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Award bonus points to the #1 spot (can be multiple users!) |
| 46 | + const topRankings = topUsers.filter(user => user.rank === 1); |
| 47 | + const pointsPerUser = Math.floor(bonusPointsPerHour / topRankings.length); |
| 48 | + for (const topUser of topRankings) { |
| 49 | + await handleFixedPointScore(prisma, fixedType, null, topUser.user.id, pointsPerUser, `Bonus points for being in${topRankings.length > 1 ? ' shared' : ''} first place on the ${ranking.name} Ranking`, atDateTime); |
| 50 | + console.log(` - Awarded ${pointsPerUser} bonus points to ${topUser.user.login} (coalition ${(topUser.coalition ? topUser.coalition?.name : 'N/A')}) for ranking ${ranking.name}`); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +export const handleRankingBonuses = async function(): Promise<void> { |
| 55 | + console.log('Checking if any ranking bonuses need to be applied...'); |
| 56 | + const now = new Date(); |
| 57 | + const currentBloc = await getBlocAtDate(prisma, now); |
| 58 | + |
| 59 | + // Check if we're in the final week of the current season |
| 60 | + if (currentBloc === null) { |
| 61 | + console.log(' - No ongoing season, skipping ranking bonus application.'); |
| 62 | + return; |
| 63 | + } |
| 64 | + const oneWeekBeforeEnd = new Date(currentBloc.end_at.getTime() - (7 * 24 * 60 * 60 * 1000)); |
| 65 | + if (now < oneWeekBeforeEnd) { |
| 66 | + console.log(` - Not in the final week of the season yet, skipping ranking bonus application. Final week starts at ${oneWeekBeforeEnd.toISOString()}`); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Get all rankings that are not disabled and have bonus points defined |
| 71 | + const rankings = await prisma.codamCoalitionRanking.findMany({ |
| 72 | + where: { |
| 73 | + disabled: false, |
| 74 | + bonus_points: { |
| 75 | + gt: 0, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + for (const ranking of rankings) { |
| 81 | + if (!ranking.last_bonus_run || ranking.last_bonus_run === undefined || ranking.last_bonus_run < oneWeekBeforeEnd) { |
| 82 | + console.log(` - Setting last bonus run to 1 week before the end of the current season...`); |
| 83 | + ranking.last_bonus_run = oneWeekBeforeEnd; |
| 84 | + await prisma.codamCoalitionRanking.update({ |
| 85 | + where: { |
| 86 | + type: ranking.type, |
| 87 | + }, |
| 88 | + data: { |
| 89 | + last_bonus_run: ranking.last_bonus_run, |
| 90 | + }, |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + // Apply bonus points for each hour since last run up until now |
| 95 | + for (let bonusDateTime = new Date(ranking.last_bonus_run.getTime() + 60 * 60 * 1000); bonusDateTime <= now; bonusDateTime = new Date(bonusDateTime.getTime() + 60 * 60 * 1000)) { |
| 96 | + await handleRankingBonusForDateTime(ranking, bonusDateTime); |
| 97 | + } |
| 98 | + } |
| 99 | +}; |
0 commit comments