From 042982ae71ffe38aa37f6edae8ff547a1e522865 Mon Sep 17 00:00:00 2001 From: Hazim Bin Ijaz Date: Fri, 24 Jan 2025 16:31:32 +0500 Subject: [PATCH] Cleaned the coed --- .../Scripts/Gameplay/PlayerScoreComponent.cs | 21 ++------- Assets/Scripts/Gameplay/ScoreManager.cs | 35 +++++++------- Assets/Scripts/Gameplay/Scoreboard.cs | 46 ++----------------- 3 files changed, 22 insertions(+), 80 deletions(-) diff --git a/Assets/Scripts/Gameplay/PlayerScoreComponent.cs b/Assets/Scripts/Gameplay/PlayerScoreComponent.cs index e7c1cac..f083e62 100644 --- a/Assets/Scripts/Gameplay/PlayerScoreComponent.cs +++ b/Assets/Scripts/Gameplay/PlayerScoreComponent.cs @@ -10,27 +10,12 @@ public class PlayerScoreComponent : NetworkBehaviour public PlayerItem playerItem; public override void OnNetworkSpawn() { - //if (IsOwner && IsClient) - //{ - // Debug.Log($"[PlayerScoreComponent] Requesting score initialization for Player {OwnerClientId}"); - // ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId,serverCharacter.uIStateDisplayHandler.m_UIState.playerName); - //} - //// For the server player (host), ensure the PlayerScoreComponent is registered properly - //if (IsServer && OwnerClientId == NetworkManager.Singleton.LocalClientId) - //{ - // ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId, serverCharacter.uIStateDisplayHandler.m_UIState.playerName); - //} - - - - if(IsOwner) + if (IsOwner) { Debug.Log($"[PlayerScoreComponent] Requesting score initialization for Player {OwnerClientId}"); - ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId, serverCharacter.uIStateDisplayHandler.m_UIState.playerName); - + ScoreManager.Instance?.InitializeAndSyncPlayerServerRpc(OwnerClientId, serverCharacter.uIStateDisplayHandler.m_UIState.playerName); } - - serverCharacter =GetComponent(); + serverCharacter = GetComponent(); } public void UpdateScore(int newScore) diff --git a/Assets/Scripts/Gameplay/ScoreManager.cs b/Assets/Scripts/Gameplay/ScoreManager.cs index ca313a9..c9dd26f 100644 --- a/Assets/Scripts/Gameplay/ScoreManager.cs +++ b/Assets/Scripts/Gameplay/ScoreManager.cs @@ -30,39 +30,36 @@ public class ScoreManager : NetworkBehaviour } [ServerRpc(RequireOwnership = false)] - public void InitializePlayerScoreServerRpc(ulong ownerClientId, string name) + public void InitializeAndSyncPlayerServerRpc(ulong ownerClientId, string playerName) { if (!playerScores.ContainsKey(ownerClientId)) { playerScores[ownerClientId] = 200; - playerNames[ownerClientId] = name; - Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of 200 and name '{name}'."); - Scoreboard.instance.ScoreBoardItemInitializer(ownerClientId, playerScores[ownerClientId], name); - InitializePlayerScoreClientRpc(ownerClientId, name); - UpdatePlayerScoreClientRpc(ownerClientId, 200); - } - else - { - Debug.LogWarning($"[ScoreManager] Player {ownerClientId} already initialized."); + playerNames[ownerClientId] = playerName; } + Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of {playerScores[ownerClientId]} and name '{playerName}'."); + SyncPlayerDataClientRpc(ownerClientId, playerName, playerScores[ownerClientId]); } [ClientRpc] - public void InitializePlayerScoreClientRpc(ulong ownerClientId, string name) + public void SyncPlayerDataClientRpc(ulong ownerClientId, string playerName, int score) { + // Update local data if (!playerScores.ContainsKey(ownerClientId)) { - playerScores[ownerClientId] = 200; - playerNames[ownerClientId] = name; - Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of 200 and name '{name}'."); - Scoreboard.instance.ScoreBoardItemInitializer(ownerClientId, playerScores[ownerClientId], name); - - Scoreboard.instance.ScoreBoardUpdaterLocal(name, playerScores[ownerClientId]); + playerScores[ownerClientId] = score; + playerNames[ownerClientId] = playerName; } else { - Debug.LogWarning($"[ScoreManager] Player (from client) {ownerClientId} already initialized."); + playerScores[ownerClientId] = score; } + + Debug.Log($"[ScoreManager] Synced Player {ownerClientId} data: Name = {playerName}, Score = {score}"); + + // Update the scoreboard + Scoreboard.instance.InitializeScoreboardItem(ownerClientId, score, playerName); } + public void UpdatePlayerScore(ulong ownerClientId, int newScore) { if (playerScores.ContainsKey(ownerClientId)) @@ -84,7 +81,7 @@ public class ScoreManager : NetworkBehaviour { string playerName = playerNames[ownerClientId]; Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId} (Name: {playerName}): {newScore}"); - Scoreboard.instance.ScoreBoardUpdaterLocal(playerName, newScore); + Scoreboard.instance.UpdateScoreboard(playerName, newScore); } else { diff --git a/Assets/Scripts/Gameplay/Scoreboard.cs b/Assets/Scripts/Gameplay/Scoreboard.cs index cb01b45..2f3b3ad 100644 --- a/Assets/Scripts/Gameplay/Scoreboard.cs +++ b/Assets/Scripts/Gameplay/Scoreboard.cs @@ -14,12 +14,7 @@ public class Scoreboard : NetworkBehaviour private bool coroutineStarter = false; private void Awake() => instance = this; - public void ScoreBoardItemInitializer(ulong clientId, int score, string name) - { - if (IsServer) ScoreBoardItemInitializerClientRpc(clientId, score, name); - } - [ClientRpc] - public void ScoreBoardItemInitializerClientRpc(ulong clientId, int score, string name) + public void InitializeScoreboardItem(ulong clientId, int score, string name) { var item = Instantiate(playerItemPrefab, Parent).GetComponent(); item.PlayerName.text = name; @@ -28,26 +23,7 @@ public class Scoreboard : NetworkBehaviour playerItems.Add(item); } - public void ScoreBoardUpdater(string playerName, int score) - { - if (IsServer) ScoreBoardUpdaterClientRpc(playerName, score); - } - [ClientRpc] - public void ScoreBoardUpdaterClientRpc(string playerName, int score, ClientRpcParams clientRpcParams = default) - { - foreach (var item in playerItems) - { - if (item.PlayerName.text == playerName) - { - item.PlayerScore.text = score.ToString(); - if (NetworkManager.Singleton.LocalClientId == item.PlayerClientID) - item.PlayerScore.color = Color.green; - break; - } - } - SortAndUpdateScoreboard(); - } - public void ScoreBoardUpdaterLocal(string playerName, int score, ClientRpcParams clientRpcParams = default) + public void UpdateScoreboard(string playerName, int score, ClientRpcParams clientRpcParams = default) { foreach (var item in playerItems) { @@ -76,21 +52,5 @@ public class Scoreboard : NetworkBehaviour playerItems.Sort((p1, p2) => int.Parse(p2.PlayerScore.text).CompareTo(int.Parse(p1.PlayerScore.text))); for (int i = 0; i < playerItems.Count; i++) playerItems[i].transform.SetSiblingIndex(i); } - public void StartScoreUpdater() - { - if (!coroutineStarter) - { - coroutineStarter = true; - StartCoroutine(ScoreUpdater()); - } - } - private IEnumerator ScoreUpdater() - { - yield return new WaitUntil(() => coroutineStarter); - while (true) - { - SortAndUpdateScoreboard(); - yield return new WaitForSeconds(0.5f); - } - } + }