This commit is contained in:
@@ -108,35 +108,105 @@ 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');
|
||||
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') || 'SQ_SENTENCE';
|
||||
|
||||
// Auto preview
|
||||
if (window.self === window.parent && this.mode === 'live') {
|
||||
console.log("⚠️ Standalone detected - Switching to PREVIEW");
|
||||
this.mode = 'preview';
|
||||
}
|
||||
|
||||
// Globals cho Construct 2 (GIỮ)
|
||||
window.answerResult = -1;
|
||||
window.gameState = 0;
|
||||
|
||||
// BG
|
||||
var 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
|
||||
}, "*");
|
||||
|
||||
if (urlParams.has('offline') || urlParams.has('demo')) {
|
||||
console.log("🔧 SDK: Offline/Demo mode - Loading default data");
|
||||
this.loadDefaultData();
|
||||
} else {
|
||||
console.log(`🚀 TDV SDK Init | Mode: ${this.mode} | Code: ${this.game_code}`);
|
||||
|
||||
// ===== CHECK SDK =====
|
||||
if (typeof SenaGameSDK === 'undefined') {
|
||||
console.error("❌ SenaGameSDK not found! Fallback to local default data.");
|
||||
setTimeout(function () {
|
||||
if (!self.serverDataLoaded && self.list.length === 0) {
|
||||
console.warn("⚠️ SDK: No server data after 3s - Loading default data");
|
||||
self.loadDefaultData();
|
||||
self.load(self.defaultData);
|
||||
}, 500);
|
||||
return;
|
||||
}
|
||||
}, 3000);
|
||||
|
||||
// ===== INIT SDK =====
|
||||
this.sdk = new SenaGameSDK({
|
||||
iframePath: './sdk/package/dist/sdk-iframe/index.html',
|
||||
mode: this.mode,
|
||||
gameCode: this.game_code,
|
||||
debug: true,
|
||||
|
||||
// SDK sẵn sàng → push data
|
||||
onReady: function (sdk) {
|
||||
console.log("✅ SDK Ready → pushing defaultData");
|
||||
sdk.pushData({
|
||||
items: self.defaultData.data // QUAN TRỌNG: map đúng field
|
||||
});
|
||||
},
|
||||
|
||||
// SDK trả data đã xử lý
|
||||
onDataReady: function (data) {
|
||||
var items = data && data.items ? data.items : [];
|
||||
console.log("📥 SDK DataReady:", items.length);
|
||||
|
||||
if (items.length > 0) {
|
||||
self.serverDataLoaded = true;
|
||||
self.load({
|
||||
data: items,
|
||||
blank_count: data.blank_count || self.defaultData.blank_count
|
||||
});
|
||||
} else {
|
||||
console.warn("⚠️ SDK returned empty items");
|
||||
}
|
||||
},
|
||||
|
||||
// Kết quả từng câu
|
||||
onAnswerResult: function (result) {
|
||||
console.log("📝 Answer Result:", result);
|
||||
self.handleAnswerResult(result);
|
||||
},
|
||||
|
||||
// Game complete
|
||||
onGameComplete: function (result) {
|
||||
console.log("🏁 Game Complete:", result);
|
||||
self.onGameFinished(result);
|
||||
},
|
||||
|
||||
onSessionStart: function (session) {
|
||||
console.log("🎮 Session:", session);
|
||||
self.gameID = session.gameId;
|
||||
self.userId = session.userId;
|
||||
},
|
||||
|
||||
onError: function (error) {
|
||||
console.error("❌ SDK Error:", error);
|
||||
}
|
||||
});
|
||||
|
||||
// ===== TIMEOUT FALLBACK =====
|
||||
setTimeout(function () {
|
||||
if (!self.serverDataLoaded) {
|
||||
console.warn("⚠️ SDK timeout → fallback defaultData");
|
||||
self.load(self.defaultData);
|
||||
}
|
||||
}, 5000);
|
||||
},
|
||||
|
||||
|
||||
loadDefaultData: function () {
|
||||
if (this.list && this.list.length > 0) {
|
||||
console.log("📦 SDK: Data already loaded, skipping default");
|
||||
@@ -1186,10 +1256,3 @@ var tdv_sdk = {
|
||||
return (attr === 'score') ? "0" : (attr === 'time_spent') ? "0s" : "";
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("message", function (event) {
|
||||
if (!event.data) return;
|
||||
if (event.data.type === "SERVER_PUSH_DATA") tdv_sdk.load(event.data.jsonData);
|
||||
if (event.data.type === "SERVER_PUSH_LEADERBOARD") tdv_sdk.loadLeaderboard(event.data.leaderboardData);
|
||||
if (event.data.type === "SDK_ANSWER_RESULT") { tdv_sdk.handleAnswerResult(event.data); }
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user