diff --git a/G102-sequence/tdv_sdk.js b/G102-sequence/tdv_sdk.js index 7d1a353..49888ac 100644 --- a/G102-sequence/tdv_sdk.js +++ b/G102-sequence/tdv_sdk.js @@ -108,33 +108,99 @@ var tdv_sdk = { onLeaderboardLoaded: function (data) { console.log("SDK: Leaderboard Loaded",data); }, // --- KHỞI TẠO & LOAD DỮ LIỆU --- - init: function () { + init: function (config) { var self = this; - const urlParams = new URLSearchParams(window.location.search); - const bgParam = urlParams.get('bg'); - if (bgParam) { - this.themeSettings.current_bg = bgParam.startsWith('bg') ? bgParam : "bg" + bgParam; - } - const id = urlParams.get('game_id') || this.gameID; - console.log("🔌 SDK: Sentence Game Initialized. Waiting for data..."); - window.parent.postMessage({ - type: "GAME_READY", - game_id: id, - available_bgs: this.themeSettings.bg_list, - selected_bg: this.themeSettings.current_bg - }, "*"); + config = config || {}; + var urlParams = new URLSearchParams(window.location.search); - if (urlParams.has('offline') || urlParams.has('demo')) { - console.log("🔧 SDK: Offline/Demo mode - Loading default data"); - this.loadDefaultData(); - } else { - setTimeout(function () { - if (!self.serverDataLoaded && self.list.length === 0) { - console.warn("⚠️ SDK: No server data after 3s - Loading default data"); - self.loadDefaultData(); - } - }, 3000); + this.mode = config.mode || urlParams.get('mode') || 'live'; + this.game_code = config.game_code || urlParams.get('game_code') || 'G110'; + + // Auto preview check + if (window.self === window.parent && this.mode === 'live') { + console.log("⚠️ Standalone detected - Switching to PREVIEW"); + this.mode = 'preview'; } + + // Init window globals for C2 + window.answerResult = -1; + window.gameState = 0; + + var bgParam = urlParams.get('bg'); + if (bgParam) this.themeSettings.current_bg = bgParam.startsWith('bg') ? bgParam : "bg" + bgParam; + + console.log(`🚀 TDV SDK v4.0 Init | Mode: ${this.mode} | Code: ${this.game_code}`); + + // === CHECK EXTERNAL SDK === + if (typeof SenaGameSDK === 'undefined') { + console.error("❌ SenaGameSDK not found! Please import 'sena-game-sdk.js' in HTML."); + // Fallback to preview mode with default data + setTimeout(function () { + self._loadDataToGame(self.defaultData); + }, 500); + return; + } + + // === INIT EXTERNAL SDK (theo SDK_USAGE.md) === + this.sdk = new SenaGameSDK({ + iframePath: './sdk/package/dist/sdk-iframe/index.html', + mode: this.mode, + gameCode: this.game_code, + debug: true, + + // CALLBACK 1: SDK Ready → Push game items + onReady: function (sdk) { + console.log("✅ SDK Ready! Pushing game data...", self.defaultData.items.length, "questions"); + + // Push items vào SDK (SDK sẽ xử lý và trả về qua onDataReady) + sdk.pushData({ + items: self.defaultData.items + }); + }, + + // CALLBACK 2: Data đã xử lý → Load vào game + onDataReady: function (data) { + var len = (data && data.items) ? data.items.length : 0; + console.log("📥 SDK CALLBACK onDataReady received:", len, "items"); + if (len > 0) { + self._loadDataToGame(data); + } else { + console.warn("⚠️ SDK CALLBACK onDataReady had 0 items, ignoring..."); + } + }, + + // CALLBACK 3: Kết quả từng câu + onAnswerResult: function (result) { + console.log("📝 Answer Result:", result); + self._handleAnswerResult(result); + }, + + // CALLBACK 4: Game hoàn thành + onGameComplete: function (result) { + console.log("🏆 Game Complete!", result); + self._handleGameComplete(result); + }, + + // CALLBACK 5: Session bắt đầu (optional) + onSessionStart: function (session) { + console.log("🎮 Session Started:", session); + self.gameID = session.gameId; + self.userId = session.userId; + }, + + // CALLBACK 6: Lỗi + onError: function (error) { + console.error("❌ SDK Error:", error); + } + }); + + // Timeout fallback + setTimeout(function () { + if (!self.serverDataLoaded) { + console.warn("⚠️ SDK Data timeout - Using default local data as fallback"); + self._loadDataToGame(self.defaultData); + } + }, 5000); // Giảm xuống 5s cho nhanh }, loadDefaultData: function () { @@ -323,7 +389,7 @@ var tdv_sdk = { this.shuffleArray(this.missingWords); console.log("🧩 Missing words:", this.missingWords); }, - + // ==================== TÍNH SCALE CHO TỪ ==================== getWordScale: function () { var wordCount = this.currentWords.length;