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()); }
private void ReceivedFromServer(string[] _data) { lastCommands.Clear(); foreach (string data in _data) { try { ServerCommand command = JsonUtility.FromJson <ServerCommand>(data); lastCommands.Add(command); if (command.commandType == ServerCommandType.SetPlayerInfos) { if (command.scoreboardScoreFormat != null) { scoreboardScoreFormat = command.scoreboardScoreFormat; } _playerInfos.Clear(); foreach (string playerStr in command.playerInfos) { PlayerInfo player = JsonUtility.FromJson <PlayerInfo>(playerStr); if (!String.IsNullOrEmpty(player.playerAvatar) && Config.Instance.ShowAvatarsInGame) { 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); } localPlayerIndex = FindIndexInList(_playerInfos, localPlayerInfo); if (Config.Instance.ShowAvatarsInGame) { 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>()); } } List <PlayerInfo> _playerInfosByID = _playerInfos.OrderBy(x => x.playerId).ToList(); for (int i = 0; i < _playerInfos.Count; i++) { _avatars[i].SetPlayerInfo(_playerInfosByID[i], (i - FindIndexInList(_playerInfosByID, localPlayerInfo)) * 3f, localPlayerInfo.Equals(_playerInfosByID[i])); } } catch (Exception e) { Console.WriteLine($"AVATARS EXCEPTION: {e}"); } } if (_playerInfos.Count <= 5) { for (int i = 0; i < _playerInfos.Count; i++) { scoreDisplays[i].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } for (int i = _playerInfos.Count; i < scoreDisplays.Count; i++) { scoreDisplays[i].UpdatePlayerInfo(null, 0); } } else { if (localPlayerIndex < 3) { for (int i = 0; i < 5; i++) { scoreDisplays[i].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } } else if (localPlayerIndex > _playerInfos.Count - 3) { for (int i = _playerInfos.Count - 5; i < _playerInfos.Count; i++) { scoreDisplays[i - (_playerInfos.Count - 5)].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } } else { for (int i = localPlayerIndex - 2; i < localPlayerIndex + 3; i++) { scoreDisplays[i - (localPlayerIndex - 2)].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } } } if (PlayerInfosReceived != null) { PlayerInfosReceived.Invoke(_playerInfos); } } else if (command.commandType == ServerCommandType.Kicked) { Console.WriteLine($"You were kicked! Reason: {command.kickReason}"); GameSongController controller = Resources.FindObjectsOfTypeAll <GameSongController>().FirstOrDefault(); ReflectionUtil.SetPrivateField(controller, "_songDidFinish", true); controller.SendSongDidFinishEvent(); } } catch (Exception e) { Console.WriteLine("EXCEPTION ON RECEIVED: " + e); } } StartCoroutine(ReceiveFromServerCoroutine()); }
private void ReceivedFromServer(string[] _data) { foreach (string data in _data) { try { ServerCommand command = JsonUtility.FromJson <ServerCommand>(data); if (command.commandType == ServerCommandType.SetPlayerInfos) { _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); } lastLocalPlayerIndex = localPlayerIndex; localPlayerIndex = FindIndexInList(_playerInfos, localPlayerInfo); 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>()); } } List <PlayerInfo> _playerInfosByID = _playerInfos.OrderBy(x => x.playerId).ToList(); for (int i = 0; i < _playerInfos.Count; i++) { _avatars[i].SetPlayerInfo(_playerInfosByID[i], (i - FindIndexInList(_playerInfosByID, localPlayerInfo)) * 3f, localPlayerInfo.Equals(_playerInfosByID[i])); } }catch (Exception e) { Console.WriteLine($"AVATARS EXCEPTION: {e}"); } if (_playerInfos.Count <= 5) { for (int i = 0; i < _playerInfos.Count; i++) { scoreDisplays[i].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } for (int i = _playerInfos.Count; i < scoreDisplays.Count; i++) { scoreDisplays[i].UpdatePlayerInfo(null, 0); } } else { if (localPlayerIndex < 3) { for (int i = 0; i < 5; i++) { scoreDisplays[i].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } } else if (localPlayerIndex > _playerInfos.Count - 3) { for (int i = _playerInfos.Count - 5; i < _playerInfos.Count; i++) { scoreDisplays[i - (_playerInfos.Count - 5)].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } } else { for (int i = localPlayerIndex - 2; i < localPlayerIndex + 3; i++) { scoreDisplays[i - (localPlayerIndex - 2)].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } } } if (lastLocalPlayerIndex != 0 && localPlayerIndex == 0) { TextMeshPro player1stPlaceText = ui.CreateWorldText(transform, "You are number one!"); player1stPlaceText.transform.position = new Vector3(0f, 1f, 12f); player1stPlaceText.fontSize = 10f; Destroy(player1stPlaceText.gameObject, 2f); } if (PlayerInfosReceived != null) { PlayerInfosReceived.Invoke(_playerInfos); } } } catch (Exception e) { Console.WriteLine("EXCEPTION ON RECEIVED: " + e); } } StartCoroutine(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()); }