private void DataReceived(string[] _data) { if (_data != null && _data.Length > 0) { foreach (string data in _data) { ServerCommand command = null; try { command = JsonUtility.FromJson <ServerCommand>(data); } catch (Exception e) { Console.WriteLine("Can't parse received JSON! Exception: " + e); } try { if (command != null) { switch (command.commandType) { case ServerCommandType.UpdateRequired: { _selectText.text = "Plugin version mismatch:\nServer: " + command.version + "\nClient: " + BSMultiplayerClient.version; BSMultiplayerClient._instance.DisconnectFromServer(); _loading = false; }; break; case ServerCommandType.SetServerState: { if (command.serverState == ServerState.Playing) { _loading = false; TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID); _selectedSongCell.gameObject.SetActive(true); (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(14f, 39f); _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>"; _selectedSongCell.author = _selectedSong.authorName; CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId); _selectedSongCell.coverImage = _songData.coverImage; _songPreviewPlayer.CrossfadeTo(_songData.previewAudioClip, (float)command.selectedSongPlayTime, (float)(command.selectedSongDuration - command.selectedSongPlayTime - 5f), 1f); } }; break; case ServerCommandType.SetLobbyTimer: { _loading = false; Console.WriteLine("Set timer text to " + command.lobbyTimer); _timerText.text = command.lobbyTimer.ToString(); if (_multiplayerLeaderboard != null && _viewControllers.IndexOf(_multiplayerLeaderboard) >= 0) { _viewControllers.Remove(_multiplayerLeaderboard); Destroy(_multiplayerLeaderboard.gameObject); _songPreviewPlayer.CrossfadeToDefault(); _selectedSongCell.gameObject.SetActive(false); } }; break; case ServerCommandType.SetSelectedSong: { Console.WriteLine("Set selected song " + command.selectedLevelID); _loading = false; if (_selectedSong == null) { try { _selectedSongDifficulty = command.selectedSongDifficlty; _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID); _selectText.text = "Next song:"; _selectedSongCell.gameObject.SetActive(true); (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(-25f, 0); _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>"; _selectedSongCell.author = _selectedSong.authorName; CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId); _selectedSongCell.coverImage = _songData.coverImage; PlayPreview(_songData); } catch (Exception e) { Console.WriteLine("EXCEPTION: " + e); } } Console.WriteLine("Done"); }; break; case ServerCommandType.StartSelectedSongLevel: { Console.WriteLine("Starting selected song! Song: " + _selectedSong.songName + ", Diff: " + ((LevelStaticData.Difficulty)_selectedSongDifficulty).ToString()); BSMultiplayerClient._instance.DataReceived -= DataReceived; GameplayOptions gameplayOptions = new GameplayOptions(); gameplayOptions.noEnergy = true; gameplayOptions.mirror = false; if (BSMultiplayerClient._instance._mainGameSceneSetupData != null) { BSMultiplayerClient._instance._mainGameSceneSetupData.SetData(_selectedSong.levelId, (LevelStaticData.Difficulty)_selectedSongDifficulty, null, null, 0f, gameplayOptions, GameplayMode.SoloStandard, null); BSMultiplayerClient._instance._mainGameSceneSetupData.TransitionToScene(0.7f); _selectedSong = null; return; } else { Console.WriteLine("SceneSetupData is null!"); } }; break; case ServerCommandType.DownloadSongs: { if (!AllSongsDownloaded(command.songsToDownload)) { StartCoroutine(DownloadSongs(command.songsToDownload)); BSMultiplayerClient._instance.DisconnectFromServer(); } }; break; case ServerCommandType.SetPlayerInfos: { if (command.serverState == ServerState.Playing) { if (_multiplayerLeaderboard != null) { TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); try { _multiplayerLeaderboard.SetLeaderboard(command.playerInfos.Select(x => JsonUtility.FromJson <PlayerInfo>(x)).ToArray()); } catch (Exception e) { Console.WriteLine("Leaderboard exception: " + e); } } } else { _playerInfos.Clear(); foreach (string playerStr in command.playerInfos) { PlayerInfo player = JsonUtility.FromJson <PlayerInfo>(playerStr); if (!String.IsNullOrEmpty(player.playerAvatar)) { byte[] avatar = Convert.FromBase64String(player.playerAvatar); player.rightHandPos = Serialization.ToVector3(avatar.Take(12).ToArray()); player.leftHandPos = Serialization.ToVector3(avatar.Skip(12).Take(12).ToArray()); player.headPos = Serialization.ToVector3(avatar.Skip(24).Take(12).ToArray()); player.rightHandRot = Serialization.ToQuaternion(avatar.Skip(36).Take(16).ToArray()); player.leftHandRot = Serialization.ToQuaternion(avatar.Skip(52).Take(16).ToArray()); player.headRot = Serialization.ToQuaternion(avatar.Skip(68).Take(16).ToArray()); } _playerInfos.Add(player); } try { if (_avatars.Count > _playerInfos.Count) { List <AvatarController> avatarsToRemove = new List <AvatarController>(); for (int i = _playerInfos.Count; i < _avatars.Count; i++) { avatarsToRemove.Add(_avatars[i]); } foreach (AvatarController avatar in avatarsToRemove) { _avatars.Remove(avatar); Destroy(avatar.gameObject); } } else if (_avatars.Count < _playerInfos.Count) { for (int i = 0; i < (_playerInfos.Count - _avatars.Count); i++) { _avatars.Add(new GameObject("Avatar").AddComponent <AvatarController>()); } } for (int i = 0; i < _playerInfos.Count; i++) { _avatars[i].SetPlayerInfo(_playerInfos[i], 0f, localPlayerInfo.Equals(_playerInfos[i])); } } catch (Exception e) { Console.WriteLine($"AVATARS EXCEPTION: {e}"); } } }; break; } _serverState = command.serverState; } else { Console.WriteLine("Server Command is null!"); } }catch (Exception e) { Console.WriteLine("Exception (parse switch): " + e); } } } StartCoroutine(BSMultiplayerClient._instance.ReceiveFromServerCoroutine()); }
public void SetLeaderboard(PlayerInfo[] _playerInfos) { _multiplayerLeaderboard.SetLeaderboard(_playerInfos); }
private void DataReceived(string[] _data) { if (_data != null && _data.Length > 0) { foreach (string data in _data) { ServerCommand command = null; try { command = JsonUtility.FromJson <ServerCommand>(data); } catch (Exception e) { Console.WriteLine("Can't parse received JSON! Exception: " + e); } try { if (command != null) { switch (command.commandType) { case ServerCommandType.UpdateRequired: { _selectText.text = "Plugin version mismatch:\nServer: " + command.version + "\nClient: " + BSMultiplayerMain.version; BSMultiplayerMain._instance.DisconnectFromServer(); _loading = false; }; break; case ServerCommandType.SetServerState: { if (command.serverState == ServerState.Playing) { _loading = false; TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID); _selectedSongCell.gameObject.SetActive(true); (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(14f, 39f); _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>"; _selectedSongCell.author = _selectedSong.authorName; CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId); _selectedSongCell.coverImage = _songData.coverImage; _songPreviewPlayer.CrossfadeTo(_songData.previewAudioClip, (float)command.selectedSongPlayTime, (float)(command.selectedSongDuration - command.selectedSongPlayTime - 5f), 1f); } }; break; case ServerCommandType.SetLobbyTimer: { _loading = false; Console.WriteLine("Set timer text to " + command.lobbyTimer); _timerText.text = command.lobbyTimer.ToString(); if (_multiplayerLeaderboard != null && _viewControllers.IndexOf(_multiplayerLeaderboard) >= 0) { _viewControllers.Remove(_multiplayerLeaderboard); Destroy(_multiplayerLeaderboard.gameObject); _songPreviewPlayer.CrossfadeToDefault(); _selectedSongCell.gameObject.SetActive(false); } }; break; case ServerCommandType.SetSelectedSong: { Console.WriteLine("Set selected song " + command.selectedLevelID); _loading = false; if (_selectedSong == null) { try { _selectedSongDifficulty = command.selectedSongDifficlty; _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID); _selectText.text = "Next song:"; _selectedSongCell.gameObject.SetActive(true); (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(-25f, 0); _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>"; _selectedSongCell.author = _selectedSong.authorName; CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId); _selectedSongCell.coverImage = _songData.coverImage; PlayPreview(_songData); } catch (Exception e) { Console.WriteLine("EXCEPTION: " + e); } } Console.WriteLine("Done"); }; break; case ServerCommandType.StartSelectedSongLevel: { Console.WriteLine("Starting selected song! Song: " + _selectedSong.songName + ", Diff: " + ((LevelStaticData.Difficulty)_selectedSongDifficulty).ToString()); BSMultiplayerMain._instance.DataReceived -= DataReceived; GameplayOptions gameplayOptions = new GameplayOptions(); gameplayOptions.noEnergy = true; gameplayOptions.mirror = false; if (BSMultiplayerMain._instance._mainGameSceneSetupData != null) { BSMultiplayerMain._instance._mainGameSceneSetupData.SetData(_selectedSong.levelId, (LevelStaticData.Difficulty)_selectedSongDifficulty, null, null, 0f, gameplayOptions, GameplayMode.SoloStandard, null); BSMultiplayerMain._instance._mainGameSceneSetupData.TransitionToScene(0.7f); _selectedSong = null; return; } else { Console.WriteLine("SceneSetupData is null!"); } }; break; case ServerCommandType.DownloadSongs: { if (!AllSongsDownloaded(command.songsToDownload)) { StartCoroutine(DownloadSongs(command.songsToDownload)); BSMultiplayerMain._instance.DisconnectFromServer(); } }; break; case ServerCommandType.SetPlayerInfos: { if (_multiplayerLeaderboard != null) { TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); try { _multiplayerLeaderboard.SetLeaderboard(command.playerInfos.Select(x => JsonUtility.FromJson <PlayerInfo>(x)).ToArray()); } catch (Exception e) { Console.WriteLine("Leaderboard exception: " + e); } } }; break; } } else { Console.WriteLine("Server Command is null!"); } } catch (Exception e) { Console.WriteLine("Exception (parse switch): " + e); } } } StartCoroutine(BSMultiplayerMain._instance.ReceiveFromServerCoroutine()); }
private void DataReceived(string[] _data) { if (_data != null && _data.Length > 0) { foreach (string data in _data) { ServerCommand command = null; try { command = JsonUtility.FromJson <ServerCommand>(data); } catch (Exception e) { Console.WriteLine("Can't parse received JSON! Exception: " + e); } try { if (command != null && BSMultiplayerClient._instance.IsConnected()) { _serverState = command.serverState; switch (command.commandType) { case ServerCommandType.UpdateRequired: { _selectText.text = "Plugin version mismatch:\nServer: " + command.version + "\nClient: " + BSMultiplayerClient.version; _timerText.text = ""; BSMultiplayerClient._instance.DataReceived -= DataReceived; BSMultiplayerClient._instance.DisconnectFromServer(); _loading = false; }; break; case ServerCommandType.SetServerState: { Console.WriteLine($"Server state is {command.serverState}"); if (_serverState == ServerState.Playing) { _loading = false; TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); _selectedSong = SongLoader.CustomLevels.First(x => x.levelID.Substring(0, 32) == command.selectedLevelID.Substring(0, 32)); _selectedSongCell.gameObject.SetActive(true); (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(18f, 39f); _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>"; _selectedSongCell.author = _selectedSong.songAuthorName; _selectedSongCell.coverImage = _selectedSong.coverImage; SongLoader.Instance.LoadAudioClipForLevel(_selectedSong, PlayPreview); CreateLeaderboard(); PushViewController(_multiplayerLeaderboard, true); } else if (_serverState == ServerState.Preparing) { if (_multiplayerLeaderboard != null) { _viewControllers.Remove(_multiplayerLeaderboard); Destroy(_multiplayerLeaderboard.gameObject); } UpdateSelectedSong(command); } else if (_serverState == ServerState.Voting) { if (_multiplayerLeaderboard != null) { _viewControllers.Remove(_multiplayerLeaderboard); Destroy(_multiplayerLeaderboard.gameObject); } CreateSongList(); PushViewController(_votingSongList, true); _votingSongList.SetSongs(serverSongs.ToList()); } }; break; case ServerCommandType.SetLobbyTimer: { _loading = false; Console.WriteLine("Set timer text to " + command.lobbyTimer); _timerText.text = command.lobbyTimer.ToString(); if (_multiplayerLeaderboard != null) { _viewControllers.Remove(_multiplayerLeaderboard); Destroy(_multiplayerLeaderboard.gameObject); if (_serverState == ServerState.Voting) { _songPreviewPlayer.CrossfadeToDefault(); _selectedSongCell.gameObject.SetActive(false); } } }; break; case ServerCommandType.SetSelectedSong: { if (command.serverState == ServerState.Preparing) { if (_votingSongList != null) { _viewControllers.Remove(_votingSongList); Destroy(_votingSongList.gameObject); } Console.WriteLine("Next song level id is " + command.selectedLevelID); UpdateSelectedSong(command); } }; break; case ServerCommandType.StartSelectedSongLevel: { Console.WriteLine("Removing avatars from lobby"); foreach (AvatarController avatar in _avatars) { Destroy(avatar.gameObject); } _avatars.Clear(); _selectedSong = SongLoader.CustomLevels.First(x => x.levelID.Substring(0, 32) == command.selectedLevelID.Substring(0, 32)); _selectedSongDifficulty = command.selectedSongDifficlty; Console.WriteLine("Starting selected song! Song: " + _selectedSong.songName + ", Diff: " + ((LevelDifficulty)_selectedSongDifficulty).ToString()); BSMultiplayerClient._instance.DataReceived -= DataReceived; GameplayOptions gameplayOptions = new GameplayOptions(); gameplayOptions.noEnergy = command.noFailMode; if (BSMultiplayerClient._instance._mainGameSceneSetupData != null) { BSMultiplayerClient._instance._mainGameSceneSetupData.Init(_selectedSong.GetDifficultyLevel((LevelDifficulty)_selectedSongDifficulty), gameplayOptions, GameplayMode.SoloStandard, 0f); BSMultiplayerClient._instance.BindToFinishEvent(); BSMultiplayerClient._instance._mainGameSceneSetupData.TransitionToScene(0.7f); _selectedSong = null; return; } else { Console.WriteLine("SceneSetupData is null!"); } }; break; case ServerCommandType.DownloadSongs: { if (!AllSongsDownloaded(command.songsToDownload)) { _timerText.text = ""; if (_votingSongList != null) { _viewControllers.Remove(_votingSongList); Destroy(_votingSongList.gameObject); } if (_multiplayerLeaderboard != null) { _viewControllers.Remove(_multiplayerLeaderboard); Destroy(_multiplayerLeaderboard.gameObject); } _songPreviewPlayer.CrossfadeToDefault(); _selectedSongCell.gameObject.SetActive(false); StartCoroutine(DownloadSongs(command.songsToDownload)); BSMultiplayerClient._instance.DataReceived -= DataReceived; BSMultiplayerClient._instance.DisconnectFromServer(); } else { serverSongs.Clear(); foreach (string song in command.songsToDownload) { serverSongs.Add(SongLoader.CustomLevels.FirstOrDefault(x => x.levelID.Substring(0, 32) == song.Substring(0, 32))); } } }; break; case ServerCommandType.SetPlayerInfos: { if (command.serverState == ServerState.Playing) { if (_multiplayerLeaderboard != null) { TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime); if (timeRemaining.Ticks >= 0) { _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00"); } else { _timerText.text = "RESULTS " + Math.Abs(timeRemaining.Seconds + 15).ToString("00"); } try { _multiplayerLeaderboard.SetLeaderboard(command.playerInfos.Select(x => JsonUtility.FromJson <PlayerInfo>(x)).ToArray()); } catch (Exception e) { Console.WriteLine("Leaderboard exception: " + e); } } } else { if (Config.Instance.ShowAvatarsInLobby) { _playerInfos.Clear(); foreach (string playerStr in command.playerInfos) { PlayerInfo player = JsonUtility.FromJson <PlayerInfo>(playerStr); if (!String.IsNullOrEmpty(player.playerAvatar)) { byte[] avatar = Convert.FromBase64String(player.playerAvatar); player.rightHandPos = Serialization.ToVector3(avatar.Take(12).ToArray()); player.leftHandPos = Serialization.ToVector3(avatar.Skip(12).Take(12).ToArray()); player.headPos = Serialization.ToVector3(avatar.Skip(24).Take(12).ToArray()); player.rightHandRot = Serialization.ToQuaternion(avatar.Skip(36).Take(16).ToArray()); player.leftHandRot = Serialization.ToQuaternion(avatar.Skip(52).Take(16).ToArray()); player.headRot = Serialization.ToQuaternion(avatar.Skip(68).Take(16).ToArray()); } _playerInfos.Add(player); } try { if (_avatars.Count > _playerInfos.Count) { List <AvatarController> avatarsToRemove = new List <AvatarController>(); for (int i = _playerInfos.Count; i < _avatars.Count; i++) { avatarsToRemove.Add(_avatars[i]); } foreach (AvatarController avatar in avatarsToRemove) { _avatars.Remove(avatar); Destroy(avatar.gameObject); } } else if (_avatars.Count < _playerInfos.Count) { for (int i = 0; i < (_playerInfos.Count - _avatars.Count); i++) { _avatars.Add(new GameObject("Avatar").AddComponent <AvatarController>()); } } for (int i = 0; i < _playerInfos.Count; i++) { _avatars[i].SetPlayerInfo(_playerInfos[i], 0f, localPlayerInfo.Equals(_playerInfos[i])); } } catch (Exception e) { Console.WriteLine($"AVATARS EXCEPTION: {e}"); } } } }; break; case ServerCommandType.Kicked: { _selectText.text = $"You were kicked{(string.IsNullOrEmpty(command.kickReason) ? "" : $"\nReason: {command.kickReason}")}"; _timerText.text = ""; if (_multiplayerLeaderboard != null && _viewControllers.IndexOf(_multiplayerLeaderboard) >= 0) { _viewControllers.Remove(_multiplayerLeaderboard); _multiplayerLeaderboard.gameObject.SetActive(false); _songPreviewPlayer.CrossfadeToDefault(); _selectedSongCell.gameObject.SetActive(false); } BSMultiplayerClient._instance.DataReceived -= DataReceived; BSMultiplayerClient._instance.DisconnectFromServer(); _loading = false; }; break; } } else { Console.WriteLine("Server Command is null!"); } }catch (Exception e) { Console.WriteLine("Exception (parse switch): " + e); } } } StartCoroutine(BSMultiplayerClient._instance.ReceiveFromServerCoroutine()); }