Ejemplo n.º 1
0
        public void KickPlayerResult(JObject jsonData)
        {
            if (jsonData == null)
            {
                throw new ArgumentNullException(nameof(jsonData));
            }
            if (PlayerManager.Instance.RoomOwner.UserID == _localPlayer.UserID)
            {
                _kickPlayerInterceptor.EndWaitResponse();
            }
            string result = (string)jsonData.SelectToken("Paras.Result");
            string info   = (string)jsonData.SelectToken("Paras.Info");

            if (result == NotifyConsts.CommonNotification.Succeed)
            {
                string kickPlayerId = (string)jsonData.SelectToken("Paras.KickPlayerId");
                if (kickPlayerId == _localPlayer.UserID)
                {
                    PlayerManager.Instance.RoomOwner = null;
                    PlayerManager.Instance.RemoveAllNetPlayer();
                    SendNotification(NotifyConsts.RoomNotification.KickPlayerResult, Tuple.Create <bool, string, string, List <PlayerVO> >(true, info, kickPlayerId, null), null);
                }
                else
                {
                    PlayerManager.Instance.RemoveNetPlayer(kickPlayerId);
                    var playerList = new List <PlayerVO>();
                    foreach (JToken jToken in jsonData.SelectTokens("Paras.RoomInfo.PlayerList").Children())
                    {
                        var playerId     = (string)jToken.SelectToken("PlayerId");
                        var playerName   = (string)jToken.SelectToken("PlayerName");
                        var playerLevel  = (string)jToken.SelectToken("PlayerLevel");
                        var prepareState = (bool)jToken.SelectToken("PrepareState");
                        //var playerTeam = (string)jToken.SelectToken("PlayerTeam");

                        var vehicleId        = (string)jToken.SelectToken("VehicleInfo.Id");
                        var vehicleName      = (string)jToken.SelectToken("VehicleInfo.Name");
                        var vehicleType      = (string)jToken.SelectToken("VehicleInfo.Type");
                        var vehicleAttack    = (float)jToken.SelectToken("VehicleInfo.Attack");
                        var vehicleDefend    = (float)jToken.SelectToken("VehicleInfo.Defend");
                        var vehicleMotility  = (float)jToken.SelectToken("VehicleInfo.Motility");
                        var vehicleMaxHealth = (float)jToken.SelectToken("VehicleInfo.MaxHealth");

                        var vehicle = new VehicleVO(vehicleId, vehicleName, Enum.TryParse(vehicleType, true, out VehicleType vt) ? vt : throw new InvalidCastException(nameof(vehicleType)), vehicleAttack, vehicleMotility, vehicleDefend, (int)vehicleMaxHealth, 0, null);

                        var player = new PlayerVO(playerId, playerName, int.Parse(playerLevel), vehicle)
                        {
                            PrepareState = prepareState,
                            //Team = Enum.TryParse(playerTeam, true, out Team team) ? team : throw new InvalidCastException(nameof(playerTeam))
                        };
                        playerList.Add(player); //用于界面显示
                    }
                    SendNotification(NotifyConsts.RoomNotification.KickPlayerResult, Tuple.Create <bool, string, string, List <PlayerVO> >(true, info, kickPlayerId, playerList), null);
                }
            }
            else
            {
                SendNotification(NotifyConsts.RoomNotification.KickPlayerResult, Tuple.Create <bool, string, string, List <PlayerVO> >(false, info, null, null), null);
            }
        }
