CSP Safety Officer Challenge Games
import React, { useEffect, useMemo, useState } from "react";
/*
CSP Safety Officer Challenge — Interactive Learning Game (Light Theme)
Single-file React + TailwindCSS
Stores progress in localStorage under: csp_game_state_v1
*/
const STORE_KEY = "csp_game_state_v1";
function useStoredState(init) {
const [state, setState] = useState(() => {
try { const raw = localStorage.getItem(STORE_KEY); return raw ? JSON.parse(raw) : init; } catch { return init; }
});
useEffect(() => { try { localStorage.setItem(STORE_KEY, JSON.stringify(state)); } catch {} }, [state]);
return [state, setState];
}
function dayISO(d) { const t = new Date(d); return isNaN(+t) ? "" : t.toISOString().slice(0,10); }
function addDays(date, days) { const d = new Date(date); d.setDate(d.getDate()+days); return d; }
function rnd(min, max){ return Math.floor(Math.random()*(max-min+1))+min; }
function pick(arr){ return arr[Math.floor(Math.random()*arr.length)]; }
function shuffle(arr){ const a = [...arr]; for(let i=a.length-1;i>0;i--){ const j=Math.floor(Math.random()*(i+1)); [a[i],a[j]]=[a[j],a[i]]; } return a; }
function cx(...c){ return c.filter(Boolean).join(" "); }
// Shared expedited windows used by game logic
const EXPEDITED = { susarFatalLife: 7, susarOther: 15, saeExpected: 0, nonSeriousRelated: 0 };
function classifyEvent(ev, windows = EXPEDITED) {
const isSAE = !!ev.serious;
const isSUSAR = isSAE && ev.related && !ev.expected;
const expedited = isSUSAR || (isSAE && ev.expected && ev.related && windows.saeExpected>0) || (!isSAE && ev.related && windows.nonSeriousRelated>0);
let windowDays = 0;
if (isSUSAR) windowDays = ev.fatalOrLifeThreat ? windows.susarFatalLife : windows.susarOther;
else if (isSAE && ev.expected && ev.related) windowDays = windows.saeExpected;
else if (!isSAE && ev.related) windowDays = windows.nonSeriousRelated;
const onset = ev.onsetDate ? new Date(ev.onsetDate) : new Date();
const deadline = expedited && windowDays>0 ? addDays(onset, windowDays) : null;
const label = isSUSAR ? "SUSAR" : isSAE ? "SAE" : "AE";
return { isSAE, isSUSAR, expedited, windowDays, deadline, label };
}
// ----- Question Generators -----
function genTriageScenario() {
// Randomized but controlled generation of an AE case
const serious = Math.random() < 0.55; // ~55% serious
const expected = Math.random() < 0.5;
const related = Math.random() < 0.7;
const fatalOrLifeThreat = serious && Math.random() < 0.2;
const onsetDate = dayISO(addDays(new Date(), -rnd(0, 10)));
const country = pick(["US","DE","BR","IN","JP","ZA","GB","FR","CA","AU"]);
const description = pick([
"Chest pain and dyspnea within 48 h.",
"ALT/AST elevation on Week 4 labs.",
"Injection-site swelling and pruritus.",
"Syncope with brief hospitalization.",
"Generalized urticaria post first dose.",
"Headache and nausea, no hospitalization.",
"Anaphylaxis requiring epinephrine.",
"Neutropenia on routine labs.",
]);
const subjectId = `S${rnd(1001, 9999)}`;
const ev = { subjectId, onsetDate, country, description, serious, expected, related, fatalOrLifeThreat };
const cls = classifyEvent(ev);
// Build choices: one correct + two distractors
const correct = cls.isSUSAR
? `SUSAR — expedited within ${cls.windowDays} day(s)`
: cls.isSAE
? `SAE — include in periodic/aggregate reporting`
: `AE — record and include in periodic reporting`;
const distractors = new Set();
while (distractors.size < 2) {
const d = pick([
`SUSAR — expedited within ${cls.fatalOrLifeThreat?15:7} day(s)`, // swapped window
`SAE — expedited within 7 day(s)`,
`AE — expedited within 15 day(s)`,
`SUSAR — periodic only`,
`SAE — record only`,
]);
if (d !== correct) distractors.add(d);
}
const choices = shuffle([correct, ...Array.from(distractors)]);
const explain = [
cls.isSUSAR ? "Serious + Related + Unexpected ⇒ SUSAR." : cls.isSAE ? "Serious, but expected or not related ⇒ SAE, no expedited (periodic)." : "Non-serious AE ⇒ periodic reporting.",
];
if (cls.isSUSAR) explain.push(cls.windowDays === 7 ? "Fatal/Life-threatening SUSAR ⇒ 7-day clock." : "Other SUSAR ⇒ 15-day clock.");
if (ev.related) explain.push("Related to IP.");
if (ev.expected) explain.push("Expected vs RSI."); else explain.push("Unexpected vs RSI.");
return { type: "triage", ev, cls, prompt: "Pick the correct classification and reporting action.", choices, answer: correct, rationale: explain };
}
function genStoppingScenario() {
const windowDays = pick([14, 28, 30]);
const numeratorTag = pick(["SUSAR","SAE"]);
const thresholdPct = pick([5, 10, 15]);
const subjects = rnd(12, 40);
// simulate events over window
const den = subjects; // proxy for enrolled/at-risk subjects
const trueRate = pick([thresholdPct - 3, thresholdPct + 4, thresholdPct + 8, thresholdPct - 1]);
const num = Math.max(0, Math.round((trueRate/100) * den));
const trigger = (num/den)*100 >= thresholdPct;
const choices = shuffle([trigger?"Trigger boundary":"No trigger", trigger?"No trigger":"Trigger boundary", "Need DSMB only" ]);
return {
type: "stopping",
prompt: `Rule: ${numeratorTag} rate ≥ ${thresholdPct}% within last ${windowDays} days. Data: ${num}/${den} subjects with ${numeratorTag}. Do we trigger?`,
choices,
answer: trigger?"Trigger boundary":"No trigger",
rationale: [
`Calculated rate = ${(num/den*100).toFixed(1)}% vs threshold ${thresholdPct}%`,
trigger?"Boundary crossed ⇒ follow stopping procedure.":"Below threshold ⇒ continue with monitoring.",
],
};
}
function genDsmbScenario() {
const first = addDays(new Date(), -rnd(7, 28));
const cadenceWeeks = pick([4,6,8]);
const occurrences = rnd(4, 8);
const idx = rnd(2, occurrences); // ask for a future meeting index
const correctDate = addDays(first, idx* cadenceWeeks * 7);
const options = shuffle([
correctDate,
addDays(correctDate, rnd(-3, -1)),
addDays(correctDate, rnd(1, 3)),
]);
const choices = options.map(d => dayISO(d));
return {
type: "dsmb",
prompt: `DSMB cadence ${cadenceWeeks} weeks. First meeting ${dayISO(first)}. What is meeting #${idx}?`,
choices,
answer: dayISO(correctDate),
rationale: [
`Meeting #${idx} = first + (${idx} × ${cadenceWeeks} weeks) ⇒ ${dayISO(correctDate)}`,
],
};
}
function genQuestion() {
const t = pick(["triage","stopping","dsmb","triage","triage","stopping"]); // bias toward triage
if (t === "triage") return genTriageScenario();
if (t === "stopping") return genStoppingScenario();
return genDsmbScenario();
}
export default function App(){
const [state, setState] = useStoredState({
round: 1,
score: 0,
lives: 3,
streak: 0,
history: [], // {q, correct, choice}
});
const [q, setQ] = useState(genQuestion());
const [locked, setLocked] = useState(false);
const [choice, setChoice] = useState("");
const reset = () => { setState({ round: 1, score: 0, lives: 3, streak: 0, history: [] }); setQ(genQuestion()); setLocked(false); setChoice(""); };
const next = () => { setQ(genQuestion()); setLocked(false); setChoice(""); setState(s=>({ ...s, round: s.round+1 })); };
const onPick = (c) => {
if (locked) return;
setChoice(c);
const correct = c === q.answer;
const delta = correct ? (10 + Math.min(20, state.streak*2)) : 0;
setLocked(true);
setState(s=>({
...s,
score: s.score + delta,
streak: correct ? s.streak+1 : 0,
lives: correct ? s.lives : Math.max(0, s.lives-1),
history: [{ q, correct, choice: c, gained: delta }, ...s.history].slice(0, 50)
}));
};
const gameOver = state.lives <= 0;
return (
{gameOver ? (
) : (
)}
);
}
function Header({ state, reset }){
return (
);
}
function Badge({ label, tone="default" }){
const tones = { default:"bg-slate-100 text-slate-700 border-slate-200", ok:"bg-emerald-100 text-emerald-800 border-emerald-200", warn:"bg-amber-100 text-amber-800 border-amber-200" };
return {label};
}
function QuestionCard({ q, locked, choice, onPick, next }){
return (
{q.type}
{q.type === "triage" && }
)}
);
}
function titleFor(q){
if (q.type === "triage") return "AE/SUSAR Triage";
if (q.type === "stopping") return "Stopping Rule Check";
return "DSMB Scheduler";
}
function TriagePreview({ ev, cls }){
return (
);
}
function KV({ k, v, wide }){
return (
);
}
function GameOver({ state, reset }){
const accuracy = useMemo(() => {
const total = state.history.length || 1;
const correct = state.history.filter(h=>h.correct).length;
return Math.round(100*correct/total);
}, [state.history]);
return (
);
}
function History({ history }){
if (!history.length) return null;
return (
);
}
CSP Safety Officer Challenge
Train triage, stopping rules, and DSMB schedule instincts
{titleFor(q)}
{q.prompt}
{q.choices.map((c,i) => (
))}
{locked && (
Correct: {q.answer}
-
{q.rationale.map((r,i)=>(
- {r} ))}
{k}
{String(v)}
Game Over
Score: {state.score} · Accuracy: {accuracy}% · Max Streak: {Math.max(0, ...state.history.map(h=>h.correct?1:0))}
Recent Rounds
| Type | Prompt | Your Choice | Correct | Points |
|---|---|---|---|---|
| {h.q.type} | {h.q.prompt} | {h.choice.slice(0,48)} | {h.correct?"✔":"✖"} | {h.gained} |
