|
|
@@ -3,6 +3,8 @@
|
|
|
let levelSending = false;
|
|
|
let gameOverSending = false;
|
|
|
let lastExitGame=false;
|
|
|
+let exitSending = false;
|
|
|
+let exitTimeoutId = 0;
|
|
|
|
|
|
let lastLevelCompleted = 0;
|
|
|
let lastGameOver = 0;
|
|
|
@@ -78,6 +80,8 @@ function HandleMessage(runtime, event)
|
|
|
runtime.globalVars.root =
|
|
|
event.data.root ?? "";
|
|
|
|
|
|
+ runtime.globalVars.InitGameTypeSuccess =1;
|
|
|
+
|
|
|
console.log(
|
|
|
"Game Init:",
|
|
|
runtime.globalVars.gameType,
|
|
|
@@ -85,6 +89,17 @@ 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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -155,67 +170,80 @@ 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
|
|
|
- };
|
|
|
-
|
|
|
+ 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 response = await fetch(runtime.globalVars.root+"/Game/SaveLevelResult",
|
|
|
+ {
|
|
|
+ method: "POST",
|
|
|
+ headers: {
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ body: JSON.stringify(body)
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!response.ok)
|
|
|
+ throw new Error("Save level HTTP " + response.status);
|
|
|
|
|
|
const result = await response.json();
|
|
|
|
|
|
console.log("Level Result:", result);
|
|
|
|
|
|
- // 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 ?? "";
|
|
|
+ if (!result || result.success !== true)
|
|
|
+ throw new Error(result?.message || "Save level failed");
|
|
|
|
|
|
- // Xác định phần thưởng
|
|
|
- runtime.globalVars.gGiftValues = 0;
|
|
|
- runtime.globalVars.gGiftText = "";
|
|
|
+ // Ví dụ dữ liệu trả về
|
|
|
+ runtime.globalVars.point =
|
|
|
+ result.point ?? "0";
|
|
|
|
|
|
- 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";
|
|
|
- }
|
|
|
+ runtime.globalVars.data =
|
|
|
+ result.data ?? "";
|
|
|
+
|
|
|
+ runtime.globalVars.score =
|
|
|
+ result.score ?? "0";
|
|
|
|
|
|
+ runtime.globalVars.transId =
|
|
|
+ result.transId ?? runtime.globalVars.transId;
|
|
|
+
|
|
|
+ // Chi hien popup sau khi MVC xac nhan luu thanh cong.
|
|
|
runtime.globalVars.showPrizePopup = 1;
|
|
|
|
|
|
- console.log("Gift Type:", runtime.globalVars.gGiftText);
|
|
|
- console.log("Gift Value:", runtime.globalVars.gGiftValues);
|
|
|
+ // 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!!!");
|
|
|
+
|
|
|
}
|
|
|
catch (error)
|
|
|
{
|
|
|
@@ -227,14 +255,39 @@ 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)
|
|
|
@@ -255,12 +308,29 @@ 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);
|
|
|
}
|
|
|
}
|
|
|
@@ -373,6 +443,3 @@ function MovePlayer(offsetX, offsetY)
|
|
|
player.x += offsetX;
|
|
|
player.y += offsetY;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|