Cleaned the coed

dev-main-mobile
Hazim Bin Ijaz 3 months ago
parent a316c3d81b
commit 042982ae71

@ -10,27 +10,12 @@ public class PlayerScoreComponent : NetworkBehaviour
public PlayerItem playerItem; public PlayerItem playerItem;
public override void OnNetworkSpawn() public override void OnNetworkSpawn()
{ {
//if (IsOwner && IsClient) if (IsOwner)
//{
// 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)
{ {
Debug.Log($"[PlayerScoreComponent] Requesting score initialization for Player {OwnerClientId}"); 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>();
serverCharacter =GetComponent<ServerCharacter>();
} }
public void UpdateScore(int newScore) public void UpdateScore(int newScore)

@ -30,39 +30,36 @@ public class ScoreManager : NetworkBehaviour
} }
[ServerRpc(RequireOwnership = false)] [ServerRpc(RequireOwnership = false)]
public void InitializePlayerScoreServerRpc(ulong ownerClientId, string name) public void InitializeAndSyncPlayerServerRpc(ulong ownerClientId, string playerName)
{ {
if (!playerScores.ContainsKey(ownerClientId)) if (!playerScores.ContainsKey(ownerClientId))
{ {
playerScores[ownerClientId] = 200; playerScores[ownerClientId] = 200;
playerNames[ownerClientId] = name; playerNames[ownerClientId] = playerName;
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.");
} }
Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of {playerScores[ownerClientId]} and name '{playerName}'.");
SyncPlayerDataClientRpc(ownerClientId, playerName, playerScores[ownerClientId]);
} }
[ClientRpc] [ClientRpc]
public void InitializePlayerScoreClientRpc(ulong ownerClientId, string name) public void SyncPlayerDataClientRpc(ulong ownerClientId, string playerName, int score)
{ {
// Update local data
if (!playerScores.ContainsKey(ownerClientId)) if (!playerScores.ContainsKey(ownerClientId))
{ {
playerScores[ownerClientId] = 200; playerScores[ownerClientId] = score;
playerNames[ownerClientId] = name; playerNames[ownerClientId] = playerName;
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]);
} }
else 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) public void UpdatePlayerScore(ulong ownerClientId, int newScore)
{ {
if (playerScores.ContainsKey(ownerClientId)) if (playerScores.ContainsKey(ownerClientId))
@ -84,7 +81,7 @@ public class ScoreManager : NetworkBehaviour
{ {
string playerName = playerNames[ownerClientId]; string playerName = playerNames[ownerClientId];
Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId} (Name: {playerName}): {newScore}"); Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId} (Name: {playerName}): {newScore}");
Scoreboard.instance.ScoreBoardUpdaterLocal(playerName, newScore); Scoreboard.instance.UpdateScoreboard(playerName, newScore);
} }
else else
{ {

@ -14,12 +14,7 @@ public class Scoreboard : NetworkBehaviour
private bool coroutineStarter = false; private bool coroutineStarter = false;
private void Awake() => instance = this; private void Awake() => instance = this;
public void ScoreBoardItemInitializer(ulong clientId, int score, string name) public void InitializeScoreboardItem(ulong clientId, int score, string name)
{
if (IsServer) ScoreBoardItemInitializerClientRpc(clientId, score, name);
}
[ClientRpc]
public void ScoreBoardItemInitializerClientRpc(ulong clientId, int score, string name)
{ {
var item = Instantiate(playerItemPrefab, Parent).GetComponent<PlayerItem>(); var item = Instantiate(playerItemPrefab, Parent).GetComponent<PlayerItem>();
item.PlayerName.text = name; item.PlayerName.text = name;
@ -28,26 +23,7 @@ public class Scoreboard : NetworkBehaviour
playerItems.Add(item); playerItems.Add(item);
} }
public void ScoreBoardUpdater(string playerName, int score) public void UpdateScoreboard(string playerName, int score, ClientRpcParams clientRpcParams = default)
{
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)
{ {
foreach (var item in playerItems) 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))); 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); 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);
}
}
} }

Loading…
Cancel
Save