up
All checks were successful
Deploy to Production / deploy (push) Successful in 9s

This commit is contained in:
lubukhu
2026-01-24 13:40:55 +07:00
parent 65fd0158a3
commit 84539fa97a

View File

@@ -108,33 +108,99 @@ 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
setTimeout(function () { if (window.self === window.parent && this.mode === 'live') {
if (!self.serverDataLoaded && self.list.length === 0) { console.log("⚠️ Standalone detected - Switching to PREVIEW");
console.warn("⚠️ SDK: No server data after 3s - Loading default data"); this.mode = 'preview';
self.loadDefaultData();
}
}, 3000);
} }
// 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 () { loadDefaultData: function () {
@@ -323,7 +389,7 @@ var tdv_sdk = {
this.shuffleArray(this.missingWords); this.shuffleArray(this.missingWords);
console.log("🧩 Missing words:", this.missingWords); console.log("🧩 Missing words:", this.missingWords);
}, },
// ==================== TÍNH SCALE CHO TỪ ==================== // ==================== TÍNH SCALE CHO TỪ ====================
getWordScale: function () { getWordScale: function () {
var wordCount = this.currentWords.length; var wordCount = this.currentWords.length;