Exemplo n.º 1
0
        //可读队列消息处理
        public void ReadQueuePro()
        {
            while (server.ReadQueue.Count != 0)
            {
                //读取并删除
                BaseMessage bs = server.ReadQueue.Dequeue();
                //Console.WriteLine(System.Text.Encoding.ASCII.GetString(bs.bytes));
                //SendMessage(bs.bytes, (EndPoint)bs.sender);

                //读取消息包号  消息协议号
                //BinaryReader br = new BinaryReader(new MemoryStream(bs.bytes), Encoding.UTF8);
                //UInt32 msgNum = br.ReadUInt32();
                //ushort msgID = br.ReadUInt16();

                //读取版本号 用户ID 消息包号  消息协议号
                UInt32 version = BitConverter.ToUInt32(bs.bytes, 0);
                if(version != server.gameVersion)
                    continue;

                //用户ID
                UInt64 _userid = BitConverter.ToUInt64(bs.bytes, 4);                
                    
                //消息包号
                UInt64 msgNum = BitConverter.ToUInt64(bs.bytes, 12);
                if (msgNum % 100 == 0) Console.WriteLine("userID:" + _userid.ToString() + ",包号:" + msgNum.ToString());//100个打印一次                            

                //消息ID
                UInt32 msgID = BitConverter.ToUInt32(bs.bytes, 20);                             

                //消息体
                MemoryStream ms = new MemoryStream();
                ms.Write(bs.bytes, 24, bs.bytes.Length - 24);
                ms.Position = 0;

                //特殊包处理
                if (m_dicUserinfo.ContainsKey(_userid))
                {
                    //心跳包
                    if ((CmdNum)msgID == CmdNum.CS_Ping)
                    {
                        OnPingPro(ms, _userid, bs);
                        continue;
                    }
                    //是否是重复包或者丢弃包
                    else if (msgNum <= m_dicUserinfo[_userid].msgRecvNum)
                    {
                        //if (_userid == 10002 && msgNum < m_dicUserinfo[_userid].msgRecvNum) Console.WriteLine("序号不对,userID:" + _userid.ToString() + ",包号:" + msgNum.ToString());//打印10002
                        continue;                        
                    }

                    m_dicUserinfo[_userid].msgRecvNum = msgNum;//消息接收号
                    m_dicUserinfo[_userid].heartTime = MsgResend.GetTime();//记录心跳
                }
                else if ((CmdNum)msgID == CmdNum.CS_Login)
                {
                    //登录包处理
                    OnLoginPro(ms, bs, msgNum);
                    continue;  
                }

                //消息处理
                switch ((CmdNum)msgID)
                {
                    case CmdNum.CS_PVP:
                        {
                            LoginMsg.C2SPVP msg = new LoginMsg.C2SPVP();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2SPVP>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            if (m_dicUserinfo[_userid].roomID != 0)//匹配中
                                break;

                            foreach (var item in BattleManager.Instance.dicRoomInfo)//查找房间
                            {
                                if ((BattleManager.EModel)msg.type == item.Value.matchType 
                                    && 
                                    item.Value.actorList.Count < BattleManager.Instance.dicRoomTypeInfo[item.Value.matchType] && !item.Value.begin)
                                {
                                    item.Value.time = MsgResend.GetUintTime();//记录开始匹配时间
                                    item.Value.actorList.Add(_userid);
                                    m_dicUserinfo[_userid].roomID = item.Key;
                                    //m_dicUserinfo[_userid].hp = 100;
                                    m_dicUserinfo[_userid].currHp = m_dicUserinfo[_userid].hp;
                                    switch (item.Value.matchType)
                                    {
                                        case BattleManager.EModel.PVP_1V1:
                                        case BattleManager.EModel.PVP_2V2:
                                        case BattleManager.EModel.PVP_3V3:
                                            {
                                                m_dicUserinfo[_userid].friendly = !m_dicUserinfo[item.Value.actorList[item.Value.actorList.Count - 2]].friendly;
                                            }                                            
                                            break;
                                        case BattleManager.EModel.PVE_1:
                                        case BattleManager.EModel.PVE_2:
                                        case BattleManager.EModel.PVE_3:
                                            {
                                                m_dicUserinfo[_userid].friendly = true;
                                            }
                                            break;
                                        default:
                                            break;
                                    }                                        
                                }
                            }

                            //建立房间
                            if (m_dicUserinfo[_userid].roomID == 0)
                            {
                                BattleManager.Instance.roomNum++;
                                BattleManager.RoomInfo roomInfo = new BattleManager.RoomInfo();
                                roomInfo.roomID = BattleManager.Instance.roomNum;
                                roomInfo.roomMaster = _userid;
                                roomInfo.matchType = (BattleManager.EModel)msg.type;
                                roomInfo.actorList.Add(_userid);
                                roomInfo.time = MsgResend.GetUintTime();//记录开始匹配时间
                                BattleManager.Instance.dicRoomInfo.Add(roomInfo.roomID, roomInfo);

                                m_dicUserinfo[_userid].roomID = roomInfo.roomID;
                                //m_dicUserinfo[_userid].hp = 100;
                                m_dicUserinfo[_userid].currHp = m_dicUserinfo[_userid].hp;
                                m_dicUserinfo[_userid].friendly = true;
                                m_dicUserinfo[_userid].roomMaster = true;
                            }

                            if(m_dicUserinfo[_userid].roomID != 0)
                            {
                                if (BattleManager.Instance.dicRoomInfo[m_dicUserinfo[_userid].roomID].actorList.Count 
                                    == 
                                    BattleManager.Instance.dicRoomTypeInfo[BattleManager.Instance.dicRoomInfo[m_dicUserinfo[_userid].roomID].matchType])//人数满
                                {
                                    BattleManager.Instance.dicRoomInfo[m_dicUserinfo[_userid].roomID].begin = true;//房间开始游戏
                                    for (int i = 0; i < BattleManager.Instance.dicRoomInfo[m_dicUserinfo[_userid].roomID].actorList.Count; i++)//给i发房间的里人信息
                                    {
                                        LoginMsg.S2CPVP rmsg = new LoginMsg.S2CPVP();
                                        rmsg.roomID = m_dicUserinfo[_userid].roomID;
                                        rmsg.roomMaster = (_userid == BattleManager.Instance.dicRoomInfo[rmsg.roomID].roomMaster);
                                        rmsg.result = (int)BattleManager.Instance.dicRoomInfo[m_dicUserinfo[_userid].roomID].matchType;

                                        ulong iUserID = BattleManager.Instance.dicRoomInfo[m_dicUserinfo[_userid].roomID].actorList[i];//iID
                                        #region
                                        for (int j = 0; j < BattleManager.Instance.dicRoomInfo[m_dicUserinfo[_userid].roomID].actorList.Count; j++)
                                        {
                                            ulong jUserID = BattleManager.Instance.dicRoomInfo[m_dicUserinfo[_userid].roomID].actorList[j];//jID
                                            if (iUserID != jUserID)//发除了i之外的人的信息
                                            {
                                                LoginMsg.PVPInfo info = new LoginMsg.PVPInfo();
                                                info.userId = jUserID;
                                                deploy.USER_INFO userinfo = GameConfig.Instance.userInfoData.GetData((int)jUserID);
                                                info.name = userinfo.name;
                                                info.type = (int)Enum.Parse(typeof(BaseActor.Type), userinfo.actor_type);
                                                if (m_dicUserinfo[iUserID].friendly == m_dicUserinfo[jUserID].friendly)//同类属性为友方
                                                    info.friendly = true;
                                                else
                                                    info.friendly = false;
                                                info.hp = m_dicUserinfo[jUserID].hp;
                                                info.modelType = userinfo.model_type;
                                                //填写枪信息                            
                                                string[] use_gun = userinfo.use_gun.Split(',');
                                                string[] gun_id = userinfo.gun_id.Split(',');
                                                for (int k = 0; k < gun_id.Length; k++)
                                                {
                                                    //枪信息
                                                    int gunid = Convert.ToInt32(gun_id[k]);
                                                    bool use = Convert.ToBoolean(use_gun[k]);
                                                    deploy.GUN_INFO gf = GameConfig.Instance.gunInfoData.GetData(gunid);

                                                    LoginMsg.GunInfo guninfo = new LoginMsg.GunInfo();
                                                    info.GunInfo.Add(guninfo);
                                                    guninfo.id = gunid;
                                                    guninfo.use = use;
                                                    guninfo.experience = gf.experience;
                                                    guninfo.quality = (int)Enum.Parse(typeof(Gun.Quality), gf.quality);
                                                    guninfo.count = gf.count;
                                                    guninfo.bullet_quality = gf.bullet_quality;
                                                    guninfo.caliber = gf.caliber;
                                                    guninfo.speed = gf.speed;
                                                    guninfo.frequency = gf.frequency;
                                                    guninfo.accurate = gf.accurate;
                                                    guninfo.range = gf.range;
                                                    guninfo.recoil = gf.recoil;
                                                    guninfo.weight = gf.weight;
                                                    guninfo.swap_speed = gf.swap_speed;
                                                    guninfo.aim_scale = gf.aim_scale;
                                                }
                                                //
                                                rmsg.pvpInfo.Add(info);                                                
                                            }
                                        }
                                        #endregion
                                        LoginServerManager.Instance.Server.SendMessage(iUserID, CmdNum.SC_PVP, rmsg, m_dicUserinfo[iUserID].sender, LoginServerManager.Instance.sendTime.interval_1, LoginServerManager.Instance.sendTime.count_1, false);
                                    }
                                }                                       
                            }                            
                        }
                        break;
                    case CmdNum.CS_Actor_Animation:
                        {
                            LoginMsg.C2S_Actor_Animation msg = new LoginMsg.C2S_Actor_Animation();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_Actor_Animation>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            if (msg.num <= m_dicUserinfo[_userid].recvAnimationNum)
                                break;
                            m_dicUserinfo[_userid].recvAnimationNum = msg.num;

                            if (m_dicUserinfo[_userid].currHp == 0)
                                break;

                            //m_dicUserinfo[_userid].msgRecvNum = msgNum;//消息接收号

                            LoginMsg.S2C_Actor_Animation rmsg = new LoginMsg.S2C_Actor_Animation();
                            rmsg.animatiom = msg.animatiom;
                            rmsg.userId = _userid;
                            rmsg.num = msg.num;

                            if (BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                            {
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    if (item != _userid)
                                    {
                                        LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_Actor_Animation, rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_3, LoginServerManager.Instance.sendTime.count_3);
                                    }
                                }
                            }
                        }
                        break;
                    case CmdNum.CS_NPC_Animation:
                        {
                            LoginMsg.C2S_NPC_Animation msg = new LoginMsg.C2S_NPC_Animation();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_NPC_Animation>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            if (!BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                                break;

                            //不存在npc
                            if (!BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo.ContainsKey(msg.npcId))
                                BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo.Add(msg.npcId, new UserInfo());

                            if (msg.num <= BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo[msg.npcId].recvAnimationNum)
                                break;
                            BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo[msg.npcId].recvAnimationNum = msg.num;//消息号

                            LoginMsg.S2C_NPC_Animation rmsg = new LoginMsg.S2C_NPC_Animation();
                            rmsg.npcId = msg.npcId;
                            rmsg.animatiom = msg.animatiom;
                            rmsg.roomNum = msg.roomNum;
                            rmsg.num = msg.num;

                            if (BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                            {
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    if (item != _userid)
                                    {
                                        LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_NPC_Animation, rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_3, LoginServerManager.Instance.sendTime.count_3);
                                    }
                                }
                            }
                        }
                        break;
                    case CmdNum.CS_Actor_Move:
                        {
                            LoginMsg.C2S_Actor_Move msg = new LoginMsg.C2S_Actor_Move();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_Actor_Move>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            if (msg.num <= m_dicUserinfo[_userid].recvMoveNum)
                                break;
                            m_dicUserinfo[_userid].recvMoveNum = msg.num;

                            if (m_dicUserinfo[_userid].currHp == 0)
                                break;

                            //if (_userid == 10002)
                            //{
                            //    int timeR = (int)((msg.time - delateTime) * 1000.0f);
                            //    delateTime = msg.time;

                            //    if (msg.moveState == 0) Console.WriteLine("state:stop" + ",ping:" + timeR.ToString());//打印10002
                            //    else Console.WriteLine("state:go" + ",ping:" + timeR.ToString());//打印10002
                            //}

                            //m_dicUserinfo[_userid].msgRecvNum = msgNum;//消息接收号

                            LoginMsg.S2C_Actor_Move rmsg = new LoginMsg.S2C_Actor_Move();
                            rmsg.x = msg.x;
                            rmsg.y = msg.y;
                            rmsg.z = msg.z;
                            rmsg.moveState = msg.moveState;
                            rmsg.dirState = msg.dirState;
                            rmsg.userId = _userid;
                            rmsg.num = msg.num;

                            if (BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                            {
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    if (item != _userid)
                                    {
                                        LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_Actor_Move, rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_3, LoginServerManager.Instance.sendTime.count_3);
                                    }
                                }
                            }
                        }
                        break;
                    case CmdNum.CS_NPC_Move_Target:
                        {
                            LoginMsg.C2S_NPC_Move_Target msg = new LoginMsg.C2S_NPC_Move_Target();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_NPC_Move_Target>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            if (!BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                                break;

                            //不存在npc
                            if (!BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo.ContainsKey(msg.npcId))
                                BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo.Add(msg.npcId, new UserInfo());

                            if (msg.num <= BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo[msg.npcId].recvMoveNum)
                                break;
                            BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo[msg.npcId].recvMoveNum = msg.num;//消息号

                            LoginMsg.S2CNPC_Move_Target rmsg = new LoginMsg.S2CNPC_Move_Target();
                            rmsg.tarId = msg.tarId;
                            rmsg.npcId = msg.npcId;
                            rmsg.num = msg.num;

                            if (BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                            {
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    if (item != _userid)
                                    {
                                        LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_NPC_Move_Target, rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_4, LoginServerManager.Instance.sendTime.count_4);
                                    }
                                }
                            }
                        }
                        break;
                    case CmdNum.CS_Actor_Rotation:
                        {
                            LoginMsg.C2S_Actor_Rotation msg = new LoginMsg.C2S_Actor_Rotation();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_Actor_Rotation>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            if (msg.num <= m_dicUserinfo[_userid].recvRotationNum)
                                break;
                            m_dicUserinfo[_userid].recvRotationNum = msg.num;

                            if (m_dicUserinfo[_userid].currHp == 0)
                                break;
                            
                            //m_dicUserinfo[_userid].msgRecvNum = msgNum;//消息接收号

                            LoginMsg.S2C_Actor_Rotation rmsg = new LoginMsg.S2C_Actor_Rotation();
                            rmsg.t = msg.t;
                            rmsg.y = msg.y;
                            rmsg.userId = _userid;
                            rmsg.num = msg.num;

                            if (BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                            {
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    if (item != _userid)
                                    {
                                        LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_Actor_Rotation, rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_3, LoginServerManager.Instance.sendTime.count_3);
                                    }
                                }
                            }
                        }
                        break;
                    case CmdNum.CS_Actor_Aim:
                        {
                            LoginMsg.C2S_Actor_Aim msg = new LoginMsg.C2S_Actor_Aim();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_Actor_Aim>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            if (msg.num <= m_dicUserinfo[_userid].recvAimNum)
                                break;
                            m_dicUserinfo[_userid].recvAimNum = msg.num;

                            if (m_dicUserinfo[_userid].currHp == 0)
                                break;

                            //m_dicUserinfo[_userid].msgRecvNum = msgNum;//消息接收号

                            LoginMsg.S2C_Actor_Aim rmsg = new LoginMsg.S2C_Actor_Aim();
                            rmsg.time = msg.time;
                            rmsg.model = msg.model;
                            rmsg.z = msg.z;
                            rmsg.userId = _userid;
                            rmsg.num = msg.num;

                            if (BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                            {
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    if (item != _userid)
                                    {
                                        LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_Actor_Aim, rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_3, LoginServerManager.Instance.sendTime.count_3);
                                    }
                                }
                            }
                        }
                        break;
                    case CmdNum.CS_Actor_Fire:
                        {
                            LoginMsg.C2S_Actor_Fire msg = new LoginMsg.C2S_Actor_Fire();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_Actor_Fire>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            if (msg.num <= m_dicUserinfo[_userid].recvFireNum)
                                break;
                            m_dicUserinfo[_userid].recvFireNum = msg.num;

                            if (m_dicUserinfo[_userid].currHp == 0)
                                break;

                            //m_dicUserinfo[_userid].msgRecvNum = msgNum;//消息接收号

                            LoginMsg.S2C_Actor_Fire rmsg = new LoginMsg.S2C_Actor_Fire();
                            rmsg.x = msg.x;
                            rmsg.y = msg.y;
                            rmsg.z = msg.z;
                            rmsg.userId = _userid;
                            rmsg.num = msg.num;

                            if (BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                            {
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    if (item != _userid)
                                    {
                                        //m_dicUserinfo[item].msgSendNum++;
                                        //for (int i = 0; i < 3; i++)
                                        //{
                                        //    LoginServerManager.Instance.Server.SendMessage(m_dicUserinfo[item].msgSendNum, item, CmdNum.SC_PVP_Fire, rmsg, m_dicUserinfo[item].sender);
                                        //}
                                        LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_Actor_Fire, rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_3, LoginServerManager.Instance.sendTime.count_3);
                                    }
                                }
                            }
                        }
                        break;
                    case CmdNum.CS_NPC_Fire:
                        {
                            LoginMsg.C2S_NPC_Fire msg = new LoginMsg.C2S_NPC_Fire();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_NPC_Fire>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            if (!BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                                break;

                            //不存在npc
                            if (!BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo.ContainsKey(msg.npcId))
                                BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo.Add(msg.npcId, new UserInfo());

                            if (msg.num <= BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo[msg.npcId].recvFireNum)
                                break;
                            BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo[msg.npcId].recvFireNum = msg.num;//消息号

                            LoginMsg.S2C_NPC_Fire rmsg = new LoginMsg.S2C_NPC_Fire();
                            rmsg.x = msg.x;
                            rmsg.y = msg.y;
                            rmsg.z = msg.z;
                            rmsg.npcId = msg.npcId;
                            rmsg.num = msg.num;
                            msg.roomNum = msg.roomNum;

                            if (BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                            {
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    if (item != _userid)
                                    {
                                        //m_dicUserinfo[item].msgSendNum++;
                                        //for (int i = 0; i < 3; i++)
                                        //{
                                        //    LoginServerManager.Instance.Server.SendMessage(m_dicUserinfo[item].msgSendNum, item, CmdNum.SC_PVP_Fire, rmsg, m_dicUserinfo[item].sender);
                                        //}
                                        LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_NPC_Fire, rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_3, LoginServerManager.Instance.sendTime.count_3);
                                    }
                                }
                            }
                        }
                        break;
                    case CmdNum.CS_Actor_Hit:
                        {
                            LoginMsg.C2S_Actor_Hit msg = new LoginMsg.C2S_Actor_Hit();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_Actor_Hit>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                    break;

                            if (!BattleManager.Instance.dicRoomInfo.ContainsKey(msg.roomNum))
                                break;

                            if (_userid == msg.userId)//非npc攻击者
                            {
                                if (msg.num <= m_dicUserinfo[_userid].recvHitNum)
                                    break;
                                m_dicUserinfo[_userid].recvHitNum = msg.num;//消息号

                                if (m_dicUserinfo[_userid].currHp == 0)
                                    break;
                            }
                            else//npc攻击者
                            {
                                //不存在npc
                                if (!BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo.ContainsKey(msg.userId))
                                    BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo.Add(msg.userId, new UserInfo());

                                if (msg.num <= BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo[msg.userId].recvHitNum)
                                    break;
                                BattleManager.Instance.dicRoomInfo[msg.roomNum].dicNpcInfo[msg.userId].recvHitNum = msg.num;//消息号
                            }

                            bool die = false;
                            if (m_dicUserinfo.ContainsKey(msg.tarId))//非NPC被击者减血
                            {
                                Console.WriteLine("玩家ID:" + msg.tarId.ToString() + "玩家消息号:" + msg.num.ToString() + ":掉血:" + msg.date.ToString());

                                if (m_dicUserinfo[msg.tarId].currHp > 0)
                                {
                                    if (m_dicUserinfo[msg.tarId].currHp >= msg.date)
                                        m_dicUserinfo[msg.tarId].currHp -= msg.date;
                                    else
                                        m_dicUserinfo[msg.tarId].currHp = 0;
                                }
                                else
                                {
                                    break;
                                }

                                if (m_dicUserinfo[msg.tarId].currHp == 0) die = true;
                            }
                            else //NPC被击者减血
                            {
                                if (msg.date == 0) die = true;
                            }

                            if (die)
                            {
                                //死亡
                                LoginMsg.S2C_Actor_Die _rmsg = new LoginMsg.S2C_Actor_Die();
                                _rmsg.tarId = msg.tarId;
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    //发给所有人
                                    LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_Actor_Die, _rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_5, LoginServerManager.Instance.sendTime.count_5);
                                }
                            }
                            else
                            {
                                //掉血
                                LoginMsg.S2C_Actor_Hit rmsg = new LoginMsg.S2C_Actor_Hit();
                                rmsg.userId = msg.userId;
                                rmsg.tarId = msg.tarId;
                                if (m_dicUserinfo.ContainsKey(msg.tarId))//非NPC被击者减血
                                {
                                    rmsg.date = m_dicUserinfo[msg.tarId].currHp;
                                }
                                else//NPC被击者减血
                                    rmsg.date = msg.date;

                                rmsg.num = msg.num;
                                foreach (var item in BattleManager.Instance.dicRoomInfo[msg.roomNum].actorList)
                                {
                                    if (item != _userid)
                                    {
                                        //m_dicUserinfo[item].msgSendNum++;
                                        //for (int i = 0; i < 3; i++)
                                        //{
                                        //    LoginServerManager.Instance.Server.SendMessage(m_dicUserinfo[item].msgSendNum, item, CmdNum.SC_PVP_Hit, rmsg, m_dicUserinfo[item].sender);
                                        //}
                                        LoginServerManager.Instance.Server.SendMessage(item, CmdNum.SC_Actor_Hit, rmsg, m_dicUserinfo[item].sender, LoginServerManager.Instance.sendTime.interval_3, LoginServerManager.Instance.sendTime.count_3);
                                    }
                                }
                            }
                        }
                        break;                   
                    case CmdNum.CS_ExitRoom:
                        {
                            LoginMsg.C2S_ExitRoom msg = new LoginMsg.C2S_ExitRoom();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.C2S_ExitRoom>(ms);
                            ms.Close();

                            if (!m_dicUserinfo.ContainsKey(_userid))
                                break;

                            //m_dicUserinfo[_userid].msgRecvNum = msgNum;//消息接收号

                            BattleManager.Instance.RemoveRoomUserAndSendMsg(_userid, msg.RoomId);//删除房间玩家并发消息                           
                        }
                        break;
                    default:
                        break;
                }
            }
        }
