-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLobbyScreen.cs
More file actions
344 lines (263 loc) · 11.9 KB
/
Copy pathLobbyScreen.cs
File metadata and controls
344 lines (263 loc) · 11.9 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
using SpotifyAPI.Web;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using Unity.Services.Authentication;
using Unity.Services.Lobbies;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using GOJ.Attributes;
using GOJ.Extensions;
using GOJ.Game;
using GOJ.Networking;
namespace GOJ.UI.Screens
{
[ScreenName("LobbyScreen")]
public class LobbyScreen : IVisualElement
{
public VisualElement Root { get; private set; }
public bool IsInitialised { get; private set; }
Dictionary<int, PersonalizationTopRequest.TimeRange> m_timeRangeMap = new Dictionary<int, PersonalizationTopRequest.TimeRange>()
{
{0, PersonalizationTopRequest.TimeRange.ShortTerm },
{1, PersonalizationTopRequest.TimeRange.MediumTerm },
{2, PersonalizationTopRequest.TimeRange.LongTerm }
};
//Displayed to everyone
Label m_lobbyCode;
CopyStringToClipboardButton m_copyCodeButton;
Label m_playerCount;
VisualElement m_playerNamesParent;
Dictionary<string, Label> m_playerNames = new Dictionary<string, Label>();
Button m_leaveLobbyButton;
#region Host Only
SpotifySettings m_settings;
VisualElement m_hostOptionsParent;
Slider m_songStartPercentageSlider;
Label m_percentageLabel;
Toggle m_useTopSongsToggle;
Toggle m_useLikedSongsToggle;
Toggle m_useSongsInPlaylistToggle;
RadioButtonGroup m_timeRangeOptions;
Button m_startGameButton;
bool m_startingGame = false;
#endregion
GameObject m_gameSessionManagerPrefab;
public void Initialise(VisualElement root, params object[] args)
{
Root = root;
root.RegisterCallback<DetachFromPanelEvent>(Cleanup);
InitialisePlayerObjects();
bool isHost = AuthenticationService.Instance.PlayerId == LobbyManager.Instance.JoinedLobby.HostId;
if (isHost)
m_settings = CreateSessionSettings();
InitialiseHostObjects();
if (!isHost)
{
LobbyManager.Instance.OnRelayConnectionCreated += ConnectToRelay;
}
m_gameSessionManagerPrefab = args[0] as GameObject;
IsInitialised = true;
}
public void UpdateData(params object[] args)
{
m_lobbyCode.text = string.Format("Lobby Code: {0}", LobbyManager.Instance.JoinedLobby.LobbyCode);
bool isHost = AuthenticationService.Instance.PlayerId == LobbyManager.Instance.JoinedLobby.HostId;
UpdateHostObjects(isHost);
UpdatePlayerNames();
}
void UpdateHostObjects(bool isHost)
{
if (isHost)
{
m_useTopSongsToggle.value = m_settings.UseTopSongs;
m_useLikedSongsToggle.value = m_settings.UseLikedSongs;
m_useSongsInPlaylistToggle.value = m_settings.UseSongsInPlaylists;
m_songStartPercentageSlider.value = m_settings.SongStartPercentage;
}
m_hostOptionsParent.SetVisibility(isHost);
m_startGameButton.SetVisibility(isHost);
}
SpotifySettings CreateSessionSettings()
{
//set up defaults for the session settings
SpotifySettings sessionSettings = new SpotifySettings();
//The enum order here is reverse to what the UI shows so using medium term as it's in the middle for both
sessionSettings.SelectedTopSongsTimeRange = PersonalizationTopRequest.TimeRange.MediumTerm;
sessionSettings.UseRecentlyPlayed = false;
sessionSettings.UseLikedSongs = true;
sessionSettings.UseTopSongs = true;
sessionSettings.UseSongsInPlaylists = true;
sessionSettings.SongStartPercentage = 0;
return sessionSettings;
}
void UpdatePlayerNames()
{
UpdatePlayerCount(LobbyManager.Instance.JoinedLobby.Players.Count);
m_playerNames.Clear();
m_playerNamesParent.Clear();
foreach (var player in LobbyManager.Instance.JoinedLobby.Players)
{
AddPlayerName(player.Id, player.Data["PlayerName"].Value);
}
}
void AddPlayerName(string playerId, string playerName)
{
if(!m_playerNames.ContainsKey(playerId))
{
Label label = new Label(playerName);
//TODO make host display an icon (crown?) instead of asterisks
if(LobbyManager.Instance.JoinedLobby.HostId == playerId)
{
label.text = string.Format("** {0} **", playerName);
}
label.AddToClassList("lobby-player-name");
m_playerNames.Add(playerId, label);
m_playerNamesParent.Add(label);
}
else
{
Logger.LogFormat("Already have player {0}", playerName);
}
}
public void Reset()
{
throw new System.NotImplementedException();
}
void InitialisePlayerObjects()
{
m_lobbyCode = Root.Q<Label>("LobbyCode");
m_copyCodeButton = Root.Q<CopyStringToClipboardButton>("CopyLobbyCodeButton");
m_copyCodeButton.SetTextToCopy(LobbyManager.Instance.JoinedLobby.LobbyCode);
m_playerCount = Root.Q<Label>("PlayerCount");
m_playerNamesParent = Root.Q("PlayerInfos");
LobbyManager.Instance.OnLobbyPlayersChanged += UpdatePlayerNames;
LobbyManager.Instance.OnLobbyHostChanged += OnHostChanged;
m_leaveLobbyButton = Root.Q<Button>("LeaveButton");
m_leaveLobbyButton.clicked += LeaveLobby;
}
void InitialiseHostObjects()
{
m_hostOptionsParent = Root.Q("LobbySettings");
//Host only
m_percentageLabel = Root.Q<Label>("PercentageLabel");
m_songStartPercentageSlider = Root.Q<Slider>("SongStartSlider");
m_songStartPercentageSlider.RegisterValueChangedCallback(OnSongStartPercentageUpdated);
Button beginningButton = Root.Q<Button>("BeginningButton");
beginningButton.clicked += () => { m_songStartPercentageSlider.value = 0; };
Button middleButton = Root.Q<Button>("MiddleButton");
middleButton.clicked += () => { m_songStartPercentageSlider.value = 0.5f; };
Button endbutton = Root.Q<Button>("EndButton");
endbutton.clicked += () => { m_songStartPercentageSlider.value = 0.9f; };
m_useLikedSongsToggle= Root.Q<Toggle>("LikedSongsToggle");
m_useLikedSongsToggle.RegisterValueChangedCallback(OnUseLikedSongsToggled);
m_useSongsInPlaylistToggle = Root.Q<Toggle>("SongsOnPlaylistToggle");
m_useSongsInPlaylistToggle.RegisterValueChangedCallback(OnUseSongsInPlaylistsToggled);
m_timeRangeOptions = Root.Q<RadioButtonGroup>("TimeRangeOptions");
m_timeRangeOptions.SetValueWithoutNotify((int)m_settings.SelectedTopSongsTimeRange);
m_timeRangeOptions.RegisterValueChangedCallback(OnTimeRangeUpdated);
m_useTopSongsToggle = Root.Q<Toggle>("TopPlayedSongsToggle");
m_useTopSongsToggle.RegisterValueChangedCallback(OnUseTopSongsToggled);
m_startGameButton = Root.Q<Button>("StartButton");
m_startGameButton.clicked += StartGame;
}
void OnSongStartPercentageUpdated(ChangeEvent<float> evt)
{
m_settings.SongStartPercentage = Mathf.Clamp01(evt.newValue);
m_percentageLabel.text = FormatPercentageString(evt.newValue);
}
void OnUseTopSongsToggled(ChangeEvent<bool> evt)
{
m_settings.UseTopSongs = evt.newValue;
m_timeRangeOptions.SetVisibility(evt.newValue, false);
ValidateStartButton();
}
void OnTimeRangeUpdated(ChangeEvent<int> evt)
{
m_settings.SelectedTopSongsTimeRange = m_timeRangeMap[evt.newValue];
Logger.LogFormat("Changed time range to {0}", m_settings.SelectedTopSongsTimeRange);
}
void OnUseLikedSongsToggled(ChangeEvent<bool> evt)
{
m_settings.UseLikedSongs = evt.newValue;
ValidateStartButton();
}
void OnUseSongsInPlaylistsToggled(ChangeEvent<bool> evt)
{
m_settings.UseSongsInPlaylists = evt.newValue;
ValidateStartButton();
}
void ValidateStartButton()
{
bool enabled = m_settings.UseLikedSongs || m_settings.UseTopSongs || m_settings.UseSongsInPlaylists;
m_startGameButton.SetEnabled(enabled);
}
void OnHostChanged(string newHostId)
{
bool isHost = AuthenticationService.Instance.PlayerId == LobbyManager.Instance.JoinedLobby.HostId;
if (!isHost)
return;
UpdateHostObjects(isHost);
}
async void StartGame()
{
if (m_startingGame)
return;
m_startingGame = true;
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
NetworkManager.Singleton.OnServerStopped += OnServerStopped;
var allocation = await Helpers.NetworkHelpers.CreateRelayAsHost(LobbyManager.Instance.JoinedLobby);
var gameSessionManager = Helpers.NetworkObjectHelpers.SpawnNetworkObject<GameSessionManager>(m_gameSessionManagerPrefab, false);
GameSessionManager.Instance.SpotifySessionSettings.Value = m_settings;
//manually spawn the host's player object rather than automatic from network manager
GameSessionManager.Instance.SpawnPlayer(NetworkManager.Singleton.LocalClientId);
NetworkManager.Singleton.SceneManager.LoadScene("WhoseSongIsItScene", LoadSceneMode.Single);
await Helpers.NetworkHelpers.SetLobbyAllocationId(LobbyManager.Instance.JoinedLobby, allocation);
}
void OnClientConnected(ulong clientId)
{
if (clientId == NetworkManager.Singleton.LocalClientId)
return;
GameSessionManager.Instance.SpawnPlayer(clientId);
}
void OnServerStopped(bool stopped)
{
NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnected;
}
async void ConnectToRelay(string relayJoinCode)
{
await Helpers.NetworkHelpers.JoinRelay(relayJoinCode, LobbyManager.Instance.JoinedLobby, AuthenticationService.Instance.PlayerId);
}
async void LeaveLobby()
{
//tell server i'm leaving
//if i was host, close the lobby or try to transfer host
//change back to menu screen
try
{
await LobbyManager.Instance.LeaveLobby();
}
catch (LobbyServiceException ex)
{
Logger.LogErrorFormat("Error leaving lobby: {0}", ex.Message);
}
}
void UpdatePlayerCount(int count)
{
m_playerCount.text = string.Format("Players: {0}/{1}", count, LobbyManager.Instance.JoinedLobby.MaxPlayers);
}
string FormatPercentageString(float percentage)
{
return string.Format("{0}%", Mathf.FloorToInt(percentage * 100));
}
public void Cleanup(DetachFromPanelEvent evt)
{
LobbyManager.Instance.OnRelayConnectionCreated -= ConnectToRelay;
LobbyManager.Instance.OnLobbyPlayersChanged -= UpdatePlayerNames;
LobbyManager.Instance.OnLobbyHostChanged -= OnHostChanged;
Root.UnregisterCallback<DetachFromPanelEvent>(Cleanup);
}
}
}