Example #1
0
        //---------------------------------------------------------------------
        // Client->Cell请求
        async Task IGrainPlayer.ClientRequest(PlayerRequest player_request)
        {
            switch (player_request.id)
            {
                case PlayerRequestId.SearchPlayers:// 搜索好友
                    {
                        var request = EbTool.protobufDeserialize<SearchPlayersRequest>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() SearchPlayers GrainId={0}", GrainKey);

                        SearchPlayersResponse response = new SearchPlayersResponse();
                        response.result = IMProtocolResult.Failed;
                        response.list_friend = new List<PlayerInfo>();

                        PlayerNotify player_notify;
                        player_notify.id = PlayerNotifyId.SearchPlayers;
                        player_notify.data = EbTool.protobufSerialize<SearchPlayersResponse>(MemoryStream, response);

                        ((IGrainPlayer)this).ClientNotify(player_notify);
                    }
                    break;
                case PlayerRequestId.AddFriendRequest:// 添加好友请求
                    {
                        var request = EbTool.protobufDeserialize<string>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() AddFriendRequest GrainId={0}", GrainKey);

                        IMProtocolResult result = IMProtocolResult.Success;

                        // 是否是已存在的玩家

                        // 是否是本人黑名单中的玩家

                        // 向ToPlayer发送添加好友请求
                        if (result == IMProtocolResult.Success)
                        {
                            string friend_key = string.Format("{0}_{1}", AppId, request);
                            var grainPlayer = GrainFactory.GetGrain<IGrainPlayer>(friend_key);
                            grainPlayer.EvAddFriendRequest(PlayerGuid);
                        }

                        // 向Client发送回执
                        PlayerNotify player_notify;
                        player_notify.id = PlayerNotifyId.AddFriendRequest;
                        player_notify.data = EbTool.protobufSerialize<IMProtocolResult>(MemoryStream, result);

                        ((IGrainPlayer)this).ClientNotify(player_notify);
                    }
                    break;
                case PlayerRequestId.AddFriendResponse:// 添加好友响应(ToPlayer)
                    {
                        var request = EbTool.protobufDeserialize<AddFriendResponse>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() AddFriendResponse GrainId={0}", GrainKey);

                        IMProtocolResult result = IMProtocolResult.Success;

                        // 向Client发送回执
                        {
                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.AddFriendResponse;
                            player_notify.data = EbTool.protobufSerialize<IMProtocolResult>(MemoryStream, result);

                            ((IGrainPlayer)this).ClientNotify(player_notify);
                        }

                        // 本地直接添加到好友列表,并通知Client
                        if (result == IMProtocolResult.Success)
                        {
                            // 获取双方PlayerInfo
                            var player_info_from = await IMContext.Instance.Mongo.ReadAsync<PlayerInfo>(
                            e => e.Id == request.player_guid_request, IMContext.DbCollectPlayerInfo);

                            var player_info_to = await IMContext.Instance.Mongo.ReadAsync<PlayerInfo>(
                            e => e.Id == request.player_guid_response, IMContext.DbCollectPlayerInfo);

                            // 添加到好友列表
                            //ListFriend.Add(request.player_guid_request);

                            // 通知Client
                            AddFriendResponseNotify addfriend_response_notify = new AddFriendResponseNotify();
                            addfriend_response_notify.dealwith_type = request.dealwith_type;
                            addfriend_response_notify.player_request = player_info_from;
                            addfriend_response_notify.player_response = player_info_to;

                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.AddFriendResponseNotify;
                            player_notify.data = EbTool.protobufSerialize<AddFriendResponseNotify>(MemoryStream, addfriend_response_notify);

                            ((IGrainPlayer)this).ClientNotify(player_notify);

                            // 通知FromPlayer好友事件缓存
                            string friend_key = string.Format("{0}_{1}", AppId, request.player_guid_request);
                            var grainPlayer = GrainFactory.GetGrain<IGrainPlayer>(friend_key);
                            grainPlayer.EvAddFriendResponse(PlayerGuid);

                            // 向FromPlayer发送添加好友响应
                            //var grain_fromplayer = GrainFactory.GetGrain<IGrainPlayer>(new Guid(request.player_guid_request));
                            //grain_fromplayer.S2SAddFriendResponseNotify(addfriend_response_notify);
                        }
                    }
                    break;
                case PlayerRequestId.DeleteFriend:// 删除好友
                    {
                        var request = EbTool.protobufDeserialize<string>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() DeleteFriend GrainId={0}", GrainKey);

                        // 通知ToPlayer删除好友
                        {
                            string friend_key = string.Format("{0}_{1}", AppId, request);
                            var grainPlayer = GrainFactory.GetGrain<IGrainPlayer>(friend_key);
                            await grainPlayer.EvDeleteFriend(PlayerGuid);
                        }

                        // 更新GrainCachePlayer缓存    
                        PlayerCache.DeleteFriend(request);

                        // 直接从本地列表删除好友
                        //ListFriend.RemoveAll(item =>
                        //{
                        //    if (item == request) return true;
                        //    else return false;
                        //});

                        // 向Client发送回执
                        IMProtocolResult result = IMProtocolResult.Success;

                        {
                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.DeleteFriend;
                            player_notify.data = EbTool.protobufSerialize<IMProtocolResult>(MemoryStream, result);

                            ((IGrainPlayer)this).ClientNotify(player_notify);
                        }

                        // 通知Client删除好友
                        if (!GrainClientKey.Equals(Guid.Empty))
                        {
                            // 推送给Client
                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.DeleteFriendNotify;
                            player_notify.data = EbTool.protobufSerialize<string>(MemoryStream, request);

                            ((IGrainPlayer)this).ClientNotify(player_notify);
                        }
                    }
                    break;
                case PlayerRequestId.GetFriendMoreInfo:// 获取好友详细信息
                    {
                        var request = EbTool.protobufDeserialize<string>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() GetFriendMoreInfo GrainId={0}", GrainKey);

                        // 获取PlayerMoreInfo
                        var player_moreinfo = await IMContext.Instance.Mongo.ReadAsync<PlayerMoreInfo>(
                        e => e.Id == request, IMContext.DbCollectPlayerMoreInfo);

                        PlayerNotify player_notify;
                        player_notify.id = PlayerNotifyId.GetFriendMoreInfo;
                        player_notify.data = EbTool.protobufSerialize<PlayerMoreInfo>(MemoryStream, player_moreinfo);

                        ((IGrainPlayer)this).ClientNotify(player_notify);
                    }
                    break;
                case PlayerRequestId.GetFriendsMoreInfo:// 获取多个好友详细信息
                    {
                        var request = EbTool.protobufDeserialize<List<string>>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() GetFriendsMoreInfo GrainId={0}", GrainKey);
                    }
                    break;
                case PlayerRequestId.SendChat2Friend:// 发送消息给好友
                    {
                        var request = EbTool.protobufDeserialize<ChatInfoSend>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() SendChat2Friend GrainId={0}", GrainKey);

                        string grain_cache_chat_key = GrainChatCache.GenGrainKey(AppId, request.et_player_send, request.et_player_recv);
                        var grain_cache_chat = GrainFactory.GetGrain<IGrainChatCache>(grain_cache_chat_key);
                        grain_cache_chat.RecvChatFromGrainPlayer(request);
                    }
                    break;
                case PlayerRequestId.GetMailList:// 获取邮件列表
                    {
                        Logger.Info("ClientRequest() GetMailList GrainId={0}", GrainKey);

                        // 从GrainCachePlayerMailBox中读取MailList
                        var grain_cache_playermailbox = GrainFactory.GetGrain<IGrainMailBoxCache>(GrainKey);
                        var mail_list = await grain_cache_playermailbox.GetMailList();

                        // 通知Client收到消息
                        if (!GrainClientKey.Equals(Guid.Empty))
                        {
                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.GetMailList;
                            player_notify.data = EbTool.protobufSerialize<List<Mail>>(MemoryStream, mail_list);

                            ((IGrainPlayer)this).ClientNotify(player_notify);
                        }
                    }
                    break;
                case PlayerRequestId.SendMail:// 发送邮件
                    {
                        var request = EbTool.protobufDeserialize<SendMailRequest>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() SendMail GrainId={0}", GrainKey);

                        IMProtocolResult result = IMProtocolResult.Success;
                        request.mail.Id = Guid.NewGuid().ToString();
                        request.mail.SenderType = MailSenderType.Player;
                        request.mail.MailState = MailState.New;

                        // 通知Client收到消息
                        if (!GrainClientKey.Equals(Guid.Empty))
                        {
                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.SendMail;
                            player_notify.data = EbTool.protobufSerialize<IMProtocolResult>(MemoryStream, result);

                            ((IGrainPlayer)this).ClientNotify(player_notify);
                        }

                        // 通知ToPlayer接收邮件
                        string grain_key = string.Format("{0}_{1}", AppId, request.to_et_player_guid);
                        var grain_cache_playermailbox = GrainFactory.GetGrain<IGrainMailBoxCache>(grain_key);
                        grain_cache_playermailbox.AddMail(request.mail);
                    }
                    break;
                case PlayerRequestId.DeleteMail:// 删除邮件
                    {
                        var request = EbTool.protobufDeserialize<string>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() DeleteMail GrainId={0}", GrainKey);

                        IMProtocolResult result = IMProtocolResult.Success;

                        // 从本地删除邮件
                        //ListMail.RemoveAll(item =>
                        //{
                        //    if (item.Id == request) return true;
                        //    else return false;
                        //});

                        // 更新GrainCachePlayerMailBox
                        var grain_cache_playermailbox = GrainFactory.GetGrain<IGrainMailBoxCache>(GrainKey);
                        grain_cache_playermailbox.DeleteMail(request);

                        // 通知Client收到消息
                        if (!GrainClientKey.Equals(Guid.Empty))
                        {
                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.DeleteMail;
                            player_notify.data = EbTool.protobufSerialize<IMProtocolResult>(MemoryStream, result);

                            ((IGrainPlayer)this).ClientNotify(player_notify);
                        }

                        // 通知Client删除邮件
                        if (!GrainClientKey.Equals(Guid.Empty))
                        {
                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.DeleteMailNotify;
                            player_notify.data = EbTool.protobufSerialize<string>(MemoryStream, request);

                            ((IGrainPlayer)this).ClientNotify(player_notify);
                        }
                    }
                    break;
                case PlayerRequestId.ReadMail:// 读取邮件
                    {
                        var request = EbTool.protobufDeserialize<string>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() ReadMail GrainId={0}", GrainKey);

                        IMProtocolResult result = IMProtocolResult.Success;

                        // 更新GrainCachePlayerMailBox
                        var grain_cache_playermailbox = GrainFactory.GetGrain<IGrainMailBoxCache>(GrainKey);
                        grain_cache_playermailbox.ReadMail(request);

                        // 通知Client收到消息
                        if (!GrainClientKey.Equals(Guid.Empty))
                        {
                            ReadMailResponse response = new ReadMailResponse();
                            response.result = result;
                            response.mail_guid = request;
                            response.new_mail_state = MailState.Read;

                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.ReadMail;
                            player_notify.data = EbTool.protobufSerialize<ReadMailResponse>(MemoryStream, response);

                            ((IGrainPlayer)this).ClientNotify(player_notify);
                        }
                    }
                    break;
                case PlayerRequestId.ReadMailAndRecvAttachments:// 读取邮件并且领取附件
                    {
                        var request = EbTool.protobufDeserialize<string>(MemoryStream, player_request.data);

                        Logger.Info("ClientRequest() ReadMailAndRecvAttachments GrainId={0}", GrainKey);

                        IMProtocolResult result = IMProtocolResult.Success;

                        // 更新GrainCachePlayerMailBox
                        var grain_cache_playermailbox = GrainFactory.GetGrain<IGrainMailBoxCache>(GrainKey);
                        var list_attachment = await grain_cache_playermailbox.ReadMailAndRecvAttachments(request);

                        // 通知Client收到消息
                        if (!GrainClientKey.Equals(Guid.Empty))
                        {
                            ReadMailAndRecvAttachmentsResponse response = new ReadMailAndRecvAttachmentsResponse();
                            response.result = result;
                            response.mail_guid = request;
                            response.new_mail_state = MailState.ReadAndRecvAttachments;
                            response.list_attachment = list_attachment;

                            PlayerNotify player_notify;
                            player_notify.id = PlayerNotifyId.ReadMail;
                            player_notify.data = EbTool.protobufSerialize<ReadMailAndRecvAttachmentsResponse>(MemoryStream, response);

                            ((IGrainPlayer)this).ClientNotify(player_notify);
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        //---------------------------------------------------------------------
        Task _c2sPlayerRequest(PlayerRequest player_request)
        {
            if (string.IsNullOrEmpty(AccId))
            {
                return TaskDone.Done;
            }

            var grain_player = GrainFactory.GetGrain<IGrainPlayer>(GrainPlayerKey);
            return grain_player.ClientRequest(player_request);
        }