Lashtw commited on
Commit
797c912
·
1 Parent(s): 92abf74

refactor: 階段3完成-天機閣演武與軍備箱抽離為tianji.js,game.js再精簡760+行

Browse files
Files changed (3) hide show
  1. js/game.js +17 -766
  2. js/merchant-ui.js +1 -0
  3. js/tianji.js +730 -0
js/game.js CHANGED
@@ -31,7 +31,10 @@ import {
31
  setupMerchantUI, openTodayWordsModal, openCaravanLevelsModal,
32
  openMerchantQuizModal, openMerchantEventModal
33
  } from './merchant-ui.js?v=20261031_v1';
34
- export { openTodayWordsModal, openCaravanLevelsModal, openMerchantQuizModal, openMerchantEventModal };
 
 
 
35
  window.showAnnouncementModal = showAnnouncementModal;
36
  window.getRandomBeggingSpeech = getRandomBeggingSpeech;
37
 
@@ -350,6 +353,18 @@ async function loadStudentData() {
350
  renderMapSpots: () => renderMapSpots(),
351
  });
352
 
 
 
 
 
 
 
 
 
 
 
 
 
353
  updateHeader();
354
  renderBuild();
355
  checkGifts();
@@ -1356,771 +1371,7 @@ function broadcastConquerVictory(bDef) {
1356
  });
1357
  }
1358
 
