This commit is contained in:
@@ -108,35 +108,101 @@ var tdv_sdk = {
|
|||||||
onLeaderboardLoaded: function (data) { console.log("SDK: Leaderboard Loaded",data); },
|
onLeaderboardLoaded: function (data) { console.log("SDK: Leaderboard Loaded",data); },
|
||||||
|
|
||||||
// --- KHỞI TẠO & LOAD DỮ LIỆU ---
|
// --- KHỞI TẠO & LOAD DỮ LIỆU ---
|
||||||
init: function () {
|
init: function (config) {
|
||||||
var self = this;
|
var self = this;
|
||||||
const urlParams = new URLSearchParams(window.location.search);
|
config = config || {};
|
||||||
const bgParam = urlParams.get('bg');
|
var urlParams = new URLSearchParams(window.location.search);
|
||||||
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')) {
|
this.mode = config.mode || urlParams.get('mode') || 'live';
|
||||||
console.log("🔧 SDK: Offline/Demo mode - Loading default data");
|
this.game_code = config.game_code || urlParams.get('game_code') || 'G110';
|
||||||
this.loadDefaultData();
|
|
||||||
} else {
|
// 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 () {
|
setTimeout(function () {
|
||||||
if (!self.serverDataLoaded && self.list.length === 0) {
|
self._loadDataToGame(self.defaultData);
|
||||||
console.warn("⚠️ SDK: No server data after 3s - Loading default data");
|
}, 500);
|
||||||
self.loadDefaultData();
|
return;
|
||||||
}
|
}
|
||||||
}, 3000);
|
|
||||||
|
// === 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 () {
|
loadDefaultData: function () {
|
||||||
if (this.list && this.list.length > 0) {
|
if (this.list && this.list.length > 0) {
|
||||||
console.log("📦 SDK: Data already loaded, skipping default");
|
console.log("📦 SDK: Data already loaded, skipping default");
|
||||||
|
|||||||
Reference in New Issue
Block a user