|
|
|
@ -3,56 +3,13 @@ using System.Collections.Generic;
|
|
|
|
|
using Unity.Multiplayer.Samples.BossRoom;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Unity.Netcode;
|
|
|
|
|
using Unity.Collections;
|
|
|
|
|
|
|
|
|
|
public class ScoreManager : NetworkBehaviour
|
|
|
|
|
{
|
|
|
|
|
public static ScoreManager Instance { get; private set; }
|
|
|
|
|
|
|
|
|
|
// Use a NetworkList to store PlayerData
|
|
|
|
|
private NetworkList<PlayerData> playerDataList = new NetworkList<PlayerData>();
|
|
|
|
|
|
|
|
|
|
// NetworkVariable to sync scores (key: clientId, value: score)
|
|
|
|
|
private NetworkVariable<SerializableDictionary<ulong, int>> playerScores =
|
|
|
|
|
new NetworkVariable<SerializableDictionary<ulong, int>>(new SerializableDictionary<ulong, int>());
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public struct PlayerData : System.IEquatable<PlayerData>
|
|
|
|
|
{
|
|
|
|
|
public ulong ClientId;
|
|
|
|
|
public FixedString64Bytes Name;
|
|
|
|
|
|
|
|
|
|
public bool Equals(PlayerData other)
|
|
|
|
|
{
|
|
|
|
|
return ClientId == other.ClientId && Name.Equals(other.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
return obj is PlayerData other && Equals(other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
unchecked
|
|
|
|
|
{
|
|
|
|
|
int hash = 17;
|
|
|
|
|
hash = hash * 31 + ClientId.GetHashCode();
|
|
|
|
|
hash = hash * 31 + Name.GetHashCode();
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(PlayerData left, PlayerData right)
|
|
|
|
|
{
|
|
|
|
|
return left.Equals(right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(PlayerData left, PlayerData right)
|
|
|
|
|
{
|
|
|
|
|
return !(left == right);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public Dictionary<ulong, int> playerScores = new Dictionary<ulong, int>();
|
|
|
|
|
public Dictionary<ulong, string> playerNames = new Dictionary<ulong, string>();
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
@ -68,69 +25,76 @@ public class ScoreManager : NetworkBehaviour
|
|
|
|
|
{
|
|
|
|
|
if (IsServer)
|
|
|
|
|
{
|
|
|
|
|
StartCrowPenaltyCoroutine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsClient)
|
|
|
|
|
{
|
|
|
|
|
playerDataList.OnListChanged += OnPlayerDataListChanged;
|
|
|
|
|
playerScores.OnValueChanged += OnPlayerScoresChanged;
|
|
|
|
|
StartCrowPenaltyCoroutineServerRpc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
|
|
|
public void InitializePlayerScoreServerRpc(ulong ownerClientId, string name)
|
|
|
|
|
{
|
|
|
|
|
if (IsClient)
|
|
|
|
|
if (!playerScores.ContainsKey(ownerClientId))
|
|
|
|
|
{
|
|
|
|
|
playerDataList.OnListChanged -= OnPlayerDataListChanged;
|
|
|
|
|
playerScores.OnValueChanged -= OnPlayerScoresChanged;
|
|
|
|
|
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);
|
|
|
|
|
UpdatePlayerScoreClientRpc(ownerClientId, 200);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"[ScoreManager] Player {ownerClientId} already initialized.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
|
|
|
public void InitializePlayerScoreServerRpc(ulong ownerClientId, string name)
|
|
|
|
|
public void UpdatePlayerScore(ulong ownerClientId, int newScore)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
|
if (playerScores.ContainsKey(ownerClientId))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"[ScoreManager] Player name for Client {ownerClientId} is null or empty. Assigning default name.");
|
|
|
|
|
name = $"Player {ownerClientId}"; // Default name
|
|
|
|
|
playerScores[ownerClientId] = newScore;
|
|
|
|
|
Debug.Log($"[ScoreManager] Updating Player {ownerClientId} score to {newScore}.");
|
|
|
|
|
UpdatePlayerScoreClientRpc(ownerClientId, newScore);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fixedName = new FixedString64Bytes(name); // Convert to FixedString
|
|
|
|
|
if (!playerScores.Value.ContainsKey(ownerClientId))
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Add initial score and name to the server-side collections
|
|
|
|
|
playerScores.Value[ownerClientId] = 200;
|
|
|
|
|
playerDataList.Add(new PlayerData { ClientId = ownerClientId, Name = fixedName });
|
|
|
|
|
Debug.LogWarning($"[ScoreManager] No entry found for Player {ownerClientId}. Cannot update score.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of 200 and name '{fixedName}'.");
|
|
|
|
|
[ClientRpc]
|
|
|
|
|
public void UpdatePlayerScoreClientRpc(ulong ownerClientId, int newScore)
|
|
|
|
|
{
|
|
|
|
|
if (playerNames.ContainsKey(ownerClientId))
|
|
|
|
|
{
|
|
|
|
|
string playerName = playerNames[ownerClientId];
|
|
|
|
|
Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId} (Name: {playerName}): {newScore}");
|
|
|
|
|
Scoreboard.instance.ScoreBoardUpdater(playerName, newScore);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"[ScoreManager] Player {ownerClientId} already initialized.");
|
|
|
|
|
Debug.LogWarning($"[ScoreManager] Player name not found for Player {ownerClientId}. Cannot update scoreboard.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddPlayerScore(ulong ownerClientId, int scoreToAdd)
|
|
|
|
|
{
|
|
|
|
|
if (IsServer && playerScores.Value.ContainsKey(ownerClientId))
|
|
|
|
|
if (playerScores.ContainsKey(ownerClientId))
|
|
|
|
|
{
|
|
|
|
|
playerScores.Value[ownerClientId] += scoreToAdd;
|
|
|
|
|
Debug.Log($"[ScoreManager] Added {scoreToAdd} points to Player {ownerClientId}. New score: {playerScores.Value[ownerClientId]}.");
|
|
|
|
|
playerScores[ownerClientId] += scoreToAdd;
|
|
|
|
|
UpdatePlayerScore(ownerClientId, playerScores[ownerClientId]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SubtractPlayerScore(ulong ownerClientId, int scoreToSubtract)
|
|
|
|
|
{
|
|
|
|
|
if (IsServer && playerScores.Value.ContainsKey(ownerClientId))
|
|
|
|
|
if (playerScores.ContainsKey(ownerClientId))
|
|
|
|
|
{
|
|
|
|
|
int currentScore = playerScores.Value[ownerClientId];
|
|
|
|
|
playerScores.Value[ownerClientId] = Mathf.Max(0, currentScore - scoreToSubtract);
|
|
|
|
|
Debug.Log($"[ScoreManager] Subtracted {scoreToSubtract} points from Player {ownerClientId}. New score: {playerScores.Value[ownerClientId]}.");
|
|
|
|
|
int newScore = Mathf.Max(0, playerScores[ownerClientId] - scoreToSubtract);
|
|
|
|
|
UpdatePlayerScore(ownerClientId, newScore);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartCrowPenaltyCoroutine()
|
|
|
|
|
[ServerRpc]
|
|
|
|
|
public void StartCrowPenaltyCoroutineServerRpc()
|
|
|
|
|
{
|
|
|
|
|
StartCoroutine(ApplyCrowPenaltyCoroutine());
|
|
|
|
|
}
|
|
|
|
@ -141,191 +105,27 @@ public class ScoreManager : NetworkBehaviour
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
foreach (var entry in playerScores.Value)
|
|
|
|
|
// Create a list to store client IDs whose scores need to be updated
|
|
|
|
|
List<ulong> clientsToModify = new List<ulong>();
|
|
|
|
|
|
|
|
|
|
foreach (var entry in playerScores)
|
|
|
|
|
{
|
|
|
|
|
ulong ownerClientId = entry.Key;
|
|
|
|
|
|
|
|
|
|
if (CrowManager.Instance != null && CrowManager.Instance.GetCurrentCrow().OwnerClientId == ownerClientId)
|
|
|
|
|
{
|
|
|
|
|
SubtractPlayerScore(ownerClientId, 2);
|
|
|
|
|
Debug.Log($"[ScoreManager] Applied crow penalty to Player {ownerClientId}. New score: {playerScores.Value[ownerClientId]}.");
|
|
|
|
|
clientsToModify.Add(ownerClientId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(5f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPlayerDataListChanged(NetworkListEvent<PlayerData> changeEvent)
|
|
|
|
|
{
|
|
|
|
|
if (changeEvent.Type == NetworkListEvent<PlayerData>.EventType.Add)
|
|
|
|
|
{
|
|
|
|
|
var newData = changeEvent.Value;
|
|
|
|
|
string playerName = newData.Name.ToString(); // Convert FixedString to string
|
|
|
|
|
|
|
|
|
|
Debug.Log($"[ScoreManager] Player {newData.ClientId} added with name '{playerName}' on client.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPlayerScoresChanged(SerializableDictionary<ulong, int> oldValue, SerializableDictionary<ulong, int> newValue)
|
|
|
|
|
{
|
|
|
|
|
foreach (var entry in newValue)
|
|
|
|
|
{
|
|
|
|
|
ulong clientId = entry.Key;
|
|
|
|
|
int score = entry.Value;
|
|
|
|
|
|
|
|
|
|
// Manually search for the player entry in playerDataList
|
|
|
|
|
FixedString64Bytes playerName = default;
|
|
|
|
|
foreach (var playerData in playerDataList)
|
|
|
|
|
// Apply the penalties after the iteration
|
|
|
|
|
foreach (ulong clientId in clientsToModify)
|
|
|
|
|
{
|
|
|
|
|
if (playerData.ClientId == clientId)
|
|
|
|
|
{
|
|
|
|
|
playerName = playerData.Name;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
SubtractPlayerScore(clientId, 2);
|
|
|
|
|
Debug.Log($"[ScoreManager] Applied crow penalty to Player {clientId}. New score: {playerScores[clientId]}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!playerName.IsEmpty)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"[ScoreManager] Score updated for Player {clientId} (Name: {playerName}): {score}");
|
|
|
|
|
Scoreboard.instance.ScoreBoardUpdater(playerName.ToString(), score);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"[ScoreManager] Player name not found for Client {clientId}. Cannot update scoreboard.");
|
|
|
|
|
}
|
|
|
|
|
yield return new WaitForSeconds(5f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//using System.Collections;
|
|
|
|
|
//using System.Collections.Generic;
|
|
|
|
|
//using Unity.Multiplayer.Samples.BossRoom;
|
|
|
|
|
//using UnityEngine;
|
|
|
|
|
//using Unity.Netcode;
|
|
|
|
|
|
|
|
|
|
//public class ScoreManager : NetworkBehaviour
|
|
|
|
|
//{
|
|
|
|
|
// public static ScoreManager Instance { get; private set; }
|
|
|
|
|
|
|
|
|
|
// public Dictionary<ulong, int> playerScores = new Dictionary<ulong, int>();
|
|
|
|
|
// public Dictionary<ulong, string> playerNames = new Dictionary<ulong, string>();
|
|
|
|
|
|
|
|
|
|
// private void Awake()
|
|
|
|
|
// {
|
|
|
|
|
// if (Instance != null && Instance != this)
|
|
|
|
|
// {
|
|
|
|
|
// Destroy(gameObject);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// Instance = this;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public override void OnNetworkSpawn()
|
|
|
|
|
// {
|
|
|
|
|
// if (IsServer)
|
|
|
|
|
// {
|
|
|
|
|
// StartCrowPenaltyCoroutine();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// //[ServerRpc(RequireOwnership = false)]
|
|
|
|
|
// public void InitializePlayerScore(ulong ownerClientId, string name)
|
|
|
|
|
// {
|
|
|
|
|
// 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);
|
|
|
|
|
// UpdatePlayerScoreClientRpc(ownerClientId, 200);
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// Debug.LogWarning($"[ScoreManager] Player {ownerClientId} already initialized.");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public void UpdatePlayerScore(ulong ownerClientId, int newScore)
|
|
|
|
|
// {
|
|
|
|
|
// if (playerScores.ContainsKey(ownerClientId))
|
|
|
|
|
// {
|
|
|
|
|
// playerScores[ownerClientId] = newScore;
|
|
|
|
|
// Debug.Log($"[ScoreManager] Updating Player {ownerClientId} score to {newScore}.");
|
|
|
|
|
// UpdatePlayerScoreClientRpc(ownerClientId, newScore);
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// Debug.LogWarning($"[ScoreManager] No entry found for Player {ownerClientId}. Cannot update score.");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// [ClientRpc]
|
|
|
|
|
// public void UpdatePlayerScoreClientRpc(ulong ownerClientId, int newScore)
|
|
|
|
|
// {
|
|
|
|
|
// if (playerNames.ContainsKey(ownerClientId))
|
|
|
|
|
// {
|
|
|
|
|
// string playerName = playerNames[ownerClientId];
|
|
|
|
|
// Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId} (Name: {playerName}): {newScore}");
|
|
|
|
|
// Scoreboard.instance.ScoreBoardUpdater(playerName, newScore);
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// Debug.LogWarning($"[ScoreManager] Player name not found for Player {ownerClientId}. Cannot update scoreboard.");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public void AddPlayerScore(ulong ownerClientId, int scoreToAdd)
|
|
|
|
|
// {
|
|
|
|
|
// if (playerScores.ContainsKey(ownerClientId))
|
|
|
|
|
// {
|
|
|
|
|
// playerScores[ownerClientId] += scoreToAdd;
|
|
|
|
|
// UpdatePlayerScore(ownerClientId, playerScores[ownerClientId]);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public void SubtractPlayerScore(ulong ownerClientId, int scoreToSubtract)
|
|
|
|
|
// {
|
|
|
|
|
// if (playerScores.ContainsKey(ownerClientId))
|
|
|
|
|
// {
|
|
|
|
|
// int newScore = Mathf.Max(0, playerScores[ownerClientId] - scoreToSubtract);
|
|
|
|
|
// UpdatePlayerScore(ownerClientId, newScore);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public void StartCrowPenaltyCoroutine()
|
|
|
|
|
// {
|
|
|
|
|
// StartCoroutine(ApplyCrowPenaltyCoroutine());
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// private IEnumerator ApplyCrowPenaltyCoroutine()
|
|
|
|
|
// {
|
|
|
|
|
// yield return new WaitUntil(() => PlatformManager.Instance != null && PlatformManager.Instance.AreAllPlatformsOccupied());
|
|
|
|
|
|
|
|
|
|
// while (true)
|
|
|
|
|
// {
|
|
|
|
|
// // Create a list to store client IDs whose scores need to be updated
|
|
|
|
|
// List<ulong> clientsToModify = new List<ulong>();
|
|
|
|
|
|
|
|
|
|
// foreach (var entry in playerScores)
|
|
|
|
|
// {
|
|
|
|
|
// ulong ownerClientId = entry.Key;
|
|
|
|
|
|
|
|
|
|
// if (CrowManager.Instance != null && CrowManager.Instance.GetCurrentCrow().OwnerClientId == ownerClientId)
|
|
|
|
|
// {
|
|
|
|
|
// clientsToModify.Add(ownerClientId);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Apply the penalties after the iteration
|
|
|
|
|
// foreach (ulong clientId in clientsToModify)
|
|
|
|
|
// {
|
|
|
|
|
// SubtractPlayerScore(clientId, 2);
|
|
|
|
|
// Debug.Log($"[ScoreManager] Applied crow penalty to Player {clientId}. New score: {playerScores[clientId]}");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// yield return new WaitForSeconds(5f);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|