1359
- // 取得天機閣急速快答題庫
1360
- async function getTianjiQuestions() {
1361
- const bankId = student?.bankId || 'wiring';
1362
- const today = todayStr();
1363
- let pool = [];
1364
- try {
1365
- const bank = await loadBank(bankId);
1366
- if (bank && Array.isArray(bank.questions) && bank.questions.length > 0) {
1367
- // 依當前班級開放之單元篩選題目(嚴格對齊後台開放狀態,支援教師手動勾選與日程)
1368
- let openUnits = SCHEDULE.filter(r => isRangeOpen(r, classInfo, today));
1369
- // 若尚未有開放單元,保底僅限第 1 單元(u1:第 1~25 題),絕不跨出未開放範圍!
1370
- if (!openUnits || openUnits.length === 0) {
1371
- openUnits = [SCHEDULE[0]];
1372
- }
1373
- // 嚴格篩選題號落在已開放單元任一 [r.from, r.to] 區間內的題目
1374
- const filtered = bank.questions.filter(q =>
1375
- openUnits.some(r => typeof q.n === 'number' && q.n >= r.from && q.n <= r.to)
1376
- );
1377
- if (filtered && filtered.length > 0) {
1378
- pool = filtered.map(q => ({
1379
- q: q.q,
1380
- o: q.o,
1381
- a: q.a,
1382
- n: q.n,
1383
- bank: bankId
1384
- }));
1385
- }
1386
- }
1387
- } catch (err) {
1388
- console.warn('載入題庫失敗,使用備用題目', err);
1389
- }
1390
-
1391
- if (!pool || pool.length === 0) {
1392
- pool = [
1393
- { q: '在戰略推演中,兵馬未動,何者先行?', o: ['糧草輜重', '金旗帥印', '戰鼓齊鳴', '先鋒部隊'], a: 'A' },
1394
- { q: '以下何者為大軍出征收復名都之核心重要憑證?', o: ['免死金牌', '調兵兵符', '兵部官牒', '錦囊妙計'], a: 'B' },
1395
- { q: '守禦九州要塞時,最關鍵的基礎戰略資源為何?', o: ['軍備糧草', '精美絲綢', '金銀珠寶', '樓船木材'], a: 'A' },
1396
- { q: '哪一座九州名都專門以產出萬頃稻浪、囤積軍糧著稱?', o: ['平原稻鄉', '景陽瓷都', '百草藥谷', '神兵鐵城'], a: 'A' },
1397
- { q: '天機閣每日急速挑戰演武中,初始士氣心為幾顆?', o: ['1 顆心', '2 顆心', '3 顆心', '5 顆心'], a: 'C' },
1398
- { q: '每日天機閣急速快答挑戰的限時時間為何?', o: ['30 秒', '60 秒', '90 秒', '120 秒'], a: 'B' },
1399
- { q: '在天機閣或大考中,使用何種戰略卷軸可直接高亮正確解答?', o: ['偷看卷', '刪去卷', '急行卷', '免死卷'], a: 'A' },
1400
- { q: '在天機閣或大考中,使用何種戰略卷軸可剔除 2 個錯誤選項?', o: ['偷看卷', '刪去卷', '急行卷', '鼓舞卷'], a: 'B' }
1401
- ].map((item, idx) => ({ ...item, bank: 'tianji_lore', n: idx + 1 }));
1402
- }
1403
-
1404
- return shuffle(pool);
1405
- }
1406
-
1407
- // 計算軍備箱隨機掉落物資
1408
- function rollChestRewards(chestType, curProd) {
1409
- const prod = Math.max(2, curProd);
1410
- if (chestType === 'bronze') {
1411
- // 答對 1 ~ 7 題:
1412
- // 🌾 糧草 100%(5分鐘掛機產量)
1413
- // 穩得 1 張卷軸(70% 刪去卷,30% 偷看卷)
1414
- const isElim = Math.random() < 0.70;
1415
- return {
1416
- coins: Math.round(prod * 300),
1417
- bp: 0,
1418
- peek: isElim ? 0 : 1,
1419
- eliminate: isElim ? 1 : 0
1420
- };
1421
- } else if (chestType === 'silver') {
1422
- // 答對 8 ~ 14 題:
1423
- // 🌾 糧草 100%(15分鐘掛機產量)
1424
- // 🐅 兵符 20% 機率得 1 枚
1425
- // ✂️ 刪去卷 100% 必得 1 張
1426
- // 📜 偷看卷 50% 機率得 1 張
1427
- return {
1428
- coins: Math.round(prod * 900),
1429
- bp: Math.random() < 0.20 ? 1 : 0,
1430
- eliminate: 1,
1431
- peek: Math.random() < 0.50 ? 1 : 0
1432
- };
1433
- } else if (chestType === 'gold') {
1434
- // 答對 15 題以上:
1435
- // 🌾 糧草 100%(30分鐘暴風掛機產量)
1436
- // 🐅 兵符 100% 必得 1~2 枚
1437
- // 📜 偷看卷 100% 必得 1~2 張
1438
- // ✂️ 刪去卷 100% 必得 1 張
1439
- return {
1440
- coins: Math.round(prod * 1800),
1441
- bp: 1 + (Math.random() < 0.35 ? 1 : 0),
1442
- peek: 1 + (Math.random() < 0.35 ? 1 : 0),
1443
- eliminate: 1
1444
- };
1445
- }
1446
- // 答對 0 題(安慰獎)
1447
- return {
1448
- coins: Math.round(prod * 60),
1449
- bp: 0,
1450
- peek: 0,
1451
- eliminate: 0
1452
- };
1453
- }
1454
-
1455
- // 開啟天機閣主面板
1456
- function openTianjiModal() {
1457
- const ts = getTianjiStatus(game);
1458
- if (!game.items) game.items = { peek: 0, eliminate: 0 };
1459
- const peekCount = game.items.peek || 0;
1460
- const elimCount = game.items.eliminate || 0;
1461
-
1462
- const html = `
1463
- <div class="tianji-head-banner">
1464
- <img src="assets/building_tianji.png" class="tianji-banner-thumb" alt="天機閣">
1465
- <div style="flex:1;">
1466
- <div style="display:flex; align-items:center; gap:8px; flex-wrap:wrap;">
1467
- <h2 style="margin:0; font-size:20px;">🏯 天機閣</h2>
1468
- <span class="badge ${ts.canPlay ? 'badge-gold' : ''}" style="${ts.canPlay ? '' : 'background:#e2e8f0; color:#64748b;'}">
1469
- ${ts.canPlay ? '今日遊玩次數 (0/1)' : '今日遊玩次數 (1/1)'}
1470
- </span>
1471
- </div>
1472
- <p class="dim" style="margin:4px 0 0; font-size:13px; line-height:1.5;">
1473
- 每日一次 60 秒急速快答演武!答對題數越多,可解鎖越豐厚之軍備箱。累積答錯 3 題士氣潰散則演武終止。
1474
- </p>
1475
- </div>
1476
- </div>
1477
-
1478
- <!-- 我的戰略錦囊庫存 -->
1479
- <div style="display:flex; gap:12px; margin-bottom:14px; padding:10px 14px; background:var(--bg-2); border-radius:12px; border:1px solid var(--line); align-items:center; justify-content:space-around;">
1480
- <div style="display:flex; align-items:center; gap:8px;">
1481
- <span style="font-size:24px;">📜</span>
1482
- <div>
1483
- <div style="font-size:11px; color:var(--text-dim); font-weight:700;">偷看卷(秒顯正解)</div>
1484
- <b class="num" style="font-size:16px; color:#b45309;">${peekCount} 張</b>
1485
- </div>
1486
- </div>
1487
- <div style="height:28px; width:1px; background:var(--line);"></div>
1488
- <div style="display:flex; align-items:center; gap:8px;">
1489
- <span style="font-size:24px;">✂️</span>
1490
- <div>
1491
- <div style="font-size:11px; color:var(--text-dim); font-weight:700;">刪去卷(二選一)</div>
1492
- <b class="num" style="font-size:16px; color:#dc2626;">${elimCount} 張</b>
1493
- </div>
1494
- </div>
1495
- </div>
1496
-
1497
- <!-- 三大軍備箱卡片展示 -->
1498
- <div class="tianji-chests-row">
1499
- <div class="chest-preview-card">
1500
- <img src="assets/chest_bronze.png" class="chest-preview-img" alt="青銅軍備箱">
1501
- <b style="font-size:13px; color:#78350f;">青銅軍備箱</b>
1502
- <span class="chest-target-tag tag-bronze">答對 1 ~ 7 題</span>
1503
- <span style="font-size:11px; color:var(--text-mute);">穩得 1 張救急卷軸</span>
1504
- </div>
1505
- <div class="chest-preview-card">
1506
- <img src="assets/chest_silver.png" class="chest-preview-img" alt="白銀軍備箱">
1507
- <b style="font-size:13px; color:#334155;">白銀軍備箱</b>
1508
- <span class="chest-target-tag tag-silver">答對 8 ~ 14 題</span>
1509
- <span style="font-size:11px; color:var(--text-mute);">必得刪去+機率兵符</span>
1510
- </div>
1511
- <div class="chest-preview-card">
1512
- <img src="assets/chest_gold.png" class="chest-preview-img" alt="黃金軍備箱">
1513
- <b style="font-size:13px; color:#b45309;">黃金軍備箱</b>
1514
- <span class="chest-target-tag tag-gold">答對 15 題以上</span>
1515
- <span style="font-size:11px; color:var(--text-mute);">100% 必得兵符</span>
1516
- </div>
1517
- </div>
1518
-
1519
- <!-- 查看機率抽屜 -->
1520
- <div class="chest-prob-toggle" id="btnToggleChestProb">
1521
- <span>🎲 查看各軍備箱物資掉落機率公示</span>
1522
- <span id="probToggleIcon">▼</span>
1523
- </div>
1524
- <div class="chest-prob-detail hidden" id="chestProbDetail">
1525
- <div style="font-weight:800; color:var(--gold-dark); margin-bottom:8px;">📊 各軍備箱物資掉落機率明細表:</div>
1526
- <table class="chest-prob-table">
1527
- <thead>
1528
- <tr>
1529
- <th>軍備箱階級</th>
1530
- <th>答對題數</th>
1531
- <th>軍餉糧草</th>
1532
- <th>奇策錦囊(卷軸)</th>
1533
- <th>兵權兵符</th>
1534
- </tr>
1535
- </thead>
1536
- <tbody>
1537
- <tr>
1538
- <td><b style="color:#78350f;">🥉 青銅軍備箱</b></td>
1539
- <td>1 ~ 7 題</td>
1540
- <td>🌾 5 分鐘糧草 (100%)</td>
1541
- <td>✂️ 刪去卷 (70%)<br>📜 偷看卷 (30%)</td>
1542
- <td style="color:var(--text-mute);">—</td>
1543
- </tr>
1544
- <tr>
1545
- <td><b style="color:#334155;">🥈 白銀軍備箱</b></td>
1546
- <td>8 ~ 14 題</td>
1547
- <td>🌾 15 分鐘糧草 (100%)</td>
1548
- <td>✂️ 刪去卷 (100%)<br>📜 偷看卷 (50%)</td>
1549
- <td>🐅 兵符 (20%)</td>
1550
- </tr>
1551
- <tr>
1552
- <td><b style="color:#b45309;">🥇 黃金軍備箱</b></td>
1553
- <td>15 題以上</td>
1554
- <td>🌾 30 分鐘暴風糧草 (100%)</td>
1555
- <td>📜 偷看卷 1~2張 (100%)<br>✂️ 刪去卷 1張 (100%)</td>
1556
- <td>🐅 兵符 1~2枚 (100%)</td>
1557
- </tr>
1558
- </tbody>
1559
- </table>
1560
- </div>
1561
-
1562
- <!-- 操作按鈕區 -->
1563
- <div style="margin-top:14px;">
1564
- ${ts.canPlay ? `
1565
- <button id="btnStartTianjiArena" class="btn btn-primary" style="width:100%; min-height:46px; font-size:16px; font-weight:800; box-shadow:0 4px 14px rgba(239,90,86,0.35);">
1566
- ⚔️ 開始 60 秒急速快答演武
1567
- </button>
1568
- ` : `
1569
- <div style="text-align:center; padding:12px; background:var(--bg-2); border-radius:12px; border:1px solid var(--line);">
1570
- <p style="margin:0; color:var(--text-mute); font-weight:700; font-size:14px;">今日演武已完成(1/1)</p>
1571
- <span style="font-size:12px; color:var(--text-dim);">每日限演武一次・明日 00:00 晨光重設,歡迎明日再來挑戰!</span>
1572
- </div>
1573
- <button id="btnCloseTianjiModal" class="btn btn-ghost" style="width:100%; margin-top:10px;">關閉</button>
1574
- `}
1575
- </div>
1576
- `;
1577
-
1578
- const modal = openModal(html, { wide: true });
1579
-
1580
- const btnToggleProb = $('#btnToggleChestProb', modal.el);
1581
- const detailProb = $('#chestProbDetail', modal.el);
1582
- const iconProb = $('#probToggleIcon', modal.el);
1583
- if (btnToggleProb && detailProb) {
1584
- bind(btnToggleProb, 'click', () => {
1585
- const isHidden = detailProb.classList.toggle('hidden');
1586
- if (iconProb) iconProb.textContent = isHidden ? '▼' : '▲';
1587
- });
1588
- }
1589
-
1590
- const btnStart = $('#btnStartTianjiArena', modal.el);
1591
- if (btnStart) {
1592
- bind(btnStart, 'click', () => {
1593
- modal.close();
1594
- startTianjiArena();
1595
- });
1596
- }
1597
-
1598
- const btnClose = $('#btnCloseTianjiModal', modal.el);
1599
- if (btnClose) {
1600
- bind(btnClose, 'click', () => modal.close());
1601
- }
1602
- }
1603
-
1604
- // 60秒急速快答挑戰競技場(嚴禁中途退出重來)
1605
- async function startTianjiArena() {
1606
- const modal = openModal(`
1607
- <div style="text-align:center; padding:40px 10px;">
1608
- <div class="loader" style="margin:0 auto 16px;"></div>
1609
- <h3 style="margin:0;">天機推演題庫中…</h3>
1610
- <p class="dim" style="font-size:13px; margin-top:6px;">匯集當週各單元戰略題庫・限時 60 秒・3 顆士氣心・開局後不可重來</p>
1611
- </div>
1612
- `, { wide: true, closable: false });
1613
-
1614
- let questions = [];
1615
- try {
1616
- questions = await getTianjiQuestions();
1617
- } catch (err) {
1618
- console.error('天機閣題目載入失敗', err);
1619
- toast('題目載入失敗,請稍後重試', 'error');
1620
- modal.close();
1621
- return;
1622
- }
1623
-
1624
- // 題目載入完畢,演武正式啟動:立即鎖定並存檔今日遊玩狀態,防止學生以重新整理(F5)、切換分頁或重開來洗分!
1625
- game.tianjiDate = todayStr();
1626
- game.tianjiPlayed = 1;
1627
- save();
1628
- renderBuild();
1629
- renderMapSpots();
1630
-
1631
- // 監聽離開警告
1632
- const preventLeave = (e) => {
1633
- e.preventDefault();
1634
- e.returnValue = '⚠️ 天機急速快答演武正在進行中!若離開將直接結算,今日無法重來!';
1635
- return e.returnValue;
1636
- };
1637
- window.addEventListener('beforeunload', preventLeave);
1638
-
1639
- let timeLeft = 60;
1640
- let hearts = 3;
1641
- let correctCount = 0;
1642
- let combo = 0;
1643
- let qIdx = 0;
1644
- let arenaTimer = null;
1645
- let answered = false;
1646
- const wrongLogs = [];
1647
-
1648
- const endArena = (reason) => {
1649
- if (arenaTimer) {
1650
- clearInterval(arenaTimer);
1651
- arenaTimer = null;
1652
- }
1653
- window.removeEventListener('beforeunload', preventLeave);
1654
- showChestUnboxModal(modal, correctCount, reason, wrongLogs);
1655
- };
1656
-
1657
- const renderArenaQ = () => {
1658
- if (hearts <= 0) {
1659
- endArena('defeat');
1660
- return;
1661
- }
1662
- if (timeLeft <= 0) {
1663
- endArena('timeout');
1664
- return;
1665
- }
1666
- if (qIdx >= questions.length) {
1667
- qIdx = 0;
1668
- }
1669
-
1670
- const q = questions[qIdx];
1671
- answered = false;
1672
-
1673
- let forecastTag = '<span class="chest-target-tag tag-bronze">🥉 青銅軍備箱</span>';
1674
- if (correctCount >= 15) {
1675
- forecastTag = '<span class="chest-target-tag tag-gold">🥇 黃金軍備箱</span>';
1676
- } else if (correctCount >= 8) {
1677
- forecastTag = '<span class="chest-target-tag tag-silver">🥈 白銀軍備箱</span>';
1678
- } else if (correctCount === 0) {
1679
- forecastTag = '<span class="chest-target-tag" style="background:#f1f5f9; color:#64748b;">待答對解鎖</span>';
1680
- }
1681
-
1682
- const heartsHtml = [
1683
- hearts >= 1 ? '❤️' : '🖤',
1684
- hearts >= 2 ? '❤️' : '🖤',
1685
- hearts >= 3 ? '❤️' : '🖤'
1686
- ].join('');
1687
-
1688
- const peekCount = game.items?.peek || 0;
1689
- const elimCount = game.items?.eliminate || 0;
1690
-
1691
- const optsHtml = q.o.map((txt, i) => {
1692
- const label = String.fromCharCode(65 + i);
1693
- return `<button class="qz-opt" data-idx="${i}"><span class="L">${label}</span><span class="grow">${renderRichQuizText(txt)}</span></button>`;
1694
- }).join('');
1695
-
1696
- modal.el.innerHTML = `
1697
- <div class="tianji-arena">
1698
- <div class="tianji-timer-wrap">
1699
- <div class="tianji-timer-bar ${timeLeft <= 10 ? 'urgent' : ''}" id="tianjiTimerBar" style="width:${(timeLeft / 60) * 100}%;"></div>
1700
- </div>
1701
- <div class="tianji-hud-row">
1702
- <div class="tianji-hearts" title="剩餘士氣心">${heartsHtml}</div>
1703
- <div class="tianji-score-box">
1704
- <span>🎯 答對:<b>${correctCount}</b> 題</span>
1705
- ${forecastTag}
1706
- </div>
1707
- <div class="tianji-timer-num num" id="tianjiTimerNum">⏱️ ${Math.ceil(timeLeft)}s</div>
1708
- </div>
1709
-
1710
- <div class="tianji-items-bar">
1711
- <button class="item-btn btn-peek" id="btnArenaPeek" ${peekCount > 0 ? '' : 'disabled'}>
1712
- 📜 偷看卷 (${peekCount})
1713
- </button>
1714
- <button class="item-btn btn-elim" id="btnArenaElim" ${elimCount > 0 ? '' : 'disabled'}>
1715
- ✂️ 刪去卷 (${elimCount})
1716
- </button>
1717
- </div>
1718
-
1719
- <div class="qz-q">${renderRichQuizText(q.q)}</div>
1720
- <div class="qz-opts">${optsHtml}</div>
1721
- </div>
1722
- `;
1723
-
1724
- const btnPeek = $('#btnArenaPeek', modal.el);
1725
- if (btnPeek) {
1726
- bind(btnPeek, 'click', () => {
1727
- if (!game.items || game.items.peek <= 0) {
1728
- toast('偷看卷不足!可於軍備箱中獲取', 'error');
1729
- return;
1730
- }
1731
- game.items.peek--;
1732
- btnPeek.textContent = `📜 偷看卷 (${game.items.peek})`;
1733
- btnPeek.disabled = game.items.peek <= 0;
1734
- markDirty();
1735
- $$('.qz-opt', modal.el).forEach(ob => {
1736
- const oL = String.fromCharCode(65 + parseInt(ob.dataset.idx));
1737
- if (oL === q.a) ob.classList.add('opt-peeked');
1738
- });
1739
- toast('📜 已使用偷看卷!正解已閃現金光!', 'gold');
1740
- });
1741
- }
1742
-
1743
- const btnElim = $('#btnArenaElim', modal.el);
1744
- if (btnElim) {
1745
- bind(btnElim, 'click', () => {
1746
- if (!game.items || game.items.eliminate <= 0) {
1747
- toast('刪去卷不足!可於軍備箱中獲取', 'error');
1748
- return;
1749
- }
1750
- game.items.eliminate--;
1751
- btnElim.textContent = `✂️ 刪去卷 (${game.items.eliminate})`;
1752
- btnElim.disabled = game.items.eliminate <= 0;
1753
- markDirty();
1754
- const wrongOpts = Array.from($$('.qz-opt', modal.el)).filter(ob => {
1755
- const oL = String.fromCharCode(65 + parseInt(ob.dataset.idx));
1756
- return oL !== q.a && !ob.classList.contains('opt-eliminated');
1757
- });
1758
- shuffle(wrongOpts).slice(0, 2).forEach(ob => ob.classList.add('opt-eliminated'));
1759
- toast('✂️ 已使用刪去卷!已剔除 2 個錯誤選項!', 'gold');
1760
- });
1761
- }
1762
-
1763
- $$('.qz-opt', modal.el).forEach(ob => {
1764
- bind(ob, 'click', () => {
1765
- if (answered) return;
1766
- answered = true;
1767
- const myIdx = parseInt(ob.dataset.idx);
1768
- const myLabel = String.fromCharCode(65 + myIdx);
1769
- const isCorrect = myLabel === q.a;
1770
-
1771
- $$('.qz-opt', modal.el).forEach(b => {
1772
- b.disabled = true;
1773
- const oL = String.fromCharCode(65 + parseInt(b.dataset.idx));
1774
- if (oL === q.a) b.classList.add('correct');
1775
- else if (oL === myLabel) b.classList.add('wrong');
1776
- });
1777
-
1778
- if (isCorrect) {
1779
- correctCount++;
1780
- combo++;
1781
- recordCorrect(game, q);
1782
- if (combo > 0 && combo % 5 === 0) {
1783
- timeLeft = Math.min(60, timeLeft + 2);
1784
- toast(`🔥 勢如破竹!連續答對 ${combo} 題,獎勵時間 +2 秒!`, 'gold');
1785
- }
1786
- qIdx++;
1787
- setTimeout(renderArenaQ, 300);
1788
- } else {
1789
- combo = 0;
1790
- hearts--;
1791
- recordMistake(game, q, myLabel, 'tianji');
1792
- markDirty();
1793
- wrongLogs.push({
1794
- q: q.q,
1795
- myLabel,
1796
- myText: q.o[myIdx],
1797
- correctLabel: q.a,
1798
- correctText: q.o[q.a.charCodeAt(0) - 65],
1799
- });
1800
- if (hearts <= 0) {
1801
- setTimeout(() => endArena('defeat'), 600);
1802
- } else {
1803
- qIdx++;
1804
- setTimeout(renderArenaQ, 600);
1805
- }
1806
- }
1807
- });
1808
- });
1809
- };
1810
-
1811
- arenaTimer = setInterval(() => {
1812
- timeLeft -= 0.1;
1813
- const bar = $('#tianjiTimerBar', modal.el);
1814
- const num = $('#tianjiTimerNum', modal.el);
1815
- if (bar) {
1816
- const pct = Math.max(0, (timeLeft / 60) * 100);
1817
- bar.style.width = `${pct}%`;
1818
- if (timeLeft <= 10) bar.classList.add('urgent');
1819
- }
1820
- if (num) {
1821
- num.textContent = `⏱️ ${Math.max(0, Math.ceil(timeLeft))}s`;
1822
- }
1823
- if (timeLeft <= 0) {
1824
- endArena('timeout');
1825
- }
1826
- }, 100);
1827
-
1828
- renderArenaQ();
1829
- }
1830
-
1831
- // 開箱舞台與動畫效果
1832
- function showChestUnboxModal(modal, correctCount, reason, wrongLogs = []) {
1833
- let chestType = null;
1834
- let chestName = '安慰軍備物資';
1835
- let chestImg = 'assets/chest_bronze.png';
1836
- let chestColor = '#78350f';
1837
- let chestTag = '🥉 青銅軍備箱';
1838
-
1839
- if (correctCount >= 15) {
1840
- chestType = 'gold';
1841
- chestName = '黃金軍備箱';
1842
- chestImg = 'assets/chest_gold.png';
1843
- chestColor = '#b45309';
1844
- chestTag = '🥇 黃金軍備箱';
1845
- } else if (correctCount >= 8) {
1846
- chestType = 'silver';
1847
- chestName = '白銀軍備箱';
1848
- chestImg = 'assets/chest_silver.png';
1849
- chestColor = '#334155';
1850
- chestTag = '🥈 白銀軍備箱';
1851
- } else if (correctCount >= 1) {
1852
- chestType = 'bronze';
1853
- chestName = '青銅軍備箱';
1854
- chestImg = 'assets/chest_bronze.png';
1855
- chestColor = '#78350f';
1856
- chestTag = '🥉 青銅軍備箱';
1857
- }
1858
-
1859
- const curProd = totalProd(game, dState);
1860
- const endReasonText = reason === 'defeat'
1861
- ? '士氣耗盡(累積答錯 3 題),演武圓滿結算!'
1862
- : '60 秒急速快答時間截止,演武圓滿結算!';
1863
-
1864
- if (chestType === null) {
1865
- const rewards = rollChestRewards(null, curProd);
1866
- modal.el.innerHTML = `
1867
- <div class="chest-stage">
1868
- <h2 style="margin:0; font-size:22px;">⚔️ 天機急速演武結算</h2>
1869
- <p class="dim" style="margin:0; font-size:13px;">${endReasonText}</p>
1870
- <div style="margin:20px 0 10px; padding:18px; background:var(--bg-2); border-radius:14px; border:1px solid var(--line); max-width:380px;">
1871
- <div style="font-size:36px; margin-bottom:8px;">💦</div>
1872
- <b style="font-size:16px;">本次答對 0 題</b>
1873
- <p class="dim" style="font-size:13px; margin:6px 0 0;">勝敗乃兵家常事,天機閣特頒發安慰糧草以資督勵!明日再來定能大展身手!</p>
1874
- <div style="margin-top:14px; font-size:15px; font-weight:800; color:var(--gold-dark);">
1875
- 🌾 獲得安慰糧草:+${fmt(rewards.coins)}
1876
- </div>
1877
- </div>
1878
-
1879
- ${wrongLogs.length > 0 ? `
1880
- <div style="margin-bottom:16px; width:100%; max-width:380px;">
1881
- <button id="btnTianjiReviewWrongs" class="btn btn-ghost btn-sm" style="font-weight:700; color:#b45309; border:1px solid rgba(217,119,6,0.3); background:#fffbeb; width:100%;">
1882
- 📖 查看剛才演武錯題與正解 (${wrongLogs.length}題)
1883
- </button>
1884
- <div id="tianjiWrongDrawer" class="hidden" style="margin-top:8px; text-align:left; max-height:200px; overflow-y:auto; background:#fff; border-radius:10px; padding:10px 14px; border:1px solid var(--line);">
1885
- ${wrongLogs.map((w, wi) => `
1886
- <div style="margin-bottom:8px; padding-bottom:6px; border-bottom:1px dashed var(--line);">
1887
- <div style="font-weight:700; font-size:13px; margin-bottom:3px;">${wi + 1}. ${w.q}</div>
1888
- <div style="font-size:12px; color:#dc2626;">❌ 你的作答:(${w.myLabel}) ${w.myText}</div>
1889
- <div style="font-size:12px; color:#059669; font-weight:700;">✅ 正確解答:(${w.correctLabel}) ${w.correctText}</div>
1890
- </div>
1891
- `).join('')}
1892
- </div>
1893
- </div>
1894
- ` : ''}
1895
-
1896
- <button id="btnClaimZeroRewards" class="btn btn-primary" style="min-width:200px; font-size:16px; font-weight:800; padding:10px 24px;">
1897
- 領取補給入庫
1898
- </button>
1899
- </div>
1900
- `;
1901
-
1902
- const btnReview0 = $('#btnTianjiReviewWrongs', modal.el);
1903
- const drawer0 = $('#tianjiWrongDrawer', modal.el);
1904
- if (btnReview0 && drawer0) {
1905
- bind(btnReview0, 'click', () => {
1906
- const isHidden = drawer0.classList.toggle('hidden');
1907
- btnReview0.textContent = isHidden ? `📖 查看剛才演武錯題與正解 (${wrongLogs.length}題)` : '📕 收起錯題解析';
1908
- });
1909
- }
1910
-
1911
- bind($('#btnClaimZeroRewards', modal.el), 'click', () => {
1912
- game.coins += rewards.coins;
1913
- game.totalEarned += rewards.coins;
1914
- game.tianjiDate = todayStr();
1915
- game.tianjiPlayed = 1;
1916
- save();
1917
- renderRes();
1918
- renderBuild();
1919
- modal.close();
1920
- toast(`領取安慰糧草 +${fmt(rewards.coins)}!明日 00:00 歡迎再來挑戰!`, 'gold');
1921
- });
1922
- return;
1923
- }
1924
-
1925
- // 1題以上:展示寶箱圖,開啟前可看機率,點擊有開箱震撼動效
1926
- modal.el.innerHTML = `
1927
- <div class="chest-stage">
1928
- <div style="text-align:center;">
1929
- <h2 style="margin:0; font-size:22px;">⚔️ 天機演武大捷!答對 ${correctCount} 題</h2>
1930
- <p class="dim" style="margin:4px 0 0; font-size:13px;">${endReasonText}</p>
1931
- </div>
1932
-
1933
- <div class="chest-big-wrap" id="chestBigWrap" title="點擊寶箱立即開啟!">
1934
- <div class="chest-rays"></div>
1935
- <img src="${chestImg}" class="chest-big-img" alt="${chestName}">
1936
- </div>
1937
-
1938
- <div style="text-align:center;">
1939
- <span class="chest-target-tag" style="background:rgba(245,158,11,0.15); color:${chestColor}; font-size:13px; font-weight:900;">
1940
- ${chestTag}
1941
- </span>
1942
- <h3 style="margin:6px 0 2px; font-size:18px; color:${chestColor};">恭獲【${chestName}】</h3>
1943
- <p class="dim" id="chestActionHint" style="margin:0; font-size:13px;">點擊寶箱或下方按鈕解鎖開啟</p>
1944
- </div>
1945
-
1946
- <!-- 開��之前查看機率按鈕 -->
1947
- <button id="btnViewThisChestProb" class="btn btn-ghost btn-sm" style="font-weight:700; color:var(--gold);">
1948
- 🎲 開啟前查看【${chestName}】掉落物資機率
1949
- </button>
1950
-
1951
- <div id="thisChestProbDetail" class="chest-prob-detail hidden" style="text-align:left; width:100%; max-width:440px; margin:0 auto;">
1952
- ${chestType === 'bronze' ? `
1953
- <div style="font-weight:800; color:#78350f; margin-bottom:6px;">🥉 青銅軍備箱掉落明細:</div>
1954
- <table class="chest-prob-table">
1955
- <thead><tr><th>物資項目</th><th style="text-align:right;">掉落機率</th></tr></thead>
1956
- <tbody>
1957
- <tr><td>🌾 糧草補給 (5分鐘掛機產量)</td><td style="text-align:right;"><b>100%</b></td></tr>
1958
- <tr><td>✂️ 刪去卷 (1張)</td><td style="text-align:right;"><b>70%</b></td></tr>
1959
- <tr><td>📜 偷看卷 (1張)</td><td style="text-align:right;"><b>30%</b></td></tr>
1960
- </tbody>
1961
- </table>
1962
- ` : chestType === 'silver' ? `
1963
- <div style="font-weight:800; color:#334155; margin-bottom:6px;">🥈 白銀軍備箱掉落明細:</div>
1964
- <table class="chest-prob-table">
1965
- <thead><tr><th>物資項目</th><th style="text-align:right;">掉落機率</th></tr></thead>
1966
- <tbody>
1967
- <tr><td>🌾 糧草補給 (15分鐘掛機產量)</td><td style="text-align:right;"><b>100%</b></td></tr>
1968
- <tr><td>✂️ 刪去卷 (1張)</td><td style="text-align:right;"><b>100%</b></td></tr>
1969
- <tr><td>📜 偷看卷 (1張)</td><td style="text-align:right;"><b>50%</b></td></tr>
1970
- <tr><td>🐅 兵符 (1枚)</td><td style="text-align:right;"><b>20%</b></td></tr>
1971
- </tbody>
1972
- </table>
1973
- ` : `
1974
- <div style="font-weight:800; color:#b45309; margin-bottom:6px;">🥇 黃金軍備箱掉落明細:</div>
1975
- <table class="chest-prob-table">
1976
- <thead><tr><th>物資項目</th><th style="text-align:right;">掉落機率</th></tr></thead>
1977
- <tbody>
1978
- <tr><td>🌾 暴風糧草 (30分鐘掛機產量)</td><td style="text-align:right;"><b>100%</b></td></tr>
1979
- <tr><td>🐅 兵符 (1~2枚)</td><td style="text-align:right;"><b>100%</b></td></tr>
1980
- <tr><td>📜 偷看卷 (1~2張)</td><td style="text-align:right;"><b>100%</b></td></tr>
1981
- <tr><td>✂️ 刪去卷 (1張)</td><td style="text-align:right;"><b>100%</b></td></tr>
1982
- </tbody>
1983
- </table>
1984
- `}
1985
- </div>
1986
-
1987
- <div id="chestRewardsContainer" class="hidden" style="width:100%; display:flex; justify-content:center;"></div>
1988
-
1989
- <div id="chestBtnArea" style="width:100%; text-align:center;">
1990
- <button id="btnOpenChestAction" class="btn btn-primary" style="min-width:220px; font-size:16px; font-weight:800; padding:12px 28px; box-shadow:0 4px 16px rgba(239,90,86,0.35);">
1991
- 🎁 立即開啟軍備箱
1992
- </button>
1993
- </div>
1994
-
1995
- ${wrongLogs && wrongLogs.length > 0 ? `
1996
- <div style="margin-top:14px; width:100%; max-width:440px; text-align:center;">
1997
- <button id="btnTianjiReviewWrongs" class="btn btn-ghost btn-sm" style="font-weight:700; color:#b45309; border:1px solid rgba(217,119,6,0.3); background:#fffbeb;">
1998
- 📖 查看剛才演武錯題與正解 (${wrongLogs.length}題)
1999
- </button>
2000
- <div id="tianjiWrongDrawer" class="hidden" style="margin-top:8px; text-align:left; max-height:200px; overflow-y:auto; background:var(--bg-2); border-radius:10px; padding:10px 14px; border:1px solid var(--line);">
2001
- ${wrongLogs.map((w, wi) => `
2002
- <div style="margin-bottom:8px; padding-bottom:6px; border-bottom:1px dashed var(--line);">
2003
- <div style="font-weight:700; font-size:13px; margin-bottom:3px;">${wi + 1}. ${w.q}</div>
2004
- <div style="font-size:12px; color:#dc2626;">❌ 你的作答:(${w.myLabel}) ${w.myText}</div>
2005
- <div style="font-size:12px; color:#059669; font-weight:700;">✅ 正確解答:(${w.correctLabel}) ${w.correctText}</div>
2006
- </div>
2007
- `).join('')}
2008
- </div>
2009
- </div>
2010
- ` : ''}
2011
- </div>
2012
- `;
2013
-
2014
- const btnViewProb = $('#btnViewThisChestProb', modal.el);
2015
- const thisDetail = $('#thisChestProbDetail', modal.el);
2016
- if (btnViewProb && thisDetail) {
2017
- bind(btnViewProb, 'click', () => {
2018
- thisDetail.classList.toggle('hidden');
2019
- });
2020
- }
2021
-
2022
- const btnReview = $('#btnTianjiReviewWrongs', modal.el);
2023
- const drawer = $('#tianjiWrongDrawer', modal.el);
2024
- if (btnReview && drawer) {
2025
- bind(btnReview, 'click', () => {
2026
- const isHidden = drawer.classList.toggle('hidden');
2027
- btnReview.textContent = isHidden ? `📖 查看剛才演武錯題與正解 (${wrongLogs.length}題)` : '📕 收起錯題解析';
2028
- });
2029
- }
2030
-
2031
- const chestWrap = $('#chestBigWrap', modal.el);
2032
- const btnOpen = $('#btnOpenChestAction', modal.el);
2033
- const hintText = $('#chestActionHint', modal.el);
2034
-
2035
- let isOpening = false;
2036
- const doOpen = () => {
2037
- if (isOpening) return;
2038
- isOpening = true;
2039
- if (btnOpen) btnOpen.disabled = true;
2040
- if (btnViewProb) btnViewProb.classList.add('hidden');
2041
- if (thisDetail) thisDetail.classList.add('hidden');
2042
-
2043
- if (hintText) hintText.innerHTML = '<span style="color:var(--gold); font-weight:800;">🌟 正在奮力破鎖開啟寶箱中…</span>';
2044
- chestWrap.classList.add('rumble'); // CSS 晃動動畫
2045
-
2046
- setTimeout(() => {
2047
- chestWrap.classList.remove('rumble');
2048
- chestWrap.classList.add('open'); // CSS 金芒爆發動畫
2049
- if (hintText) hintText.innerHTML = '<span style="color:var(--gold-dark); font-size:16px; font-weight:900;">✨ 寶箱開啟!軍備物資噴湧而出!</span>';
2050
-
2051
- const rewards = rollChestRewards(chestType, curProd);
2052
-
2053
- let cardsHtml = `
2054
- <div class="reward-item-card">
2055
- <img src="assets/icon_grain.jpg" alt="糧草">
2056
- <b>+${fmt(rewards.coins)}</b>
2057
- <span>軍備糧草</span>
2058
- </div>
2059
- `;
2060
- if (rewards.bp > 0) {
2061
- cardsHtml += `
2062
- <div class="reward-item-card">
2063
- <img src="assets/icon_tally.jpg" alt="兵符">
2064
- <b>+${rewards.bp} 枚</b>
2065
- <span>兵符</span>
2066
- </div>
2067
- `;
2068
- }
2069
- if (rewards.peek > 0) {
2070
- cardsHtml += `
2071
- <div class="reward-item-card">
2072
- <span style="font-size:36px;">📜</span>
2073
- <b>+${rewards.peek} 張</b>
2074
- <span>偷看卷(秒顯正解)</span>
2075
- </div>
2076
- `;
2077
- }
2078
- if (rewards.eliminate > 0) {
2079
- cardsHtml += `
2080
- <div class="reward-item-card">
2081
- <span style="font-size:36px;">✂️</span>
2082
- <b>+${rewards.eliminate} 張</b>
2083
- <span>刪去卷(二選一)</span>
2084
- </div>
2085
- `;
2086
- }
2087
-
2088
- const container = $('#chestRewardsContainer', modal.el);
2089
- container.innerHTML = `<div class="rewards-grid">${cardsHtml}</div>`;
2090
- container.classList.remove('hidden');
2091
-
2092
- const btnArea = $('#chestBtnArea', modal.el);
2093
- btnArea.innerHTML = `
2094
- <button id="btnClaimTianjiRewards" class="btn btn-primary" style="min-width:220px; font-size:16px; font-weight:800; padding:12px 28px; box-shadow:0 4px 16px rgba(16,185,129,0.35); background:linear-gradient(135deg, #10b981, #059669);">
2095
- 📥 領取戰利品入庫
2096
- </button>
2097
- `;
2098
-
2099
- bind($('#btnClaimTianjiRewards', modal.el), 'click', () => {
2100
- game.coins += rewards.coins;
2101
- game.totalEarned += rewards.coins;
2102
- if (rewards.bp > 0) game.blueprints += rewards.bp;
2103
- if (!game.items) game.items = { peek: 0, eliminate: 0 };
2104
- if (rewards.peek > 0) game.items.peek = (game.items.peek || 0) + rewards.peek;
2105
- if (rewards.eliminate > 0) game.items.eliminate = (game.items.eliminate || 0) + rewards.eliminate;
2106
-
2107
- // 標記今日已玩完
2108
- game.tianjiDate = todayStr();
2109
- game.tianjiPlayed = 1;
2110
-
2111
- save();
2112
- renderRes();
2113
- renderBuild();
2114
- modal.close();
2115
-
2116
- toast(`🎉 軍備已悉數入庫!獲得 +${fmt(rewards.coins)} 糧草${rewards.bp ? `、+${rewards.bp} 兵符` : ''}${rewards.peek ? `、+${rewards.peek} 偷看卷` : ''}${rewards.eliminate ? `、+${rewards.eliminate} 刪去卷` : ''}`, 'gold', 5000);
2117
- });
2118
- }, 1200);
2119
- };
2120
-
2121
- if (btnOpen) bind(btnOpen, 'click', doOpen);
2122
- bind(chestWrap, 'click', doOpen);
2123
- }
2124
 
