This commit is contained in:
@@ -129,17 +129,12 @@ var tdv_sdk = {
|
|||||||
|
|
||||||
// SDK trả data đã xử lý
|
// SDK trả data đã xử lý
|
||||||
onDataReady: function (data) {
|
onDataReady: function (data) {
|
||||||
var items = data && data.items ? data.items : [];
|
var len = (data && data.items) ? data.items.length : 0;
|
||||||
console.log("📥 SDK DataReady:", items.length);
|
console.log("📥 SDK CALLBACK onDataReady received:", len, "items");
|
||||||
|
if (len > 0) {
|
||||||
if (items.length > 0) {
|
self.load(data);
|
||||||
self.serverDataLoaded = true;
|
|
||||||
self.load({
|
|
||||||
data: items,
|
|
||||||
blank_count: data.blank_count || self.defaultData.blank_count
|
|
||||||
});
|
|
||||||
} else {
|
} 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);
|
this.load(defaultJson);
|
||||||
},
|
},
|
||||||
|
|
||||||
load: function (inputJson) {
|
load: function (data) {
|
||||||
if (!inputJson) return;
|
if (!data) return;
|
||||||
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;
|
|
||||||
|
|
||||||
|
var rawItems = data.items || data.data || [];
|
||||||
|
var itemsCount = Array.isArray(rawItems) ? rawItems.length : 0;
|
||||||
|
|
||||||
if (inputJson.end_time_iso) {
|
// CHỐNG LOAD ĐÈ: Nếu đã có dữ liệu thật thì không load lại nữa
|
||||||
this.endTime = new Date(inputJson.end_time_iso);
|
if (this.serverDataLoaded && this.list && this.list.length > 0) {
|
||||||
} else {
|
return;
|
||||||
let fallbackSeconds = inputJson.total_time || 180;
|
|
||||||
this.endTime = new Date(Date.now() + fallbackSeconds * 1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inputJson.metadata && inputJson.metadata.description) {
|
console.log("⭐ [FORCE-UPDATED] SDK Processing Data. Items found:", itemsCount);
|
||||||
this.instructions = inputJson.metadata.description;
|
|
||||||
}
|
|
||||||
|
|
||||||
const completedData = inputJson.completed_question_ids || [];
|
if (itemsCount > 0) {
|
||||||
this.userResults = [];
|
this.serverDataLoaded = true;
|
||||||
let resumeLevel = 0;
|
this.list = JSON.parse(JSON.stringify(rawItems));
|
||||||
for (let i = 0; i < this.list.length; i++) {
|
this.totalQuestions = data.totalQuestions || data.total_questions || this.list.length;
|
||||||
const done = completedData.find(item => (item.id || item) === this.list[i].id);
|
this.completedCount = data.completedCount || data.completed_count || 0;
|
||||||
if (done) {
|
this.level = 0;
|
||||||
this.userResults.push({ id: this.list[i].id, result: done.result !== undefined ? done.result : 0 });
|
|
||||||
resumeLevel = i + 1;
|
this.gameStartTime = new Date();
|
||||||
} else {
|
this.loadQuestions();
|
||||||
resumeLevel = i;
|
|
||||||
break;
|
// 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.");
|
||||||
}
|
}
|
||||||
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']);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// --- LOGIC THỜI GIAN ---
|
// --- LOGIC THỜI GIAN ---
|
||||||
@@ -254,22 +241,31 @@ var tdv_sdk = {
|
|||||||
lastLoadedLevel: -1,
|
lastLoadedLevel: -1,
|
||||||
|
|
||||||
loadQuestions: function () {
|
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];
|
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();
|
// Chuẩn công ty
|
||||||
this.waitingForServerVerify = false;
|
if (q.question && !q.sentence) {
|
||||||
|
q.sentence = q.question;
|
||||||
|
}
|
||||||
|
|
||||||
// Normalize question array
|
// Options / words
|
||||||
this.sequence.originalQuestion =
|
if (Array.isArray(q.options) && !q.parts) {
|
||||||
JSON.parse(JSON.stringify(this.currentQuestion.question || []));
|
q.parts = q.options;
|
||||||
this.sequence.userSequence =
|
}
|
||||||
JSON.parse(JSON.stringify(this.sequence.originalQuestion));
|
|
||||||
|
|
||||||
this.sequence.isCompleted = false;
|
// Fallback cuối
|
||||||
|
if (!q.parts && q.sentence) {
|
||||||
window.answerResult = -1;
|
q.parts = q.sentence.split(/\s+/);
|
||||||
window.gameState = 0;
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// Tính width dựa trên số từ và độ dài từ dài nhất
|
// Tính width dựa trên số từ và độ dài từ dài nhất
|
||||||
|
|||||||
Reference in New Issue
Block a user