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

This commit is contained in:
lubukhu
2026-01-24 15:44:31 +07:00
parent 959a38e451
commit eaa2d53d7b

View File

@@ -1,32 +1,48 @@
/** /**
* ========================================= * =========================================
* TDV_SDK GAME HELPER (G120) * TDV_SDK SENTENCE SEQUENCE (G120)
* Compatible with Construct 2
* ========================================= * =========================================
* - KHÔNG làm SDK
* - KHÔNG load server
* - CHỈ quản lý UI + slot + drag
*/ */
var tdv_sdk = { var tdv_sdk = {
// ===== DATA FROM SDK BRIDGE ===== /* ================= CORE ================= */
mode: "preview",
game_code: "G120",
gameData: [], gameData: [],
currentQuestionIndex: 0, currentQuestionIndex: 0,
currentQuestion: null, currentQuestion: null,
// ===== QUESTION DATA ===== /* ================= QUESTION ================= */
correctSentence: "", correctSentence: "",
currentWords: [],
correctWords: [], correctWords: [],
currentWords: [],
blankCount: 1, blankCount: 1,
// ===== SLOT DATA ===== /* ================= SLOT ================= */
placedWords: [], placedWords: [],
blankIndexes: [], blankIndexes: [],
missingWords: [], missingWords: [],
canSubmit: 0, canSubmit: 0,
// ===== LOAD QUESTION ===== /* ================= INIT ================= */
init: function (config) {
console.log("✅ tdv_sdk.init called", config);
window.answerResult = -1;
window.gameState = 0;
// Data được push từ SDK Bridge
if (window.SDK_GAME_DATA) {
this.gameData = window.SDK_GAME_DATA;
}
this.loadQuestions();
},
/* ================= LOAD QUESTION ================= */
loadQuestions: function () { loadQuestions: function () {
var q = this.gameData[this.currentQuestionIndex]; var q = this.gameData[this.currentQuestionIndex];
if (!q) { if (!q) {
@@ -36,87 +52,107 @@ var tdv_sdk = {
this.currentQuestion = q; this.currentQuestion = q;
// G120 chuẩn // Chuẩn G120
this.correctSentence = q.question || ""; this.correctSentence = q.sentence || "";
this.currentWords = q.options || []; this.correctWords = this.correctSentence.split(/\s+/);
this.correctWords = q.correctSequence || this.currentWords.slice();
this.blankCount = q.blank_count || 1; this.blankCount = q.blank_count || 1;
this.currentWords = [...this.correctWords];
this.resetPlacedWords(); this.resetPlacedWords();
this.generateBlankIndexes(); this.generateBlankIndexes();
this.generateMissingWords(); this.generateMissingWords();
console.log("🧩 Question loaded:", q); console.log("📝 Loaded sentence:", this.correctSentence);
console.log("📝 Sentence:", this.correctSentence);
console.log("🧩 Words:", this.currentWords);
}, },
// ===== SLOT LOGIC ===== /* ================= SLOT LOGIC ================= */
resetPlacedWords: function () { resetPlacedWords: function () {
this.placedWords = new Array(this.correctWords.length).fill(""); this.placedWords = new Array(this.correctWords.length).fill("");
this.canSubmit = 0; this.canSubmit = 0;
console.log("🔄 Reset placedWords:", this.placedWords);
}, },
setWordAtSlot: function (word, index) { setWordAtSlot: function (word, index) {
if (this.placedWords[index] !== "") return 0;
this.placedWords[index] = word; this.placedWords[index] = word;
this.updateSubmitState(); this.updateSubmitState();
return 1;
},
clearWordAtSlot: function (index) {
this.placedWords[index] = "";
this.updateSubmitState();
}, },
updateSubmitState: function () { updateSubmitState: function () {
var filled = this.placedWords.filter(w => w !== "").length; var filled = this.blankIndexes.filter(i => this.placedWords[i]).length;
this.canSubmit = (filled === this.blankIndexes.length) ? 1 : 0; this.canSubmit = (filled === this.blankIndexes.length) ? 1 : 0;
}, },
// ===== BLANK LOGIC ===== /* ================= BLANK ================= */
generateBlankIndexes: function () { generateBlankIndexes: function () {
this.blankIndexes = [];
var pool = [...Array(this.correctWords.length).keys()]; var pool = [...Array(this.correctWords.length).keys()];
pool.sort(() => Math.random() - 0.5);
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); this.blankIndexes = pool.slice(0, this.blankCount);
console.log("🕳️ Blank indexes:", this.blankIndexes);
}, },
generateMissingWords: function () { generateMissingWords: function () {
this.missingWords = []; this.missingWords = this.blankIndexes.map(i => this.correctWords[i]);
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 ===== /* ================= BUILD ================= */
buildUserSequence: function () { buildUserSentence: function () {
var result = []; var result = [];
for (var i = 0; i < this.correctWords.length; i++) { for (var i = 0; i < this.correctWords.length; i++) {
if (this.blankIndexes.indexOf(i) !== -1) { if (this.blankIndexes.includes(i)) {
result.push(this.placedWords[i] || ""); result.push(this.placedWords[i] || "");
} else { } else {
result.push(this.correctWords[i]); result.push(this.correctWords[i]);
} }
} }
return result; return result.join(" ").replace(/\s+/g, " ").trim();
}, },
// ===== SUBMIT ===== /* ================= SUBMIT ================= */
submitAnswer: function () { submitAnswer: function () {
if (this.canSubmit !== 1) return; if (this.canSubmit !== 1) return -1;
var sequence = this.buildUserSequence(); var userSentence = this.buildUserSentence();
console.log("📤 Submit sequence:", sequence); console.log("📤 SUBMIT:", userSentence);
window.submitSequenceAnswer(sequence); window.answerResult = (userSentence === this.correctSentence) ? 1 : 0;
window.gameState = 2;
return window.answerResult;
},
/* ================= API FOR CONSTRUCT 2 ================= */
getInstructions: function () {
return "Drag the missing words to complete the sentence.";
},
getWordsCount: function () {
return this.correctWords.length;
},
getBlankCount: function () {
return this.blankIndexes.length;
},
canSubmitAnswer: function () {
return this.canSubmit;
},
getCurrentScore: function () {
return window.answerResult === 1 ? 1 : 0;
},
getRemainingTime: function () {
return 0;
},
getCorrectResultText: function () {
return this.correctSentence;
},
forceFinishGame: function () {
window.gameState = 2;
},
leaderboard: function () {
return "";
} }
}; };