2125
  // ===================== 測驗(每日精熟複習至多 4 卷機制) =====================
2126
  function updateQuizRedDot() {
 
31
  setupMerchantUI, openTodayWordsModal, openCaravanLevelsModal,
32
  openMerchantQuizModal, openMerchantEventModal
33
  } from './merchant-ui.js?v=20261031_v1';
34
+ import {
35
+ setupTianji, openTianjiModal, rollChestRewards, showChestUnboxModal
36
+ } from './tianji.js?v=20261031_v1';
37
+ export { openTodayWordsModal, openCaravanLevelsModal, openMerchantQuizModal, openMerchantEventModal, openTianjiModal };
38
  window.showAnnouncementModal = showAnnouncementModal;
39
  window.getRandomBeggingSpeech = getRandomBeggingSpeech;
40
 
 
353
  renderMapSpots: () => renderMapSpots(),
354
  });
355
 
356
+ setupTianji({
357
+ getGame: () => game,
358
+ getClassInfo: () => classInfo,
359
+ getStudent: () => student,
360
+ getDState: () => dState,
361
+ save: () => save(),
362
+ renderRes: () => renderRes(),
363
+ renderBuild: () => renderBuild(),
364
+ renderMapSpots: () => renderMapSpots(),
365
+ markDirty: () => markDirty(),
366
+ });
367
+
368
  updateHeader();
