From 0f36b90290f42e02c5a12840120717cd525822fc Mon Sep 17 00:00:00 2001 From: Hazim Bin Ijaz Date: Mon, 30 Dec 2024 15:49:24 +0500 Subject: [PATCH] Fixed revisit platform score spamming logic --- .../Character/ServerCharacter.cs | 38 +++++++++---------- Assets/Scripts/Gameplay/Platform.cs | 24 +++++++++--- Assets/Scripts/Gameplay/ScoreManager.cs | 30 +++++++++++++++ 3 files changed, 68 insertions(+), 24 deletions(-) diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs index 137c445..79c9104 100644 --- a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs @@ -144,6 +144,8 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character public ulong? PendingSwapRequest { get; set; } public int? TargetPlatformId { get; private set; } = null; + public int? CurrentPlatformId { get; private set; } = null; + public int? PreviousPlatformId { get; private set; } = null; public bool IsOnAPlatform { get; private set; } = false; public bool IsCrow { get; private set; } = false; public bool IsSwapping { get; private set; } = false; @@ -180,18 +182,6 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character { Debug.LogError("SwapConfirmationPanel not found in the scene!"); } - //// Show the confirmation panel for the specific player - //var panel = FindObjectOfType(); - //if (panel != null) - //{ - // panel.ShowPanel(this); // Pass the current ServerCharacter reference - //} - //else - //{ - // Debug.LogError("SwapConfirmationPanel not found in the scene!"); - //} - - } } @@ -246,23 +236,24 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character TargetPlatformId = platformId != -1 ? platformId : (int?)null; // Update the value for all clients } - public void OnArrivalOnPlatform() + public void OnArrivalOnPlatform(int platformId) { ClearTargetPlatform(); - SetOnPlatform(true); // Automatically syncs to all clients + SetOnPlatform(true, platformId); // Automatically syncs to all clients } - public void OnLeavingPlatform() + public void OnLeavingPlatform(int platformId) { - SetOnPlatform(false); // Automatically syncs to all clients + + SetOnPlatform(false, platformId); // Automatically syncs to all clients } - public void SetOnPlatform(bool status) + public void SetOnPlatform(bool status, int platformId) { if (IsServer) { IsOnAPlatform = status; // Update on the server - UpdatePlatformStatusClientRpc(status); // Notify all clients + UpdatePlatformStatusClientRpc(status, platformId); // Notify all clients } else { @@ -271,10 +262,19 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character } [ClientRpc] - private void UpdatePlatformStatusClientRpc(bool status) + private void UpdatePlatformStatusClientRpc(bool status, int platformId) { IsOnAPlatform = status; // Update the value for all clients IsSwapping = false; + if (status) + { + CurrentPlatformId = platformId; + } + else + { + PreviousPlatformId = platformId; + CurrentPlatformId = null; + } } diff --git a/Assets/Scripts/Gameplay/Platform.cs b/Assets/Scripts/Gameplay/Platform.cs index 04a2910..a869755 100644 --- a/Assets/Scripts/Gameplay/Platform.cs +++ b/Assets/Scripts/Gameplay/Platform.cs @@ -44,13 +44,29 @@ namespace Unity.Multiplayer.Samples.BossRoom return; } + bool giveScore = player.PreviousPlatformId != PlatformID; + // Check if the player who occupied this platform had this platform as their target platform + if (giveScore) + { + if (player.TargetPlatformId.HasValue && player.TargetPlatformId.Value == PlatformID) + { + // Successful swap + ScoreManager.Instance.AddPlayerScore(player.OwnerClientId, 10); + Debug.Log($"Player {player.OwnerClientId} successfully swapped to Platform {PlatformID}. Awarded 10 score."); + } + else + { + ScoreManager.Instance.AddPlayerScore(player.OwnerClientId, 20); + Debug.Log($"Crow Player {player.OwnerClientId} successfully stole Platform {PlatformID}. Awarded 20 score."); + } + } IsOccupied = true; OccupierId.Value = player.OwnerClientId; - player.SetOnPlatform(true); - + player.OnArrivalOnPlatform(PlatformID); Debug.Log($"Platform {PlatformID} is now occupied by Player {player.OwnerClientId}."); } + public void Vacate(ServerCharacter player) { if (!IsServer) @@ -67,7 +83,7 @@ namespace Unity.Multiplayer.Samples.BossRoom IsOccupied = false; OccupierId.Value = 0; - player.SetOnPlatform(false); + player.OnLeavingPlatform(PlatformID); Debug.Log($"Platform {PlatformID} is now vacated."); } @@ -81,7 +97,6 @@ namespace Unity.Multiplayer.Samples.BossRoom if (!IsOccupied) { Occupy(player); - player.OnArrivalOnPlatform(); } else { @@ -102,7 +117,6 @@ namespace Unity.Multiplayer.Samples.BossRoom if (other.TryGetComponent(out var player) && OccupierId.Value == player.OwnerClientId) { Vacate(player); - player.OnLeavingPlatform(); } } } diff --git a/Assets/Scripts/Gameplay/ScoreManager.cs b/Assets/Scripts/Gameplay/ScoreManager.cs index c972b5b..ba030d1 100644 --- a/Assets/Scripts/Gameplay/ScoreManager.cs +++ b/Assets/Scripts/Gameplay/ScoreManager.cs @@ -57,6 +57,36 @@ public class ScoreManager : NetworkBehaviour Debug.LogError($"[ScoreManager] No entry found for Player {ownerClientId}. Cannot update score."); } } + + public void AddPlayerScore(ulong ownerClientId, int newScore) + { + if (playerScores.ContainsKey(ownerClientId)) + { + playerScores[ownerClientId] += newScore; + Debug.Log($"[ScoreManager] Updating Player {ownerClientId} score to {playerScores[ownerClientId]}."); + UpdatePlayerScoreClientRpc(ownerClientId, playerScores[ownerClientId]); + } + else + { + Debug.LogError($"[ScoreManager] No entry found for Player {ownerClientId}. Cannot update score."); + } + } + + public void SubtractPlayerScore(ulong ownerClientId, int scoreToSubtract) + { + if (playerScores.ContainsKey(ownerClientId)) + { + int value = Mathf.Max(0, playerScores[ownerClientId] - scoreToSubtract); + playerScores[ownerClientId] = value; + Debug.Log($"[ScoreManager] Updating Player {ownerClientId} score to {playerScores[ownerClientId]}."); + UpdatePlayerScoreClientRpc(ownerClientId, playerScores[ownerClientId]); + } + else + { + Debug.LogError($"[ScoreManager] No entry found for Player {ownerClientId}. Cannot update score."); + } + } + public int GetPlayerScore(ulong ownerClientId) {