Exemplo n.º 2
0
        //可读队列消息处理
        public void ReadQueuePro()
        {
            while (server.ReadQueue.Count != 0)
            {
                //读取并删除
                BaseMessage bs = server.ReadQueue.Dequeue();
                //Console.WriteLine(System.Text.Encoding.ASCII.GetString(bs.bytes));
                //SendMessage(bs.bytes, (EndPoint)bs.sender);

                //读取版本号 用户ID 消息包号  消息协议号
                UInt32 version = BitConverter.ToUInt32(bs.bytes, 0);
                if (version != server.gameVersion)
                    continue;

                //用户ID
                UInt64 _userid = BitConverter.ToUInt64(bs.bytes, 4);

                //消息包号
                UInt64 msgNum = BitConverter.ToUInt64(bs.bytes, 12);               

                //消息ID
                UInt32 msgID = BitConverter.ToUInt32(bs.bytes, 20);

                //消息体
                MemoryStream ms = new MemoryStream();
                ms.Write(bs.bytes, 24, bs.bytes.Length - 24);
                ms.Position = 0;

                BaseActor baseactor = ActorManager.Instance.GetActor(_userid);
                if (baseactor != null)
                {
                    //心跳包
                    if ((ICmdNum.CmdNum)msgID == ICmdNum.CmdNum.SC_Ping)
                    {
                        OnPingPro(ms);
                        continue;
                    }
                    //是否是重复包或者丢弃包
                    else if (msgNum <= baseactor.info.msgRecvNum)
                        continue;

                    baseactor.info.msgRecvNum = msgNum;//消息接收号
                }
                else if ((ICmdNum.CmdNum)msgID == ICmdNum.CmdNum.SC_Login)
                {
                    //登录包处理
                    OnLoginPro(ms, _userid);
                    continue;  
                }

                //消息处理
                switch ((ICmdNum.CmdNum)msgID)
                {
                    case ICmdNum.CmdNum.SC_PVP:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Main)//非主界面丢包
                                break;

                            LoginMsg.S2CPVP msg = new LoginMsg.S2CPVP();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2CPVP>(ms);
                            ms.Close();

                            if (msg.result == -1)
                                break;

                            server.msgResend.RemoveMsg(ICmdNum.CmdNum.CS_PVP);//停止重发

                            Global.Instance.gameState = Global.EGameState.Battle;

                            Global.Instance.PVPMsg = msg;

                            Global.Instance.main.MainUI.GetComponent<MainUI>().OnBattle();
                        }
                        break;                    
                    case ICmdNum.CmdNum.SC_Actor_Animation:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;
                            //packetCount++;
                            //NGUIDebug.LogToScreen("SC_PVP_Move:" + packetCount.ToString());

                            LoginMsg.S2C_Actor_Animation msg = new LoginMsg.S2C_Actor_Animation();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_Actor_Animation>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;

                            BaseActor tarBaseactor = ActorManager.Instance.GetActor(msg.userId);
                            if (tarBaseactor == null)
                                break;

                            if (msg.num <= tarBaseactor.info.recvAnimationNum)
                                break;
                            tarBaseactor.info.recvAnimationNum = msg.num;

                            //if ((BaseActor.MoveState)msg.moveState == BaseActor.MoveState.Walk_Stop) NGUIDebug.LogToScreen("is Stand");
                            switch (msg.animatiom)
                            {
                                case "SwapGun":
                                    tarBaseactor.info.obj.GetComponent<AnimationControl>().SwapGun();
                                    break;
                                case "SwapBullet":
                                    tarBaseactor.info.obj.GetComponent<AnimationControl>().SwapBullet();
                                    break;
                                default:
                                    break;
                            }                            
                        }
                        break;
                    case ICmdNum.CmdNum.SC_NPC_Animation:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;
                            //packetCount++;
                            //NGUIDebug.LogToScreen("SC_PVP_Move:" + packetCount.ToString());

                            LoginMsg.S2C_NPC_Animation msg = new LoginMsg.S2C_NPC_Animation();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_NPC_Animation>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;

                            BaseActor npcBaseactor = ActorManager.Instance.GetActor(msg.npcId);
                            if (npcBaseactor == null)
                                break;

                            if (msg.num <= npcBaseactor.info.recvAnimationNum)
                                break;
                            npcBaseactor.info.recvAnimationNum = msg.num;

                            switch (msg.animatiom)
                            {
                                case "SwapGun":
                                    npcBaseactor.info.obj.GetComponent<AnimationControl>().SwapGun();
                                    break;
                                case "SwapBullet":
                                    npcBaseactor.info.obj.GetComponent<AnimationControl>().SwapBullet();
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case ICmdNum.CmdNum.SC_Actor_Move:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;
                            //packetCount++;
                            //NGUIDebug.LogToScreen("SC_PVP_Move:" + packetCount.ToString());

                            LoginMsg.S2C_Actor_Move msg = new LoginMsg.S2C_Actor_Move();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_Actor_Move>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;                           

                            BaseActor tarBaseactor = ActorManager.Instance.GetActor(msg.userId);
                            if (tarBaseactor == null)
                                break;

                            if (msg.num <= tarBaseactor.info.recvMoveNum)
                                break;
                            tarBaseactor.info.recvMoveNum = msg.num;

                            //if ((BaseActor.MoveState)msg.moveState == BaseActor.MoveState.Walk_Stop) NGUIDebug.LogToScreen("is Stand");
                            tarBaseactor.moveProMsgControl.MoveMsgPro(tarBaseactor,msg);
                        }
                        break;
                    case ICmdNum.CmdNum.SC_NPC_Move_Target:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;
                            //packetCount++;
                            //NGUIDebug.LogToScreen("SC_PVP_Move:" + packetCount.ToString());

                            LoginMsg.S2CNPC_Move_Target msg = new LoginMsg.S2CNPC_Move_Target();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2CNPC_Move_Target>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;

                            BaseActor npcBaseactor = ActorManager.Instance.GetActor(msg.npcId);
                            BaseActor tarBaseactor = ActorManager.Instance.GetActor(msg.tarId);
                            if (npcBaseactor == null || tarBaseactor == null)
                                break;

                            if (msg.num <= npcBaseactor.info.recvMoveNum)
                                break;
                            npcBaseactor.info.recvMoveNum = msg.num;

                            //if ((BaseActor.MoveState)msg.moveState == BaseActor.MoveState.Walk_Stop) NGUIDebug.LogToScreen("is Stand");
                            npcBaseactor.info.obj.GetComponent<NavControlActor>().MoveMsgPro(tarBaseactor);
                            Debug.Log("SC_NPC_Move_Target");
                        }
                        break;
                    case ICmdNum.CmdNum.SC_Actor_Rotation:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;
                            //packetCount++;
                            //NGUIDebug.LogToScreen("SC_PVP_Move:" + packetCount.ToString());

                            LoginMsg.S2C_Actor_Rotation msg = new LoginMsg.S2C_Actor_Rotation();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_Actor_Rotation>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;

                            BaseActor tarBaseactor = ActorManager.Instance.GetActor(msg.userId);
                            if (tarBaseactor == null)
                                break;

                            if (msg.num <= tarBaseactor.info.recvRotationNum)
                                break;
                            tarBaseactor.info.recvRotationNum = msg.num;

                            tarBaseactor.rotationProMsgControl.RotationMsgPro(tarBaseactor, msg);
                        }
                        break;
                    case ICmdNum.CmdNum.SC_Actor_Aim:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;
                            //packetCount++;
                            //NGUIDebug.LogToScreen("SC_PVP_Move:" + packetCount.ToString());

                            LoginMsg.S2C_Actor_Aim msg = new LoginMsg.S2C_Actor_Aim();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_Actor_Aim>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;

                            BaseActor tarBaseactor = ActorManager.Instance.GetActor(msg.userId);
                            if (tarBaseactor == null)
                                break;

                            if (msg.num <= tarBaseactor.info.recvAimNum)
                                break;
                            tarBaseactor.info.recvAimNum = msg.num;

                            tarBaseactor.aimProMsgControl.AimMsgPro(tarBaseactor, msg);
                        }
                        break;
                    case ICmdNum.CmdNum.SC_Actor_Fire:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;

                            LoginMsg.S2C_Actor_Fire msg = new LoginMsg.S2C_Actor_Fire();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_Actor_Fire>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;                                

                            BaseActor tarBaseactor = ActorManager.Instance.GetActor(msg.userId);
                            if (tarBaseactor == null)
                                break;

                            if (msg.num <= tarBaseactor.info.recvFireNum)
                                break;
                            tarBaseactor.info.recvFireNum = msg.num;

                            Vector3 gunTarpos = new Vector3();
                            gunTarpos.x = msg.x;
                            gunTarpos.y = msg.y;
                            gunTarpos.z = msg.z;
                            tarBaseactor.info.m_GunList[tarBaseactor.info.useGunIndex].info.obj.GetComponent<GunScript>().firePoint.GetComponent<BulletGenerator>().Shoot(tarBaseactor, tarBaseactor.info.m_GunList[tarBaseactor.info.useGunIndex].info.obj.GetComponent<GunScript>().firePoint.transform.position, gunTarpos, false);//射击
                        }
                        break;
                    case ICmdNum.CmdNum.SC_NPC_Fire:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;

                            LoginMsg.S2C_NPC_Fire msg = new LoginMsg.S2C_NPC_Fire();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_NPC_Fire>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;

                            BaseActor npcBaseactor = ActorManager.Instance.GetActor(msg.npcId);
                            if (npcBaseactor == null)
                                break;

                            if (msg.num <= npcBaseactor.info.recvFireNum)
                                break;
                            npcBaseactor.info.recvFireNum = msg.num;

                            Vector3 gunTarpos = new Vector3();
                            gunTarpos.x = msg.x;
                            gunTarpos.y = msg.y;
                            gunTarpos.z = msg.z;
                            npcBaseactor.info.m_GunList[npcBaseactor.info.useGunIndex].info.obj.GetComponent<GunScript>().firePoint.GetComponent<BulletGenerator>().Shoot(npcBaseactor, npcBaseactor.info.m_GunList[npcBaseactor.info.useGunIndex].info.obj.GetComponent<GunScript>().firePoint.transform.position, gunTarpos, false);//射击
                        }
                        break;
                    case ICmdNum.CmdNum.SC_Actor_Hit:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;

                            LoginMsg.S2C_Actor_Hit msg = new LoginMsg.S2C_Actor_Hit();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_Actor_Hit>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;

                            BaseActor fireBaseactor = ActorManager.Instance.GetActor(msg.userId);
                            BaseActor tarBaseactor = ActorManager.Instance.GetActor(msg.tarId);
                            if (fireBaseactor == null || tarBaseactor == null)
                                break;

                            if (msg.num <= fireBaseactor.info.recvHitNum)//攻击者
                                break;
                            fireBaseactor.info.recvHitNum = msg.num;

                            tarBaseactor.info.currHp = msg.date;
                            tarBaseactor.info.obj.GetComponent<AnimationControl>().headUI.GetComponent<HeadUI>().uiSlider.value = (float)tarBaseactor.info.currHp / (float)tarBaseactor.info.hp;
                        }
                        break;                   
                    case ICmdNum.CmdNum.SC_Actor_Die:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;
                            //packetCount++;
                            //NGUIDebug.LogToScreen("SC_PVP_Move:" + packetCount.ToString());

                            LoginMsg.S2C_Actor_Die msg = new LoginMsg.S2C_Actor_Die();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_Actor_Die>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;

                            BaseActor tarBaseactor = ActorManager.Instance.GetActor(msg.tarId);
                            if (tarBaseactor == null)
                                break;

                            if (tarBaseactor.info.die)
                                break;

                            tarBaseactor.info.die = true;

                            //if (tarBaseactor.info.obj.GetComponent<AnimationControl>().IsPlayDie())
                            //    break;

                            //血量显示UI
                            tarBaseactor.info.currHp = 0;
                            tarBaseactor.info.obj.GetComponent<AnimationControl>().headUI.GetComponent<HeadUI>().uiSlider.value = (float)tarBaseactor.info.currHp / (float)tarBaseactor.info.hp;

                            //NPC停止导航
                            if (tarBaseactor.info.npc)
                            {
                                tarBaseactor.info.obj.GetComponent<NavControlActor>().curTarBaseActor = null;
                                tarBaseactor.info.obj.GetComponent<NavControlActor>().begin = false;
                                //tarBaseactor.info.obj.GetComponent<NavControlActor>().agent.Stop();
                            }
                            else
                            {
                                tarBaseactor.info.obj.GetComponent<AnimationControl>().Die();//动作
                            } 

                            if (tarBaseactor == ActorManager.Instance.controlActor)//是自己
                            {
                                if (ActorManager.Instance.AllFriendlyDie())
                                    Global.Instance.SetPopupBattleResultUI("失败");
                                else
                                    Global.Instance.SetPopupBattleResultUI("阵亡");

                                Global.Instance.popupBattleResultUI.GetComponent<PopupBattleResultUI>().cancelButton.transform.gameObject.SetActive(false);
                                Global.Instance.battle.BattleUI.GetComponent<BattleUI>().StopMove();
                            }
                            else
                            {
                                tarBaseactor.moveProMsgControl.move = false;

                                if (ActorManager.Instance.AllAnimyDie())
                                    Global.Instance.SetPopupBattleResultUI("胜利");
                            }
                        }
                        break;                    
                    case ICmdNum.CmdNum.SC_ExitRoom:
                        {
                            if (Global.Instance.gameState != Global.EGameState.Battle)
                                break;
                            //packetCount++;
                            //NGUIDebug.LogToScreen("SC_PVP_Move:" + packetCount.ToString());

                            LoginMsg.S2C_ExitRoom msg = new LoginMsg.S2C_ExitRoom();
                            msg = ProtoBuf.Serializer.Deserialize<LoginMsg.S2C_ExitRoom>(ms);
                            ms.Close();

                            if (baseactor == null)
                                break;

                            BaseActor tarBaseactor = ActorManager.Instance.GetActor(msg.userId);
                            if (tarBaseactor == null)
                                break;

                            Global.Instance.battle.pvpModel.ActorExitRoom(tarBaseactor);                            
                        }
                        break;
                    default:
                        break;
                }
            }
        }