This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
var tdv_sdk = {
|
var tdv_sdk = {
|
||||||
// --- KHO DỮ LIỆU NỘI BỘ ---
|
// --- KHO DỮ LIỆU NỘI BỘ ---
|
||||||
|
mode: 'live',
|
||||||
|
game_code: 'G120',
|
||||||
list: [],
|
list: [],
|
||||||
currentQuestion: null,
|
currentQuestion: null,
|
||||||
level: 0,
|
level: 0,
|
||||||
@@ -111,96 +113,64 @@ var tdv_sdk = {
|
|||||||
init: function (config) {
|
init: function (config) {
|
||||||
var self = this;
|
var self = this;
|
||||||
config = config || {};
|
config = config || {};
|
||||||
|
|
||||||
var urlParams = new URLSearchParams(window.location.search);
|
var urlParams = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
this.mode = config.mode || urlParams.get('mode') || 'live';
|
this.mode = config.mode || urlParams.get("mode") || "live";
|
||||||
this.game_code = config.game_code || urlParams.get('game_code') || 'G110';
|
this.game_code = config.game_code || urlParams.get("game_code") || "G120";
|
||||||
|
|
||||||
// Auto preview check
|
// Standalone → Preview
|
||||||
if (window.self === window.parent && this.mode === 'live') {
|
if (window.self === window.parent && this.mode === "live") {
|
||||||
console.log("⚠️ Standalone detected - Switching to PREVIEW");
|
console.warn("Standalone detected → PREVIEW mode");
|
||||||
this.mode = 'preview';
|
this.mode = "preview";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init window globals for C2
|
// Globals for Construct 2 polling
|
||||||
window.answerResult = -1;
|
window.answerResult = -1;
|
||||||
window.gameState = 0;
|
window.gameState = 0; // 0: idle | 1: waiting | 2: answered
|
||||||
|
|
||||||
var bgParam = urlParams.get('bg');
|
console.log("TDV SDK INIT:", this.mode, this.game_code);
|
||||||
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}`);
|
if (typeof SenaGameSDK === "undefined") {
|
||||||
|
console.error("SenaGameSDK not found → fallback preview");
|
||||||
// === CHECK EXTERNAL SDK ===
|
this._loadDataToGame(this.defaultData);
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// === INIT EXTERNAL SDK (theo SDK_USAGE.md) ===
|
/* ===== INIT SDK ===== */
|
||||||
this.sdk = new SenaGameSDK({
|
this.sdk = new SenaGameSDK({
|
||||||
iframePath: './sdk/package/dist/sdk-iframe/index.html',
|
iframePath: "./dist/sdk-iframe/index.html",
|
||||||
mode: this.mode,
|
mode: this.mode,
|
||||||
gameCode: this.game_code,
|
gameCode: this.game_code,
|
||||||
debug: true,
|
debug: true,
|
||||||
|
|
||||||
// CALLBACK 1: SDK Ready → Push game items
|
|
||||||
onReady: function (sdk) {
|
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({
|
sdk.pushData({
|
||||||
items: self.defaultData.items
|
items: self.defaultData.items
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// CALLBACK 2: Data đã xử lý → Load vào game
|
|
||||||
onDataReady: function (data) {
|
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);
|
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) {
|
onAnswerResult: function (result) {
|
||||||
console.log("📝 Answer Result:", result);
|
|
||||||
self._handleAnswerResult(result);
|
self._handleAnswerResult(result);
|
||||||
},
|
},
|
||||||
|
|
||||||
// CALLBACK 4: Game hoàn thành
|
|
||||||
onGameComplete: function (result) {
|
onGameComplete: function (result) {
|
||||||
console.log("🏆 Game Complete!", result);
|
|
||||||
self._handleGameComplete(result);
|
self._handleGameComplete(result);
|
||||||
},
|
},
|
||||||
|
|
||||||
// CALLBACK 5: Session bắt đầu (optional)
|
|
||||||
onSessionStart: function (session) {
|
onSessionStart: function (session) {
|
||||||
console.log("🎮 Session Started:", session);
|
|
||||||
self.gameID = session.gameId;
|
self.gameID = session.gameId;
|
||||||
self.userId = session.userId;
|
self.userId = session.userId;
|
||||||
},
|
},
|
||||||
|
|
||||||
// CALLBACK 6: Lỗi
|
onError: function (err) {
|
||||||
onError: function (error) {
|
console.error("SDK ERROR:", err);
|
||||||
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 () {
|
loadDefaultData: function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user