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,EAAO,IAAI,EACrB,YAAa,CAAA,EACb,OAAQ,CAAA,EACR,SAAUE,EAAIF,EAAO,QAAQ,EAC7B,IAAK,CAAA,EACL,KAAM,CAAA,EACN,MAAOA,EAAO,MACd,UAAW,CAAA,CAAC,CAEd,CAGO,SAASI,EAAS5C,EAAYiC,EAAuB,CAC3D,OAAOA,EAAO,SAASjC,EAAK,IAAI,MAAMiC,CAAI,GAAK,SAASjC,EAAK,IAAI,EAClE","names":["SCALE","optionCount","q","pointsFor","answer","key","keyBounds","quiz","out","min","max","pts","_","i","scoreAnswers","answers","bounds","scores","total","a","resultOf","rank","order","b","leanShare","left","right","l","r","leanOutcome","middle","cut","share","bandOf","score","bands","from","encodeScores","k","decodeScores","code","clean","n","arenaHref","prefill","params","prefillIdentity","picked","sanitizeIdentity","one","v","quizPath"],"sources":["../../../../../../src/lib/quiz/engine.ts"],"sourcesContent":["import { POSITIONS, ROLES, sanitizeIdentity } from '$lib/types/arena-identity';\n\n/**\n * RAT-648: the quiz engine. A quiz is data: questions whose answers add\n * weights to scored keys, and a rule that turns the keys' 0–100 scores into\n * an outcome. One runner (QuizRunner.svelte) plays every quiz; a page only\n * brings its own copy. Nothing is stored server-side: a finished result\n * lives in the page address (?r=, see encodeScores), which is also what a\n * shared link carries.\n */\n\n/** Points per key. Keys a question doesn't touch are left out. */\nexport type Weights = Readonly<Record<string, number>>;\n\n/** A statement answered on the agree scale: the step's value times each weight. */\nexport interface ScaleQuestion {\n\tkind: 'scale';\n\ttext: string;\n\tweights: Weights;\n}\n\nexport interface ChoiceOption {\n\tlabel: string;\n\tweights: Weights;\n}\n\n/** A question with its own answers, each adding its own weights. */\nexport interface ChoiceQuestion {\n\tkind: 'choice';\n\ttext: string;\n\toptions: readonly ChoiceOption[];\n}\n\nexport type Question = ScaleQuestion | ChoiceQuestion;\n\n/** The agree scale, left to right as it reads on screen. */\nexport const SCALE = [\n\t{ value: -2, label: 'Strongly disagree' },\n\t{ value: -1, label: 'Disagree' },\n\t{ value: 0, label: 'Not sure' },\n\t{ value: 1, label: 'Agree' },\n\t{ value: 2, label: 'Strongly agree' }\n] as const;\n\n/** 0–100 per key, whole numbers. */\nexport type Scores = Record<string, number>;\n\n/** One answer per question: the index of the option (or of the SCALE step); null = unanswered. */\nexport type Answers = readonly (number | null)[];\n\n/**\n * How a result reads: every key as a bar (the first `shown` open, ties in\n * `order`), a lean between two keys, or one score.\n */\nexport type QuizView =\n\t| { kind: 'profile'; shown: number; order: readonly string[] }\n\t| { kind: 'lean'; left: string; right: string }\n\t| { kind: 'score'; key: string };\n\n/** An outcome's name (no full stop; the headline adds it) and its one-line meaning. */\nexport interface Outcome {\n\tlabel: string;\n\tline: string;\n}\n\nexport type ArenaRole = (typeof ROLES)[number];\nexport type ArenaPosition = (typeof POSITIONS)[number];\n\n/** What a result pre-selects in the arena's \"who i am\" picker (/a/submit). */\nexport interface ArenaPrefill {\n\trole?: ArenaRole;\n\tposition?: ArenaPosition;\n\tkinks?: string[];\n}\n\nexport interface QuizResult {\n\tscores: Scores;\n\toutcome: string;\n}\n\nexport interface Quiz {\n\t/** the page is /quiz/<slug> */\n\tslug: string;\n\t/** \"BDSM test\": how the page and the share text name it */\n\tname: string;\n\t/** what its button says: \"Take the test\" or \"Take the quiz\" */\n\tnoun: 'test' | 'quiz';\n\t/** the scored keys, in the order the share code writes them */\n\tkeys: readonly string[];\n\tquestions: readonly Question[];\n\tview: QuizView;\n\t/** every outcome decide() can return, and for a profile every key */\n\toutcomes: Readonly<Record<string, Outcome>>;\n\tdecide(scores: Scores): string;\n\tarena(result: QuizResult): ArenaPrefill;\n\t/** the text that goes out with a shared link */\n\tshareText(result: QuizResult): string;\n}\n\n/** How many answers a question offers. */\nexport function optionCount(q: Question): number {\n\treturn q.kind === 'scale' ? SCALE.length : q.options.length;\n}\n\n/** The points one answer adds to one key. */\nexport function pointsFor(q: Question, answer: number, key: string): number {\n\tif (q.kind === 'scale') return (SCALE[answer]?.value ?? 0) * (q.weights[key] ?? 0);\n\treturn q.options[answer]?.weights[key] ?? 0;\n}\n\n/** The lowest and highest total each key can reach over the whole quiz. */\nexport function keyBounds(quiz: Quiz): Record<string, { min: number; max: number }> {\n\tconst out: Record<string, { min: number; max: number }> = {};\n\tfor (const key of quiz.keys) {\n\t\tlet min = 0;\n\t\tlet max = 0;\n\t\tfor (const q of quiz.questions) {\n\t\t\tconst pts = Array.from({ length: optionCount(q) }, (_, i) => pointsFor(q, i, key));\n\t\t\tmin += Math.min(...pts);\n\t\t\tmax += Math.max(...pts);\n\t\t}\n\t\tout[key] = { min, max };\n\t}\n\treturn out;\n}\n\n/**\n * Each key's total, placed between the lowest and highest it could have been:\n * 0 is every answer against it, 100 every answer for it. A key no question\n * moves scores 0. Unanswered questions add nothing.\n */\nexport function scoreAnswers(quiz: Quiz, answers: Answers): Scores {\n\tconst bounds = keyBounds(quiz);\n\tconst scores: Scores = {};\n\tfor (const key of quiz.keys) {\n\t\tlet total = 0;\n\t\tquiz.questions.forEach((q, i) => {\n\t\t\tconst a = answers[i];\n\t\t\tif (a !== null && a !== undefined) total += pointsFor(q, a, key);\n\t\t});\n\t\tconst { min, max } = bounds[key];\n\t\tscores[key] = max > min ? Math.round(((total - min) / (max - min)) * 100) : 0;\n\t}\n\treturn scores;\n}\n\nexport function resultOf(quiz: Quiz, scores: Scores): QuizResult {\n\treturn { scores, outcome: quiz.decide(scores) };\n}\n\n/** Keys from highest score to lowest; a tie goes to whichever `order` lists first. */\nexport function rank(scores: Scores, order: readonly string[]): string[] {\n\treturn [...order].sort(\n\t\t(a, b) => (scores[b] ?? 0) - (scores[a] ?? 0) || order.indexOf(a) - order.indexOf(b)\n\t);\n}\n\n/** The left key's share of the two, 0–100 (50 when neither scored). */\nexport function leanShare(scores: Scores, left: string, right: string): number {\n\tconst l = scores[left] ?? 0;\n\tconst r = scores[right] ?? 0;\n\treturn l + r > 0 ? Math.round((l / (l + r)) * 100) : 50;\n}\n\n/**\n * A lean read as one of three: the left key at 63% or more, the right key at\n * 63% or more, the middle (vers, switch) in between. Questions are written\n * so the two keys can reach the same maximum, which keeps the split fair.\n */\nexport function leanOutcome(\n\tscores: Scores,\n\tleft: string,\n\tright: string,\n\tmiddle: string,\n\tcut = 63\n): string {\n\tconst share = leanShare(scores, left, right);\n\tif (share >= cut) return left;\n\tif (share <= 100 - cut) return right;\n\treturn middle;\n}\n\n/** The outcome whose range a score falls in: `bands` lists each outcome's lowest score, rising. */\nexport function bandOf(score: number, bands: readonly (readonly [number, string])[]): string {\n\tlet out = bands[0][1];\n\tfor (const [from, key] of bands) if (score >= from) out = key;\n\treturn out;\n}\n\n/**\n * A result in the page address: two base-36 digits per key, in the quiz's\n * key order (100 is \"2s\"). A code that doesn't fit this quiz is no result.\n */\nexport function encodeScores(quiz: Quiz, scores: Scores): string {\n\treturn quiz.keys\n\t\t.map((k) =>\n\t\t\tMath.max(0, Math.min(100, Math.round(scores[k] ?? 0)))\n\t\t\t\t.toString(36)\n\t\t\t\t.padStart(2, '0')\n\t\t)\n\t\t.join('');\n}\n\nexport function decodeScores(quiz: Quiz, code: string | null | undefined): Scores | null {\n\t// a share sheet's \"copy\" can glue the message onto the link: read the code, stop at the rest\n\tconst clean =\n\t\t(code ?? '')\n\t\t\t.trim()\n\t\t\t.toLowerCase()\n\t\t\t.match(/^[0-9a-z]+/)?.[0] ?? '';\n\tif (clean.length !== quiz.keys.length * 2) return null;\n\tconst scores: Scores = {};\n\tfor (let i = 0; i < quiz.keys.length; i++) {\n\t\tconst n = parseInt(clean.slice(i * 2, i * 2 + 2), 36);\n\t\tif (!Number.isInteger(n) || n < 0 || n > 100) return null;\n\t\tscores[quiz.keys[i]] = n;\n\t}\n\treturn scores;\n}\n\n/** The arena submit page with the result's picks in its address (read there by prefillIdentity). */\nexport function arenaHref(prefill: ArenaPrefill): string {\n\tconst params = new URLSearchParams();\n\tif (prefill.role) params.set('role', prefill.role);\n\tif (prefill.position) params.set('position', prefill.position);\n\tif (prefill.kinks?.length) params.set('kinks', prefill.kinks.join(','));\n\tconst q = params.toString();\n\treturn q ? `/a/submit?${q}` : '/a/submit';\n}\n\n/**\n * The other end of arenaHref: the submit page's \"who i am\" picks from its\n * address, in the picker's shape (every axis a list, single axes at most one).\n * sanitizeIdentity drops whatever the axes don't know and caps the kinks, so a\n * hand-edited link can't tag more than a card may carry. Only a starting point:\n * every pick stays changeable and nothing is sent until the person deploys.\n */\nexport function prefillIdentity(params: URLSearchParams): Record<string, string[]> {\n\tconst picked = sanitizeIdentity({\n\t\trole: params.get('role'),\n\t\tposition: params.get('position'),\n\t\tkinks: params.get('kinks')?.split(',') ?? []\n\t});\n\tconst one = (v: string | null) => (v ? [v] : []);\n\treturn {\n\t\trole: one(picked.role),\n\t\torientation: [],\n\t\tgender: [],\n\t\tposition: one(picked.position),\n\t\tage: [],\n\t\tbody: [],\n\t\tkinks: picked.kinks,\n\t\tethnicity: []\n\t};\n}\n\n/** The page address of a quiz, with a result when there is one. */\nexport function quizPath(quiz: Quiz, code?: string): string {\n\treturn code ? `/quiz/${quiz.slug}?r=${code}` : `/quiz/${quiz.slug}`;\n}\n"],"version":3}