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

This commit is contained in:
lubukhu
2026-01-24 14:48:19 +07:00
parent fd80f4b5ed
commit de61a67c61

View File

@@ -129,17 +129,12 @@ var tdv_sdk = {
// 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
});
var len = (data && data.items) ? data.items.length : 0;
console.log("📥 SDK CALLBACK onDataReady received:", len, "items");
if (len > 0) {
self.load(data);
} else {
console.warn("⚠️ SDK returned empty items");
console.warn("⚠️ SDK CALLBACK onDataReady had 0 items, ignoring...");
}
},
@@ -189,45 +184,37 @@ var tdv_sdk = {
this.load(defaultJson);
},
load: function (inputJson) {
if (!inputJson) return;
load: function (data) {
if (!data) return;
var rawItems = data.items || data.data || [];
var itemsCount = Array.isArray(rawItems) ? rawItems.length : 0;
// CHỐNG LOAD ĐÈ: Nếu đã có dữ liệu thật thì không load lại nữa
if (this.serverDataLoaded && this.list && this.list.length > 0) {
return;
}
console.log("⭐ [FORCE-UPDATED] SDK Processing Data. Items found:", itemsCount);
if (itemsCount > 0) {
this.serverDataLoaded = true;
this.list = inputJson.data || inputJson.questions || [];
this.gameID = inputJson.game_id;
this.userId = inputJson.user_id;
this.totalQuestions = this.list.length;
this.blankCount = inputJson.blank_count || 1;
this.list = JSON.parse(JSON.stringify(rawItems));
this.totalQuestions = data.totalQuestions || data.total_questions || this.list.length;
this.completedCount = data.completedCount || data.completed_count || 0;
this.level = 0;
if (inputJson.end_time_iso) {
this.endTime = new Date(inputJson.end_time_iso);
} else {
let fallbackSeconds = inputJson.total_time || 180;
this.endTime = new Date(Date.now() + fallbackSeconds * 1000);
}
if (inputJson.metadata && inputJson.metadata.description) {
this.instructions = inputJson.metadata.description;
}
const completedData = inputJson.completed_question_ids || [];
this.userResults = [];
let resumeLevel = 0;
for (let i = 0; i < this.list.length; i++) {
const done = completedData.find(item => (item.id || item) === this.list[i].id);
if (done) {
this.userResults.push({ id: this.list[i].id, result: done.result !== undefined ? done.result : 0 });
resumeLevel = i + 1;
} else {
resumeLevel = i;
break;
}
}
this.level = Math.min(resumeLevel, this.totalQuestions - 1);
this.gameStartTime = new Date();
this.loadQuestions();
this.onGameStart();
if (window['TDVTriger']) window['TDVTriger'].runtime.trigger(cr.plugins_.TDVplugin.prototype.cnds.OnLoad, window['TDVTriger']);
// Trigger Construct 2
if (window['TDVTriger']) {
window['TDVTriger'].runtime.trigger(cr.plugins_.TDVplugin.prototype.cnds.OnLoad, window['TDVTriger']);
}
console.log(`✅ SUCCESS: Game loaded with ${this.list.length} questions.`);
} else {
console.warn("⏳ Received 0 items. Game will wait for real data message.");
}
},
// --- LOGIC THỜI GIAN ---
@@ -254,22 +241,31 @@ var tdv_sdk = {
lastLoadedLevel: -1,
loadQuestions: function () {
if (this.lastLoadedLevel === this.level && this.currentWords.length > 0) {
console.log("⏭️ Sentence", this.level + 1, "already loaded, skipping...");
return;
}
this.currentQuestion = this.list[this.level];
if (!this.currentQuestion) return;
if (this.currentQuestion) {
// ===== NORMALIZE DATA (COMPANY STANDARD) =====
var q = this.currentQuestion;
this.questionStartTime = new Date();
this.waitingForServerVerify = false;
// Chuẩn công ty
if (q.question && !q.sentence) {
q.sentence = q.question;
}
// Normalize question array
this.sequence.originalQuestion =
JSON.parse(JSON.stringify(this.currentQuestion.question || []));
this.sequence.userSequence =
JSON.parse(JSON.stringify(this.sequence.originalQuestion));
// Options / words
if (Array.isArray(q.options) && !q.parts) {
q.parts = q.options;
}
this.sequence.isCompleted = false;
window.answerResult = -1;
window.gameState = 0;
// Fallback cuối
if (!q.parts && q.sentence) {
q.parts = q.sentence.split(/\s+/);
}
}
},
// Tính width dựa trên số từ và độ dài từ dài nhất