hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import type { BadgeStat, ProfileStats } from "@/api/client"; |
| 2 | import type { MessageKey } from "@/i18n/messages"; |
| 3 | |
| 4 | // The backend hands back badge ids and nothing else — the label and the icon |
| 5 | // behind one live here, exactly as they do for a role or a course kind. A |
| 6 | // badge shipped by the backend before this file has copy for it must still |
| 7 | // render, so every lookup falls back rather than throwing. |
| 8 | const KNOWN_BADGE_IDS = new Set([ |
| 9 | "homework_submitted_1", |
| 10 | "homework_submitted_10", |
| 11 | "homework_submitted_50", |
| 12 | "homework_on_time_10", |
| 13 | "homework_on_time_25", |
| 14 | "exam_sat_1", |
| 15 | "exam_sat_10", |
| 16 | "exam_sat_25", |
| 17 | "pomodoro_finished_10", |
| 18 | "pomodoro_finished_50", |
| 19 | "pomodoro_finished_200", |
| 20 | "pomodoro_focus_ms_36000000", |
| 21 | "pomodoro_focus_ms_180000000", |
| 22 | "marks_given_10", |
| 23 | "marks_given_50", |
| 24 | "marks_given_250", |
| 25 | "lessons_held_10", |
| 26 | "lessons_held_50", |
| 27 | "lessons_held_200", |
| 28 | "pool_approved_5", |
| 29 | "pool_approved_25", |
| 30 | "pool_approved_100", |
| 31 | "pool_published_1", |
| 32 | "pool_published_10", |
| 33 | "pool_published_50", |
| 34 | "lessons_attended_10", |
| 35 | "lessons_attended_50", |
| 36 | "lessons_attended_200", |
| 37 | "high_mark_1", |
| 38 | "high_mark_10", |
| 39 | "high_mark_25", |
| 40 | "study_streak_3", |
| 41 | "study_streak_7", |
| 42 | "study_streak_30", |
| 43 | ]); |
| 44 | |
| 45 | // Which lifetime counter on the profile's stats block a badge's stat reads. |
| 46 | const STAT_TOTAL_FIELD: Record<BadgeStat, keyof ProfileStats> = { |
| 47 | homework_submitted: "homework_submitted_total", |
| 48 | homework_on_time: "homework_on_time_total", |
| 49 | exam_sat: "exam_sat_total", |
| 50 | pomodoro_finished: "pomodoro_finished_total", |
| 51 | pomodoro_focus_ms: "pomodoro_focus_ms_total", |
| 52 | marks_given: "marks_given_total", |
| 53 | lessons_held: "lessons_held_total", |
| 54 | pool_approved: "pool_approved_total", |
| 55 | pool_published: "pool_published_total", |
| 56 | lessons_attended: "lessons_attended_total", |
| 57 | high_mark: "high_mark_total", |
| 58 | study_streak: "study_streak_total", |
| 59 | }; |
| 60 | |
| 61 | export function badgeNameKey(id: string): MessageKey | null { |
| 62 | return KNOWN_BADGE_IDS.has(id) ? (`badge.${id}.name` as MessageKey) : null; |
| 63 | } |
| 64 | |
| 65 | export function badgeDescKey(id: string): MessageKey | null { |
| 66 | return KNOWN_BADGE_IDS.has(id) ? (`badge.${id}.desc` as MessageKey) : null; |
| 67 | } |
| 68 | |
| 69 | /** Where the person stands on the counter this badge reads, or null if unknown. */ |
| 70 | export function badgeProgress(stat: string, stats: ProfileStats): number | null { |
| 71 | const field = STAT_TOTAL_FIELD[stat as BadgeStat]; |
| 72 | return field ? stats[field] : null; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Focus-time badges count milliseconds, which no one reads as a number. Every |
| 77 | * other stat is already a plain count. |
| 78 | */ |
| 79 | export function badgeProgressLabel(stat: string, value: number): string { |
| 80 | if (stat === "pomodoro_focus_ms") return `${Math.floor(value / 3_600_000)}`; |
| 81 | return `${value}`; |
| 82 | } |
| 83 | |
| 84 | export function badgeThresholdLabel(stat: string, threshold: number): string { |
| 85 | return badgeProgressLabel(stat, threshold); |
| 86 | } |
| 87 | |
| 88 | /** How much of the threshold is done, 0..1 — drives the progress bar's width. */ |
| 89 | export function badgeProgressRatio(current: number, threshold: number): number { |
| 90 | if (threshold <= 0) return 1; |
| 91 | return Math.min(1, Math.max(0, current / threshold)); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * What is left to earn it, in the same unit the labels use (hours for focus |
| 96 | * time, plain counts otherwise). Never negative — a permanent award whose |
| 97 | * counter later fell back still reads as done. |
| 98 | */ |
| 99 | export function badgeRemaining(stat: string, current: number, threshold: number): number { |
| 100 | if (stat === "pomodoro_focus_ms") { |
| 101 | return Math.max(0, Math.ceil((threshold - current) / 3_600_000)); |
| 102 | } |
| 103 | return Math.max(0, threshold - current); |
| 104 | } |
| 105 | |
| 106 | // ---- Grouping for the profile's badge board ---------------------------------- |
| 107 | |
| 108 | const KNOWN_STATS = new Set<string>(Object.keys(STAT_TOTAL_FIELD)); |
| 109 | |
| 110 | /** Section title for one counter's badge ladder, or null for a stat this build has no copy for. */ |
| 111 | export function badgeGroupKey(stat: string): MessageKey | null { |
| 112 | return KNOWN_STATS.has(stat) ? (`badges.group.${stat}` as MessageKey) : null; |
| 113 | } |
| 114 | |
| 115 | export type BadgeLadder<T extends { stat: string; threshold: number }> = { stat: string; entries: T[] }; |
| 116 | |
| 117 | /** The catalogue split into one ladder per counter, each ladder in threshold order. */ |
| 118 | export function badgeLadders<T extends { stat: string; threshold: number }>(catalog: T[]): BadgeLadder<T>[] { |
| 119 | const byStat = new Map<string, T[]>(); |
| 120 | for (const entry of catalog) byStat.set(entry.stat, [...(byStat.get(entry.stat) ?? []), entry]); |
| 121 | return [...byStat].map(([stat, entries]) => ({ |
| 122 | stat, |
| 123 | entries: [...entries].sort((a, b) => a.threshold - b.threshold), |
| 124 | })); |
| 125 | } |
| 126 | |
| 127 | export type BadgeTier = "bronze" | "silver" | "gold"; |
| 128 | |
| 129 | /** Medal colour for a rung: the last rung of a ladder is gold, the one before silver. */ |
| 130 | export function badgeTier(index: number, count: number): BadgeTier { |
| 131 | const fromTop = count - 1 - index; |
| 132 | if (fromTop === 0 && count > 1) return "gold"; |
| 133 | if (fromTop === 1) return "silver"; |
| 134 | return "bronze"; |
| 135 | } |
| 136 | |
| 137 | export type BadgeLevel = "rookie" | "explorer" | "master" | "legend"; |
| 138 | |
| 139 | /** A title that follows the share of the catalogue already earned — no score of its own. */ |
| 140 | export function badgeLevel(earned: number, total: number): BadgeLevel { |
| 141 | const share = total === 0 ? 0 : earned / total; |
| 142 | if (share >= 0.75) return "legend"; |
| 143 | if (share >= 0.4) return "master"; |
| 144 | if (earned > 0) return "explorer"; |
| 145 | return "rookie"; |
| 146 | } |