From 1f41843f4dd2f3557b50d67d38d4d79df4489ee4 Mon Sep 17 00:00:00 2001 From: Ali Sharoz Date: Thu, 2 Jan 2025 19:34:49 +0500 Subject: [PATCH] Tried to fix the bug where other clients receive the scoreboard --- Assets/Scenes/Startup.unity | 2 +- .../Gameplay/GameState/ServerBossRoomState.cs | 25 +++ .../GameState/ServerCharSelectState.cs | 3 +- .../Scripts/Gameplay/PlayerScoreComponent.cs | 6 + Assets/Scripts/Gameplay/Scoreboard.cs | 185 ++++++++++++++++-- 5 files changed, 203 insertions(+), 18 deletions(-) diff --git a/Assets/Scenes/Startup.unity b/Assets/Scenes/Startup.unity index f29fa43..ef24794 100644 --- a/Assets/Scenes/Startup.unity +++ b/Assets/Scenes/Startup.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_IndirectSpecularColor: {r: 0.12731749, g: 0.13414757, b: 0.1210787, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: diff --git a/Assets/Scripts/Gameplay/GameState/ServerBossRoomState.cs b/Assets/Scripts/Gameplay/GameState/ServerBossRoomState.cs index 31c5a98..6b15471 100644 --- a/Assets/Scripts/Gameplay/GameState/ServerBossRoomState.cs +++ b/Assets/Scripts/Gameplay/GameState/ServerBossRoomState.cs @@ -215,9 +215,34 @@ namespace Unity.BossRoom.Gameplay.GameState // spawn players characters with destroyWithScene = true newPlayer.SpawnWithOwnership(clientId, true); + //ScoreboardCaller(newPlayer); + ScoreboardCallerServerRpc(newPlayer); //var playerScoreComponent = newPlayer.GetComponent(); + } + void ScoreboardCaller(NetworkObject newplayer) + { + scoreboard.ScoreBoardListFiller(newplayer); + } + + [ServerRpc(RequireOwnership = false)] + void ScoreboardCallerServerRpc(NetworkObject newPlayer) + { + // Call the method on the server first scoreboard.ScoreBoardListFiller(newPlayer); + + // Then invoke it on the client + ScoreboardCallerClientRpc(newPlayer); + } + + [ClientRpc] + void ScoreboardCallerClientRpc(NetworkObject newPlayer) + { + // Call the method on the client + if (NetworkManager.Singleton.IsClient) + { + scoreboard.ScoreBoardListFiller(newPlayer); + } } void OnLifeStateChangedEventMessage(LifeStateChangedEventMessage message) diff --git a/Assets/Scripts/Gameplay/GameState/ServerCharSelectState.cs b/Assets/Scripts/Gameplay/GameState/ServerCharSelectState.cs index b53b902..9c4b01e 100644 --- a/Assets/Scripts/Gameplay/GameState/ServerCharSelectState.cs +++ b/Assets/Scripts/Gameplay/GameState/ServerCharSelectState.cs @@ -22,7 +22,7 @@ namespace Unity.BossRoom.Gameplay.GameState public override GameState ActiveState => GameState.CharSelect; public NetworkCharSelection networkCharSelection { get; private set; } - + //public static int numOfPlayers; Coroutine m_WaitToEndLobbyCoroutine; [Inject] @@ -165,6 +165,7 @@ namespace Unity.BossRoom.Gameplay.GameState void SaveLobbyResults() { int numofPlayersInLobby = networkCharSelection.LobbyPlayers.Count; + //numOfPlayers = numofPlayersInLobby; PlayerPrefs.SetInt("NumberOfLobbyPlayers", numofPlayersInLobby); Debug.Log("Number of Players in lobby are: " + numofPlayersInLobby); foreach (NetworkCharSelection.LobbyPlayerState playerInfo in networkCharSelection.LobbyPlayers) diff --git a/Assets/Scripts/Gameplay/PlayerScoreComponent.cs b/Assets/Scripts/Gameplay/PlayerScoreComponent.cs index 8ae864f..05b8455 100644 --- a/Assets/Scripts/Gameplay/PlayerScoreComponent.cs +++ b/Assets/Scripts/Gameplay/PlayerScoreComponent.cs @@ -7,6 +7,7 @@ public class PlayerScoreComponent : NetworkBehaviour public int CurrentScore { get; private set; } public ServerCharacter serverCharacter; public int m_index; + public PlayerItem playerItem; public override void OnNetworkSpawn() { if (IsOwner && IsClient) @@ -34,4 +35,9 @@ public class PlayerScoreComponent : NetworkBehaviour // Update the player's score display in the UI. Debug.Log($"[PlayerScoreComponent] Updated score UI for Player {OwnerClientId}: {CurrentScore}"); } + private void OnDestroy() + { + Destroy(playerItem.gameObject); + Scoreboard.instance.DestroyChecker(); + } } diff --git a/Assets/Scripts/Gameplay/Scoreboard.cs b/Assets/Scripts/Gameplay/Scoreboard.cs index 2e4b3a4..34dbdbf 100644 --- a/Assets/Scripts/Gameplay/Scoreboard.cs +++ b/Assets/Scripts/Gameplay/Scoreboard.cs @@ -5,6 +5,7 @@ using UnityEngine; using Unity.BossRoom.Gameplay.GameState; using Unity.Netcode; using VContainer.Unity; + public class Scoreboard : MonoBehaviour { public List m_PlayerScoreComponents; @@ -13,56 +14,208 @@ public class Scoreboard : MonoBehaviour public Transform Parent; int index = 0; bool coroutineStarter = false; + public static Scoreboard instance; + + private void Awake() + { + instance = this; + } + + public void DestroyChecker() + { + for (int i = 0; i < m_PlayerScoreComponents.Count; i++) + { + if (m_PlayerScoreComponents[i] == null) + { + m_PlayerScoreComponents.RemoveAt(i); + } + } + for (int i = 0; i < playerItems.Count; i++) + { + if (playerItems[i] == null) + { + playerItems.RemoveAt(i); + } + } + } + private IEnumerator Start() { - Debug.Log("Scoreboard start1"); + Debug.Log("Scoreboard Start - Coroutine Started"); + + // Wait until coroutineStarter is set to true yield return new WaitUntil(() => coroutineStarter == true); - Debug.Log("Scoreboard start2"); + + Debug.Log("Scoreboard Start - Coroutine Finished"); yield return new WaitForSeconds(2); - Debug.Log("Scoreboard start3"); + + Debug.Log("Scoreboard Start - Starter Called"); Starter(); - Debug.Log("Scoreboard start4"); } + public void ScoreBoardListFiller(NetworkObject playerObj) { + if (!NetworkManager.Singleton.IsServer) + { + Debug.LogError("ScoreBoardListFiller called on client, should be on server"); + return; // Only execute this on the server + } + + Debug.Log("ScoreBoardListFiller called on server"); m_PlayerScoreComponents.Add(playerObj.GetComponent()); m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].m_index = index; + + Instantiator(index); index++; - Instantiator(m_PlayerScoreComponents.Count); + if (!coroutineStarter) { coroutineStarter = true; } } + public void Starter() { + // Start the coroutine for updating the scores StartCoroutine(ScoreUpdater()); } - public void Instantiator(int count) + + public void Instantiator(int index) { - for (int i = 0; i < count; i++) + // Only instantiate UI elements on the client side + if (!NetworkManager.Singleton.IsClient) { - GameObject temp = Instantiate(playerItemPrefab, Parent); - PlayerItem item = temp.GetComponent(); - playerItems.Add(item); - playerItems[playerItems.Count-1].PlayerScore.text = m_PlayerScoreComponents[i].CurrentScore.ToString(); - playerItems[i].PlayerName.text = m_PlayerScoreComponents[i].serverCharacter.uIStateDisplayHandler.m_UIState.playerName.ToString(); + Debug.LogError("Instantiator called on server, should be on client"); + return; } + + Debug.Log("Instantiator called on client"); + + GameObject temp = Instantiate(playerItemPrefab, Parent); + PlayerItem item = temp.GetComponent(); + m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].playerItem = item; + playerItems.Add(item); + + playerItems[playerItems.Count - 1].PlayerScore.text = m_PlayerScoreComponents[index].CurrentScore.ToString(); + playerItems[playerItems.Count - 1].PlayerName.text = m_PlayerScoreComponents[index].serverCharacter.uIStateDisplayHandler.m_UIState.playerName.ToString(); } + IEnumerator ScoreUpdater() { - yield return new WaitForSeconds(0.5f); - Debug.Log("Scoreboard start5"); + Debug.Log("Scoreboard Start - ScoreUpdater started"); + while (true) { yield return new WaitForSeconds(0.1f); + m_PlayerScoreComponents.Sort((p1, p2) => p2.CurrentScore.CompareTo(p1.CurrentScore)); - for (int i = 0; i < playerItems.Count; i++) + + // Update the UI on the client side only + if (NetworkManager.Singleton.IsClient) + { + Debug.Log("Scoreboard Update - UI update on client"); + for (int i = 0; i < playerItems.Count; i++) + { + playerItems[i].transform.SetSiblingIndex(m_PlayerScoreComponents[i].m_index); + } + } + else { - playerItems[i].transform.SetSiblingIndex(m_PlayerScoreComponents[i].m_index); + Debug.LogWarning("Scoreboard Update - Trying to update UI on the server side"); } } } } + + + +//using System.Collections; +//using System.Collections.Generic; +//using Unity.Multiplayer.Samples.BossRoom; +//using UnityEngine; +//using Unity.BossRoom.Gameplay.GameState; +//using Unity.Netcode; +//using VContainer.Unity; +//public class Scoreboard : MonoBehaviour +//{ +// public List m_PlayerScoreComponents; +// public List playerItems; +// public GameObject playerItemPrefab; +// public Transform Parent; +// int index = 0; +// bool coroutineStarter = false; +// public static Scoreboard instance; +// private void Awake() +// { +// instance = this; +// } +// public void DestroyChecker() +// { +// for(int i=0;i< m_PlayerScoreComponents.Count;i++) +// { +// if(m_PlayerScoreComponents[i]==null) +// { +// m_PlayerScoreComponents.RemoveAt(i); +// } +// } +// for(int i=0;i< playerItems.Count;i++) +// { +// if(playerItems[i]==null) +// { +// playerItems.RemoveAt(i); +// } +// } +// } +// private IEnumerator Start() +// { +// Debug.Log("Scoreboard start1"); +// yield return new WaitUntil(() => coroutineStarter == true); +// Debug.Log("Scoreboard start2"); +// yield return new WaitForSeconds(2); +// Debug.Log("Scoreboard start3"); +// Starter(); +// Debug.Log("Scoreboard start4"); +// } +// public void ScoreBoardListFiller(NetworkObject playerObj) +// { + +// m_PlayerScoreComponents.Add(playerObj.GetComponent()); +// m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].m_index = index; + +// Instantiator(index); +// index++; +// if (!coroutineStarter) +// { +// coroutineStarter = true; +// } +// } +// public void Starter() +// { +// StartCoroutine(ScoreUpdater()); +// } +// public void Instantiator(int index) +// { +// GameObject temp = Instantiate(playerItemPrefab, Parent); +// PlayerItem item = temp.GetComponent(); +// m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].playerItem = item; +// playerItems.Add(item); +// playerItems[playerItems.Count - 1].PlayerScore.text = m_PlayerScoreComponents[index].CurrentScore.ToString(); +// playerItems[playerItems.Count - 1].PlayerName.text = m_PlayerScoreComponents[index].serverCharacter.uIStateDisplayHandler.m_UIState.playerName.ToString(); +// } +// IEnumerator ScoreUpdater() +// { +// yield return new WaitForSeconds(0.5f); +// Debug.Log("Scoreboard start5"); +// while (true) +// { +// yield return new WaitForSeconds(0.1f); +// m_PlayerScoreComponents.Sort((p1, p2) => p2.CurrentScore.CompareTo(p1.CurrentScore)); +// for (int i = 0; i < playerItems.Count; i++) +// { +// playerItems[i].transform.SetSiblingIndex(m_PlayerScoreComponents[i].m_index); +// } +// } +// } +//}