Ejemplo n.º 2
0
        public void JoinRoomResult(JObject jsonData)
        {
            if (jsonData == null)
            {
                throw new ArgumentNullException(nameof(jsonData));
            }

            string result = (string)jsonData.SelectToken("Paras.Result");
            string info   = (string)jsonData.SelectToken("Paras.Info");

            if (result == NotifyConsts.CommonNotification.Succeed)
            {
                string joinPlayerId = (string)jsonData.SelectToken("Paras.JoinPlayerId");
                if (joinPlayerId == _localPlayer.UserID)
                {
                    _joinRoomInterceptor.EndWaitResponse();
                    var roomId    = (string)jsonData.SelectToken("Paras.RoomInfo.RoomId");
                    var roomName  = (string)jsonData.SelectToken("Paras.RoomInfo.RoomName");
                    var ownerId   = (string)jsonData.SelectToken("Paras.RoomInfo.OwnerId");
                    var ownerName = (string)jsonData.SelectToken("Paras.RoomInfo.OwnerName");
                    var roomMode  = (string)jsonData.SelectToken("Paras.RoomInfo.RoomMode");
                    var roomMap   = (string)jsonData.SelectToken("Paras.RoomInfo.RoomMap");
                    var playerNum = (int)jsonData.SelectToken("Paras.RoomInfo.PlayerNum");
                    //获取房间信息
                    var room = new RoomVO(roomId, roomName, ownerId, ownerName, Enum.TryParse(roomMode, true, out RoomMode roomModeType) ? roomModeType : throw new InvalidCastException(nameof(roomMode)), Enum.TryParse(roomMap, true, out RoomMap roomMapType) ? roomMapType : throw new InvalidCastException(nameof(roomMap)), playerNum);
                    //获取玩家列表
                    var playerList = new List <PlayerVO>();
                    foreach (JToken jToken in jsonData.SelectTokens("Paras.RoomInfo.PlayerList").Children())
                    {
                        var playerId     = (string)jToken.SelectToken("PlayerId");
                        var playerName   = (string)jToken.SelectToken("PlayerName");
                        var playerLevel  = (string)jToken.SelectToken("PlayerLevel");
                        var prepareState = (bool)jToken.SelectToken("PrepareState");
                        //var playerTeam = (string)jToken.SelectToken("PlayerTeam");

                        var vehicleId        = (string)jToken.SelectToken("VehicleInfo.Id");
                        var vehicleName      = (string)jToken.SelectToken("VehicleInfo.Name");
                        var vehicleType      = (string)jToken.SelectToken("VehicleInfo.Type");
                        var vehicleAttack    = (float)jToken.SelectToken("VehicleInfo.Attack");
                        var vehicleDefend    = (float)jToken.SelectToken("VehicleInfo.Defend");
                        var vehicleMotility  = (float)jToken.SelectToken("VehicleInfo.Motility");
                        var vehicleMaxHealth = (float)jToken.SelectToken("VehicleInfo.MaxHealth");
                        //获取玩家载具信息
                        var vehicle = new VehicleVO(vehicleId, vehicleName, Enum.TryParse(vehicleType, true, out VehicleType vt) ? vt : throw new InvalidCastException(nameof(vehicleType)), vehicleAttack, vehicleMotility, vehicleDefend, (int)vehicleMaxHealth, 0, null);
                        //获取玩家信息
                        var player = new PlayerVO(playerId, playerName, int.Parse(playerLevel), vehicle)
                        {
                            PrepareState = prepareState,
                            //Team = Enum.TryParse(playerTeam, true, out Team team) ? team : throw new InvalidCastException(nameof(playerTeam))
                        };
                        playerList.Add(player); //用于界面显示
                        if (player.UserID == ownerId)
                        {
                            PlayerManager.Instance.RoomOwner = player;
                        }
                        if (playerId != _localPlayer.UserID)
                        {
                            PlayerManager.Instance.AddNetPlayer(player);    //用于后台玩家数据
                        }
                    }

                    room.PlayerList = playerList;

                    SendNotification(NotifyConsts.LobbyNotification.JoinRoomResult, Tuple.Create(true, info), nameof(Tuple <bool, string>));
                    SendNotification(NotifyConsts.RoomNotification.InitRoomInfo, room, nameof(RoomVO));
                }
                else
                {
                    var      playerList = new List <PlayerVO>();
                    PlayerVO joinPlayer = null;
                    foreach (JToken jToken in jsonData.SelectTokens("Paras.RoomInfo.PlayerList").Children())
                    {
                        var playerId     = (string)jToken.SelectToken("PlayerId");
                        var playerName   = (string)jToken.SelectToken("PlayerName");
                        var playerLevel  = (string)jToken.SelectToken("PlayerLevel");
                        var prepareState = (bool)jToken.SelectToken("PrepareState");
                        //var playerTeam = (string)jToken.SelectToken("PlayerTeam");

                        var vehicleId        = (string)jToken.SelectToken("VehicleInfo.Id");
                        var vehicleName      = (string)jToken.SelectToken("VehicleInfo.Name");
                        var vehicleType      = (string)jToken.SelectToken("VehicleInfo.Type");
                        var vehicleAttack    = (float)jToken.SelectToken("VehicleInfo.Attack");
                        var vehicleDefend    = (float)jToken.SelectToken("VehicleInfo.Defend");
                        var vehicleMotility  = (float)jToken.SelectToken("VehicleInfo.Motility");
                        var vehicleMaxHealth = (float)jToken.SelectToken("VehicleInfo.MaxHealth");

                        var vehicle = new VehicleVO(vehicleId, vehicleName, Enum.TryParse(vehicleType, true, out VehicleType t) ? t : throw new InvalidCastException(nameof(vehicleType)), vehicleAttack, vehicleMotility, vehicleDefend, (int)vehicleMaxHealth, 0, null);

                        var player = new PlayerVO(playerId, playerName, int.Parse(playerLevel), vehicle)
                        {
                            PrepareState = prepareState,
                            //Team = Enum.TryParse(playerTeam, true, out Team team) ? team : throw new InvalidCastException(nameof(playerTeam))
                        };
                        if (player.UserID == joinPlayerId)
                        {
                            joinPlayer = player;
                        }
                        playerList.Add(player);
                    }
                    if (joinPlayer != null)
                    {
                        PlayerManager.Instance.AddNetPlayer(joinPlayer);
                    }
                    SendNotification(NotifyConsts.RoomNotification.NewPlayerJoinRoom, Tuple.Create(joinPlayer, playerList), nameof(Tuple <PlayerVO, List <PlayerVO> >));
                }
            }
            else
            {
                _joinRoomInterceptor.EndWaitResponse();
                SendNotification(NotifyConsts.LobbyNotification.JoinRoomResult, Tuple.Create(false, info), nameof(Tuple <bool, string>));
            }
        }