示例#1
0
        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());
        }
示例#2
0
        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());
        }