Просмотр исходного кода

SaveLevelResult()
Chặn gọi API trùng bằng levelSending.
Không cho response cũ ghi đè response mới.
Chỉ hiển thị popup khi response thành công.
Ưu tiên score khi point = 0.
Gán giá trị popup trực tiếp sau khi nhận response.

student 1 месяц назад
Родитель
Сommit
4c0c784d1a

+ 1 - 1
Kitty_Fall/Kitty_Fall/Kitty_Fall.Website/wwwroot/catpuzzle/offline.json

@@ -1 +1 @@
-{"version":1784775413573,"fileList":["scripts/main.js","scripts/c3runtime.js","scripts/objRefTable.js","scripts/project/main.js","scripts/c3main.js","scripts/dispatchworker.js","scripts/jobworker.js","data.json","scripts/modernjscheck.js","scripts/supportcheck.js","scripts/offlineclient.js","icons/loading-logo.png","icons/icon-16.png","icons/icon-32.png","icons/icon-64.png","icons/icon-128.png","icons/icon-256.png","icons/icon-512.png","media/congratulations.webm","media/click.webm","media/Button03.webm","media/cute-cat-352656.webm","media/win2.webm","media/timeup_.webm","media/click_sound.webm","media/playground.webm","box2d.wasm.js","box2d.wasm","style.css","images/scroll-sheet0.webp","images/shared-0-sheet6.webp","images/shared-0-sheet3.webp","images/shared-0-sheet0.webp","images/shared-0-sheet5.webp","images/shared-0-sheet4.webp","images/shared-0-sheet2.webp","images/confettix-sheet0.webp","images/confettix-sheet1.webp","images/cats-sheet0.webp","images/shared-0-sheet1.webp","images/cats-sheet2.webp","images/btn_lvl-sheet0.webp","images/btn_lvl-sheet1.webp","images/cats-sheet1.webp","images/cats-sheet4.webp","images/cats-sheet3.webp","images/block1a-sheet1.webp","images/block1a-sheet0.webp","images/sprite3-sheet0.webp","images/sprite3-sheet1.webp","images/sprite3-sheet2.webp","scripts/register-sw.js"]}
+{"version":1784775413574,"fileList":["scripts/main.js","scripts/c3runtime.js","scripts/objRefTable.js","scripts/project/main.js","scripts/c3main.js","scripts/dispatchworker.js","scripts/jobworker.js","data.json","scripts/modernjscheck.js","scripts/supportcheck.js","scripts/offlineclient.js","icons/loading-logo.png","icons/icon-16.png","icons/icon-32.png","icons/icon-64.png","icons/icon-128.png","icons/icon-256.png","icons/icon-512.png","media/congratulations.webm","media/click.webm","media/Button03.webm","media/cute-cat-352656.webm","media/win2.webm","media/timeup_.webm","media/click_sound.webm","media/playground.webm","box2d.wasm.js","box2d.wasm","style.css","images/scroll-sheet0.webp","images/shared-0-sheet6.webp","images/shared-0-sheet3.webp","images/shared-0-sheet0.webp","images/shared-0-sheet5.webp","images/shared-0-sheet4.webp","images/shared-0-sheet2.webp","images/confettix-sheet0.webp","images/confettix-sheet1.webp","images/cats-sheet0.webp","images/shared-0-sheet1.webp","images/cats-sheet2.webp","images/btn_lvl-sheet0.webp","images/btn_lvl-sheet1.webp","images/cats-sheet1.webp","images/cats-sheet4.webp","images/cats-sheet3.webp","images/block1a-sheet1.webp","images/block1a-sheet0.webp","images/sprite3-sheet0.webp","images/sprite3-sheet1.webp","images/sprite3-sheet2.webp","scripts/register-sw.js"]}

+ 31 - 7
Kitty_Fall/Kitty_Fall/Kitty_Fall.Website/wwwroot/catpuzzle/scripts/project/main.js

@@ -267,6 +267,14 @@ function Tick(runtime)
 //Set showPrizePopup = 0
 //Set showPrizePopup = 0
 async function SaveLevelResult(runtime)
 async function SaveLevelResult(runtime)
 {
 {
+	if (levelSending)
+	{
+		console.warn("SaveLevelResult ignored because a request is already in progress.");
+		return;
+	}
+
+	levelSending = true;
+
 	try
 	try
 	{
 	{
 		console.log("Calling SaveLevelResult...");
 		console.log("Calling SaveLevelResult...");
@@ -281,8 +289,6 @@ async function SaveLevelResult(runtime)
 			gameOver: 0
 			gameOver: 0
 		};
 		};
 
 
-		console.log("Request Body:", body);
-
 		const response = await fetch(
 		const response = await fetch(
 			runtime.globalVars.root + "/Game/SaveLevelResult",
 			runtime.globalVars.root + "/Game/SaveLevelResult",
 			{
 			{
@@ -298,10 +304,19 @@ async function SaveLevelResult(runtime)
 
 
 		console.log("Level Result:", result);
 		console.log("Level Result:", result);
 
 
+		if (!response.ok || result.success !== true)
+		{
+			throw new Error(result.message || `Save level failed (${response.status})`);
+		}
+
 		// Lưu dữ liệu gốc
 		// 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);
+		const point = Number(result.point ?? 0);
+		const data = Number(result.data ?? 0);
+		const score = Number(result.score ?? 0);
+
+		runtime.globalVars.point = Number.isFinite(point) ? point : 0;
+		runtime.globalVars.data = Number.isFinite(data) ? data : 0;
+		runtime.globalVars.score = Number.isFinite(score) ? score : 0;
 		runtime.globalVars.transId = result.transId ?? "";
 		runtime.globalVars.transId = result.transId ?? "";
 
 
 		// Xác định phần thưởng
 		// Xác định phần thưởng
@@ -324,6 +339,17 @@ async function SaveLevelResult(runtime)
 			runtime.globalVars.gGiftText = "Score";
 			runtime.globalVars.gGiftText = "Score";
 		}
 		}
 
 
+		// Update the visible fields immediately as well as notifying the event sheet.
+		// This prevents one stale frame when the response arrives between game ticks.
+		const giftValueText = runtime.objects.txtGiftValues?.getFirstInstance();
+		const giftTypeText = runtime.objects.txtGiftText?.getFirstInstance();
+
+		if (giftValueText)
+			giftValueText.text = String(runtime.globalVars.gGiftValues);
+
+		if (giftTypeText)
+			giftTypeText.text = runtime.globalVars.gGiftText;
+
 		runtime.globalVars.showPrizePopup = 1;
 		runtime.globalVars.showPrizePopup = 1;
 
 
 		console.log("Gift Type:", runtime.globalVars.gGiftText);
 		console.log("Gift Type:", runtime.globalVars.gGiftText);
@@ -487,5 +513,3 @@ function MovePlayer(offsetX, offsetY)
     player.y += offsetY;
     player.y += offsetY;
 }
 }
 
 
-
-