|
|
@@ -22,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",
|
|
|
@@ -49,6 +52,113 @@ 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";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ const maxLevel = parseInt(params.get("max_level"), 10);
|
|
|
+
|
|
|
+ if (Number.isInteger(maxLevel) && maxLevel >= 1)
|
|
|
+ {
|
|
|
+ runtime.globalVars.unlocked_level = maxLevel;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ runtime.globalVars.unlocked_level = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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,
|
|
|
+ "max_level:",
|
|
|
+ runtime.globalVars.unlocked_level
|
|
|
+ );
|
|
|
+
|
|
|
+ }
|
|
|
+ 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");
|
|
|
@@ -63,8 +173,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 ?? "";
|
|
|
@@ -78,6 +188,8 @@ function HandleMessage(runtime, event)
|
|
|
runtime.globalVars.root =
|
|
|
event.data.root ?? "";
|
|
|
|
|
|
+ runtime.globalVars.InitGameTypeSuccess =1;
|
|
|
+
|
|
|
console.log(
|
|
|
"Game Init:",
|
|
|
runtime.globalVars.gameType,
|
|
|
@@ -158,50 +270,64 @@ 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
|
|
|
- };
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ );
|
|
|
|
|
|
const result = await response.json();
|
|
|
|
|
|
console.log("Level Result:", result);
|
|
|
|
|
|
- // Ví dụ dữ liệu trả về
|
|
|
- runtime.globalVars.point =
|
|
|
- result.point ?? "";
|
|
|
+ // 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 ?? "";
|
|
|
-
|
|
|
- runtime.globalVars.transId =
|
|
|
- result.transId ?? "";
|
|
|
+ if (runtime.globalVars.point > 0)
|
|
|
+ {
|
|
|
+ runtime.globalVars.gGiftValues = runtime.globalVars.point;
|
|
|
+ runtime.globalVars.gGiftText = "Loyalty";
|
|
|
+ }
|
|
|
+ else if (runtime.globalVars.data > 0)
|
|
|
+ {
|
|
|
+ runtime.globalVars.gGiftValues = runtime.globalVars.data;
|
|
|
+ runtime.globalVars.gGiftText = "MB";
|
|
|
+ }
|
|
|
+ else if (runtime.globalVars.score > 0)
|
|
|
+ {
|
|
|
+ runtime.globalVars.gGiftValues = runtime.globalVars.score;
|
|
|
+ runtime.globalVars.gGiftText = "Score";
|
|
|
+ }
|
|
|
|
|
|
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!!!");
|
|
|
-
|
|
|
+
|
|
|
+ console.log("Gift Type:", runtime.globalVars.gGiftText);
|
|
|
+ console.log("Gift Value:", runtime.globalVars.gGiftValues);
|
|
|
}
|
|
|
catch (error)
|
|
|
{
|
|
|
@@ -209,7 +335,7 @@ async function SaveLevelResult(runtime)
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
- levelSending = false;
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -233,7 +359,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);
|