// main.js let levelSending = false; let gameOverSending = false; let lastExitGame=false; let lastLevelCompleted = 0; let lastGameOver = 0; runOnStartup(async runtime => { runtime.addEventListener( "beforeprojectstart", () => OnBeforeProjectStart(runtime) ); runtime.addEventListener("function", OnFunction); }); async function OnBeforeProjectStart(runtime) { console.log("Construct3 Started"); // Nhận dữ liệu từ MVC window.addEventListener( "message", (event) => HandleMessage(runtime, event) ); // Tick runtime.addEventListener( "tick", () => Tick(runtime) ); console.log("Add event success"); // Báo MVC biết game đã sẵn sàng window.parent.postMessage( { type: "GAME_READY" }, "*" ); console.log("GAME_READY sent"); } function HandleMessage(runtime, event) { console.log("MESSAGE RECEIVED"); console.log(event.data); if (!event.data || !event.data.type) return; switch (event.data.type) { case "INIT_GAME": console.log("INIT_GAME received: "+ event.data); runtime.globalVars.gameType = event.data.gameType ?? ""; runtime.globalVars.msisdn = event.data.msisdn ?? ""; runtime.globalVars.token = event.data.token ?? ""; runtime.globalVars.transId = event.data.transId ?? ""; runtime.globalVars.root = event.data.root ?? ""; console.log( "Game Init:", runtime.globalVars.gameType, runtime.globalVars.msisdn ); break; } } function Tick(runtime) { // ===== LEVEL COMPLETE (CHỈ 1 LẦN) ===== if ( runtime.globalVars.levelCompleted === 1 && lastLevelCompleted === 0 ) { lastLevelCompleted = 1; SaveLevelResult(runtime); } if (runtime.globalVars.levelCompleted === 0) { lastLevelCompleted = 0; } // ===== GAME OVER (CHỈ 1 LẦN) ===== /* if ( runtime.globalVars.gameOver === 1 && lastGameOver === 0 ) { lastGameOver = 1; SaveGameOver(runtime); } if (runtime.globalVars.gameOver === 0) { lastGameOver = 0; } */ // ===== Exit game ===== if ( runtime.globalVars.exitGame === 1 && lastExitGame === 0 ) { lastExitGame = 1; SaveExitGame(runtime); } if (runtime.globalVars.exitGame === 0) { lastExitGame = 0; } } //chu ý là khi hoàn thành level cần set //Set levelCompleted = 1 //Sau khi vao level moi can set lại //Set levelCompleted = 0 //Set showPrizePopup = 0 async function SaveLevelResult(runtime) { try { console.log("Calling SaveLevelResult..."); const body = { token: runtime.globalVars.token, msisdn: runtime.globalVars.msisdn, transId: runtime.globalVars.transId, level: runtime.globalVars.cur_level, score: runtime.globalVars.score, gameType: runtime.globalVars.gameType, gameOver: 0 }; console.log("Request Body:", body); console.log("JSON:", JSON.stringify(body)); const response = await fetch(runtime.globalVars.root+"/Game/SaveLevelResult", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }); const result = await response.json(); console.log("Level Result:", result); // Ví dụ dữ liệu trả về runtime.globalVars.point = result.point ?? ""; runtime.globalVars.data = result.data ?? ""; runtime.globalVars.score = result.score ?? ""; runtime.globalVars.transId = result.transId ?? ""; runtime.globalVars.showPrizePopup = 1; console.log("Chuẩn bị gọi vào function trong event sheet:"); //runtime.callFunction("Quangbh"); console.log("Da goi goi vao function event sheet xong nhe!!!"); } catch (error) { console.error("SaveLevelResult Error:", error); } finally { levelSending = false; } } // Khi muốn thoát game về MVC function SaveExitGame(runtime) { try { const form = document.createElement("form"); form.method = "POST"; form.action = runtime.globalVars.root + "/Game/ExitGame"; // Thêm input hidden function add(name, value) { const input = document.createElement("input"); input.type = "hidden"; input.name = name; input.value = value ?? ""; form.appendChild(input); } add("token", runtime.globalVars.token); add("msisdn", runtime.globalVars.msisdn); add("transId", runtime.globalVars.transId); add("level", runtime.globalVars.cur_level); add("score", runtime.globalVars.score); add("gameType", runtime.globalVars.gameType); add("gameOver", runtime.globalVars.gameOver); document.body.appendChild(form); // Submit form HTMLFormElement.prototype.submit.call(form); } catch (error) { console.error("SaveExitGame Error:", error); } } //Khi thua game chi can set //Set gameOver = 1 async function SaveGameOver(runtime) { try { console.log("Calling GameOver..."); const response = await fetch( runtime.globalVars.root+"/Game/GameOver", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify( { token: runtime.globalVars.token, msisdn: runtime.globalVars.msisdn, transId: runtime.globalVars.transId, score: runtime.globalVars.score, gameType: runtime.globalVars.gameType }) } ); const result = await response.json(); console.log("GameOver Result:", result); runtime.globalVars.finalPrize = result.finalPrize ?? ""; runtime.globalVars.showGameOverPopup = 1; } catch (error) { console.error("GameOver Error:", error); } finally { gameOverSending = false; } } function OnFunction(e) { switch (e.name) { case "ShowMessage": ShowMessage(e.args[0]); break; case "AddScore": AddScore(e.args[0]); break; case "MovePlayer": MovePlayer(e.args[0], e.args[1]); break; } } //=================================== // Hiện thông báo //=================================== function ShowMessage(message) { console.log(message); } //=================================== // Cộng điểm //=================================== function AddScore(score) { runtime.globalVars.Score += score; console.log("Score:", runtime.globalVars.Score); } //=================================== // Di chuyển Player //=================================== function MovePlayer(offsetX, offsetY) { const player = runtime.objects.Player.getFirstInstance(); if (!player) return; player.x += offsetX; player.y += offsetY; }