Skip to content

Commit 75d045d

Browse files
fix: same player number for multiple players in second playthrough (#490)
* Added verification that player number is available before re-assigning it to reconnecting player
1 parent 59d8322 commit 75d045d

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

Assets/BossRoom/Scripts/Client/Game/State/ClientCharSelectState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void OnLobbyPlayerStateChanged(NetworkListEvent<CharSelectData.LobbyPlayerState>
218218
// we haven't chosen a seat yet (or were kicked out of our seat by someone else)
219219
UpdateCharacterSelection(CharSelectData.SeatState.Inactive);
220220
// make sure our player num is properly set in Lobby UI
221-
OnAssignedPlayerNumber(CharSelectData.LobbyPlayers[localPlayerIdx].PlayerNum);
221+
OnAssignedPlayerNumber(CharSelectData.LobbyPlayers[localPlayerIdx].PlayerNumber);
222222
}
223223
else
224224
{
@@ -319,7 +319,7 @@ void UpdateSeats()
319319
// now actually update the seats in the UI
320320
for (int i = 0; i < m_PlayerSeats.Count; ++i)
321321
{
322-
m_PlayerSeats[i].SetState(curSeats[i].SeatState, curSeats[i].PlayerNum, curSeats[i].PlayerName);
322+
m_PlayerSeats[i].SetState(curSeats[i].SeatState, curSeats[i].PlayerNumber, curSeats[i].PlayerName);
323323
}
324324
}
325325

Assets/BossRoom/Scripts/Server/Game/State/ServerCharSelectState.cs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void OnClientChangedSeat(ulong clientId, int newSeatIdx, bool lockedIn)
5353
// Instead of granting lock request, change this player to Inactive state.
5454
CharSelectData.LobbyPlayers[idx] = new CharSelectData.LobbyPlayerState(clientId,
5555
CharSelectData.LobbyPlayers[idx].PlayerName,
56-
CharSelectData.LobbyPlayers[idx].PlayerNum,
56+
CharSelectData.LobbyPlayers[idx].PlayerNumber,
5757
CharSelectData.SeatState.Inactive);
5858

5959
// then early out
@@ -64,7 +64,7 @@ private void OnClientChangedSeat(ulong clientId, int newSeatIdx, bool lockedIn)
6464

6565
CharSelectData.LobbyPlayers[idx] = new CharSelectData.LobbyPlayerState(clientId,
6666
CharSelectData.LobbyPlayers[idx].PlayerName,
67-
CharSelectData.LobbyPlayers[idx].PlayerNum,
67+
CharSelectData.LobbyPlayers[idx].PlayerNumber,
6868
lockedIn ? CharSelectData.SeatState.LockedIn : CharSelectData.SeatState.Active,
6969
newSeatIdx,
7070
Time.time);
@@ -81,7 +81,7 @@ private void OnClientChangedSeat(ulong clientId, int newSeatIdx, bool lockedIn)
8181
CharSelectData.LobbyPlayers[i] = new CharSelectData.LobbyPlayerState(
8282
CharSelectData.LobbyPlayers[i].ClientId,
8383
CharSelectData.LobbyPlayers[i].PlayerName,
84-
CharSelectData.LobbyPlayers[i].PlayerNum,
84+
CharSelectData.LobbyPlayers[i].PlayerNumber,
8585
CharSelectData.SeatState.Inactive);
8686
}
8787
}
@@ -185,46 +185,52 @@ private void OnSceneEvent(SceneEvent sceneEvent)
185185
SeatNewPlayer(sceneEvent.ClientId);
186186
}
187187

188-
private int GetAvailablePlayerNum()
188+
private int GetAvailablePlayerNumber()
189189
{
190-
for (int possiblePlayerNum = 0; possiblePlayerNum < CharSelectData.k_MaxLobbyPlayers; ++possiblePlayerNum)
190+
for (int possiblePlayerNumber = 0; possiblePlayerNumber < CharSelectData.k_MaxLobbyPlayers; ++possiblePlayerNumber)
191191
{
192-
bool found = false;
193-
foreach (CharSelectData.LobbyPlayerState playerState in CharSelectData.LobbyPlayers)
192+
if (IsPlayerNumberAvailable(possiblePlayerNumber))
194193
{
195-
if (playerState.PlayerNum == possiblePlayerNum)
196-
{
197-
found = true;
198-
break;
199-
}
200-
}
201-
if (!found)
202-
{
203-
return possiblePlayerNum;
194+
return possiblePlayerNumber;
204195
}
205196
}
206197
// we couldn't get a Player# for this person... which means the lobby is full!
207198
return -1;
208199
}
209200

