示例#1
0
        public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer)
        {
            MobaServer.LogInfo("处理玩家上线的请求");

            // 获取自身的Player数据
            DataBase.Model.Player player = UserManager.GetPlayer(peer.Username);
            if (player == null)
            {
                MobaServer.LogError(">>>>>>>>>> 数据异常 : PlayerOnlineHandler");
                return;
            }

            // 防止重复上线
            if (Caches.Player.IsOnline(player.Identification))
            {
                MobaServer.LogError(">>>>>>>>> Player:" + player.Name + " 已上线");
                return;
            }

            Caches.Player.Online(player.Identification, peer);

            OperationResponse response = new OperationResponse();

            // 上线通知好友
            if (!String.IsNullOrEmpty(player.FriendIdList))
            {
                string[] friends  = player.FriendIdList.Split(',');
                MobaPeer tempPeer = null;
                response.OperationCode = (byte)OperationCode.FriendStateChange;
                foreach (string friend in friends)
                {
                    if (string.IsNullOrEmpty(friend))
                    {
                        continue;
                    }
                    int id = int.Parse(friend);
                    if (!Caches.Player.IsOnline(id))
                    {
                        continue;
                    }
                    tempPeer = Caches.Player.GetPeer(id);

                    // 发送好友上线的消息
                    response.Parameters = new Dictionary <byte, object>();
                    DtoFriend dto = new DtoFriend(player.Identification, player.Name, true);
                    response[(byte)ParameterCode.DtoFriend] = JsonMapper.ToJson(dto);
                    tempPeer.SendOperationResponse(response, sendParameters);
                }
            }

            // 发送自身的玩家数据给客户端
            response.OperationCode = request.OperationCode;
            response.Parameters    = new Dictionary <byte, object>();
            response[(byte)ParameterCode.DtoPlayer] = JsonMapper.ToJson(player.ConvertToDot());
            peer.SendOperationResponse(response, sendParameters);
        }
示例#2
0
        public override void OnOperationRequest(OperationRequest request, SendParameters sendParameters, MobaPeer peer)
        {
            MobaServer.LogInfo("处理是否添加好友的反馈");

            // 是否同意
            bool isAccept = (bool)request.Parameters.ExTryGet((byte)ParameterCode.AcceptAddFriend);
            // 请求添加的玩家数据
            int    fromId   = (int)request.Parameters.ExTryGet((byte)ParameterCode.PlayerId);
            string fromName = (string)request.Parameters.ExTryGet((byte)ParameterCode.PlayerName);

            // 请求的客户端
            MobaPeer fromPeer = Caches.Player.GetPeer(fromId);

            OperationResponse response = new OperationResponse((byte)OperationCode.PlayerAddResult);

            if (isAccept)
            {
                response.ReturnCode = (short)ReturnCode.Suceess;

                // 自身的数据
                DataBase.Model.Player player = UserManager.GetPlayer(peer.Username);
                // 好友的数据
                DataBase.Model.Player friend = PlayerManager.GetById(fromId);

                // 添加好友
                PlayerManager.AddFriend(player.Identification, fromId);

                // 发送更新后的数据
                DtoPlayer dtoPlayer = player.ConvertToDot();
                response.Parameters = new Dictionary <byte, object>();
                response[(byte)ParameterCode.DtoFriend] = JsonMapper.ToJson(dtoPlayer.Friends.Last());
                peer.SendOperationResponse(response, sendParameters);

                dtoPlayer           = friend.ConvertToDot();
                response.Parameters = new Dictionary <byte, object>();
                response[(byte)ParameterCode.DtoFriend] = JsonMapper.ToJson(dtoPlayer.Friends.Last());
                fromPeer.SendOperationResponse(response, sendParameters);

                return;
            }
            else
            {
                response.ReturnCode   = (short)ReturnCode.Falied;
                response.DebugMessage = fromName + " 拒绝添加你为好友";
            }

            fromPeer.SendOperationResponse(response, sendParameters);
        }