369
  renderBuild();
370
  checkGifts();
 
1371
  });
1372
  }
1373
 
1374
+ // (天機閣急速快答演武與三階軍備箱開箱系統已抽離至 js/tianji.js)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1375
 
1376
  // ===================== 測驗(每日精熟複習至多 4 卷機制) =====================
1377
  function updateQuizRedDot() {
js/merchant-ui.js CHANGED
@@ -1033,3 +1033,4 @@ export async function openMerchantEventModal() {
1033
  showIntroView();
1034
  }
1035
  if (typeof window !== 'undefined') window.openMerchantEvent = openMerchantEventModal;
 
 
1033
  showIntroView();
1034
  }
1035
  if (typeof window !== 'undefined') window.openMerchantEvent = openMerchantEventModal;
1036
+
js/tianji.js ADDED
@@ -0,0 +1,730 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // ============================================================================
2
+ // js/tianji.js
3
+ // 天機閣急速快答演武與三階軍備箱開箱系統
4
+ // ============================================================================
5
+ import { SCHEDULE, APP_BUILD_VERSION } from './config.js?v=20261031_v1';
6
+ import {
7
+ isRangeOpen, totalProd, getTianjiStatus, recordMistake, recordCorrect
8
+ } from './engine.js?v=20261031_v1';
9
+ import { loadBank } from './quiz.js?v=20261031_v1';
10
+ import {
11
+ $, $$, fmt, todayStr, bind, toast, openModal, esc, shuffle, renderRichQuizText
12
+ } from './util.js?v=20261031_v1';
13
+
14
+ let tianjiCtx = null;
15
+
16
+ export function setupTianji(context) {
17
+ tianjiCtx = context;
18
+ }
19
+
20
+ /**
21
+ * 取得天機閣急速快答題庫
22
+ */
23
+ export async function getTianjiQuestions() {
24
+ const student = tianjiCtx?.getStudent?.();
25
+ const classInfo = tianjiCtx?.getClassInfo?.();
26
+ const bankId = student?.bankId || 'wiring';
27
+ const today = todayStr();
28
+ let pool = [];
29
+ try {
30
+ const bank = await loadBank(bankId);
31
+ if (bank && Array.isArray(bank.questions) && bank.questions.length > 0) {
32
+ // 依當前班級開放之單元篩選題目(嚴格對齊後台開放狀態,支援教師手動勾選與日程)
33
+ let openUnits = SCHEDULE.filter(r => isRangeOpen(r, classInfo, today));
34
+ // 若尚未有開放單元,保底僅限第 1 單元(u1:第 1~25 題),絕不跨出未開放範圍!
35
+ if (!openUnits || openUnits.length === 0) {
36
+ openUnits = [SCHEDULE[0]];
37
+ }
38
+ // 嚴格篩選題號落在已開放單元任一 [r.from, r.to] 區間內的題目
39
+ const filtered = bank.questions.filter(q =>
40
+ openUnits.some(r => typeof q.n === 'number' && q.n >= r.from && q.n <= r.to)
41
+ );
42
+ if (filtered && filtered.length > 0) {
43
+ pool = filtered.map(q => ({
44
+ q: q.q,
45
+ o: q.o,
46
+ a: q.a,
47
+ n: q.n,
48
+ bank: bankId
49
+ }));
50
+ }
51
+ }
52
+ } catch (err) {
53
+ console.warn('載入題庫失敗,使用備用題目', err);
54
+ }
55
+
56
+ if (!pool || pool.length === 0) {
57
+ pool = [
58
+ { q: '在戰略推演中,兵馬未動,何者先行?', o: ['糧草輜重', '金旗帥印', '戰鼓齊鳴', '先鋒部隊'], a: 'A' },
59
+ { q: '以下何者為大軍出征收復名都之核心重要憑證?', o: ['免死金牌', '調兵兵符', '兵部官牒', '錦囊妙計'], a: 'B' },
60
+ { q: '守禦九州要塞時,最關鍵的基礎戰略資源為何?', o: ['軍備糧草', '精美絲綢', '金銀珠寶', '樓船木材'], a: 'A' },
61
+ { q: '哪一座九州名都專門以產出萬頃稻浪、囤積軍糧著稱?', o: ['平原稻鄉', '景陽瓷都', '百草藥谷', '神兵鐵城'], a: 'A' },
62
+ { q: '天機閣每日急速挑戰演武中,初始士氣心為幾顆?', o: ['1 顆心', '2 顆心', '3 顆心', '5 顆心'], a: 'C' },
63
+ { q: '每日天機閣急速快答挑戰的限時時間為何?', o: ['30 秒', '60 秒', '90 秒', '120 秒'], a: 'B' },
64
+ { q: '在天機閣或大考中,使用何種戰略卷軸可直接高亮正確解答?', o: ['偷看卷', '刪去卷', '急行卷', '免死卷'], a: 'A' },
65
+ { q: '在天機閣或大考中,使用何種戰略卷軸可剔除 2 個錯誤選項?', o: ['偷看卷', '刪去卷', '急行卷', '鼓舞卷'], a: 'B' }
66
+ ].map((item, idx) => ({ ...item, bank: 'tianji_lore', n: idx + 1 }));
67
+ }
68
+
69
+ return shuffle(pool);
70
+ }
71
+
72
+ /**
73
+ * 計算軍備箱隨機掉落物資
74
+ */
75
+ export function rollChestRewards(chestType, curProd) {
76
+ const prod = Math.max(2, curProd);
77
+ if (chestType === 'bronze') {
78
+ // 答對 1 ~ 7 題:
79
+ // 🌾 糧草 100%(5分鐘掛機產量)
80
+ // 穩得 1 張卷軸(70% 刪去卷,30% 偷看卷)
81
+ const isElim = Math.random() < 0.70;
82
+ return {
83
+ coins: Math.round(prod * 300),
84
+ bp: 0,
85
+ peek: isElim ? 0 : 1,
86
+ eliminate: isElim ? 1 : 0
87
+ };
88
+ } else if (chestType === 'silver') {
89
+ // 答對 8 ~ 14 題:
90
+ // 🌾 糧草 100%(15分鐘掛機產量)
91
+ // 🐅 兵符 20% 機率得 1 枚
92
+ // ✂️ 刪去卷 100% 必得 1 張
93
+ // 📜 偷看卷 50% 機率得 1 張
94
+ return {
95
+ coins: Math.round(prod * 900),
96
+ bp: Math.random() < 0.20 ? 1 : 0,
97
+ eliminate: 1,
98
+ peek: Math.random() < 0.50 ? 1 : 0
99
+ };
100
+ } else if (chestType === 'gold') {
101
+ // 答對 15 題以上:
102
+ // 🌾 糧草 100%(30分鐘暴風掛機產量)
103
+ // 🐅 兵符 100% 必得 1~2 枚
104
+ // 📜 偷看卷 100% 必得 1~2 張
105
+ // ✂️ 刪去卷 100% 必得 1 張
106
+ return {
107
+ coins: Math.round(prod * 1800),
108
+ bp: 1 + (Math.random() < 0.35 ? 1 : 0),
109
+ peek: 1 + (Math.random() < 0.35 ? 1 : 0),
110
+ eliminate: 1
111
+ };
112
+ }
113
+ // 答對 0 題(安慰獎)
114
+ return {
115
+ coins: Math.round(prod * 60),
116
+ bp: 0,
117
+ peek: 0,
118
+ eliminate: 0
119
+ };
120
+ }
121
+
122
+ /**
123
+ * 開啟天機閣主面板
124
+ */
125
+ export function openTianjiModal() {
126
+ const game = tianjiCtx?.getGame?.();
127
+ if (!game) return;
128
+
129
+ const ts = getTianjiStatus(game);
130
+ if (!game.items) game.items = { peek: 0, eliminate: 0 };
131
+ const peekCount = game.items.peek || 0;
132
+ const elimCount = game.items.eliminate || 0;
133
+
134
+ const html = `
135
+ <div class="tianji-head-banner">
136
+ <img src="assets/building_tianji.png" class="tianji-banner-thumb" alt="天機閣">
137
+ <div style="flex:1;">
138
+ <div style="display:flex; align-items:center; gap:8px; flex-wrap:wrap;">
139
+ <h2 style="margin:0; font-size:20px;">🏯 天機閣</h2>
140
+ <span class="badge ${ts.canPlay ? 'badge-gold' : ''}" style="${ts.canPlay ? '' : 'background:#e2e8f0; color:#64748b;'}">
141
+ ${ts.canPlay ? '今日遊玩次數 (0/1)' : '今日遊玩次數 (1/1)'}
142
+ </span>
143
+ </div>
144
+ <p class="dim" style="margin:4px 0 0; font-size:13px; line-height:1.5;">
145
+ 每日一次 60 秒急速快答演武!答對題數越多,可解鎖越豐厚之軍備箱。累積答錯 3 題士氣潰散則演武終止。
146
+ </p>
147
+ </div>
148
+ </div>
149
+
150
+ <!-- 我的戰略錦囊庫存 -->
151
+ <div style="display:flex; gap:12px; margin-bottom:14px; padding:10px 14px; background:var(--bg-2); border-radius:12px; border:1px solid var(--line); align-items:center; justify-content:space-around;">
152
+ <div style="display:flex; align-items:center; gap:8px;">
153
+ <span style="font-size:24px;">📜</span>
154
+ <div>
155
+ <div style="font-size:11px; color:var(--text-dim); font-weight:700;">偷看卷(秒顯正解)</div>
156
+ <b class="num" style="font-size:16px; color:#b45309;">${peekCount} 張</b>
157
+ </div>
158
+ </div>
159
+ <div style="height:28px; width:1px; background:var(--line);"></div>
160
+ <div style="display:flex; align-items:center; gap:8px;">
161
+ <span style="font-size:24px;">✂️</span>
162
+ <div>
163
+ <div style="font-size:11px; color:var(--text-dim); font-weight:700;">刪去卷(二選一)</div>
164
+ <b class="num" style="font-size:16px; color:#dc2626;">${elimCount} 張</b>
165
+ </div>
166
+ </div>
167
+ </div>
168
+
169
+ <!-- 三大軍備箱卡片展示 -->
170
+ <div class="tianji-chests-row">
171
+ <div class="chest-preview-card">
172
+ <img src="assets/chest_bronze.png" class="chest-preview-img" alt="青銅軍備箱">
173
+ <b style="font-size:13px; color:#78350f;">青銅軍備箱</b>
174
+ <span class="chest-target-tag tag-bronze">答對 1 ~ 7 題</span>
175
+ <span style="font-size:11px; color:var(--text-mute);">穩得 1 張救急卷軸</span>
176
+ </div>
177
+ <div class="chest-preview-card">
178
+ <img src="assets/chest_silver.png" class="chest-preview-img" alt="白銀軍備箱">
179
+ <b style="font-size:13px; color:#334155;">白銀軍備箱</b>
180
+ <span class="chest-target-tag tag-silver">答對 8 ~ 14 題</span>
181
+ <span style="font-size:11px; color:var(--text-mute);">必得刪去+機率兵符</span>
182
+ </div>
183
+ <div class="chest-preview-card">
184
+ <img src="assets/chest_gold.png" class="chest-preview-img" alt="黃金軍備箱">
185
+ <b style="font-size:13px; color:#b45309;">黃金軍備箱</b>
186
+ <span class="chest-target-tag tag-gold">答對 15 題以上</span>
187
+ <span style="font-size:11px; color:var(--text-mute);">100% 必得兵符</span>
188
+ </div>
189
+ </div>
190
+
191
+ <!-- 查看機率抽屜 -->
192
+ <div class="chest-prob-toggle" id="btnToggleChestProb">
193
+ <span>🎲 查看各軍備箱物資掉落機率公示</span>
194
+ <span id="probToggleIcon">▼</span>
195
+ </div>
196
+ <div class="chest-prob-detail hidden" id="chestProbDetail">
197
+ <div style="font-weight:800; color:var(--gold-dark); margin-bottom:8px;">📊 各軍備箱物資掉落機率明細表:</div>
198
+ <table class="chest-prob-table">
199
+ <thead>
200
+ <tr>
201
+ <th>軍備箱階級</th>
202
+ <th>答對題數</th>
203
+ <th>軍餉糧草</th>
204
+ <th>奇策錦囊(卷軸)</th>
205
+ <th>兵權兵符</th>
206
+ </tr>
207
+ </thead>
208
+ <tbody>
209
+ <tr>
210
+ <td><b style="color:#78350f;">🥉 青銅軍備箱</b></td>
211
+ <td>1 ~ 7 題</td>
212
+ <td>🌾 5 分鐘糧草 (100%)</td>
213
+ <td>✂️ 刪去卷 (70%)<br>📜 偷看卷 (30%)</td>
214
+ <td style="color:var(--text-mute);">—</td>
215
+ </tr>
216
+ <tr>
217
+ <td><b style="color:#334155;">🥈 白銀軍備箱</b></td>
218
+ <td>8 ~ 14 題</td>
219
+ <td>🌾 15 分鐘糧草 (100%)</td>
220
+ <td>✂️ 刪去卷 (100%)<br>📜 偷看卷 (50%)</td>
221
+ <td>🐅 兵符 (20%)</td>
222
+ </tr>
223
+ <tr>
224
+ <td><b style="color:#b45309;">🥇 黃金軍備箱</b></td>
225
+ <td>15 題以上</td>
226
+ <td>🌾 30 分鐘暴風糧草 (100%)</td>
227
+ <td>📜 偷看卷 1~2張 (100%)<br>✂️ 刪去卷 1張 (100%)</td>
228
+ <td>🐅 兵符 1~2枚 (100%)</td>
229
+ </tr>
230
+ </tbody>
231
+ </table>
232
+ </div>
233
+
234
+ <!-- 操作按鈕區 -->
235
+ <div style="margin-top:14px;">
236
+ ${ts.canPlay ? `
237
+ <button id="btnStartTianjiArena" class="btn btn-primary" style="width:100%; min-height:46px; font-size:16px; font-weight:800; box-shadow:0 4px 14px rgba(239,90,86,0.35);">
238
+ ⚔️ 開始 60 秒急速快答演武
239
+ </button>
240
+ ` : `
241
+ <div style="text-align:center; padding:12px; background:var(--bg-2); border-radius:12px; border:1px solid var(--line);">
242
+ <p style="margin:0; color:var(--text-mute); font-weight:700; font-size:14px;">今日演武已完成(1/1)</p>
243
+ <span style="font-size:12px; color:var(--text-dim);">每日限演武一次・明日 00:00 晨光重設,歡迎明日再來挑戰!</span>
244
+ </div>
245
+ <button id="btnCloseTianjiModal" class="btn btn-ghost" style="width:100%; margin-top:10px;">關閉</button>
246
+ `}
247
+ </div>
248
+ `;
249
+
250
+ const modal = openModal(html, { wide: true });
251
+
252
+ const btnToggleProb = $('#btnToggleChestProb', modal.el);
253
+ const detailProb = $('#chestProbDetail', modal.el);
254
+ const iconProb = $('#probToggleIcon', modal.el);
255
+ if (btnToggleProb && detailProb) {
256
+ bind(btnToggleProb, 'click', () => {
257
+ const isHidden = detailProb.classList.toggle('hidden');
258
+ if (iconProb) iconProb.textContent = isHidden ? '▼' : '▲';
259
+ });
260
+ }
261
+
262
+ const btnStart = $('#btnStartTianjiArena', modal.el);
263
+ if (btnStart) {
264
+ bind(btnStart, 'click', () => {
265
+ modal.close();
266
+ startTianjiArena();
267
+ });
268
+ }
269
+
270
+ const btnClose = $('#btnCloseTianjiModal', modal.el);
271
+ if (btnClose) {
272
+ bind(btnClose, 'click', () => modal.close());
273
+ }
274
+ }
275
+
276
+ /**
277
+ * 60秒急速快答挑戰競技場(嚴禁中途退出重來)
278
+ */
279
+ export async function startTianjiArena() {
280
+ const game = tianjiCtx?.getGame?.();
281
+ if (!game) return;
282
+
283
+ const modal = openModal(`
284
+ <div style="text-align:center; padding:40px 10px;">
285
+ <div class="loader" style="margin:0 auto 16px;"></div>
286
+ <h3 style="margin:0;">天機推演題庫中…</h3>
287
+ <p class="dim" style="font-size:13px; margin-top:6px;">匯集當週各單元戰略題庫・限時 60 秒・3 顆士氣心・開局後不可重來</p>
288
+ </div>
289
+ `, { wide: true, closable: false });
290
+
291
+ let questions = [];
292
+ try {
293
+ questions = await getTianjiQuestions();
294
+ } catch (err) {
295
+ console.error('天機閣題目載入失敗', err);
296
+ toast('題目載入失敗,請稍後重試', 'error');
297
+ modal.close();
298
+ return;
299
+ }
300
+
301
+ // 題目載入完畢,演武正式啟動:立即鎖定並存檔今日遊玩狀態,防止學生以重新整理(F5)、切換分頁或重開來洗分!
302
+ game.tianjiDate = todayStr();
303
+ game.tianjiPlayed = 1;
304
+ tianjiCtx?.save?.();
305
+ tianjiCtx?.renderBuild?.();
306
+ tianjiCtx?.renderMapSpots?.();
307
+
308
+ // 監聽離開警告
309
+ const preventLeave = (e) => {
310
+ e.preventDefault();
311
+ e.returnValue = '⚠️ 天機急速快答演武正在進行中!若離開將直接結算,今日無法重來!';
312
+ return e.returnValue;
313
+ };
314
+ window.addEventListener('beforeunload', preventLeave);
315
+
316
+ let timeLeft = 60;
317
+ let hearts = 3;
318
+ let correctCount = 0;
319
+ let combo = 0;
320
+ let qIdx = 0;
321
+ let arenaTimer = null;
322
+ let answered = false;
323
+ const wrongLogs = [];
324
+
325
+ const endArena = (reason) => {
326
+ if (arenaTimer) {
327
+ clearInterval(arenaTimer);
328
+ arenaTimer = null;
329
+ }
330
+ window.removeEventListener('beforeunload', preventLeave);
331
+ showChestUnboxModal(modal, correctCount, reason, wrongLogs);
332
+ };
333
+
334
+ const renderArenaQ = () => {
335
+ if (hearts <= 0) {
336
+ endArena('defeat');
337
+ return;
338
+ }
339
+ if (timeLeft <= 0) {
340
+ endArena('timeout');
341
+ return;
342
+ }
343
+ if (qIdx >= questions.length) {
344
+ qIdx = 0;
345
+ }
346
+
347
+ const q = questions[qIdx];
348
+ answered = false;
349
+
350
+ let forecastTag = '<span class="chest-target-tag tag-bronze">🥉 青銅軍備箱</span>';
351
+ if (correctCount >= 15) {
352
+ forecastTag = '<span class="chest-target-tag tag-gold">🥇 黃金軍備箱</span>';
353
+ } else if (correctCount >= 8) {
354
+ forecastTag = '<span class="chest-target-tag tag-silver">🥈 白銀軍備箱</span>';
355
+ } else if (correctCount === 0) {
356
+ forecastTag = '<span class="chest-target-tag" style="background:#f1f5f9; color:#64748b;">待答對解鎖</span>';
357
+ }
358
+
359
+ const heartsHtml = [
360
+ hearts >= 1 ? '❤️' : '🖤',
361
+ hearts >= 2 ? '❤️' : '🖤',
362
+ hearts >= 3 ? '❤️' : '🖤'
363
+ ].join('');
364
+
365
+ const peekCount = game.items?.peek || 0;
366
+ const elimCount = game.items?.eliminate || 0;
367
+
368
+ const optsHtml = q.o.map((txt, i) => {
369
+ const label = String.fromCharCode(65 + i);
370
+ return `<button class="qz-opt" data-idx="${i}"><span class="L">${label}</span><span class="grow">${renderRichQuizText(txt)}</span></button>`;
371
+ }).join('');
372
+
373
+ modal.el.innerHTML = `
374
+ <div class="tianji-arena">
375
+ <div class="tianji-timer-wrap">
376
+ <div class="tianji-timer-bar ${timeLeft <= 10 ? 'urgent' : ''}" id="tianjiTimerBar" style="width:${(timeLeft / 60) * 100}%;"></div>
377
+ </div>
378
+ <div class="tianji-hud-row">
379
+ <div class="tianji-hearts" title="剩餘士氣心">${heartsHtml}</div>
380
+ <div class="tianji-score-box">
381
+ <span>🎯 答對:<b>${correctCount}</b> 題</span>
382
+ ${forecastTag}
383
+ </div>
384
+ <div class="tianji-timer-num num" id="tianjiTimerNum">⏱️ ${Math.ceil(timeLeft)}s</div>
385
+ </div>
386
+
387
+ <div class="tianji-items-bar">
388
+ <button class="item-btn btn-peek" id="btnArenaPeek" ${peekCount > 0 ? '' : 'disabled'}>
389
+ 📜 偷看卷 (${peekCount})
390
+ </button>
391
+ <button class="item-btn btn-elim" id="btnArenaElim" ${elimCount > 0 ? '' : 'disabled'}>
392
+ ✂️ 刪去卷 (${elimCount})
393
+ </button>
394
+ </div>
395
+
396
+ <div class="qz-q">${renderRichQuizText(q.q)}</div>
397
+ <div class="qz-opts">${optsHtml}</div>
398
+ </div>
399
+ `;
400
+
401
+ const btnPeek = $('#btnArenaPeek', modal.el);
402
+ if (btnPeek) {
403
+ bind(btnPeek, 'click', () => {
404
+ if (!game.items || game.items.peek <= 0) {
405
+ toast('偷看卷不足!可於軍備箱中獲取', 'error');
406
+ return;
407
+ }
408
+ game.items.peek--;
409
+ btnPeek.textContent = `📜 偷看卷 (${game.items.peek})`;
410
+ btnPeek.disabled = game.items.peek <= 0;
411
+ tianjiCtx?.markDirty?.();
412
+ $$('.qz-opt', modal.el).forEach(ob => {
413
+ const oL = String.fromCharCode(65 + parseInt(ob.dataset.idx));
414
+ if (oL === q.a) ob.classList.add('opt-peeked');
415
+ });
416
+ toast('📜 已使用偷看卷!正解已閃現金光!', 'gold');
417
+ });
418
+ }
419
+
420
+ const btnElim = $('#btnArenaElim', modal.el);
421
+ if (btnElim) {
422
+ bind(btnElim, 'click', () => {
423
+ if (!game.items || game.items.eliminate <= 0) {
424
+ toast('刪去卷不足!可於軍備箱中獲取', 'error');
425
+ return;
426
+ }
427
+ game.items.eliminate--;
428
+ btnElim.textContent = `✂️ 刪去卷 (${game.items.eliminate})`;
429
+ btnElim.disabled = game.items.eliminate <= 0;
430
+ tianjiCtx?.markDirty?.();
431
+ const wrongOpts = Array.from($$('.qz-opt', modal.el)).filter(ob => {
432
+ const oL = String.fromCharCode(65 + parseInt(ob.dataset.idx));
433
+ return oL !== q.a && !ob.classList.contains('opt-eliminated');
434
+ });
435
+ shuffle(wrongOpts).slice(0, 2).forEach(ob => ob.classList.add('opt-eliminated'));
436
+ toast('✂️ 已使用刪去卷!已剔除 2 個錯誤選項!', 'gold');
437
+ });
438
+ }
439
+
440
+ $$('.qz-opt', modal.el).forEach(ob => {
441
+ bind(ob, 'click', () => {
442
+ if (answered) return;
443
+ answered = true;
444
+ const myIdx = parseInt(ob.dataset.idx);
445
+ const myLabel = String.fromCharCode(65 + myIdx);
446
+ const isCorrect = myLabel === q.a;
447
+
448
+ $$('.qz-opt', modal.el).forEach(b => {
449
+ b.disabled = true;
450
+ const oL = String.fromCharCode(65 + parseInt(b.dataset.idx));
451
+ if (oL === q.a) b.classList.add('correct');
452
+ else if (oL === myLabel) b.classList.add('wrong');
453
+ });
454
+
455
+ if (isCorrect) {
456
+ correctCount++;
457
+ combo++;
458
+ recordCorrect(game, q);
459
+ if (combo > 0 && combo % 5 === 0) {
460
+ timeLeft = Math.min(60, timeLeft + 2);
461
+ toast(`🔥 勢如破竹!連續答對 ${combo} 題,獎勵時間 +2 秒!`, 'gold');
462
+ }
463
+ qIdx++;
464
+ setTimeout(renderArenaQ, 300);
465
+ } else {
466
+ combo = 0;
467
+ hearts--;
468
+ recordMistake(game, q, myLabel, 'tianji');
469
+ tianjiCtx?.markDirty?.();
470
+ wrongLogs.push({
471
+ q: q.q,
472
+ myLabel,
473
+ myText: q.o[myIdx],
474
+ correctLabel: q.a,
475
+ correctText: q.o[q.a.charCodeAt(0) - 65],
476
+ });
477
+ if (hearts <= 0) {
478
+ setTimeout(() => endArena('defeat'), 600);
479
+ } else {
480
+ qIdx++;
481
+ setTimeout(renderArenaQ, 600);
482
+ }
483
+ }
484
+ });
485
+ });
486
+ };
487
+
488
+ arenaTimer = setInterval(() => {
489
+ timeLeft -= 0.1;
490
+ const bar = $('#tianjiTimerBar', modal.el);
491
+ const num = $('#tianjiTimerNum', modal.el);
492
+ if (bar) {
493
+ const pct = Math.max(0, (timeLeft / 60) * 100);
494
+ bar.style.width = `${pct}%`;
495
+ if (timeLeft <= 10) bar.classList.add('urgent');
496
+ }
497
+ if (num) {
498
+ num.textContent = `⏱️ ${Math.max(0, Math.ceil(timeLeft))}s`;
499
+ }
500
+ if (timeLeft <= 0) {
501
+ endArena('timeout');
502
+ }
503
+ }, 100);
504
+
505
+ renderArenaQ();
506
+ }
507
+
508
+ /**
509
+ * 開箱舞台與動畫效果
510
+ */
511
+ export function showChestUnboxModal(modal, correctCount, reason, wrongLogs = []) {
512
+ const game = tianjiCtx?.getGame?.();
513
+ const dState = tianjiCtx?.getDState?.();
514
+ if (!game) return;
515
+
516
+ let chestType = null;
517
+ let chestName = '安慰軍備物資';
518
+ let chestImg = 'assets/chest_bronze.png';
519
+ let subText = '答對 0 題・明日再來定能大展身手!';
520
+
521
+ if (correctCount >= 15) {
522
+ chestType = 'gold';
523
+ chestName = '黃金軍備箱';
524
+ chestImg = 'assets/chest_gold.png';
525
+ subText = `威武!答對 ${correctCount} 題・勇奪最高階軍備!100% 必得兵符與大量卷軸!`;
526
+ } else if (correctCount >= 8) {
527
+ chestType = 'silver';
528
+ chestName = '白銀軍備箱';
529
+ chestImg = 'assets/chest_silver.png';
530
+ subText = `驍勇!答對 ${correctCount} 題・解鎖中階軍備!必得刪去卷與機率兵符!`;
531
+ } else if (correctCount >= 1) {
532
+ chestType = 'bronze';
533
+ chestName = '青銅軍備箱';
534
+ chestImg = 'assets/chest_bronze.png';
535
+ subText = `過關!答對 ${correctCount} 題・解鎖初階軍備!穩得救急卷軸!`;
536
+ }
537
+
538
+ const curProd = dState ? totalProd(game, dState) : 100;
539
+ const reasonText = reason === 'defeat'
540
+ ? '<span style="color:#ef4444; font-weight:800;">💔 士氣三度受挫,演武終止!</span>'
541
+ : '<span style="color:#f59e0b; font-weight:800;">⏱️ 60 秒時間到!演武鳴金收兵!</span>';
542
+
543
+ // 若為答對 0 題的安慰獎,不顯示寶箱破鎖,直接發放安慰糧草
544
+ if (!chestType) {
545
+ const rewards = rollChestRewards(null, curProd);
546
+ modal.el.innerHTML = `
547
+ <div class="chest-unbox-stage">
548
+ <div style="font-size:15px; margin-bottom:8px;">${reasonText}</div>
549
+ <div class="chest-wrap">
550
+ <div style="font-size:64px; margin-bottom:12px;">🌾</div>
551
+ <h2 class="chest-title" style="color:var(--text);">慰勞糧草包</h2>
552
+ <p class="dim" style="font-size:13px; margin:6px 0 0;">勝敗乃兵家常事,天機閣特頒發安慰糧草以資督勵!明日再來定能大展身手!</p>
553
+ </div>
554
+
555
+ <div class="rewards-grid" style="margin:20px 0;">
556
+ <div class="reward-item-card">
557
+ <img src="assets/icon_grain.jpg" alt="糧草">
558
+ <b>+${fmt(rewards.coins)}</b>
559
+ <span>軍備糧草</span>
560
+ </div>
561
+ </div>
562
+
563
+ <div>
564
+ <button id="btnClaimTianjiConsolation" class="btn btn-primary" style="min-width:200px; font-size:16px; font-weight:800; padding:12px 28px;">
565
+ 📥 領取安慰糧草
566
+ </button>
567
+ </div>
568
+ </div>
569
+ `;
570
+
571
+ bind($('#btnClaimTianjiConsolation', modal.el), 'click', () => {
572
+ game.coins += rewards.coins;
573
+ game.totalEarned += rewards.coins;
574
+ game.tianjiDate = todayStr();
575
+ game.tianjiPlayed = 1;
576
+ tianjiCtx?.save?.();
577
+ tianjiCtx?.renderRes?.();
578
+ tianjiCtx?.renderBuild?.();
579
+ modal.close();
580
+ toast(`🎉 已領取安慰糧草 +${fmt(rewards.coins)}!`, 'gold');
581
+ });
582
+ return;
583
+ }
584
+
585
+ modal.el.innerHTML = `
586
+ <div class="chest-unbox-stage">
587
+ <div style="font-size:14px; margin-bottom:8px;">${reasonText}</div>
588
+ <div class="chest-wrap" id="chestAnimWrap">
589
+ <div class="chest-glow"></div>
590
+ <img src="${chestImg}" class="chest-main-img" alt="${chestName}">
591
+ <h2 class="chest-title">${chestName}</h2>
592
+ <p class="chest-subtitle">${subText}</p>
593
+ </div>
594
+
595
+ <div class="chest-hint-text" id="chestHintText">
596
+ 👆 點擊軍備箱或下方按鈕開啟,揭曉戰利品!
597
+ </div>
598
+
599
+ <!-- 開箱物資網格容器(開箱後動態注入) -->
600
+ <div class="chest-rewards-container hidden" id="chestRewardsContainer"></div>
601
+
602
+ <div class="chest-btn-area" id="chestBtnArea">
603
+ <button id="btnOpenTianjiChest" class="btn btn-primary" style="min-width:200px; font-size:16px; font-weight:800; padding:12px 28px; box-shadow:0 6px 20px rgba(239,90,86,0.4);">
604
+ 🔓 破鎖開啟軍備箱!
605
+ </button>
606
+ </div>
607
+
608
+ <!-- 機率公示按鈕 -->
609
+ <div style="margin-top:16px;">
610
+ <button id="btnViewChestProbInUnbox" class="btn btn-ghost btn-sm" style="font-size:12px; color:var(--text-mute);">
611
+ 🎲 查看此軍備箱掉落機率公示
612
+ </button>
613
+ <div class="chest-prob-detail hidden" id="chestProbDetailInUnbox" style="margin-top:8px; text-align:left;">
614
+ <div style="font-size:12px; line-height:1.6; color:var(--text-dim);">
615
+ ${chestType === 'bronze' ? '🥉 青銅軍備箱:100% 獲得 5 分鐘糧草;70% 機率得刪去卷 ×1、30% 機率得偷看卷 ×1。' : ''}
616
+ ${chestType === 'silver' ? '🥈 白銀軍備箱:100% 獲得 15 分鐘糧草;100% 必得刪去卷 ×1;50% 機率得偷看卷 ×1;20% 機率額外奪得兵符 ×1!' : ''}
617
+ ${chestType === 'gold' ? '🥇 黃金軍備箱:100% 獲得 30 分鐘暴風糧草;100% 必得兵符 1~2 枚;100% 必得偷看卷 1~2 張;100% 必得刪去卷 ×1!' : ''}
618
+ </div>
619
+ </div>
620
+ </div>
621
+ </div>
622
+ `;
623
+
624
+ const btnViewProb = $('#btnViewChestProbInUnbox', modal.el);
625
+ const thisDetail = $('#chestProbDetailInUnbox', modal.el);
626
+ if (btnViewProb && thisDetail) {
627
+ bind(btnViewProb, 'click', () => {
628
+ thisDetail.classList.toggle('hidden');
629
+ });
630
+ }
631
+
632
+ const chestWrap = $('#chestAnimWrap', modal.el);
633
+ const btnOpen = $('#btnOpenTianjiChest', modal.el);
634
+ const hintText = $('#chestHintText', modal.el);
635
+
636
+ let opened = false;
637
+ const doOpen = () => {
638
+ if (opened) return;
639
+ opened = true;
640
+
641
+ if (btnOpen) btnOpen.classList.add('hidden');
642
+ if (btnViewProb) btnViewProb.classList.add('hidden');
643
+ if (thisDetail) thisDetail.classList.add('hidden');
644
+
645
+ if (hintText) hintText.innerHTML = '<span style="color:var(--gold); font-weight:800;">🌟 正在奮力破鎖開啟寶箱中…</span>';
646
+ chestWrap.classList.add('rumble'); // CSS 晃動動畫
647
+
648
+ setTimeout(() => {
649
+ chestWrap.classList.remove('rumble');
650
+ chestWrap.classList.add('open'); // CSS 金芒爆發動畫
651
+ if (hintText) hintText.innerHTML = '<span style="color:var(--gold-dark); font-size:16px; font-weight:900;">✨ 寶箱開啟!軍備物資噴湧而出!</span>';
652
+
653
+ const rewards = rollChestRewards(chestType, curProd);
654
+
655
+ let cardsHtml = `
656
+ <div class="reward-item-card">
657
+ <img src="assets/icon_grain.jpg" alt="糧草">
658
+ <b>+${fmt(rewards.coins)}</b>
659
+ <span>軍備糧草</span>
660
+ </div>
661
+ `;
662
+ if (rewards.bp > 0) {
663
+ cardsHtml += `
664
+ <div class="reward-item-card">
665
+ <img src="assets/icon_tally.jpg" alt="兵符">
666
+ <b>+${rewards.bp} 枚</b>
667
+ <span>兵符</span>
668
+ </div>
669
+ `;
670
+ }
671
+ if (rewards.peek > 0) {
672
+ cardsHtml += `
673
+ <div class="reward-item-card">
674
+ <span style="font-size:36px;">📜</span>
675
+ <b>+${rewards.peek} 張</b>
676
+ <span>偷看卷(秒顯正解)</span>
677
+ </div>
678
+ `;
679
+ }
680
+ if (rewards.eliminate > 0) {
681
+ cardsHtml += `
682
+ <div class="reward-item-card">
683
+ <span style="font-size:36px;">✂️</span>
684
+ <b>+${rewards.eliminate} 張</b>
685
+ <span>刪去卷(二選一)</span>
686
+ </div>
687
+ `;
688
+ }
689
+
690
+ const container = $('#chestRewardsContainer', modal.el);
691
+ container.innerHTML = `<div class="rewards-grid">${cardsHtml}</div>`;
692
+ container.classList.remove('hidden');
693
+
694
+ const btnArea = $('#chestBtnArea', modal.el);
695
+ btnArea.innerHTML = `
696
+ <button id="btnClaimTianjiRewards" class="btn btn-primary" style="min-width:220px; font-size:16px; font-weight:800; padding:12px 28px; box-shadow:0 4px 16px rgba(16,185,129,0.35); background:linear-gradient(135deg, #10b981, #059669);">
697
+ 📥 領取戰利品入庫
698
+ </button>
699
+ `;
700
+
701
+ bind($('#btnClaimTianjiRewards', modal.el), 'click', () => {
702
+ game.coins += rewards.coins;
703
+ game.totalEarned += rewards.coins;
704
+ if (rewards.bp > 0) game.blueprints += rewards.bp;
705
+ if (!game.items) game.items = { peek: 0, eliminate: 0 };
706
+ if (rewards.peek > 0) game.items.peek = (game.items.peek || 0) + rewards.peek;
707
+ if (rewards.eliminate > 0) game.items.eliminate = (game.items.eliminate || 0) + rewards.eliminate;
708
+
709
+ // 標記今日已玩完
710
+ game.tianjiDate = todayStr();
711
+ game.tianjiPlayed = 1;
712
+
713
+ tianjiCtx?.save?.();
714
+ tianjiCtx?.renderRes?.();
715
+ tianjiCtx?.renderBuild?.();
716
+ modal.close();
717
+
718
+ toast(`🎉 軍備已悉數入庫!獲得 +${fmt(rewards.coins)} 糧草${rewards.bp ? `、+${rewards.bp} 兵符` : ''}${rewards.peek ? `、+${rewards.peek} 偷看卷` : ''}${rewards.eliminate ? `、+${rewards.eliminate} 刪去卷` : ''}`, 'gold', 5000);
719
+ });
720
+ }, 1200);
721
+ };
722
+
723
+ if (btnOpen) bind(btnOpen, 'click', doOpen);
724
+ bind(chestWrap, 'click', doOpen);
725
+ }
726
+
727
+ if (typeof window !== 'undefined') {
728
+ window.openTianjiModal = openTianjiModal;
729
+ }
730
+