201+
bool IsPlayerNumberAvailable(int playerNumber)
202+
{
203+
bool found = false;
204+
foreach (CharSelectData.LobbyPlayerState playerState in CharSelectData.LobbyPlayers)
205+
{
206+
if (playerState.PlayerNumber == playerNumber)
207+
{
208+
found = true;
209+
break;
210+
}
211+
}
212+
213+
return !found;
214+
}
215+
210216
private void SeatNewPlayer(ulong clientId)
211217
{
212218
SessionPlayerData? sessionPlayerData = SessionManager<SessionPlayerData>.Instance.GetPlayerData(clientId);
213219
if (sessionPlayerData.HasValue)
214220
{
215221
var playerData = sessionPlayerData.Value;
216-
if (playerData.PlayerNum == -1)
222+
if (playerData.PlayerNumber == -1 || !IsPlayerNumberAvailable(playerData.PlayerNumber))
217223
{
218-
// If no player num already assigned, get an available one.
219-
playerData.PlayerNum = GetAvailablePlayerNum();
224+
// If no player num already assigned or if player num is no longer available, get an available one.
225+
playerData.PlayerNumber = GetAvailablePlayerNumber();
220226
}
221-
if (playerData.PlayerNum == -1)
227+
if (playerData.PlayerNumber == -1)
222228
{
223229
// Sanity check. We ran out of seats... there was no room!
224-
throw new Exception($"we shouldn't be here, connection approval should have refused this connection already for client ID {clientId} and player num {playerData.PlayerNum}");
230+
throw new Exception($"we shouldn't be here, connection approval should have refused this connection already for client ID {clientId} and player num {playerData.PlayerNumber}");
225231
}
226232

227-
CharSelectData.LobbyPlayers.Add(new CharSelectData.LobbyPlayerState(clientId, playerData.PlayerName, playerData.PlayerNum, CharSelectData.SeatState.Inactive));
233+
CharSelectData.LobbyPlayers.Add(new CharSelectData.LobbyPlayerState(clientId, playerData.PlayerName, playerData.PlayerNumber, CharSelectData.SeatState.Inactive));
228234
SessionManager<SessionPlayerData>.Instance.SetPlayerData(clientId, playerData);
229235
}
230236
}

Assets/BossRoom/Scripts/Shared/Game/State/CharSelectData.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ public struct LobbyPlayerState : INetworkSerializable, IEquatable<LobbyPlayerSta
2626

2727
private FixedPlayerName m_PlayerName; // I'm sad there's no 256Bytes fixed list :(
2828

29-
public int PlayerNum; // this player's assigned "P#". (0=P1, 1=P2, etc.)
29+
public int PlayerNumber; // this player's assigned "P#". (0=P1, 1=P2, etc.)
3030
public int SeatIdx; // the latest seat they were in. -1 means none
3131
public float LastChangeTime;
3232

3333
public SeatState SeatState;
3434

3535

36-
public LobbyPlayerState(ulong clientId, string name, int playerNum, SeatState state, int seatIdx = -1, float lastChangeTime = 0)
36+
public LobbyPlayerState(ulong clientId, string name, int playerNumber, SeatState state, int seatIdx = -1, float lastChangeTime = 0)
3737
{
3838
ClientId = clientId;
39-
PlayerNum = playerNum;
39+
PlayerNumber = playerNumber;
4040
SeatState = state;
4141
SeatIdx = seatIdx;
4242
LastChangeTime = lastChangeTime;
@@ -55,7 +55,7 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
5555
{
5656
serializer.SerializeValue(ref ClientId);
5757
serializer.SerializeValue(ref m_PlayerName);
58-
serializer.SerializeValue(ref PlayerNum);
58+
serializer.SerializeValue(ref PlayerNumber);
5959
serializer.SerializeValue(ref SeatState);
6060
serializer.SerializeValue(ref SeatIdx);
6161
serializer.SerializeValue(ref LastChangeTime);
@@ -65,7 +65,7 @@ public bool Equals(LobbyPlayerState other)
6565
{
6666
return ClientId == other.ClientId &&
6767
m_PlayerName.Equals(other.m_PlayerName) &&
68-
PlayerNum == other.PlayerNum &&
68+
PlayerNumber == other.PlayerNumber &&
6969
SeatIdx == other.SeatIdx &&
7070
LastChangeTime.Equals(other.LastChangeTime) &&
7171
SeatState == other.SeatState;

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/SessionPlayerData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Unity.Multiplayer.Samples.BossRoom
55
public struct SessionPlayerData : ISessionPlayerData
66
{
77
public string PlayerName;
8-
public int PlayerNum;
8+
public int PlayerNumber;
99
public Vector3 PlayerPosition;
1010
public Quaternion PlayerRotation;
1111
public NetworkGuid AvatarNetworkGuid;
@@ -16,7 +16,7 @@ public SessionPlayerData(ulong clientID, string name, NetworkGuid avatarNetworkG
1616
{
1717
ClientID = clientID;
1818
PlayerName = name;
19-
PlayerNum = -1;
19+
PlayerNumber = -1;
2020
PlayerPosition = Vector3.zero;
2121
PlayerRotation = Quaternion.identity;
2222
AvatarNetworkGuid = avatarNetworkGuid;

0 commit comments

Comments
 (0)