Skip to content

Commit

Permalink
Add and use SteamIDs for players
Browse files Browse the repository at this point in the history
  • Loading branch information
toberge committed May 13, 2024
1 parent 9283349 commit 82b8a52
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
22 changes: 9 additions & 13 deletions Assets/Scripts/Control&Input/Peer2PeerTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public struct PlayerDetails
{
public uint id;
public ulong steamID;
public PlayerType type;

public string name;
Expand Down Expand Up @@ -76,7 +77,7 @@ public class Peer2PeerTransport : NetworkManager
private SpawnHandlerDelegate onSpawnPlayer;
private UnSpawnDelegate onUnSpawnPlayer;

private const int FPSPlayerIndex = 0;
private const int FPSPlayerPrefabIndex = 0;

private readonly Dictionary<uint, PlayerDetails> players = new();

Expand Down Expand Up @@ -124,9 +125,7 @@ private void OnSpawnPlayerInput(NetworkConnectionToClient connection, PlayerConn
PlayerInputManagerController.Singleton.NetworkClients.Add(connection);
var index = SteamManager.Singleton.PlayerNames.Count - 1;
var steamName = SteamManager.Singleton.PlayerNames[index];
SteamManager.Singleton.PlayerDictionary.Add(connection.connectionId,
(steamName, index));

var steamID = SteamManager.Singleton.PlayerIDs[index];

var player = Instantiate(playerPrefab);
var playerInput = player.GetComponent<InputManager>();
Expand All @@ -145,6 +144,7 @@ private void OnSpawnPlayerInput(NetworkConnectionToClient connection, PlayerConn
var details = new PlayerDetails
{
id = (uint)index,
steamID = steamID,
type = message.type,
name = steamName,
color = PlayerInputManagerController.Singleton.PlayerColors[index],
Expand All @@ -158,8 +158,7 @@ private void OnReceivePlayerDetails(InitialPlayerDetailsMessage message)
var details = message.details;
if (players.ContainsKey(details.id))
return;
// TODO replace this check with sth else, find your netId somehow? Or use steam id?
details.type = SteamManager.Singleton.UserName == details.name ? PlayerType.Local : PlayerType.Remote;
details.type = SteamManager.Singleton.SteamID.m_SteamID == details.steamID ? PlayerType.Local : PlayerType.Remote;
if (details.type is PlayerType.Local) myId = details.id;
Debug.Log($"Received info for player {details.id}: name={details.name} type={details.type} color={details.color}");
players.Add(details.id, details);
Expand Down Expand Up @@ -218,11 +217,9 @@ private void OnSpawnFPSPlayer(NetworkConnectionToClient connection, SpawnFPSPlay
}

var spawnPoint = spawnPoints[playerIndex];
// TODO reset this
playerIndex++;

// TODO check if this is kenough
var player = Instantiate(spawnPrefabs[FPSPlayerIndex], spawnPoint.position, spawnPoint.rotation);
var player = Instantiate(spawnPrefabs[FPSPlayerPrefabIndex], spawnPoint.position, spawnPoint.rotation);
player.GetComponent<PlayerManager>().id = message.id;
NetworkServer.AddPlayerForConnection(connection, player);
NetworkServer.SendToAll(new InitializeFPSPlayerMessage(message.id, spawnPoint.position, spawnPoint.rotation));
Expand Down Expand Up @@ -271,7 +268,7 @@ private IEnumerator WaitAndInitializeFPSPlayer(InitializeFPSPlayerMessage messag
var input = PlayerInputManagerController.Singleton.LocalPlayerInputs[0];
// Make playerInput child of player it's attached to
input.transform.parent = player.transform;
// Set recieved playerInput (and most importantly its camera) at an offset from player's position
// Set received playerInput (and most importantly its camera) at an offset from player's position
input.transform.localPosition = cameraOffset.localPosition;
input.transform.rotation = player.transform.rotation;

Expand Down Expand Up @@ -307,7 +304,6 @@ private IEnumerator WaitAndInitializeFPSPlayer(InitializeFPSPlayerMessage messag
// TODO set based on playerdetails (and edit playerdetails)
playerManager.identity.SetLoadout(StaticInfo.Singleton.StartingBody, StaticInfo.Singleton.StartingBarrel,
StaticInfo.Singleton.StartingExtension);
Debug.Log($"{playerManager.identity.Body}");

// TODO do some other version of disabling HUD completely
Destroy(playerManager.HUDController);
Expand All @@ -316,11 +312,11 @@ private IEnumerator WaitAndInitializeFPSPlayer(InitializeFPSPlayerMessage messag
playerManager.GetComponent<Rigidbody>().isKinematic = true;

// Create display gun structure
var gunHolderParent = new GameObject("GunHolderParent").transform;
var gunHolderParent = new GameObject("DisplayGunParent").transform;
gunHolderParent.parent = player.transform;
gunHolderParent.position = cameraOffset.position;
gunHolderParent.rotation = player.transform.rotation;
var gunHolder = new GameObject("GunHolder").transform;
var gunHolder = new GameObject("DisplayGunHolder").transform;
gunHolder.parent = gunHolderParent.transform;
gunHolder.localPosition = Vector3.zero;
gunHolder.localRotation = Quaternion.identity;
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/PlayerSelect/PlayerSelectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ private void Start()
animatorParameters.Add(playerAnimators[0].GetParameter(i).name);
}
if (SteamManager.IsSteamActive)
SteamManager.Singleton.LobbyPlayerUpdate += SetLobby;
SteamManager.Singleton.LobbyPlayerUpdate += UpdateLobby;
}

public void SetLobby()
public void UpdateLobby()
{
for (int i = 0; i < SteamManager.Singleton.PlayerNames.Count; i++)
SetupPlayerSelectModels(SteamManager.Singleton.PlayerNames[i], playerInputManagerController.PlayerColors[i], i);
Expand Down Expand Up @@ -173,6 +173,6 @@ IEnumerator PlayRandomAnimation()
private void OnDestroy()
{
if (SteamManager.IsSteamActive)
SteamManager.Singleton.LobbyPlayerUpdate -= SetLobby;
SteamManager.Singleton.LobbyPlayerUpdate -= UpdateLobby;
}
}
2 changes: 1 addition & 1 deletion Assets/Scripts/UI/MainMenu/MainMenuController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public void HostLobby()
if (!SteamManager.IsSteamActive)
return;
SteamManager.Singleton.HostLobby();
playerSelectManager.SetLobby();
playerSelectManager.UpdateLobby();
}

public void LeaveLobby()
Expand Down
47 changes: 27 additions & 20 deletions Assets/Scripts/Utils/SteamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class SteamManager : MonoBehaviour
public static bool IsSteamActive => isSteamInitialized;
public bool IsHosting = false;
public string UserName;
public List<string> PlayerNames = new List<string>();
public Dictionary<int, (string, int)> PlayerDictionary = new Dictionary<int, (string, int)>();
public CSteamID SteamID;
public List<string> PlayerNames = new();
public List<ulong> PlayerIDs = new();
public delegate void LobbyEvent();
public LobbyEvent LobbyPlayerUpdate;

Expand Down Expand Up @@ -89,6 +90,7 @@ private void Start()

RequestStats();
UserName = SteamFriends.GetPersonaName();
SteamID = SteamUser.GetSteamID();
}

private void RequestStats()
Expand Down Expand Up @@ -152,20 +154,31 @@ private void OnJoinRequest(GameLobbyJoinRequested_t callback)
SteamMatchmaking.JoinLobby(callback.m_steamIDLobby);
}

private void OnLobbyEnter(LobbyEnter_t callback)
private void UpdateLobbyData(ulong lobbyID)
{
// All users
PlayerInputManagerController.Singleton.RemoveJoinListener();
// TODO: set steam names over players
var lobbyId = new CSteamID(callback.m_ulSteamIDLobby);
Debug.Log("onLobbyUp");
var lobbyId = new CSteamID(lobbyID);
Debug.Log("Lobby entered");
for (int i = 0; i < SteamMatchmaking.GetNumLobbyMembers(lobbyId); i++)
{
string name = SteamFriends.GetFriendPersonaName(SteamMatchmaking.GetLobbyMemberByIndex(lobbyId, i));
if (!PlayerNames.Contains(name))
PlayerNames.Add(name);
var id = SteamMatchmaking.GetLobbyMemberByIndex(lobbyId, i);
var name = SteamFriends.GetFriendPersonaName(id);

if (PlayerIDs.Contains(id.m_SteamID))
continue;
Debug.Log($"Steam user {name} (id={id.m_SteamID}) entered lobby");

// TODO replace these separate lists with *one*
PlayerNames.Add(name);
PlayerIDs.Add(id.m_SteamID);
}
LobbyPlayerUpdate?.Invoke();
}

private void OnLobbyEnter(LobbyEnter_t callback)
{
// All users
PlayerInputManagerController.Singleton.RemoveJoinListener();
UpdateLobbyData(callback.m_ulSteamIDLobby);
if (NetworkServer.active)
return;
// Only clients from here!
Expand All @@ -175,15 +188,8 @@ private void OnLobbyEnter(LobbyEnter_t callback)

private void OnLobbyUpdate(LobbyChatUpdate_t callback)
{
var lobbyId = new CSteamID(callback.m_ulSteamIDLobby);
Debug.Log("onLobbyUp");
for (int i = 0; i < SteamMatchmaking.GetNumLobbyMembers(lobbyId); i++)
{
string name = SteamFriends.GetFriendPersonaName(SteamMatchmaking.GetLobbyMemberByIndex(lobbyId, i));
if (!PlayerNames.Contains(name))
PlayerNames.Add(name);
}
LobbyPlayerUpdate?.Invoke();
Debug.Log("Lobby updated");
UpdateLobbyData(callback.m_ulSteamIDLobby);
}

public void HostLobby()
Expand All @@ -194,6 +200,7 @@ public void HostLobby()
SteamMatchmaking.CreateLobby(ELobbyType.k_ELobbyTypeFriendsOnly, transportProtocol.maxConnections);
// Disable local joining
PlayerInputManagerController.Singleton.RemoveJoinListener();
// TODO make it possible to have multiple local players in online match, then remove this
for (int i = 0; i < PlayerInputManagerController.Singleton.LocalPlayerInputs.Count; i++)
{
if (i == 0)
Expand Down

0 comments on commit 82b8a52

Please sign in to comment.