From 63e5f662221f15f414e35d054dd56f49b404dec0 Mon Sep 17 00:00:00 2001 From: lubukhu <150521080+lubukhu@users.noreply.github.com> Date: Sat, 24 Jan 2026 13:47:06 +0700 Subject: [PATCH] up --- G102-sequence/tdv_sdk.js | 70 ++++++++++++---------------------------- 1 file changed, 20 insertions(+), 50 deletions(-) diff --git a/G102-sequence/tdv_sdk.js b/G102-sequence/tdv_sdk.js index 49888ac..0f96ee3 100644 --- a/G102-sequence/tdv_sdk.js +++ b/G102-sequence/tdv_sdk.js @@ -1,5 +1,7 @@ var tdv_sdk = { // --- KHO DỮ LIỆU NỘI BỘ --- + mode: 'live', + game_code: 'G120', list: [], currentQuestion: null, level: 0, @@ -111,96 +113,64 @@ var tdv_sdk = { init: function (config) { var self = this; config = config || {}; + var urlParams = new URLSearchParams(window.location.search); - this.mode = config.mode || urlParams.get('mode') || 'live'; - this.game_code = config.game_code || urlParams.get('game_code') || 'G110'; + this.mode = config.mode || urlParams.get("mode") || "live"; + this.game_code = config.game_code || urlParams.get("game_code") || "G120"; - // Auto preview check - if (window.self === window.parent && this.mode === 'live') { - console.log("⚠️ Standalone detected - Switching to PREVIEW"); - this.mode = 'preview'; + // Standalone → Preview + if (window.self === window.parent && this.mode === "live") { + console.warn("Standalone detected → PREVIEW mode"); + this.mode = "preview"; } - // Init window globals for C2 + // Globals for Construct 2 polling window.answerResult = -1; - window.gameState = 0; + window.gameState = 0; // 0: idle | 1: waiting | 2: answered - var bgParam = urlParams.get('bg'); - if (bgParam) this.themeSettings.current_bg = bgParam.startsWith('bg') ? bgParam : "bg" + bgParam; + console.log("TDV SDK INIT:", this.mode, this.game_code); - 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); + if (typeof SenaGameSDK === "undefined") { + console.error("SenaGameSDK not found → fallback preview"); + this._loadDataToGame(this.defaultData); return; } - // === INIT EXTERNAL SDK (theo SDK_USAGE.md) === + /* ===== INIT SDK ===== */ this.sdk = new SenaGameSDK({ - iframePath: './sdk/package/dist/sdk-iframe/index.html', + iframePath: "./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..."); - } + self._loadDataToGame(data); }, - // 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); + onError: function (err) { + console.error("SDK ERROR:", err); } }); - - // 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 () {