This commit is contained in:
@@ -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 = {
|
||||
|
||||
// ===== DATA FROM SDK BRIDGE =====
|
||||
/* ================= CORE ================= */
|
||||
mode: "preview",
|
||||
game_code: "G120",
|
||||
|
||||
gameData: [],
|
||||
currentQuestionIndex: 0,
|
||||
currentQuestion: null,
|
||||
|
||||
// ===== QUESTION DATA =====
|
||||
/* ================= QUESTION ================= */
|
||||
correctSentence: "",
|
||||
currentWords: [],
|
||||
correctWords: [],
|
||||
currentWords: [],
|
||||
blankCount: 1,
|
||||
|
||||
// ===== SLOT DATA =====
|
||||
/* ================= SLOT ================= */
|
||||
placedWords: [],
|
||||
blankIndexes: [],
|
||||
missingWords: [],
|
||||
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 () {
|
||||
var q = this.gameData[this.currentQuestionIndex];
|
||||
if (!q) {
|
||||
@@ -36,87 +52,107 @@ var tdv_sdk = {
|
||||
|
||||
this.currentQuestion = q;
|
||||
|
||||
// G120 chuẩn
|
||||
this.correctSentence = q.question || "";
|
||||
this.currentWords = q.options || [];
|
||||
this.correctWords = q.correctSequence || this.currentWords.slice();
|
||||
// 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("🧩 Question loaded:", q);
|
||||
console.log("📝 Sentence:", this.correctSentence);
|
||||
console.log("🧩 Words:", this.currentWords);
|
||||
console.log("📝 Loaded sentence:", this.correctSentence);
|
||||
},
|
||||
|
||||
// ===== SLOT LOGIC =====
|
||||
/* ================= 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;
|
||||
var filled = this.blankIndexes.filter(i => this.placedWords[i]).length;
|
||||
this.canSubmit = (filled === this.blankIndexes.length) ? 1 : 0;
|
||||
},
|
||||
|
||||
// ===== BLANK LOGIC =====
|
||||
/* ================= BLANK ================= */
|
||||
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]];
|
||||
}
|
||||
|
||||
pool.sort(() => Math.random() - 0.5);
|
||||
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);
|
||||
this.missingWords = this.blankIndexes.map(i => this.correctWords[i]);
|
||||
},
|
||||
|
||||
// ===== BUILD SEQUENCE =====
|
||||
buildUserSequence: function () {
|
||||
/* ================= BUILD ================= */
|
||||
buildUserSentence: function () {
|
||||
var result = [];
|
||||
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] || "");
|
||||
} else {
|
||||
result.push(this.correctWords[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return result.join(" ").replace(/\s+/g, " ").trim();
|
||||
},
|
||||
|
||||
// ===== SUBMIT =====
|
||||
/* ================= SUBMIT ================= */
|
||||
submitAnswer: function () {
|
||||
if (this.canSubmit !== 1) return;
|
||||
if (this.canSubmit !== 1) return -1;
|
||||
|
||||
var sequence = this.buildUserSequence();
|
||||
console.log("📤 Submit sequence:", sequence);
|
||||
var userSentence = this.buildUserSentence();
|
||||
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 "";
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user