123 lines
3.4 KiB
JavaScript
123 lines
3.4 KiB
JavaScript
/**
|
||
* =========================================
|
||
* TDV_SDK – GAME HELPER (G120)
|
||
* =========================================
|
||
* - KHÔNG làm SDK
|
||
* - KHÔNG load server
|
||
* - CHỈ quản lý UI + slot + drag
|
||
*/
|
||
|
||
var tdv_sdk = {
|
||
|
||
// ===== DATA FROM SDK BRIDGE =====
|
||
gameData: [],
|
||
currentQuestionIndex: 0,
|
||
currentQuestion: null,
|
||
|
||
// ===== QUESTION DATA =====
|
||
correctSentence: "",
|
||
currentWords: [],
|
||
correctWords: [],
|
||
blankCount: 1,
|
||
|
||
// ===== SLOT DATA =====
|
||
placedWords: [],
|
||
blankIndexes: [],
|
||
missingWords: [],
|
||
canSubmit: 0,
|
||
|
||
// ===== LOAD QUESTION =====
|
||
loadQuestions: function () {
|
||
var q = this.gameData[this.currentQuestionIndex];
|
||
if (!q) {
|
||
console.warn("No question data");
|
||
return;
|
||
}
|
||
|
||
this.currentQuestion = q;
|
||
|
||
// G120 chuẩn
|
||
this.correctSentence = q.question || "";
|
||
this.currentWords = q.options || [];
|
||
this.correctWords = q.correctSequence || this.currentWords.slice();
|
||
this.blankCount = q.blank_count || 1;
|
||
|
||
this.resetPlacedWords();
|
||
this.generateBlankIndexes();
|
||
this.generateMissingWords();
|
||
|
||
console.log("🧩 Question loaded:", q);
|
||
console.log("📝 Sentence:", this.correctSentence);
|
||
console.log("🧩 Words:", this.currentWords);
|
||
},
|
||
|
||
// ===== SLOT LOGIC =====
|
||
resetPlacedWords: function () {
|
||
this.placedWords = new Array(this.correctWords.length).fill("");
|
||
this.canSubmit = 0;
|
||
console.log("🔄 Reset placedWords:", this.placedWords);
|
||
},
|
||
|
||
setWordAtSlot: function (word, index) {
|
||
if (this.placedWords[index] !== "") return 0;
|
||
this.placedWords[index] = word;
|
||
this.updateSubmitState();
|
||
return 1;
|
||
},
|
||
|
||
clearWordAtSlot: function (index) {
|
||
this.placedWords[index] = "";
|
||
this.updateSubmitState();
|
||
},
|
||
|
||
updateSubmitState: function () {
|
||
var filled = this.placedWords.filter(w => w !== "").length;
|
||
this.canSubmit = (filled === this.blankIndexes.length) ? 1 : 0;
|
||
},
|
||
|
||
// ===== BLANK LOGIC =====
|
||
generateBlankIndexes: function () {
|
||
this.blankIndexes = [];
|
||
var pool = [...Array(this.correctWords.length).keys()];
|
||
|
||
for (var i = pool.length - 1; i > 0; i--) {
|
||
var j = Math.floor(Math.random() * (i + 1));
|
||
[pool[i], pool[j]] = [pool[j], pool[i]];
|
||
}
|
||
|
||
this.blankIndexes = pool.slice(0, this.blankCount);
|
||
console.log("🕳️ Blank indexes:", this.blankIndexes);
|
||
},
|
||
|
||
generateMissingWords: function () {
|
||
this.missingWords = [];
|
||
for (var i = 0; i < this.blankIndexes.length; i++) {
|
||
this.missingWords.push(this.correctWords[this.blankIndexes[i]]);
|
||
}
|
||
console.log("🧩 Missing words:", this.missingWords);
|
||
},
|
||
|
||
// ===== BUILD SEQUENCE =====
|
||
buildUserSequence: function () {
|
||
var result = [];
|
||
for (var i = 0; i < this.correctWords.length; i++) {
|
||
if (this.blankIndexes.indexOf(i) !== -1) {
|
||
result.push(this.placedWords[i] || "");
|
||
} else {
|
||
result.push(this.correctWords[i]);
|
||
}
|
||
}
|
||
return result;
|
||
},
|
||
|
||
// ===== SUBMIT =====
|
||
submitAnswer: function () {
|
||
if (this.canSubmit !== 1) return;
|
||
|
||
var sequence = this.buildUserSequence();
|
||
console.log("📤 Submit sequence:", sequence);
|
||
|
||
window.submitSequenceAnswer(sequence);
|
||
}
|
||
};
|