This commit is contained in:
@@ -254,76 +254,13 @@ 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];
|
||||
this.questionStartTime = new Date();
|
||||
window.gameState = 0;
|
||||
window.answerResult = -1;
|
||||
if (this.currentQuestion) {
|
||||
// ===== NORMALIZE DATA FROM SDK IFRAME =====
|
||||
if (this.currentQuestion.content && !this.currentQuestion.sentence) {
|
||||
this.currentQuestion.sentence = this.currentQuestion.content;
|
||||
}
|
||||
|
||||
if (Array.isArray(this.currentQuestion.options) && !this.currentQuestion.parts) {
|
||||
this.currentQuestion.parts = this.currentQuestion.options;
|
||||
}
|
||||
|
||||
// Fallback cuối: nếu vẫn chưa có parts
|
||||
if (!this.currentQuestion.parts && this.currentQuestion.sentence) {
|
||||
this.currentQuestion.parts = this.currentQuestion.sentence.split(/\s+/);
|
||||
}
|
||||
// Lấy câu đúng từ field "sentence"
|
||||
this.correctSentence = String(this.currentQuestion.sentence || "").trim();
|
||||
|
||||
// PHƯƠNG THỨC 1: Nhận parts đã chia sẵn từ server
|
||||
if (this.currentQuestion.parts && Array.isArray(this.currentQuestion.parts) && this.currentQuestion.parts.length > 0) {
|
||||
// Server đã gửi sẵn parts (có thể đã shuffle hoặc chưa)
|
||||
this.currentWords = [...this.currentQuestion.parts];
|
||||
|
||||
// Tách correctWords từ sentence để so sánh
|
||||
this.correctWords = this.correctSentence.split(/\s+/).filter(w => w.length > 0);
|
||||
|
||||
console.log("📦 Using server-provided parts:", this.currentWords);
|
||||
}
|
||||
// PHƯƠNG THỨC 2: Tự tách câu và shuffle (fallback)
|
||||
else {
|
||||
// TÁCH CÂU THÀNH CÁC TỪ (by space)
|
||||
this.correctWords = this.correctSentence.split(/\s+/).filter(w => w.length > 0);
|
||||
|
||||
// Shuffle các từ
|
||||
this.currentWords = this.shuffleArray([...this.correctWords]);
|
||||
|
||||
console.log("🔀 Auto-split and shuffled words");
|
||||
}
|
||||
// ===== FILL BLANK MODE =====
|
||||
this.missingIndices = [];
|
||||
|
||||
if (this.gameMode === "fill_blank") {
|
||||
var total = this.correctWords.length;
|
||||
|
||||
// random 1 slot bị thiếu
|
||||
var missing = Math.floor(Math.random() * total);
|
||||
this.missingIndices.push(missing);
|
||||
|
||||
console.log("🕳️ Missing slot index:", missing);
|
||||
}
|
||||
// Tính width dựa trên số từ và độ dài từ
|
||||
this.newWidth = this.calculateWordWidth(this.currentWords);
|
||||
|
||||
// Tính vị trí X cho từng từ
|
||||
this.listPosX = this.getObjectPositions(this.currentWords.length, this.newWidth, this.margin, this.maxWWidth);
|
||||
|
||||
// Đánh dấu đã load level này
|
||||
this.lastLoadedLevel = this.level;
|
||||
|
||||
console.log("📝 Correct sentence:", this.correctSentence);
|
||||
console.log("📝 Display words:", this.currentWords);
|
||||
console.log("📝 Word count:", this.currentWords.length);
|
||||
this.generateBlankIndexes();
|
||||
this.generateMissingWords();
|
||||
|
||||
if (!this.currentQuestion.id) this.currentQuestion.id = "Q" + (this.level + 1);
|
||||
console.log(`📝 Loaded Q${this.level + 1}: ${this.currentQuestion.question}`);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user