-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameSessionManager.cs
More file actions
462 lines (372 loc) · 16.1 KB
/
Copy pathGameSessionManager.cs
File metadata and controls
462 lines (372 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
using GOJ.Game.Gamemodes;
using GOJ.Networking;
using System.Linq;
namespace GOJ.Game
{
public class GameSessionManager : NetworkBehaviour
{
public static GameSessionManager Instance { get; private set; }
//load this and add the gamemode as a component before spawning
[SerializeField]
NetworkObject m_whosSongGamemodePrefab;
NetworkObject m_spawnedGamemodeObject;
public NetworkVariable<ulong> TrackRetrievalNetworkObjectId { get; set; } = new NetworkVariable<ulong>();
[SerializeField]
public NetworkVariable<NetworkObjectReference> CurrentGamemodeObject { get; set; } = new NetworkVariable<NetworkObjectReference>();
public Gamemode CurrentGamemode { get; private set; }
[SerializeField]
PlayerColours m_playerColours;
public PlayerColours PlayerColours { get { return m_playerColours; } }
[SerializeField]
NetworkObjectSpawner m_playerObjectSpawner;
public Dictionary<ulong, string> ClientIdToPlayerId { get; private set; } = new Dictionary<ulong, string>();
public Dictionary<string, PlayerSessionData> PlayerData { get; private set; } = new Dictionary<string, PlayerSessionData>();
public NetworkList<ActivePlayerData> ActivePlayers { get; private set; }
public NetworkList<NetworkBehaviourReference> PlayersWaitingToJoin { get; private set; }
/// <summary>
/// Contains player ids of anyone who shouldn't be selectable this round, includes disconnected players and
/// rejoining players that missed the round's start
/// </summary>
public NetworkList<FixedString512Bytes> PlayersNotSelectableThisRoundIds { get; private set; }
[SerializeField]
private NetworkVariable<SpotifySettings> m_spotifySessionSettings = new NetworkVariable<SpotifySettings>();
public NetworkVariable<SpotifySettings> SpotifySessionSettings
{
get { return m_spotifySessionSettings; }
set { m_spotifySessionSettings = value; }
}
public NetworkVariable<bool> SessionHasStarted { get; private set; } = new NetworkVariable<bool>(false);
NetworkVariable<EndGameStateArgs> m_currentGameFinalResults = new NetworkVariable<EndGameStateArgs>();
public NetworkVariable<EndGameStateArgs> CurrentGameFinalResults => m_currentGameFinalResults;
List<ulong> m_playersReadyToContinueClientIds = new List<ulong>(Helpers.NetworkHelpers.MAX_PLAYERS);
List<ulong> m_playersReadyToRestartClientIds = new List<ulong>(Helpers.NetworkHelpers.MAX_PLAYERS);
NetworkVariable<GameState> m_gameState = new NetworkVariable<GameState>(GameState.GameScreenLoadingEveryoneIn);
public GameState GameState => m_gameState.Value;
private void Awake()
{
ActivePlayers = new NetworkList<ActivePlayerData>();
PlayersWaitingToJoin = new NetworkList<NetworkBehaviourReference>();
PlayersNotSelectableThisRoundIds = new NetworkList<FixedString512Bytes>();
}
public override void OnNetworkSpawn()
{
if (IsServer && Instance != null)
{
NetworkObject.Despawn();
}
Instance = this;
DontDestroyOnLoad(this.gameObject);
if (!IsServer)
return;
m_playerColours.Reinitialise();
PlayerObject.OnNetworkDespawned += OnPlayerDespawned;
CurrentGamemodeObject.OnValueChanged += OnGamemodeObjectChanged;
Gamemode.OnCalculatedResults += OnRoundEnded;
}
public override void OnNetworkDespawn()
{
Instance = null;
if (!IsServer)
return;
CurrentGamemodeObject.OnValueChanged -= OnGamemodeObjectChanged;
PlayerObject.OnNetworkDespawned -= OnPlayerDespawned;
Gamemode.OnCalculatedResults -= OnRoundEnded;
OnServerEnded();
}
#region Connection Management
void OnPlayerDespawned(NetworkBehaviourReference player, ulong clientId)
{
//Host left so don't need to bother with the rest
if (clientId == NetworkManager.ServerClientId)
return;
if (ClientIdToPlayerId.ContainsKey(clientId))
{
string despawnedPlayerId = ClientIdToPlayerId[clientId];
//have to manually loop through as NetworkList doesn't support predicates or LINQ
foreach (var playerData in ActivePlayers)
{
if (playerData.PlayerId.ToString() == despawnedPlayerId)
{
ActivePlayers.Remove(playerData);
}
else if (PlayersWaitingToJoin.Contains(player))
{
PlayersWaitingToJoin.Remove(player);
}
}
}
switch (m_gameState.Value)
{
case GameState.GameScreenGuessing:
if(CurrentGamemode != null)
CurrentGamemode.PlayerDisconnected(clientId);
break;
case GameState.GameScreenWaitingToContinueNextRound:
m_playersReadyToContinueClientIds.Remove(clientId);
EndRoundIfEveryoneReady();
break;
case GameState.EndScreen:
m_playersReadyToRestartClientIds.Remove(clientId);
if(clientId != NetworkManager.LocalClientId)
StartNewGameIfEveryoneReady();
break;
}
}
void MoveWaitingToJoinPlayersToActive()
{
if (PlayersWaitingToJoin.Count == 0)
return;
foreach (var player in PlayersWaitingToJoin)
{
if (player.TryGet(out PlayerObject playerObject))
ActivePlayers.Add(new ActivePlayerData(playerObject.PlayerId, playerObject, true));
else
Logger.LogError("Error getting player object from waiting to join player");
}
PlayersWaitingToJoin.Clear();
}
public void DisconnectClient(ulong clientId)
{
if (SessionHasStarted.Value)
{
if (ClientIdToPlayerId.TryGetValue(clientId, out var playerId))
{
if (GetPlayerData(playerId).ClientId == clientId)
{
//save client data so they can reconnect
var clientData = PlayerData[playerId];
clientData.IsConnected = false;
PlayerData[playerId] = clientData;
}
}
}
else
{
//session not started so no need to save data
if (ClientIdToPlayerId.TryGetValue(clientId, out var playerId))
{
ClientIdToPlayerId.Remove(clientId);
if (GetPlayerData(playerId).ClientId == clientId)
{
PlayerData.Remove(playerId);
}
}
}
}
bool IsDuplicateConnection(string playerId)
{
return PlayerData.ContainsKey(playerId) && PlayerData[playerId].IsConnected;
}
public void SetupConnectingPlayerSessionData(ulong clientId, string playerId, PlayerSessionData playerSessionData, PlayerObject playerObject)
{
bool isReconnecting = false;
if (IsDuplicateConnection(playerId))
{
Logger.LogErrorFormat($"Player id {playerId} already exists. This is a duplicate connection. Rejecting this session data");
return;
}
if (PlayerData.ContainsKey(playerId))
{
if (!PlayerData[playerId].IsConnected)
{
isReconnecting = true;
}
}
if (isReconnecting)
{
playerSessionData = PlayerData[playerId];
playerSessionData.ClientId = clientId;
playerSessionData.IsConnected = true;
playerSessionData.PlayerName = playerObject.UserName;
playerSessionData.PlayExplicitSongs = playerObject.PlayExplicitSongs;
PlayersNotSelectableThisRoundIds.Remove(playerId);
}
else
{
Logger.Log($"Adding new player data: {playerSessionData}");
}
ClientIdToPlayerId[clientId] = playerId;
PlayerData[playerId] = playerSessionData;
AddSpawnedPlayerToListOfPlayers(playerObject, playerSessionData.IsInThisRound);
}
void AddSpawnedPlayerToListOfPlayers(PlayerObject player, bool isInThisRound)
{
if (!IsHost)
return;
string playerId = player.PlayerId;
if (PlayerData.ContainsKey(playerId))
{
ActivePlayers.Add(new ActivePlayerData(player.PlayerId, player, isInThisRound));
}
else if (GameState != GameState.EndScreen)
{
PlayersWaitingToJoin.Add(player);
}
}
public void SpawnPlayer(ulong clientId)
{
m_playerObjectSpawner.SpawnObject(clientId);
}
public void OnServerEnded()
{
PlayerData.Clear();
ClientIdToPlayerId.Clear();
ActivePlayers.Clear();
PlayersWaitingToJoin.Clear();
SessionHasStarted.Value = false;
}
void ClearDisconnectedPlayersData()
{
List<ulong> idsToClear = new List<ulong>();
foreach (var clientId in ClientIdToPlayerId.Keys)
{
var data = GetPlayerData(clientId);
if (!data.IsConnected)
{
idsToClear.Add(clientId);
}
}
foreach (var clientId in idsToClear)
{
string playerId = ClientIdToPlayerId[clientId];
if (GetPlayerData(playerId).ClientId == clientId)
{
PlayerData.Remove(playerId);
}
ClientIdToPlayerId.Remove(clientId);
}
PlayersNotSelectableThisRoundIds.Clear();
}
public PlayerSessionData GetPlayerData(ulong clientId)
{
string playerId = ClientIdToPlayerId[clientId];
if (playerId != null)
return GetPlayerData(playerId);
return default;
}
public PlayerSessionData GetPlayerData(string playerId)
{
if (!PlayerData.TryGetValue(playerId, out PlayerSessionData result))
{
Logger.LogErrorFormat($"Error getting player session data for {playerId}");
}
return result;
}
#endregion
/// <summary>
/// <para>Currently this only sets to use the Who's Song gamemode.</para>
/// <para>TODO update this when more gamemodes get added</para>
/// </summary>
public void SetGamemode(Gamemode gamemode)
{
if (IsHost)
{
CurrentGamemodeObject.Value = gamemode.NetworkObject;
}
}
void OnGamemodeObjectChanged(NetworkObjectReference oldValue, NetworkObjectReference newValue)
{
if (newValue.TryGet(out var networkObj))
{
CurrentGamemode = networkObj.GetComponent<Gamemode>();
}
}
[ServerRpc(RequireOwnership = false)]
public void OnPlayerReadyForNextRoundServerRpc(ServerRpcParams serverRpcParams = default)
{
if(m_gameState.Value == GameState.GameScreenGuessing)
return;
m_playersReadyToContinueClientIds.Add(serverRpcParams.Receive.SenderClientId);
EndRoundIfEveryoneReady();
}
void EndRoundIfEveryoneReady()
{
Logger.Log($"Ending round if everyone ready, ready players: {m_playersReadyToContinueClientIds.Count} / {ActivePlayers.Count}");
if (m_playersReadyToContinueClientIds.Count >= ActivePlayers.Count)
{
EndRound();
}
}
void EndRound()
{
m_playersReadyToContinueClientIds.Clear();
MoveWaitingToJoinPlayersToActive();
//set whether each player is in from the start of this round, if someone leaves from here on they will be reconnected like normal
foreach (var playerId in PlayerData.Keys.ToList())
{
PlayerSessionData temp = PlayerData[playerId];
temp.IsInThisRound = temp.IsConnected;
PlayerData[playerId] = temp;
if (!temp.IsConnected && !PlayersNotSelectableThisRoundIds.Contains(playerId))
{
PlayersNotSelectableThisRoundIds.Add(playerId);
}
}
string winnerPlayerId = CurrentGamemode.CheckIfPlayerHasWon();
if (!string.IsNullOrEmpty(winnerPlayerId))
{
OpenEndGameScreen(winnerPlayerId);
}
else
{
StartNextRound();
}
}
void OpenEndGameScreen(string winnerPlayerId)
{
PlayerResultNetworkData[] playerResults = new PlayerResultNetworkData[CurrentGamemode.PlayerScores.Count];
int count = 0;
foreach (var player in CurrentGamemode.PlayerScores)
{
playerResults[count++] = new PlayerResultNetworkData(PlayerData[player.Key].PlayerName, player.Value);
}
m_currentGameFinalResults.Value = new EndGameStateArgs(winnerPlayerId, playerResults);
m_gameState.Value = GameState.EndScreen;
//open winner screen
if(NetworkManager.Singleton != null && NetworkManager.Singleton.SceneManager != null)
NetworkManager.Singleton.SceneManager.LoadScene("EndGameScene", UnityEngine.SceneManagement.LoadSceneMode.Single);
}
public void StartNextRound()
{
m_playersReadyToContinueClientIds.Clear();
SessionHasStarted.Value = true;
m_gameState.Value = GameState.GameScreenGuessing;
CurrentGamemode.StartNextRound();
}
[ServerRpc(RequireOwnership = false)]
public void OnPlayerReadyForNewGameServerRpc(ServerRpcParams serverRpcParams = default)
{
m_playersReadyToRestartClientIds.Add(serverRpcParams.Receive.SenderClientId);
StartNewGameIfEveryoneReady();
}
void StartNewGameIfEveryoneReady()
{
if(NetworkManager.Singleton == null || NetworkManager.Singleton.SceneManager == null)
{
Logger.LogWarning("Network manager or network scene manager is null, cannot start new game");
return;
}
if (m_playersReadyToRestartClientIds.Count >= ActivePlayers.Count)
{
m_playersReadyToRestartClientIds.Clear();
ClearDisconnectedPlayersData();
m_gameState.Value = GameState.GameScreenLoadingEveryoneIn;
NetworkManager.Singleton.SceneManager.LoadScene("WhoseSongIsItScene", UnityEngine.SceneManagement.LoadSceneMode.Single);
}
}
void OnRoundEnded(RoundResults results)
{
m_gameState.Value = GameState.GameScreenWaitingToContinueNextRound;
}
public bool HasEndGameResults()
{
return m_currentGameFinalResults.Value.PlayerResults != null && m_currentGameFinalResults.Value.PlayerResults.Length > 0;
}
}
}