Redesign scoreboard
This commit is contained in:
parent
af21f2bcdc
commit
fc270d437f
4 changed files with 231 additions and 90 deletions
39
constants.ts
39
constants.ts
|
|
@ -1,5 +1,5 @@
|
|||
import { Triangle, Diamond, Circle, Square } from 'lucide-react';
|
||||
import type { GameConfig, Player } from './types';
|
||||
import type { GameConfig, Player, PointsBreakdown } from './types';
|
||||
|
||||
export const COLORS = {
|
||||
red: 'bg-red-600',
|
||||
|
|
@ -48,33 +48,50 @@ interface PointsCalculationParams {
|
|||
config: GameConfig;
|
||||
}
|
||||
|
||||
export const calculatePoints = (params: PointsCalculationParams): number => {
|
||||
export const calculatePointsWithBreakdown = (params: PointsCalculationParams): PointsBreakdown => {
|
||||
const { isCorrect, timeLeftMs, questionTimeMs, streak, playerRank, isFirstCorrect, config } = params;
|
||||
|
||||
const breakdown: PointsBreakdown = {
|
||||
basePoints: 0,
|
||||
streakBonus: 0,
|
||||
comebackBonus: 0,
|
||||
firstCorrectBonus: 0,
|
||||
penalty: 0,
|
||||
total: 0,
|
||||
};
|
||||
|
||||
if (!isCorrect) {
|
||||
if (config.penaltyForWrongAnswer) {
|
||||
return -Math.round(POINTS_PER_QUESTION * (config.penaltyPercent / 100));
|
||||
breakdown.penalty = Math.round(POINTS_PER_QUESTION * (config.penaltyPercent / 100));
|
||||
breakdown.total = -breakdown.penalty;
|
||||
}
|
||||
return 0;
|
||||
return breakdown;
|
||||
}
|
||||
|
||||
let points = calculateBasePoints(timeLeftMs, questionTimeMs);
|
||||
breakdown.basePoints = calculateBasePoints(timeLeftMs, questionTimeMs);
|
||||
let pointsAfterStreak = breakdown.basePoints;
|
||||
|
||||
if (config.streakBonusEnabled && streak >= config.streakThreshold) {
|
||||
const streakBonus = streak - config.streakThreshold;
|
||||
const multiplier = config.streakMultiplier + (streakBonus * (config.streakMultiplier - 1));
|
||||
points = Math.round(points * multiplier);
|
||||
const streakCount = streak - config.streakThreshold;
|
||||
const multiplier = config.streakMultiplier + (streakCount * (config.streakMultiplier - 1));
|
||||
pointsAfterStreak = Math.round(breakdown.basePoints * multiplier);
|
||||
breakdown.streakBonus = pointsAfterStreak - breakdown.basePoints;
|
||||
}
|
||||
|
||||
if (config.comebackBonusEnabled && playerRank > 3) {
|
||||
points += config.comebackBonusPoints;
|
||||
breakdown.comebackBonus = config.comebackBonusPoints;
|
||||
}
|
||||
|
||||
if (config.firstCorrectBonusEnabled && isFirstCorrect) {
|
||||
points += config.firstCorrectBonusPoints;
|
||||
breakdown.firstCorrectBonus = config.firstCorrectBonusPoints;
|
||||
}
|
||||
|
||||
return points;
|
||||
breakdown.total = pointsAfterStreak + breakdown.comebackBonus + breakdown.firstCorrectBonus;
|
||||
return breakdown;
|
||||
};
|
||||
|
||||
export const calculatePoints = (params: PointsCalculationParams): number => {
|
||||
return calculatePointsWithBreakdown(params).total;
|
||||
};
|
||||
|
||||
export const getPlayerRank = (playerId: string, players: Player[]): number => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue