Skip to content

Commit

Permalink
Use correct PlayerIdentity in auction
Browse files Browse the repository at this point in the history
  • Loading branch information
toberge committed May 26, 2024
1 parent bde3048 commit 016943d
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/Menu.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 785370674}
m_IndirectSpecularColor: {r: 0.062482566, g: 0.21938902, b: 0.4950583, a: 1}
m_IndirectSpecularColor: {r: 0.062518954, g: 0.2194095, b: 0.49505043, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Auction/BiddingAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Start()
public void SetIdentity(PlayerIdentity identity)
{
chipText.text = identity.chips.ToString();
identity.onChipChange += AnimateChipStatus;
identity.onChipChange += UpdateChipStatus;
}

private IEnumerator WaitAndEvaluate()
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Auction/BiddingPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void CmdPlaceBid(uint playerID)
}

// TODO verify that this player belongs to the source connection
if (!MatchController.Singleton.PlayerById.TryGetValue(playerID, out var player))
if (!Peer2PeerTransport.PlayerInstanceByID.TryGetValue(playerID, out var player))
{
Debug.LogError($"Bidding platform received invalid player {playerID} from client!");
return;
Expand All @@ -171,7 +171,7 @@ private void CmdPlaceBid(uint playerID)
[ClientRpc]
private void RpcAcceptBid(uint playerID)
{
if (!MatchController.Singleton.PlayerById.TryGetValue(playerID, out var player))
if (!Peer2PeerTransport.PlayerInstanceByID.TryGetValue(playerID, out var player))
{
Debug.LogError($"Bidding platform received invalid player {playerID} from server!");
return;
Expand Down Expand Up @@ -243,7 +243,7 @@ private void EndAuction()
[ClientRpc]
private void RpcPerformTransaction(uint playerID, string itemID)
{
if (!MatchController.Singleton.PlayerById.TryGetValue(playerID, out var player))
if (!Peer2PeerTransport.PlayerInstanceByID.TryGetValue(playerID, out var player))
{
Debug.LogError($"Bidding platform received invalid player {playerID} from server!");
return;
Expand Down
38 changes: 25 additions & 13 deletions Assets/Scripts/Auction/BiddingPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;
using UnityEngine.InputSystem;

public class BiddingPlayer : MonoBehaviour
public class BiddingPlayer : NetworkBehaviour
{
[SerializeField]
protected PlayerManager playerManager;
Expand All @@ -23,12 +23,13 @@ public class BiddingPlayer : MonoBehaviour
private void Start()
{
GetComponent<PlayerIK>().RightHandIKTarget = signTarget;

playerManager.identity.onChipChange += AnimateChipStatus;
playerManager.onSelectedBiddingPlatformChange += AnimateChipStatus;
chipText.text = playerManager.identity.chips.ToString();
}

SetPlayerInput();
public void SetIdentity()
{
playerManager.identity.onChipChange += UpdateChipStatus;
chipText.text = playerManager.identity.chips.ToString();
}

public void SetPlayerInput()
Expand All @@ -38,11 +39,21 @@ public void SetPlayerInput()
playerManager.inputManager.onFirePerformed += AnimateBid;
playerManager.inputManager.onSelect += AnimateBid;
}

chipText.text = playerManager.identity.chips.ToString();
}

private void AnimateBid(InputAction.CallbackContext ctx)
{
CmdAnimateBid();
}

[Command]
private void CmdAnimateBid()
{
RpcAnimateBid();
}

[ClientRpc]
private void RpcAnimateBid()
{
if (LeanTween.isTweening(signMesh.gameObject) || !currentPlatform)
return;
Expand All @@ -55,7 +66,7 @@ private void AnimateBid(InputAction.CallbackContext ctx)
protected void AnimateChipStatus(BiddingPlatform platform)
{
if (currentPlatform)
currentPlatform.onBidPlaced -= AnimateSign;
currentPlatform.onBidPlaced -= AnimateSignContent;

currentPlatform = platform;
if (!currentPlatform)
Expand All @@ -66,11 +77,12 @@ protected void AnimateChipStatus(BiddingPlatform platform)
return;
}

currentPlatform.onBidPlaced += AnimateSign;
AnimateSign(platform);
currentPlatform.onBidPlaced += AnimateSignContent;
AnimateSignContent(platform);
}

protected void AnimateSign(BiddingPlatform platform)
// TODO this stuff plays on the wrong player for network players
protected void AnimateSignContent(BiddingPlatform platform)
{
bool isLeaderAndCanBid = (platform.LeadingBidder == playerManager.id) && (playerManager.identity.chips > 0);
if (platform.chips < playerManager.identity.chips || isLeaderAndCanBid)
Expand All @@ -84,7 +96,7 @@ protected void AnimateSign(BiddingPlatform platform)
LeanTween.value(signCross.gameObject, (alpha) => signCross.alpha = alpha, 0f, 1f, 0.5f).setLoopPingPong();
}

protected void AnimateChipStatus(int chips)
protected void UpdateChipStatus(int chips)
{
chipText.text = chips.ToString();
}
Expand All @@ -108,7 +120,7 @@ private void OnDestroy()
}
if (playerManager.identity)
{
playerManager.identity.onChipChange -= AnimateChipStatus;
playerManager.identity.onChipChange -= UpdateChipStatus;
}
playerManager.onSelectedBiddingPlatformChange -= AnimateChipStatus;
}
Expand Down
31 changes: 21 additions & 10 deletions Assets/Scripts/Control&Input/Peer2PeerTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public class Peer2PeerTransport : NetworkManager

private static List<uint> localPlayerIds = new();

private static Dictionary<uint, PlayerManager> playerInstances = new();
public static ReadOnlyDictionary<uint, PlayerManager> PlayerInstanceByID;

/// <summary>
/// List of client connections. Will be uninitialized on clients.
/// </summary>
Expand All @@ -141,6 +144,8 @@ public override void OnStartServer()

// Reinitialize player lookups
players = new();
playerInstances = new();
PlayerInstanceByID = new(playerInstances);
playerIndex = 0;
connections = new();
playersForConnection = new();
Expand All @@ -163,6 +168,8 @@ public override void OnClientConnect()
PlayerInputManagerController.Singleton.JoinAllInputs();

players = new();
playerInstances = new();
PlayerInstanceByID = new(playerInstances);
playerIndex = 0;
}

Expand Down Expand Up @@ -355,7 +362,7 @@ private static void UpdatePlayerDetailsAfterShootingRound()
// Separated from loadout update due to inconsistent timing :)))))
public static void UpdatePlayerDetailsAfterAuction()
{
foreach (var player in MatchController.Singleton.Players)
foreach (var player in playerInstances.Values)
{
UpdatePlayerInventoryForIdentity(player.identity);
}
Expand Down Expand Up @@ -636,7 +643,7 @@ private IEnumerator WaitAndInitializeFPSPlayer(InitializePlayerMessage message)
playerManager.SetGun(gunHolder);
}

playerManager.ApplyColorFromIdentity();
playerManager.ApplyIdentity();

// This ensures that behaviours on the gun have identities.
// SHOULD be safe to initialize them here as this is at roughly the same point on all clients
Expand All @@ -646,6 +653,8 @@ private IEnumerator WaitAndInitializeFPSPlayer(InitializePlayerMessage message)
{
MatchController.Singleton.RegisterPlayer(playerManager);
}

playerInstances[player.id] = player;
}

#endregion
Expand All @@ -665,13 +674,13 @@ private void InitializeBiddingPlayer(InitializePlayerMessage message)
private IEnumerator WaitAndInitializeBiddingPlayer(InitializePlayerMessage message)
{
// Wait until players must've been spawned
// TODO find a better way to wait for that
// TODO that is, move to OnWhateverAuthority overrid on PlayerManager :)
yield return null;
yield return null;

var player = FindObjectsOfType<PlayerManager>()
.FirstOrDefault(p => p.id == message.id);
PlayerManager player = null;
while (player == null)
{
player = FindObjectsOfType<PlayerManager>()
.FirstOrDefault(p => p.id == message.id);
yield return null;
}

if (!player)
{
Expand Down Expand Up @@ -733,7 +742,7 @@ private IEnumerator WaitAndInitializeBiddingPlayer(InitializePlayerMessage messa
playerManager.GetComponent<Rigidbody>().isKinematic = true;
}

playerManager.ApplyColorFromIdentity();
playerManager.ApplyIdentity();

// This ensures that behaviours on the gun have identities.
// SHOULD be safe to initialize them here as this is at roughly the same point on all clients
Expand All @@ -743,6 +752,8 @@ private IEnumerator WaitAndInitializeBiddingPlayer(InitializePlayerMessage messa
{
MatchController.Singleton.RegisterPlayer(playerManager);
}

playerInstances[player.id] = player;
}

#endregion
Expand Down
5 changes: 1 addition & 4 deletions Assets/Scripts/Gamestate/MatchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public class MatchController : MonoBehaviour
private string currentMapName;

private Dictionary<uint, PlayerManager> playerById = new();
public ReadOnlyDictionary<uint, PlayerManager> PlayerById;

private List<PlayerManager> players = new();
public ReadOnlyCollection<PlayerManager> Players;
Expand Down Expand Up @@ -104,7 +103,6 @@ private void Awake()
#endregion Singleton boilerplate

Players = new ReadOnlyCollection<PlayerManager>(players);
PlayerById = new ReadOnlyDictionary<uint, PlayerManager>(playerById);
}

void Start()
Expand All @@ -131,7 +129,6 @@ public void StartNextRound()
players = new();
playerById = new();
Players = new ReadOnlyCollection<PlayerManager>(players);
PlayerById = new ReadOnlyDictionary<uint, PlayerManager>(playerById);

StartCoroutine(WaitForClientsAndInitialize());
}
Expand Down Expand Up @@ -271,7 +268,7 @@ private void HUDTimerUpdate()
private bool IsWin()
{
var winnerId = rounds.Last().Winner;
if (!PlayerById.TryGetValue(winnerId, out var winner)) { return false; }
if (!playerById.TryGetValue(winnerId, out var winner)) { return false; }
var wins = PlayerWins(winner);
Debug.Log($"Current winner ({winner.identity.playerName}) has {wins} wins.");
if (wins >= 3)
Expand Down
2 changes: 0 additions & 2 deletions Assets/Scripts/Gamestate/PlayerIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ public void UpdateFromDetails(PlayerDetails playerDetails, string name)
color = playerDetails.color;

chips = playerDetails.chips;
// TODO find some other way of updating bidding sign on load
onChipChange?.Invoke(chips);

SetItems(playerDetails.bodies, playerDetails.barrels, playerDetails.extensions);
SetLoadout(playerDetails.body, playerDetails.barrel, playerDetails.extension);
Expand Down
10 changes: 5 additions & 5 deletions Assets/Scripts/Gamestate/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ public BiddingPlatform SelectedBiddingPlatform
set
{
selectedBiddingPlatform = value;
// TODO this is somehow not listened to on network players
Debug.Log($"CHNAGED BIDDING PLATFOR FOR PLAYER {id}");
onSelectedBiddingPlatformChange?.Invoke(value);
}
}
Expand Down Expand Up @@ -256,14 +254,16 @@ public void SetPlayerInput(InputManager playerInput)
{
biddingPlayer.SetPlayerInput();
}

ApplyColorFromIdentity();
}

public void ApplyColorFromIdentity()
public void ApplyIdentity()
{
// Set player color
meshBase.GetComponentInChildren<SkinnedMeshRenderer>().material.color = identity.color;
if (playerIK.TryGetComponent<BiddingPlayer>(out var biddingPlayer))
{
biddingPlayer.SetIdentity();
}
}

void OnDestroy()
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/HealthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void DealDamage(DamageInfo info)
[ClientRpc]
private void DealDamageRpc(NetworkDamageInfo networkInfo)
{
var source = MatchController.Singleton ? MatchController.Singleton.PlayerById[networkInfo.sourcePlayer] : null;
var source = Peer2PeerTransport.PlayerInstanceByID[networkInfo.sourcePlayer];
var info = new DamageInfo(source, networkInfo);
ActuallyDealDamage(info);
}
Expand Down

0 comments on commit 016943d

Please sign in to comment.