|
|
@@ -3,8 +3,6 @@
|
|
|
let levelSending = false;
|
|
|
let gameOverSending = false;
|
|
|
let lastExitGame=false;
|
|
|
-let exitSending = false;
|
|
|
-let exitTimeoutId = 0;
|
|
|
|
|
|
let lastLevelCompleted = 0;
|
|
|
let lastGameOver = 0;
|
|
|
@@ -24,6 +22,9 @@ async function OnBeforeProjectStart(runtime)
|
|
|
{
|
|
|
console.log("Construct3 Started");
|
|
|
|
|
|
+ // 1. Lấy gameType từ URL
|
|
|
+ LoadGameConfigFromUrl(runtime);
|
|
|
+
|
|
|
// Nhận dữ liệu từ MVC
|
|
|
window.addEventListener(
|
|
|
"message",
|
|
|
@@ -51,6 +52,97 @@ async function OnBeforeProjectStart(runtime)
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+function LoadGameConfigFromUrl(runtime)
|
|
|
+{
|
|
|
+ try
|
|
|
+ {
|
|
|
+ const params = new URLSearchParams(
|
|
|
+ window.location.search
|
|
|
+ );
|
|
|
+
|
|
|
+ const gameType =
|
|
|
+ params.get("gameType");
|
|
|
+
|
|
|
+
|
|
|
+ if (gameType)
|
|
|
+ {
|
|
|
+ runtime.globalVars.gameType = gameType;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ runtime.globalVars.gameType = "Easy";
|
|
|
+ }
|
|
|
+ runtime.globalVars.InitGameTypeSuccess =1;
|
|
|
+
|
|
|
+ // Sau khi có gameType thì set cấu hình game
|
|
|
+ SetGameConfig(runtime);
|
|
|
+
|
|
|
+
|
|
|
+ console.log(
|
|
|
+ "GameType:",
|
|
|
+ runtime.globalVars.gameType,
|
|
|
+ "TotalTime:",
|
|
|
+ runtime.globalVars.totaltime
|
|
|
+ );
|
|
|
+
|
|
|
+ }
|
|
|
+ catch(error)
|
|
|
+ {
|
|
|
+ console.error(
|
|
|
+ "LoadGameConfigFromUrl Error:",
|
|
|
+ error
|
|
|
+ );
|
|
|
+
|
|
|
+ runtime.globalVars.gameType = "Easy";
|
|
|
+
|
|
|
+ SetGameConfig(runtime);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function SetGameConfig(runtime)
|
|
|
+{
|
|
|
+ switch(runtime.globalVars.gameType)
|
|
|
+ {
|
|
|
+ case "Easy":
|
|
|
+
|
|
|
+ runtime.globalVars.totaltime = 150;
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ case "Medium":
|
|
|
+
|
|
|
+ runtime.globalVars.totaltime = 100;
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ case "Hard":
|
|
|
+
|
|
|
+ runtime.globalVars.totaltime = 60;
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ default:
|
|
|
+
|
|
|
+ runtime.globalVars.totaltime = 150;
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ console.log(
|
|
|
+ "Set Total Time:",
|
|
|
+ runtime.globalVars.totaltime
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
function HandleMessage(runtime, event)
|
|
|
{
|
|
|
console.log("MESSAGE RECEIVED");
|
|
|
@@ -65,8 +157,8 @@ function HandleMessage(runtime, event)
|
|
|
|
|
|
console.log("INIT_GAME received: "+ event.data);
|
|
|
|
|
|
- runtime.globalVars.gameType =
|
|
|
- event.data.gameType ?? "";
|
|
|
+ //runtime.globalVars.gameType =
|
|
|
+ // event.data.gameType ?? "";
|
|
|
|
|
|
runtime.globalVars.msisdn =
|
|
|
event.data.msisdn ?? "";
|
|
|
@@ -89,17 +181,6 @@ function HandleMessage(runtime, event)
|
|
|
);
|
|
|
|
|
|
break;
|
|
|
-
|
|
|
- case "EXIT_GAME_RESULT":
|
|
|
- // DEV NOTE: callback Exit chay trong iframe an, chuyen ket qua len MVC.
|
|
|
- exitSending = false;
|
|
|
- if (exitTimeoutId)
|
|
|
- {
|
|
|
- clearTimeout(exitTimeoutId);
|
|
|
- exitTimeoutId = 0;
|
|
|
- }
|
|
|
- window.parent.postMessage(event.data, "*");
|
|
|
- break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -170,80 +251,67 @@ function Tick(runtime)
|
|
|
//Set showPrizePopup = 0
|
|
|
async function SaveLevelResult(runtime)
|
|
|
{
|
|
|
- // DEV NOTE: levelCompleted co the giu gia tri 1 qua nhieu tick.
|
|
|
- // Khoa request de mot level khong bi ghi nhieu lan.
|
|
|
- if (levelSending)
|
|
|
- return;
|
|
|
-
|
|
|
- levelSending = true;
|
|
|
-
|
|
|
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));
|
|
|
+ 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
|
|
|
+ };
|
|
|
|
|
|
- const response = await fetch(runtime.globalVars.root+"/Game/SaveLevelResult",
|
|
|
- {
|
|
|
- method: "POST",
|
|
|
- headers: {
|
|
|
- "Content-Type": "application/json"
|
|
|
- },
|
|
|
- body: JSON.stringify(body)
|
|
|
- });
|
|
|
+ console.log("Request Body:", body);
|
|
|
|
|
|
- if (!response.ok)
|
|
|
- throw new Error("Save level HTTP " + response.status);
|
|
|
+ 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);
|
|
|
|
|
|
- if (!result || result.success !== true)
|
|
|
- throw new Error(result?.message || "Save level failed");
|
|
|
-
|
|
|
- // Ví dụ dữ liệu trả về
|
|
|
- runtime.globalVars.point =
|
|
|
- result.point ?? "0";
|
|
|
+ // Lưu dữ liệu gốc
|
|
|
+ runtime.globalVars.point = Number(result.point ?? 0);
|
|
|
+ runtime.globalVars.data = Number(result.data ?? 0);
|
|
|
+ runtime.globalVars.score = Number(result.score ?? 0);
|
|
|
+ runtime.globalVars.transId = result.transId ?? "";
|
|
|
|
|
|
- runtime.globalVars.data =
|
|
|
- result.data ?? "";
|
|
|
+ // Xác định phần thưởng
|
|
|
+ runtime.globalVars.gGiftValues = 0;
|
|
|
+ runtime.globalVars.gGiftText = "";
|
|
|
|
|
|
- runtime.globalVars.score =
|
|
|
- result.score ?? "0";
|
|
|
-
|
|
|
- runtime.globalVars.transId =
|
|
|
- result.transId ?? runtime.globalVars.transId;
|
|
|
+ if (runtime.globalVars.point > 0)
|
|
|
+ {
|
|
|
+ runtime.globalVars.gGiftValues = runtime.globalVars.point;
|
|
|
+ runtime.globalVars.gGiftText = "POINTS";
|
|
|
+ }
|
|
|
+ else if (runtime.globalVars.data > 0)
|
|
|
+ {
|
|
|
+ runtime.globalVars.gGiftValues = runtime.globalVars.data;
|
|
|
+ runtime.globalVars.gGiftText = "DATA";
|
|
|
+ }
|
|
|
+ else if (runtime.globalVars.score > 0)
|
|
|
+ {
|
|
|
+ runtime.globalVars.gGiftValues = runtime.globalVars.score;
|
|
|
+ runtime.globalVars.gGiftText = "SCORE";
|
|
|
+ }
|
|
|
|
|
|
- // Chi hien popup sau khi MVC xac nhan luu thanh cong.
|
|
|
runtime.globalVars.showPrizePopup = 1;
|
|
|
|
|
|
- // DEV NOTE: Website dung message nay de tong hop thuong cua phien choi.
|
|
|
- window.parent.postMessage({
|
|
|
- type: "LEVEL_REWARD",
|
|
|
- level: runtime.globalVars.cur_level,
|
|
|
- point: result.point ?? "0",
|
|
|
- data: result.data ?? "",
|
|
|
- score: result.score ?? "0",
|
|
|
- rewardType: result.rewardType ?? "",
|
|
|
- rewardValue: result.rewardValue ?? 0,
|
|
|
- rewardUnit: result.rewardUnit ?? "",
|
|
|
- transId: runtime.globalVars.transId
|
|
|
- }, "*");
|
|
|
- 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!!!");
|
|
|
-
|
|
|
+ console.log("Gift Type:", runtime.globalVars.gGiftText);
|
|
|
+ console.log("Gift Value:", runtime.globalVars.gGiftValues);
|
|
|
}
|
|
|
catch (error)
|
|
|
{
|
|
|
@@ -259,35 +327,11 @@ async function SaveLevelResult(runtime)
|
|
|
// Khi muốn thoát game về MVC
|
|
|
function SaveExitGame(runtime)
|
|
|
{
|
|
|
- // DEV NOTE: khoa ngay lan bam dau de tranh tao nhieu request Exit.
|
|
|
- if (exitSending)
|
|
|
- return;
|
|
|
-
|
|
|
- exitSending = true;
|
|
|
- const btn = runtime.objects.btn_exit?.getFirstInstance?.();
|
|
|
try
|
|
|
{
|
|
|
-
|
|
|
- if (btn)
|
|
|
- {
|
|
|
- btn.isCollisionEnabled = false;
|
|
|
- }
|
|
|
-
|
|
|
- let targetFrame = document.getElementById("kitty-exit-target");
|
|
|
- if (!targetFrame)
|
|
|
- {
|
|
|
- targetFrame = document.createElement("iframe");
|
|
|
- targetFrame.id = "kitty-exit-target";
|
|
|
- targetFrame.name = "kitty-exit-target";
|
|
|
- targetFrame.hidden = true;
|
|
|
- document.body.appendChild(targetFrame);
|
|
|
- }
|
|
|
-
|
|
|
const form = document.createElement("form");
|
|
|
form.method = "POST";
|
|
|
form.action = runtime.globalVars.root + "/Game/ExitGame";
|
|
|
- form.target = targetFrame.name;
|
|
|
- form.hidden = true;
|
|
|
|
|
|
// Thêm input hidden
|
|
|
function add(name, value)
|
|
|
@@ -299,7 +343,7 @@ function SaveExitGame(runtime)
|
|
|
form.appendChild(input);
|
|
|
}
|
|
|
|
|
|
- add("token", runtime.globalVars.token);
|
|
|
+ //add("token", runtime.globalVars.token);
|
|
|
add("msisdn", runtime.globalVars.msisdn);
|
|
|
add("transId", runtime.globalVars.transId);
|
|
|
add("level", runtime.globalVars.cur_level);
|
|
|
@@ -308,29 +352,12 @@ function SaveExitGame(runtime)
|
|
|
add("gameOver", runtime.globalVars.gameOver);
|
|
|
|
|
|
document.body.appendChild(form);
|
|
|
+
|
|
|
// Submit form
|
|
|
HTMLFormElement.prototype.submit.call(form);
|
|
|
- form.remove();
|
|
|
-
|
|
|
- exitTimeoutId = window.setTimeout(() =>
|
|
|
- {
|
|
|
- exitSending = false;
|
|
|
- exitTimeoutId = 0;
|
|
|
- lastExitGame = 0;
|
|
|
- runtime.globalVars.exitGame = 0;
|
|
|
- if (btn)
|
|
|
- btn.isCollisionEnabled = true;
|
|
|
- console.error("ExitGame timeout");
|
|
|
- }, 15000);
|
|
|
-
|
|
|
}
|
|
|
catch (error)
|
|
|
{
|
|
|
- exitSending = false;
|
|
|
- lastExitGame = 0;
|
|
|
- runtime.globalVars.exitGame = 0;
|
|
|
- if (btn)
|
|
|
- btn.isCollisionEnabled = true;
|
|
|
console.error("SaveExitGame Error:", error);
|
|
|
}
|
|
|
}
|
|
|
@@ -443,3 +470,6 @@ function MovePlayer(offsetX, offsetY)
|
|
|
player.x += offsetX;
|
|
|
player.y += offsetY;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|