Upload game.js
Browse files- js/game.js +39 -4
js/game.js
CHANGED
|
@@ -21,10 +21,11 @@ let dState = null; // derived state
|
|
| 21 |
let bankName = '';
|
| 22 |
let myGifts = [];
|
| 23 |
let buyMode = 1; // 1, 10, 'max'
|
| 24 |
-
let loopTimer = null, saveTimer = null, luckyTimer = null;
|
| 25 |
let mySessionId = '';
|
| 26 |
let isSessionLocked = false;
|
| 27 |
let lastSessionCheckAt = 0;
|
|
|
|
| 28 |
|
| 29 |
// 排行榜狀態與快取(避免頻繁讀取 Firestore 額度)
|
| 30 |
let rankSortMode = 'prod'; // 'prod' | 'coins' | 'units'
|
|
@@ -134,6 +135,7 @@ async function loadStudentData() {
|
|
| 134 |
game = normalizeGame(student.game);
|
| 135 |
progress = student.progress || {};
|
| 136 |
stats = student.stats || {};
|
|
|
|
| 137 |
|
| 138 |
// 產生本機工作階段唯一識別碼,寫入學生文檔供多載具衝突防護使用
|
| 139 |
mySessionId = 's_' + Date.now() + '_' + Math.random().toString(36).slice(2, 8);
|
|
@@ -198,6 +200,30 @@ async function checkActiveSession() {
|
|
| 198 |
`, { closable: false, wide: false });
|
| 199 |
return true;
|
| 200 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
} catch (e) {
|
| 202 |
console.warn('Session 驗證檢查略過', e);
|
| 203 |
}
|
|
@@ -257,12 +283,21 @@ function startLoop() {
|
|
| 257 |
updateLucky();
|
| 258 |
}, UI_TICK_MS);
|
| 259 |
// 只在前景定時存檔;背景分頁靠 visibilitychange 補存 + 下次登入的離線結算
|
| 260 |
-
saveTimer = setInterval(() => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
}
|
| 262 |
function stopLoop() {
|
| 263 |
-
clearInterval(loopTimer); clearInterval(saveTimer);
|
| 264 |
clearTimeout(luckyTimer); clearTimeout(dirtyTimer);
|
| 265 |
-
loopTimer = saveTimer = luckyTimer = dirtyTimer = null;
|
| 266 |
}
|
| 267 |
|
| 268 |
function updateHeader() {
|
|
|
|
| 21 |
let bankName = '';
|
| 22 |
let myGifts = [];
|
| 23 |
let buyMode = 1; // 1, 10, 'max'
|
| 24 |
+
let loopTimer = null, saveTimer = null, luckyTimer = null, sessionTimer = null;
|
| 25 |
let mySessionId = '';
|
| 26 |
let isSessionLocked = false;
|
| 27 |
let lastSessionCheckAt = 0;
|
| 28 |
+
let lastHandledAdminOpAt = 0;
|
| 29 |
|
| 30 |
// 排行榜狀態與快取(避免頻繁讀取 Firestore 額度)
|
| 31 |
let rankSortMode = 'prod'; // 'prod' | 'coins' | 'units'
|
|
|
|
| 135 |
game = normalizeGame(student.game);
|
| 136 |
progress = student.progress || {};
|
| 137 |
stats = student.stats || {};
|
| 138 |
+
lastHandledAdminOpAt = student.lastAdminOp?.at || 0;
|
| 139 |
|
| 140 |
// 產生本機工作階段唯一識別碼,寫入學生文檔供多載具衝突防護使用
|
| 141 |
mySessionId = 's_' + Date.now() + '_' + Math.random().toString(36).slice(2, 8);
|
|
|
|
| 200 |
`, { closable: false, wide: false });
|
| 201 |
return true;
|
| 202 |
}
|
| 203 |
+
|
| 204 |
+
// 檢查是否有教師後台最新操作(收回資源 / 禮物調整等),即時同步本地記憶體,防止學生端定時存檔反向覆蓋
|
| 205 |
+
if (fresh?.lastAdminOp && fresh.lastAdminOp.at > (lastHandledAdminOpAt || 0)) {
|
| 206 |
+
lastHandledAdminOpAt = fresh.lastAdminOp.at;
|
| 207 |
+
const fg = fresh.game || {};
|
| 208 |
+
let changed = false;
|
| 209 |
+
if (Number.isFinite(fg.blueprints) && game.blueprints !== fg.blueprints) {
|
| 210 |
+
game.blueprints = fg.blueprints;
|
| 211 |
+
changed = true;
|
| 212 |
+
}
|
| 213 |
+
if (Number.isFinite(fg.coins) && game.coins !== fg.coins) {
|
| 214 |
+
game.coins = fg.coins;
|
| 215 |
+
changed = true;
|
| 216 |
+
}
|
| 217 |
+
if (fg.items) {
|
| 218 |
+
game.items = { ...fg.items };
|
| 219 |
+
changed = true;
|
| 220 |
+
}
|
| 221 |
+
if (changed) {
|
| 222 |
+
renderRes();
|
| 223 |
+
updateBuildBtns();
|
| 224 |
+
toast('👨🏫 教師已於管理後台調整軍資(' + (fresh.lastAdminOp.reason || '資源校正') + ')', 'info', 5000);
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
} catch (e) {
|
| 228 |
console.warn('Session 驗證檢查略過', e);
|
| 229 |
}
|
|
|
|
| 283 |
updateLucky();
|
| 284 |
}, UI_TICK_MS);
|
| 285 |
// 只在前景定時存檔;背景分頁靠 visibilitychange 補存 + 下次登入的離線結算
|
| 286 |
+
saveTimer = setInterval(() => {
|
| 287 |
+
if (!document.hidden) {
|
| 288 |
+
checkActiveSession().then(() => save());
|
| 289 |
+
}
|
| 290 |
+
}, AUTO_SAVE_MS);
|
| 291 |
+
|
| 292 |
+
// 前景每 30 秒檢查一次後台管理員操作與多裝置衝突,即時反應扣減
|
| 293 |
+
sessionTimer = setInterval(() => {
|
| 294 |
+
if (!document.hidden) checkActiveSession();
|
| 295 |
+
}, 30 * 1000);
|
| 296 |
}
|
| 297 |
function stopLoop() {
|
| 298 |
+
clearInterval(loopTimer); clearInterval(saveTimer); clearInterval(sessionTimer);
|
| 299 |
clearTimeout(luckyTimer); clearTimeout(dirtyTimer);
|
| 300 |
+
loopTimer = saveTimer = sessionTimer = luckyTimer = dirtyTimer = null;
|
| 301 |
}
|
| 302 |
|
| 303 |
function updateHeader() {
|