const MONTH_ORDER = [9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8]; const HOLIDAY_MONTHS = new Set([1, 2, 7, 8]); const WEEKDAY_MONTHS = new Set(MONTH_ORDER.filter((month) => !HOLIDAY_MONTHS.has(month))); const WEEKDAY_PHASES = ["早上", "下午", "晚上"]; const HOLIDAY_PHASES = ["早上", "中午", "晚上"]; const STORAGE_KEY = "high-school-simulator-state-v1"; const SETUP_VERSION = 2; const STUDY_ACTION_IDS = new Set(["studyLanguage", "studyLogic", "holidayStudyLogic", "holidayStudyLanguage"]); const STAT_LABELS = { learning: "學習", language: "語文", logic: "邏輯", social: "社交", stamina: "體力", staminaMax: "體力上限", health: "生理健康", stress: "壓力", warnings: "警告", awards: "嘉獎", }; const STAT_TONES = { learning: "blue", language: "purple", logic: "purple", social: "green", stamina: "green", health: "orange", stress: "red", }; const SCORE_BANDS = [ { max: 20, text: "一竅不通" }, { max: 40, text: "有所認識" }, { max: 60, text: "普通/一般" }, { max: 80, text: "認識深刻" }, { max: 90, text: "熟練掌握" }, { max: 1000, text: "非常專業" }, ]; const els = {}; let state = loadState(); let modalContinue = null; document.addEventListener("DOMContentLoaded", () => { bindElements(); bindStaticActions(); render(); if (!state.setupComplete) { showSetupModal(); return; } if (state.history.length === 0) { addOpeningLog(); } }); function bindElements() { Object.assign(els, { calendarText: document.querySelector("#calendarText"), phaseBadge: document.querySelector("#phaseBadge"), statsGrid: document.querySelector("#statsGrid"), warningText: document.querySelector("#warningText"), awardText: document.querySelector("#awardText"), examAverageText: document.querySelector("#examAverageText"), modeText: document.querySelector("#modeText"), roundText: document.querySelector("#roundText"), slotText: document.querySelector("#slotText"), choiceGrid: document.querySelector("#choiceGrid"), logList: document.querySelector("#logList"), saveButton: document.querySelector("#saveButton"), newGameButton: document.querySelector("#newGameButton"), clearLogButton: document.querySelector("#clearLogButton"), modalOverlay: document.querySelector("#modalOverlay"), modalKicker: document.querySelector("#modalKicker"), modalTitle: document.querySelector("#modalTitle"), modalBody: document.querySelector("#modalBody"), modalResult: document.querySelector("#modalResult"), modalChoices: document.querySelector("#modalChoices"), }); } function bindStaticActions() { els.saveButton.addEventListener("click", () => { saveState(); addLog("進度已保存。"); render(); }); els.newGameButton.addEventListener("click", () => { showModal({ kicker: "重新開始", title: "要開始新的高中生活嗎?", body: "目前進度會被覆蓋。", choices: [ { label: "重新開始", action: resetToSetup, }, { label: "取消", action: closeModal }, ], }); }); els.clearLogButton.addEventListener("click", () => { state.history = []; addLog("紀錄已清空。"); saveState(); render(); }); } function createInitialState() { return { year: 1, monthPointer: 0, round: 1, phase: 0, setupVersion: SETUP_VERSION, setupComplete: false, contestUsed: false, ended: false, ending: null, stats: { learning: 30, language: 25, logic: 25, social: 30, stamina: 80, staminaMax: 80, health: 70, stress: 40, warnings: 0, awards: 0, }, examScores: [], flags: { backgroundUnlocked: false, }, history: [], }; } function loadState() { try { const raw = localStorage.getItem(STORAGE_KEY); if (!raw) return createInitialState(); const parsed = JSON.parse(raw); if (parsed.setupVersion !== SETUP_VERSION) { return createInitialState(); } const loaded = { ...createInitialState(), ...parsed, stats: { ...createInitialState().stats, ...parsed.stats }, flags: { ...createInitialState().flags, ...parsed.flags }, history: Array.isArray(parsed.history) ? parsed.history : [], examScores: Array.isArray(parsed.examScores) ? parsed.examScores : [], }; return loaded; } catch { return createInitialState(); } } function saveState() { localStorage.setItem(STORAGE_KEY, JSON.stringify(state)); } function render() { const month = getCurrentMonth(); const holiday = isHolidayMonth(month); const phases = holiday ? HOLIDAY_PHASES : WEEKDAY_PHASES; const phaseName = phases[state.phase] || phases[0]; const schoolYearLabel = `第 ${state.year} 年`; els.calendarText.textContent = `${schoolYearLabel} ${month} 月`; els.phaseBadge.textContent = phaseName; els.modeText.textContent = holiday ? "假期" : "平日"; els.roundText.textContent = `第 ${state.round} 輪`; els.slotText.textContent = phaseName; els.warningText.textContent = state.stats.warnings; els.awardText.textContent = state.stats.awards; els.examAverageText.textContent = getExamAverageText(); renderStats(); renderChoices(); renderLog(); } function renderStats() { const statItems = [ ["learning", state.stats.learning, 100], ["language", state.stats.language, 100], ["logic", state.stats.logic, 100], ["social", state.stats.social, 100], ["stamina", state.stats.stamina, state.stats.staminaMax], ["health", state.stats.health, 120], ["stress", state.stats.stress, 100], ]; els.statsGrid.innerHTML = ""; statItems.forEach(([key, value, max]) => { const card = document.createElement("article"); card.className = "stat-card"; card.dataset.tone = STAT_TONES[key] || "blue"; const percent = max > 0 ? clamp((value / max) * 100, 0, 100) : 0; const desc = key === "stamina" ? `${value}/${state.stats.staminaMax}` : describeScore(value); card.innerHTML = `
${STAT_LABELS[key]} ${Math.round(value)}${key === "stamina" ? `/${state.stats.staminaMax}` : ""}
${desc} `; els.statsGrid.appendChild(card); }); } function renderChoices() { els.choiceGrid.innerHTML = ""; if (!state.setupComplete) { const button = createChoiceButton({ label: "分配能力", hint: "語文 + 邏輯 = 50", tone: "purple", onClick: showSetupModal, }); els.choiceGrid.appendChild(button); return; } if (state.ended) { const button = createChoiceButton({ label: "重新開始", hint: "開始新的三年", tone: "red", onClick: resetToSetup, }); els.choiceGrid.appendChild(button); return; } getAvailableActions().forEach((action) => { els.choiceGrid.appendChild( createChoiceButton({ label: action.label, hint: action.hint, tone: action.tone, disabled: action.disabled, onClick: () => takeAction(action.id), }), ); }); } function createChoiceButton({ label, hint, tone, disabled, onClick }) { const button = document.createElement("button"); button.className = "choice-button"; button.type = "button"; button.dataset.tone = tone || "blue"; button.disabled = Boolean(disabled); button.innerHTML = `${label}${hint || ""}`; if (!disabled) { button.addEventListener("click", onClick); } return button; } function renderLog() { els.logList.innerHTML = ""; state.history .slice() .reverse() .forEach((entry) => { const item = document.createElement("li"); item.innerHTML = `${entry.text}`; els.logList.appendChild(item); }); } function getAvailableActions() { const month = getCurrentMonth(); if (isHolidayMonth(month)) { const base = [ { id: "holidayPlay", label: "玩樂", hint: "調整壓力與社交", tone: "green" }, { id: "holidayStudyLogic", label: "讀書(理)", hint: "學習與邏輯", tone: "blue" }, { id: "holidayStudyLanguage", label: "讀書(文)", hint: "學習與語文", tone: "purple" }, { id: "holidaySport", label: "運動", hint: "體力消耗,健康波動", tone: "orange" }, { id: "holidayNap", label: "zzZ", hint: "恢復體力,降低壓力", tone: "green" }, ]; if (state.phase === 0 && !state.contestUsed) { base.unshift( { id: "languageContest", label: "語文比賽", hint: "本輪直接結束", tone: "purple" }, { id: "logicContest", label: "邏輯競賽", hint: "本輪直接結束", tone: "blue" }, ); } return withStudyAvailability(base); } if (state.phase === 0) { return withStudyAvailability([ { id: "sport", label: "運動", hint: "體力 -20,壓力下降", tone: "orange" }, { id: "studyLanguage", label: "學習(文科)", hint: "語文與學習", tone: "purple" }, { id: "studyLogic", label: "學習(理科)", hint: "邏輯與學習", tone: "blue" }, { id: "nap", label: "zzZ", hint: "可能被記警告", tone: "green" }, { id: "skipClass", label: "逃課", hint: "裝病請假", tone: "red" }, { id: "makeFriends", label: "交友", hint: "社交機會", tone: "green" }, ]); } if (state.phase === 1) { return withStudyAvailability([ { id: "sport", label: "運動", hint: "體力 -20,壓力下降", tone: "orange" }, { id: "studyLanguage", label: "學習(文科)", hint: "語文與學習", tone: "purple" }, { id: "studyLogic", label: "學習(理科)", hint: "邏輯與學習", tone: "blue" }, { id: "nap", label: "zzZ", hint: "可能被記警告", tone: "green" }, { id: "makeFriends", label: "交友", hint: "社交機會", tone: "green" }, ]); } return withStudyAvailability([ { id: "sport", label: "運動", hint: "體力 -20,壓力下降", tone: "orange" }, { id: "studyLanguage", label: "讀書(文)", hint: "語文與學習", tone: "purple" }, { id: "studyLogic", label: "讀書(理)", hint: "邏輯與學習", tone: "blue" }, { id: "onlineFriends", label: "和朋友連機", hint: "社交與壓力波動", tone: "green" }, { id: "sleep", label: "睡覺", hint: "體力與健康恢復", tone: "green" }, ]); } function withStudyAvailability(actions) { if (state.stats.stamina > 0) return actions; return actions.map((action) => STUDY_ACTION_IDS.has(action.id) ? { ...action, hint: "體力為 0,無法學習", disabled: true } : action, ); } function takeAction(actionId) { if (state.ended) return; if (STUDY_ACTION_IDS.has(actionId) && state.stats.stamina <= 0) { addLog("體力為 0,無法學習。"); saveState(); render(); return; } const actionMap = { sport: () => weekdaySport(), studyLanguage: () => weekdayStudyLanguage(), studyLogic: () => weekdayStudyLogic(), nap: () => weekdayNap(), skipClass: () => skipClass(), makeFriends: () => makeFriends(), onlineFriends: () => onlineFriends(), sleep: () => sleep(), holidayPlay: () => holidayPlay(), holidayStudyLogic: () => holidayStudyLogic(), holidayStudyLanguage: () => holidayStudyLanguage(), holidaySport: () => holidaySport(), holidayNap: () => holidayNap(), languageContest: () => contest("language"), logicContest: () => contest("logic"), }; const handler = actionMap[actionId]; if (!handler) return; handler(); if (checkSuddenEnding()) return; if (actionId === "languageContest" || actionId === "logicContest") { advanceHolidayRound(); } else { advancePhase(); } saveState(); render(); } function weekdaySport() { const outcome = rollWeighted([ { chance: 30, text: "運動時有些勉強。", delta: { health: -10, stamina: -20, stress: -3 } }, { chance: 20, text: "訓練讓體能上限提升。", delta: { health: 5, staminaMax: 1, stamina: -20, stress: -3 } }, { chance: 50, text: "活動筋骨後身體狀態更好。", delta: { health: 5, stamina: -20, stress: -3 } }, ]); applyOutcome("運動", outcome); } function weekdayStudyLanguage() { const outcome = rollWeighted([ { chance: 50, text: "文科學習穩定進展。", delta: { language: 2, learning: 2, stamina: -10, stress: 3 } }, { chance: 35, text: "今天特別讀得進去。", delta: { language: 3, learning: 3, stamina: -10, stress: 3 } }, { chance: 15, text: "只留下了一點印象。", delta: { language: 1, stamina: -10, stress: 3 } }, ]); applyOutcome("學習(文科)", outcome); } function weekdayStudyLogic() { const outcome = rollWeighted([ { chance: 60, text: "理科學習穩定進展。", delta: { learning: 2, logic: 2, stamina: -10, stress: 4 } }, { chance: 20, text: "解題手感不錯。", delta: { learning: 3, logic: 3, stamina: -10, stress: 4 } }, { chance: 5, text: "思路完全打通。", delta: { learning: 3, logic: 4, stamina: -10, stress: 4 } }, { chance: 15, text: "讀了半天卻沒有吸收。", delta: { stamina: -10, stress: 4 } }, ]); applyOutcome("學習(理科)", outcome); } function weekdayNap() { const caughtChance = Math.min(95, 35 + state.stats.warnings * 5); const outcome = rollWeighted([ { chance: caughtChance, text: "睡過頭被抓到。", delta: { stress: 10, learning: -1, warnings: 1 } }, { chance: 100 - caughtChance, text: "休息後恢復了一點體力。", delta: { learning: -1, stamina: 10 } }, ]); applyOutcome("zzZ", outcome); } function skipClass() { const outcome = rollWeighted([ { chance: 60, text: "裝病成功,暫時逃離壓力。", delta: { stress: -20, learning: -5 } }, { chance: 40, text: "裝病讓狀況變得更糟。", delta: { stress: 10, health: -10 } }, ]); applyOutcome("逃課", outcome); } function makeFriends() { const outcome = rollWeighted([ { chance: 70, text: "和同學聊得很順利。", delta: { social: 3 } }, { chance: 30, text: "互動不太順利。", delta: { social: -1, stress: 5 } }, ]); applyOutcome("交友", outcome); } function onlineFriends() { const outcome = rollWeighted([ { chance: 65, text: "連機過程很放鬆。", delta: { social: 2, stamina: -10, stress: -5 } }, { chance: 35, text: "遊戲過程反而吵了起來。", delta: { social: -2, stamina: -10, stress: 3 } }, ]); applyOutcome("和朋友連機", outcome); } function sleep() { applyOutcome("睡覺", { text: "好好休息了一晚。", delta: { stamina: 10, health: 5 } }); } function holidayPlay() { const outcome = rollWeighted([ { chance: 60, text: "玩得很盡興。", delta: { social: 1, stress: -15 } }, { chance: 30, text: "普通地放鬆了一下。", delta: { stress: -5 } }, { chance: 10, text: "玩樂安排不順。", delta: { stress: 10, social: -1 } }, ]); applyOutcome("玩樂", outcome); } function holidayStudyLogic() { const outcome = rollWeighted([ { chance: 70, text: "假期讀書有所收穫。", delta: { learning: 2, logic: 2, stamina: -10, stress: 4 } }, { chance: 20, text: "今天效率很高。", delta: { learning: 3, logic: 3, stamina: -10, stress: 4 } }, { chance: 10, text: "越讀越混亂。", delta: { learning: -2, logic: -2, stamina: -10, stress: 4 } }, ]); applyOutcome("讀書(理)", outcome); } function holidayStudyLanguage() { const outcome = rollWeighted([ { chance: 70, text: "文科讀書有所收穫。", delta: { learning: 2, language: 2, stamina: -10, stress: 3 } }, { chance: 20, text: "今天讀得還可以。", delta: { learning: 1, language: 1, stamina: -10, stress: 3 } }, { chance: 10, text: "內容吸收得不太好。", delta: { learning: -2, language: -2, stamina: -10, stress: 3 } }, ]); applyOutcome("讀書(文)", outcome); } function holidaySport() { const outcome = rollWeighted([ { chance: 30, text: "假期運動過量。", delta: { health: -15, stamina: -20, stress: -5 } }, { chance: 20, text: "體能訓練有效果。", delta: { health: 5, staminaMax: 1, stamina: -20, stress: -5 } }, { chance: 50, text: "運動讓身體狀態更好。", delta: { health: 5, stamina: -20, stress: -5 } }, ]); applyOutcome("運動", outcome); } function holidayNap() { applyOutcome("zzZ", { text: "假期補眠讓你舒服不少。", delta: { stress: -5, stamina: 10 } }); } function contest(type) { const threshold = [55, 70, 85][state.year - 1]; const statKey = type === "language" ? "language" : "logic"; const title = type === "language" ? "語文比賽" : "邏輯競賽"; const passed = state.stats[statKey] > threshold; state.contestUsed = true; if (passed) { applyOutcome(title, { text: `達到第 ${state.year} 年門檻 ${threshold},比賽表現出色。`, delta: { [statKey]: 6, learning: 6, stamina: -20, stress: -5, awards: 1 }, }); } else { applyOutcome(title, { text: `未達第 ${state.year} 年門檻 ${threshold},仍累積了經驗。`, delta: { [statKey]: 4, learning: 4, stamina: -20 }, }); } } function applyOutcome(title, outcome) { const actual = applyDelta(outcome.delta || {}); const deltaText = formatDelta(actual); addLog(`${title}:${outcome.text}${deltaText ? `(${deltaText})` : ""}`); } function advancePhase() { const month = getCurrentMonth(); const holiday = isHolidayMonth(month); const maxPhases = holiday ? HOLIDAY_PHASES.length : WEEKDAY_PHASES.length; state.phase += 1; if (state.phase < maxPhases) return; state.phase = 0; if (holiday) { advanceHolidayRound(); return; } maybeRunExam(() => { state.round += 1; if (state.round > 2) { finishMonth(); } else { saveState(); render(); } }); } function advanceHolidayRound() { state.phase = 0; state.round += 1; if (state.round > 3) { finishMonth(); } } function maybeRunExam(afterExam) { const month = getCurrentMonth(); const examMap = { "10-1": "logic", "12-2": "language", "4-1": "logic", "6-2": "language", }; const subject = examMap[`${month}-${state.round}`]; if (!subject) { afterExam(); return; } const score = calculateExamScore(subject); state.examScores.push({ year: state.year, month, subject, score, }); const subjectLabel = subject === "logic" ? "邏輯" : "語文"; addLog(`${month} 月考試:學習 + ${subjectLabel},得到 ${score} 分。`); saveState(); showModal({ kicker: "考試", title: `${month} 月考試`, body: `本次考試科目為學習 + ${subjectLabel}。`, result: `成績:${score} 分`, choices: [{ label: "繼續", action: () => closeModal(afterExam) }], }); } function calculateExamScore(subject) { const learning = state.stats.learning; const subjectValue = state.stats[subject]; const firstIsLearning = Math.random() < 0.5; const weighted = firstIsLearning ? (learning * 3 + subjectValue) / 4 : (subjectValue * 3 + learning) / 4; const low = [-15, -30, -40][state.year - 1]; const high = [10, 5, 0][state.year - 1]; return Math.round(clamp(randomBetween(weighted + low, weighted + high), 0, 100)); } function finishMonth() { const review = getMonthlyReview(); if (review) { const actual = applyDelta(review.delta || {}); addLog(`${getCurrentMonth()} 月審核:${review.title}。${review.text}${formatDelta(actual) ? `(${formatDelta(actual)})` : ""}`); saveState(); showModal({ kicker: `${getCurrentMonth()} 月審核`, title: review.title, body: review.text, result: formatDelta(actual), choices: [ { label: "繼續", action: () => closeModal(() => afterMonthReview()), }, ], }); return; } afterMonthReview(); } function afterMonthReview() { if (checkSuddenEnding()) return; triggerSemesterEvent(() => advanceMonth()); } function triggerSemesterEvent(done) { const month = getCurrentMonth(); if (!isWeekdayMonth(month)) { done(); return; } const positionMap = { 9: 1, 10: 2, 11: 3, 12: 4, 3: 1, 4: 2, 5: 3, 6: 4, }; const position = positionMap[month]; const guaranteed = position === 3; const possible = position === 1 || position === 2; if (!guaranteed && !possible) { done(); return; } if (possible && Math.random() < 0.1) { addLog("這個月沒有發生特殊事件。"); done(); return; } const event = pickRandomEvent(); if (!event) { addLog("沒有符合條件的特殊事件。"); done(); return; } showGameEvent(event, done); } function advanceMonth() { state.monthPointer += 1; if (state.monthPointer >= MONTH_ORDER.length * 3) { showFinalEnding(); return; } state.year = Math.floor(state.monthPointer / MONTH_ORDER.length) + 1; state.round = 1; state.phase = 0; state.contestUsed = false; saveState(); render(); } function getMonthlyReview() { const month = getCurrentMonth(); const year = state.year; const s = state.stats; if (month === 9) { if (s.language > 35) return { title: "自我介紹大成功!", text: "語文組織能力讓同學留下好印象。", delta: { social: 3, stress: 1 } }; if (s.language >= 15) return { title: "中規中矩的自我介紹", text: "你順利完成了自我介紹。", delta: { social: 2, stress: 1 } }; return { title: "令人困惑的自我介紹", text: "同學對你的表達有些困惑。", delta: { social: 1, stress: 3 } }; } if (month === 10) { const high = [40, 55, 70][year - 1]; const low = [25, 30, 35][year - 1]; if (s.social > high) return { title: "報告合作順利", text: "分組報告進行得很流暢。", delta: { social: 2, learning: 3 } }; if (s.social < low) return { title: "分組報告失控", text: "組員之間爆發爭吵,報告未如期完成。", delta: { social: -2, stress: 1 } }; return { title: "報告正常完成", text: "分組報告平穩收尾。", delta: { social: 1, learning: 2 } }; } if (month === 12) { const high = [80, 90, 105][year - 1]; if (s.health > high) return { title: "絲毫不受天氣影響", text: "天氣劇烈轉變也沒有影響你。", delta: { stamina: 10 } }; if (s.health < 65) return { title: "身體變得虛弱", text: "天氣變化讓你有些不舒服。", delta: { stamina: -10, stress: 2 } }; return { title: "身體些微受影響", text: "你感受到天氣變化帶來的負擔。", delta: { stamina: -5, stress: 1 } }; } if (month === 1) { const high = [55, 65, 75][year - 1]; const low = [30, 25, 20][year - 1]; if (s.stress > high) return { title: "壓力過大", text: "你決定稍微放鬆一下。", delta: { stress: -3 } }; if (s.stress < low) return { title: "開始鬆懈", text: "你提不起對學習的興趣。", delta: { stress: -2, learning: -3 } }; return { title: "完成個人規劃", text: "你按自身情況做好了規劃。", delta: { stress: 1, learning: 2 } }; } if (month === 3) { const high = [85, 88, 91][year - 1]; if (s.staminaMax > high) return { title: "運動健將", text: "你在運動會得到了眾人的掌聲。", delta: { social: 3, stress: -3 } }; if (s.staminaMax <= 80) return { title: "沒被選上", text: "你沒有被選上運動會選手。", delta: { stress: 1 } }; return { title: "運動能手", text: "你在運動會有不錯的表現。", delta: { social: 2 } }; } if (month === 4) { if (s.stress > 80) return { title: "心理諮商", text: "老師認為你的壓力太大,安排了諮商。", delta: { stress: -10, stamina: -10 } }; if (s.stress < 20) return { title: "督促學習", text: "老師認為你太鬆懈,督促你學習。", delta: { stress: 10, stamina: -10, learning: 5 } }; return { title: "正常扛壓", text: "你不需要特殊輔導。", delta: { stress: -5, stamina: 5 } }; } if (month === 6) { const high = [50, 60, 70][year - 1]; const low = [25, 30, 35][year - 1]; if (s.logic > high) return { title: "思緒清晰", text: "你整理好了自己的想法。", delta: { learning: 5, stress: -5 } }; if (s.logic < low) return { title: "腦袋一片混亂", text: "整理思緒的過程讓你更焦慮。", delta: { stress: 5 } }; return { title: "整理好想法", text: "你逐漸理清目前的學習狀態。", delta: { learning: 2, stress: -3 } }; } if (month === 7) { const high = [55, 70, 85][year - 1]; if (s.social > high) return { title: "社交大成功", text: "娛樂活動讓你受到歡迎。", delta: { stress: -10, social: 3 } }; if (s.social < 30) return { title: "找不到隊友", text: "你在活動中感到有些孤立。", delta: { stress: 5 } }; return { title: "默默參與", text: "你低調地參加了活動。", delta: { stress: -3, social: 1 } }; } return null; } function showGameEvent(event, done) { if (event.auto) { const result = event.auto(); const actual = applyDelta(result.delta || {}); addLog(`事件:${event.title}。${result.text}${formatDelta(actual) ? `(${formatDelta(actual)})` : ""}`); saveState(); showModal({ kicker: "事件", title: event.title, body: event.body, result: `${result.text}${formatDelta(actual) ? `\n${formatDelta(actual)}` : ""}`, choices: [{ label: "繼續", action: () => closeModal(() => finishEvent(done)) }], }); return; } showModal({ kicker: "事件", title: event.title, body: event.body, choices: event.choices.map((choice) => ({ label: choice.label, action: () => { const result = choice.apply(); if (result && result.ending) { closeModal(() => { triggerSpecialEnding(result.ending.title, result.ending.body); }); return; } const actual = applyDelta((result && result.delta) || {}); addLog(`事件:${event.title}/${choice.label}。${result.text}${formatDelta(actual) ? `(${formatDelta(actual)})` : ""}`); saveState(); showModal({ kicker: "事件結果", title: event.title, body: result.text, result: formatDelta(actual), choices: [{ label: "繼續", action: () => closeModal(() => finishEvent(done)) }], }); }, })), }); } function finishEvent(done) { if (checkSuddenEnding()) return; done(); } function getEvents() { const s = state.stats; return [ { title: "目擊證人", body: "你看見了一起霸凌事件,你正站在一個沒人發現的位置。", condition: () => true, choices: [ { label: "默默離去", apply: () => ({ text: "你選擇不介入。", delta: { stress: 5 } }) }, { label: "上前制止", apply: () => { if (state.stats.staminaMax >= 85) { state.flags.backgroundUnlocked = true; return { text: "你成功制止了事件。", delta: { stress: -5, social: 3 } }; } return { text: "你沒能壓住局面。", delta: { stress: 10, social: -10 } }; }, }, { label: "通報老師", apply: () => ({ text: "你把事情交給老師處理。", delta: { social: -3, stress: 1 } }) }, ], }, { title: "背景勢力", body: "你成功擊退他們後,有一群人似乎想投靠你。", condition: () => state.flags.backgroundUnlocked, choices: [ { label: "拒絕", apply: () => state.stats.language >= 30 ? { text: "你明確拒絕,場面還算平穩。", delta: { social: -1 } } : { text: "你的拒絕方式引來誤會。", delta: { social: -3, stress: 3 } }, }, { label: "同意", apply: () => rollWeighted([ { chance: 70, text: "他們成為你的人脈。", delta: { social: 5 } }, { chance: 30, text: "事情變得有些張揚。", delta: { social: 5, warnings: 1 } }, ]), }, ], }, { title: "網路吃瓜", body: "你在網路上看見一則消息,似乎是某藝人又再度失言。", condition: () => true, choices: [ { label: "強力譴責", apply: () => ({ text: "你把情緒宣洩了出去。", delta: { stress: -5 } }) }, { label: "分析事件", apply: () => ({ text: "你嘗試拆解事件脈絡。", delta: { logic: 2 } }) }, ], }, { title: "分組", body: "又來到分組活動的時刻,你想以什麼分組策略行動呢?", condition: () => true, choices: [ { label: "默默等有人來找你", apply: () => ({ text: "等待讓你有些焦慮。", delta: { stress: 3 } }) }, { label: "主動找尋組員", apply: () => state.stats.social >= 50 ? { text: "你順利找到了合適組員。", delta: { stress: -3, social: 3 } } : { text: "主動出擊並不順利。", delta: { stress: 5, social: 1 } }, }, ], }, { title: "來自朋友的關心", body: "朋友注意到你的狀態不太好,主動來關心你。", condition: () => s.social > 40 && s.stress > 70, choices: [ { label: "和朋友分擔想法", apply: () => rollWeighted([ { chance: 90, text: "朋友理解了你。", delta: { stress: -5, social: 2 } }, { chance: 10, text: "溝通沒有完全順利。", delta: { stress: 2, social: -2 } }, ]), }, { label: "說自己沒事", apply: () => ({ text: "你把話吞了回去。", delta: { stress: 1 } }) }, ], }, { title: "意外受傷", body: "你的身體狀況不佳,發生了小意外。", condition: () => s.health < 60, auto: () => state.stats.social > 50 ? { text: "朋友照顧了你。", delta: { social: 2, stress: -1 } } : { text: "你只能獨自承受。", delta: { stress: 3 } }, }, { title: "自主學習", body: "你感覺自己可以安排一段自主學習時間。", condition: () => s.learning > 50, choices: [ { label: "奮發學習", apply: () => ({ text: "你推進了自己的學習進度。", delta: { stress: 5, learning: 3 } }) }, { label: "暫時休息", apply: () => ({ text: "你選擇調整狀態。", delta: { stress: -3 } }) }, ], }, { title: "校園霸凌", body: "你因為社交狀態低落而被盯上。", condition: () => s.social < 20, choices: [ { label: "默默承受", apply: () => ({ text: "你把事情壓在心裡。", delta: { stress: 5, health: -2 } }) }, { label: "反抗", apply: () => state.stats.health > 80 ? { text: "你成功反抗,但消耗了體力。", delta: { stamina: -10 } } : { text: "反抗失敗,狀況更糟。", delta: { stress: 5, health: -5, stamina: -10 } }, }, ], }, { title: "吵架", body: "你和同學起了衝突。", condition: () => true, choices: [ { label: "據理力爭", apply: () => state.stats.logic > 40 ? { text: "你的論點站得住腳。", delta: { stress: -5, social: -1 } } : { text: "爭論失敗讓你更煩躁。", delta: { stress: 10 } }, }, { label: "無視", apply: () => ({ text: "你不想再理會這件事。", delta: { stress: 5 } }) }, ], }, { title: "學習困境", body: "你卡在不上不下的學習瓶頸。", condition: () => s.learning > 40 && s.learning < 60, choices: [ { label: "求助同學朋友", apply: () => rollWeighted([ { chance: 80, text: "同學幫你解開疑惑。", delta: { learning: 3, stress: -2 } }, { chance: 20, text: "求助過程有些尷尬。", delta: { social: -2, stress: 2 } }, ]), }, { label: "自己領悟", apply: () => rollWeighted([ { chance: 60, text: "你靠自己理解了一點。", delta: { learning: 1, stress: 3 } }, { chance: 40, text: "你越想越焦慮。", delta: { stress: 5 } }, ]), }, ], }, { title: "家庭革命", body: "家人對你的讀書狀況非常不滿。", condition: () => s.learning < 60, choices: [ { label: "認真學習", apply: () => rollWeighted([ { chance: 60, text: "你試著回應期待。", delta: { learning: 3, stress: 5 } }, { chance: 40, text: "你勉強自己讀了一些。", delta: { learning: 1, stress: 8 } }, ]), }, { label: "無視", apply: () => ({ text: "你不想回應家人的壓力。", delta: { stress: 5 } }) }, ], }, { title: "抄襲功課", body: "同學希望你把功課借給他抄。", condition: () => s.learning > 50 && s.social > 50, choices: [ { label: "同意", apply: () => rollWeighted([ { chance: 60, text: "同學很感謝你。", delta: { social: 3, stress: 1 } }, { chance: 40, text: "你開始擔心被發現。", delta: { stress: 5 } }, ]), }, { label: "不同意", apply: () => ({ text: "同學有些不滿。", delta: { social: -3, stress: 2 } }) }, ], }, { title: "留校查看", body: "你的警告累積到需要留校查看。", condition: () => s.warnings >= 3, choices: [ { label: "逃跑", apply: () => ({ text: "你選擇逃避處分。", delta: { warnings: 1 } }) }, { label: "乖乖反省", apply: () => ({ text: "你接受處分並反省。", delta: { warnings: -1, stress: 5 } }) }, ], }, { title: "頒獎典禮", body: "你被通知要上台領獎。", condition: () => s.awards > 1, choices: [ { label: "大肆宣揚", apply: () => ({ text: "你到處宣傳這件事。", delta: { social: -3, stress: -5 } }) }, { label: "默默領獎", apply: () => ({ text: "你低調領完獎。", delta: { stress: -1 } }) }, ], }, { title: "遲到", body: "體力不足讓你沒能準時到校。", condition: () => s.stamina < 20, auto: () => ({ text: "你被記了一次警告。", delta: { stress: 3, warnings: 1 } }), }, { title: "即將遲到", body: "你發現自己快要遲到了。", condition: () => true, choices: [ { label: "竭力衝刺", apply: () => rollWeighted([ { chance: 70, text: "你趕上了。", delta: { stamina: -5 } }, { chance: 30, text: "你還是遲到了。", delta: { stamina: -5, stress: 3, warnings: 1 } }, ]), }, { label: "無所謂", apply: () => ({ text: "你選擇慢慢走。", delta: { stress: 3, warnings: 1 } }) }, ], }, { title: "讀書會", body: "學校推薦一個課後讀書會,會一起討論課外書籍並上台發表。", condition: () => true, choices: [ { label: "參加讀書會", apply: () => state.stats.language >= 40 ? { text: "你在讀書會表現得不錯。", delta: { language: 3, social: 3, stress: 3 } } : { text: "你努力參與,但有些吃力。", delta: { language: 2, social: 1, stress: 5 } }, }, { label: "不參加讀書會", apply: () => ({ text: "你省下了一些心力。", delta: { stress: -1 } }) }, ], }, { title: "諮商", body: "你的壓力讓你整天睡不好,正在考慮是否該尋求協助。", condition: () => s.stress > 70, choices: [ { label: "去看", apply: () => rollWeighted([ { chance: 70, text: "諮商讓你舒緩不少。", delta: { stress: -10 } }, { chance: 30, text: "你有些擔心他人的眼光。", delta: { social: -5 } }, ]), }, { label: "不去看", apply: () => ({ text: "你暫時把問題放著。", delta: { stress: 1 } }) }, ], }, { title: "被老師冤枉", body: "你按時交了功課,但老師把你和沒交功課的同學認錯了。", condition: () => true, choices: [ { label: "據理力爭", apply: () => state.stats.logic >= 40 ? { text: "老師發現誤會並向你道歉。", delta: { stress: -2 } } : { text: "老師不聽你的解釋。", delta: { stress: 5 } }, }, { label: "默不作聲", apply: () => ({ text: "你忍了下來。", delta: { stress: 2 } }) }, { label: "情緒爆發", apply: () => state.stats.stamina < 85 ? { text: "你的行動完全失敗,事情更嚴重了。", delta: { stress: 5, warnings: 3 } } : { text: "事情失控到無法挽回。", ending: { title: "勒令退學(Bad End 3)", body: "一份功課導致的悲劇。", }, }, }, ], }, { title: "被遺忘的它", body: "你回到家後發現今天的功課留在了學校。", condition: () => true, choices: [ { label: "重回學校", apply: () => rollWeighted([ { chance: 80, text: "你成功拿回功課。", delta: { stress: -1, stamina: -10 } }, { chance: 20, text: "學校已經關門。", delta: { stress: 3, stamina: -10, warnings: 1 } }, ]), }, { label: "放棄", apply: () => ({ text: "你準備承受後果。", delta: { stress: 5, warnings: 1 } }) }, ], }, { title: "催眠的數學課", body: "缺乏體力讓你在數學課上被睡意包圍。", condition: () => s.stamina < 20, choices: [ { label: "順應睡意", apply: () => rollWeighted([ { chance: 70, text: "你睡了一下恢復體力。", delta: { learning: -1, stamina: 10 } }, { chance: 30, text: "你睡得不安穩。", delta: { learning: -1, stress: 5 } }, ]), }, { label: "強撐學習", apply: () => ({ text: "你硬是把課聽完。", delta: { stress: 2, learning: 1 } }) }, ], }, { title: "突擊測驗", body: "老師宣布今天要考上個月的教學內容。", condition: () => true, auto: () => state.stats.learning >= 55 ? { text: "你拿到了不錯的分數。", delta: { stress: -2 } } : { text: "分數不理想,壓力開始增加。", delta: { stress: 5 } }, }, ]; } function pickRandomEvent() { const eligible = getEvents().filter((event) => event.condition()); if (eligible.length === 0) return null; return eligible[Math.floor(Math.random() * eligible.length)]; } function checkSuddenEnding() { if (state.ended) return true; if (state.stats.stress >= 100) { triggerSpecialEnding("壓力崩潰(Bad End 1)", "壓力已經超過你能承受的範圍。"); return true; } if (state.stats.warnings >= 9) { triggerSpecialEnding("被趕出學校(Bad End 2)", "警告累積到 9 次,學校決定將你退學。"); return true; } if (state.stats.health <= 0) { triggerSpecialEnding("過勞倒下(Bad End 4)", "生理健康歸零,這段高中生活被迫中止。"); return true; } return false; } function triggerSpecialEnding(title, body) { state.ended = true; state.ending = { title, body }; addLog(`結局:${title}`); saveState(); render(); showModal({ kicker: "結局", title, body, result: getFinalStatsText(), choices: [ { label: "重新開始", action: () => { state = createInitialState(); addLog("新的高中生活開始了。"); saveState(); closeModal(); render(); }, }, ], }); } function showFinalEnding() { const average = getExamAverage(); const excellent = average >= 85; const title = excellent ? "優等生" : "成功畢業"; const body = excellent ? "三年考試平均達到 85 分以上,你以優等生姿態畢業。" : "你完成了三年的高中生活,順利畢業。"; state.ended = true; state.ending = { title, body }; addLog(`終末結局:${title}`); saveState(); render(); showModal({ kicker: "終末結局", title, body, result: `考試平均:${Number.isFinite(average) ? average.toFixed(1) : "--"}\n${getFinalStatsText()}`, choices: [ { label: "重新開始", action: () => { state = createInitialState(); addLog("新的高中生活開始了。"); saveState(); closeModal(); render(); }, }, ], }); } function applyDelta(delta) { const before = { ...state.stats }; Object.entries(delta).forEach(([key, value]) => { if (typeof value !== "number") return; if (key === "staminaMax") { state.stats.staminaMax = Math.round(clamp(state.stats.staminaMax + value, 1, 120)); state.stats.stamina = clamp(state.stats.stamina, 0, state.stats.staminaMax); return; } if (!(key in state.stats)) return; state.stats[key] += value; }); state.stats.learning = Math.round(clamp(state.stats.learning, 0, 100)); state.stats.language = Math.round(clamp(state.stats.language, 0, 100)); state.stats.logic = Math.round(clamp(state.stats.logic, 0, 100)); state.stats.social = Math.round(clamp(state.stats.social, 0, 100)); state.stats.health = Math.round(clamp(state.stats.health, 0, 120)); state.stats.stress = Math.round(clamp(state.stats.stress, 0, 100)); state.stats.warnings = Math.max(0, Math.round(state.stats.warnings)); state.stats.awards = Math.max(0, Math.round(state.stats.awards)); state.stats.stamina = Math.round(clamp(state.stats.stamina, 0, state.stats.staminaMax)); const languageDrain = calculateMasteryDrain(before.language, state.stats.language); const logicDrain = calculateMasteryDrain(before.logic, state.stats.logic); if (languageDrain > 0) { state.stats.logic -= languageDrain; } if (logicDrain > 0) { state.stats.language -= logicDrain; } state.stats.language = Math.round(clamp(state.stats.language, 0, 100)); state.stats.logic = Math.round(clamp(state.stats.logic, 0, 100)); const actual = {}; Object.keys(state.stats).forEach((key) => { const diff = state.stats[key] - before[key]; if (diff !== 0) actual[key] = diff; }); return actual; } function calculateMasteryDrain(beforeValue, afterValue) { if (afterValue <= beforeValue) return 0; const over80Start = Math.max(beforeValue, 80); const over80End = Math.min(afterValue, 90); const over80Gain = Math.max(0, over80End - over80Start); const over90Start = Math.max(beforeValue, 90); const over90Gain = Math.max(0, afterValue - over90Start); return over80Gain * 2 + over90Gain * 3; } function formatDelta(delta) { const entries = Object.entries(delta || {}).filter(([, value]) => value !== 0); if (entries.length === 0) return ""; return entries .map(([key, value]) => `${STAT_LABELS[key] || key}${value > 0 ? "+" : ""}${value}`) .join("、"); } function describeScore(value) { const band = SCORE_BANDS.find((item) => value <= item.max); return band ? band.text : SCORE_BANDS[SCORE_BANDS.length - 1].text; } function rollWeighted(outcomes) { const total = outcomes.reduce((sum, item) => sum + item.chance, 0); let roll = Math.random() * total; for (const outcome of outcomes) { roll -= outcome.chance; if (roll <= 0) return outcome; } return outcomes[outcomes.length - 1]; } function addLog(text) { state.history.push({ time: `${state.year} 年 ${getCurrentMonth()} 月 ${getPhaseName()}`, text, }); if (state.history.length > 90) { state.history = state.history.slice(-90); } } function addOpeningLog() { addLog(`高中生活開始了。語文 ${state.stats.language}、邏輯 ${state.stats.logic}。`); saveState(); render(); } function resetToSetup() { state = createInitialState(); saveState(); render(); showSetupModal(); } function getCurrentMonth() { return MONTH_ORDER[state.monthPointer % MONTH_ORDER.length]; } function getPhaseName() { const month = getCurrentMonth(); const phases = isHolidayMonth(month) ? HOLIDAY_PHASES : WEEKDAY_PHASES; return phases[state.phase] || phases[0]; } function isHolidayMonth(month) { return HOLIDAY_MONTHS.has(month); } function isWeekdayMonth(month) { return WEEKDAY_MONTHS.has(month); } function getExamAverage() { if (state.examScores.length === 0) return Number.NaN; const total = state.examScores.reduce((sum, item) => sum + item.score, 0); return total / state.examScores.length; } function getExamAverageText() { const average = getExamAverage(); return Number.isFinite(average) ? average.toFixed(1) : "--"; } function getFinalStatsText() { return [ `學習:${state.stats.learning}`, `語文:${state.stats.language}`, `邏輯:${state.stats.logic}`, `社交:${state.stats.social}`, `體力:${state.stats.stamina}/${state.stats.staminaMax}`, `生理健康:${state.stats.health}`, `壓力:${state.stats.stress}`, `警告:${state.stats.warnings}`, `嘉獎:${state.stats.awards}`, ].join("\n"); } function randomBetween(min, max) { return min + Math.random() * (max - min); } function clamp(value, min, max) { return Math.min(max, Math.max(min, value)); } function showModal({ kicker, title, body, result, choices }) { modalContinue = null; els.modalKicker.textContent = kicker || ""; els.modalTitle.textContent = title || ""; els.modalBody.textContent = body || ""; els.modalResult.textContent = result || ""; els.modalChoices.innerHTML = ""; (choices || [{ label: "繼續", action: closeModal }]).forEach((choice) => { const button = document.createElement("button"); button.className = "modal-choice"; button.type = "button"; button.textContent = choice.label; button.addEventListener("click", choice.action); els.modalChoices.appendChild(button); }); els.modalOverlay.hidden = false; } function showSetupModal() { const initialLanguage = clamp(Math.round(state.stats.language || 25), 10, 40); els.modalKicker.textContent = "開局設定"; els.modalTitle.textContent = "分配語文與邏輯"; els.modalBody.textContent = "兩項合計固定 50,各項最高 40。"; els.modalChoices.innerHTML = ""; els.modalResult.innerHTML = `
語文 ${initialLanguage}
邏輯 ${50 - initialLanguage}
`; const slider = document.querySelector("#setupLanguageSlider"); const languageValue = document.querySelector("#setupLanguageValue"); const logicValue = document.querySelector("#setupLogicValue"); const startButton = document.createElement("button"); const updateValues = (value) => { const language = clamp(Math.round(Number(value)), 10, 40); slider.value = language; languageValue.textContent = language; logicValue.textContent = 50 - language; }; slider.addEventListener("input", () => updateValues(slider.value)); document.querySelectorAll(".setup-presets button").forEach((button) => { button.addEventListener("click", () => updateValues(button.dataset.language)); }); startButton.className = "modal-choice"; startButton.type = "button"; startButton.textContent = "開始高中生活"; startButton.addEventListener("click", () => { const language = clamp(Math.round(Number(slider.value)), 10, 40); state.stats.language = language; state.stats.logic = 50 - language; state.setupVersion = SETUP_VERSION; state.setupComplete = true; addOpeningLog(); closeModal(); }); els.modalChoices.appendChild(startButton); els.modalOverlay.hidden = false; } function closeModal(afterClose) { els.modalOverlay.hidden = true; modalContinue = null; if (typeof afterClose === "function") { afterClose(); } saveState(); render(); }