Files
sentence1/G102-sequence/tdv_sdk.js
lubukhu eaa2d53d7b
All checks were successful
Deploy to Production / deploy (push) Successful in 8s
up
2026-01-24 15:44:31 +07:00

159 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* =========================================
* TDV_SDK SENTENCE SEQUENCE (G120)
* Compatible with Construct 2
* =========================================
*/
var tdv_sdk = {
/* ================= CORE ================= */
mode: "preview",
game_code: "G120",
gameData: [],
currentQuestionIndex: 0,
currentQuestion: null,
/* ================= QUESTION ================= */
correctSentence: "",
correctWords: [],
currentWords: [],
blankCount: 1,
/* ================= SLOT ================= */
placedWords: [],
blankIndexes: [],
missingWords: [],
canSubmit: 0,
/* ================= 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 () {
var q = this.gameData[this.currentQuestionIndex];
if (!q) {
console.warn("No question data");
return;
}
this.currentQuestion = q;
// Chuẩn G120
this.correctSentence = q.sentence || "";
this.correctWords = this.correctSentence.split(/\s+/);
this.blankCount = q.blank_count || 1;
this.currentWords = [...this.correctWords];
this.resetPlacedWords();
this.generateBlankIndexes();
this.generateMissingWords();
console.log("📝 Loaded sentence:", this.correctSentence);
},
/* ================= SLOT LOGIC ================= */
resetPlacedWords: function () {
this.placedWords = new Array(this.correctWords.length).fill("");
this.canSubmit = 0;
},
setWordAtSlot: function (word, index) {
this.placedWords[index] = word;
this.updateSubmitState();
},
updateSubmitState: function () {
var filled = this.blankIndexes.filter(i => this.placedWords[i]).length;
this.canSubmit = (filled === this.blankIndexes.length) ? 1 : 0;
},
/* ================= BLANK ================= */
generateBlankIndexes: function () {
var pool = [...Array(this.correctWords.length).keys()];
pool.sort(() => Math.random() - 0.5);
this.blankIndexes = pool.slice(0, this.blankCount);
},
generateMissingWords: function () {
this.missingWords = this.blankIndexes.map(i => this.correctWords[i]);
},
/* ================= BUILD ================= */
buildUserSentence: function () {
var result = [];
for (var i = 0; i < this.correctWords.length; i++) {
if (this.blankIndexes.includes(i)) {
result.push(this.placedWords[i] || "");
} else {
result.push(this.correctWords[i]);
}
}
return result.join(" ").replace(/\s+/g, " ").trim();
},
/* ================= SUBMIT ================= */
submitAnswer: function () {
if (this.canSubmit !== 1) return -1;
var userSentence = this.buildUserSentence();
console.log("📤 SUBMIT:", userSentence);
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 "";
}
};