|
| 1 | +--- |
| 2 | +title: Scoring API |
| 3 | +description: TypeScript reference for every function exported from lib/scoring.ts and lib/taste.ts. |
| 4 | +sidebar_position: 4 |
| 5 | +--- |
| 6 | + |
| 7 | +# Scoring API |
| 8 | + |
| 9 | +The full TypeScript reference for [`lib/scoring.ts`](https://github.com/TabletopFoundry/gamepulse/blob/main/lib/scoring.ts) and [`lib/taste.ts`](https://github.com/TabletopFoundry/gamepulse/blob/main/lib/taste.ts). |
| 10 | + |
| 11 | +## Types |
| 12 | + |
| 13 | +### `TasteDimension` |
| 14 | + |
| 15 | +```ts |
| 16 | +export const TASTE_DIMENSIONS = [ |
| 17 | + "strategy", "thematic", "party", "family", "solo", "conflict", |
| 18 | +] as const; |
| 19 | +export type TasteDimension = (typeof TASTE_DIMENSIONS)[number]; |
| 20 | +``` |
| 21 | + |
| 22 | +### `TasteProfile` |
| 23 | + |
| 24 | +```ts |
| 25 | +export type TasteProfile = Record<TasteDimension, number>; |
| 26 | +``` |
| 27 | + |
| 28 | +A 6-element vector. Each value is `0..100`. |
| 29 | + |
| 30 | +### `ConsensusLabel` |
| 31 | + |
| 32 | +```ts |
| 33 | +export const CONSENSUS_LABELS = [ |
| 34 | + "Divisive", |
| 35 | + "Critically Acclaimed", |
| 36 | + "Community Favorite", |
| 37 | + "Hidden Gem", |
| 38 | + "On the Rise", |
| 39 | +] as const; |
| 40 | +export type ConsensusLabel = (typeof CONSENSUS_LABELS)[number]; |
| 41 | +``` |
| 42 | + |
| 43 | +## Functions |
| 44 | + |
| 45 | +### `clamp(value, min, max)` |
| 46 | + |
| 47 | +Clamp a number into `[min, max]`. |
| 48 | + |
| 49 | +```ts |
| 50 | +clamp(120, 0, 100); // → 100 |
| 51 | +clamp(-5, 0, 100); // → 0 |
| 52 | +``` |
| 53 | + |
| 54 | +### `average(numbers)` |
| 55 | + |
| 56 | +Numeric mean. Returns `0` for an empty array. |
| 57 | + |
| 58 | +```ts |
| 59 | +average([8, 9, 10]); // → 9 |
| 60 | +average([]); // → 0 |
| 61 | +``` |
| 62 | + |
| 63 | +### `cosineSimilarity(a, b)` |
| 64 | + |
| 65 | +Cosine similarity between two taste profiles. Range: `[0, 1]` (assuming non-negative profiles). |
| 66 | + |
| 67 | +```ts |
| 68 | +import { cosineSimilarity } from "@/lib/scoring"; |
| 69 | + |
| 70 | +const alex = { strategy: 90, thematic: 70, party: 30, family: 45, solo: 65, conflict: 55 }; |
| 71 | +const tara = { strategy: 95, thematic: 60, party: 15, family: 30, solo: 70, conflict: 35 }; |
| 72 | + |
| 73 | +cosineSimilarity(alex, tara); // → ~0.97 |
| 74 | +``` |
| 75 | + |
| 76 | +Used to score user × critic and game × game similarity. |
| 77 | + |
| 78 | +### `pearson(left, right)` |
| 79 | + |
| 80 | +Pearson correlation. Range: `[-1, 1]`. Returns `0` if either array has length `< 2` or zero variance. |
| 81 | + |
| 82 | +```ts |
| 83 | +pearson([8, 9, 7, 6], [85, 92, 75, 60]); // → ~0.98 (perfectly correlated) |
| 84 | +``` |
| 85 | + |
| 86 | +Used to detect calibration agreement between a user's ratings and a critic's scores on the same games. |
| 87 | + |
| 88 | +### `buildConsensus(criticsScore, communityScore, rising)` |
| 89 | + |
| 90 | +Compute a consensus label. |
| 91 | + |
| 92 | +```ts |
| 93 | +buildConsensus(90, 88, 70); // → "Critically Acclaimed" |
| 94 | +buildConsensus(88, 72, 50); // → "Divisive" |
| 95 | +buildConsensus(75, 86, 65); // → "Hidden Gem" |
| 96 | +buildConsensus(60, 90, 40); // → "Community Favorite" |
| 97 | +buildConsensus(70, 70, 40); // → "On the Rise" |
| 98 | +``` |
| 99 | + |
| 100 | +The function is pure and synchronous. Pass in already-computed 0–100 scores. |
| 101 | + |
| 102 | +### `topGenres(profile)` |
| 103 | + |
| 104 | +Return the top dimensions in a taste profile, sorted descending. Used to render "Likes strategy + thematic" badges on user/critic cards. |
| 105 | + |
| 106 | +```ts |
| 107 | +topGenres({ strategy: 90, thematic: 72, party: 30, family: 45, solo: 65, conflict: 55 }); |
| 108 | +// → ["strategy", "thematic", "solo"] |
| 109 | +``` |
| 110 | + |
| 111 | +### `getPersonalizedScore(gameId, matchedCritics, reviewsByGame?)` |
| 112 | + |
| 113 | +Compute a 0–100 personalized score for `gameId` based on a user's top matched critics. |
| 114 | + |
| 115 | +```ts |
| 116 | +import { getPersonalizedScore } from "@/lib/scoring"; |
| 117 | +import { getMatchedCritics } from "@/lib/queries/user"; |
| 118 | + |
| 119 | +const matchedCritics = await getMatchedCritics(); |
| 120 | +const score = getPersonalizedScore(brassBirminghamId, matchedCritics); |
| 121 | +// → 91 |
| 122 | +``` |
| 123 | + |
| 124 | +Algorithm: |
| 125 | + |
| 126 | +1. Collect each matched critic's score for `gameId` (if any). |
| 127 | +2. Weighted average — weight = match score (cosine × pearson blend). |
| 128 | +3. If fewer than 2 matched critics reviewed the game, fall back to the global `critics_score`. |
| 129 | + |
| 130 | +The optional `reviewsByGame` parameter pre-fetches reviews in bulk for batch scoring (used on browse/dashboard pages). |
| 131 | + |
| 132 | +### `batchFetchCriticReviews(gameIds)` |
| 133 | + |
| 134 | +Fetch all critic reviews for a list of game IDs in a single SQL query. Returns a `Map<gameId, Array<{ critic_id, score }>>` for efficient batch personalized-scoring on listing pages. |
| 135 | + |
| 136 | +```ts |
| 137 | +const reviewsByGame = batchFetchCriticReviews([1, 2, 3, 4, 5]); |
| 138 | +const matchedCritics = await getMatchedCritics(); |
| 139 | + |
| 140 | +for (const game of games) { |
| 141 | + game.personalizedScore = getPersonalizedScore(game.id, matchedCritics, reviewsByGame); |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +Use this whenever you compute personalized scores for more than one game at a time. |
| 146 | + |
| 147 | +## Worked example |
| 148 | + |
| 149 | +The dashboard's "Predicted for you" rail: |
| 150 | + |
| 151 | +```ts |
| 152 | +// app/me/page.tsx (excerpt) |
| 153 | +import { getMatchedCritics } from "@/lib/queries/user"; |
| 154 | +import { batchFetchCriticReviews, getPersonalizedScore } from "@/lib/scoring"; |
| 155 | +import { getRecommendedGames } from "@/lib/queries/dashboard"; |
| 156 | + |
| 157 | +const matchedCritics = await getMatchedCritics(); |
| 158 | +const recommendations = getRecommendedGames(); |
| 159 | +const reviewsByGame = batchFetchCriticReviews(recommendations.map((g) => g.id)); |
| 160 | + |
| 161 | +const enriched = recommendations.map((game) => ({ |
| 162 | + ...game, |
| 163 | + predicted: getPersonalizedScore(game.id, matchedCritics, reviewsByGame), |
| 164 | +})); |
| 165 | +``` |
| 166 | + |
| 167 | +That's the whole personalization pipeline in 6 lines. |
0 commit comments