public async Task <BodyResponse <LevelInfo> > Handle(GetLevelInfoCommand request,
                                                             CancellationToken cancellationToken)
        {
            //读取redis account信息
            LevelInfo levelinfo = await _redis.GetLevelInfo(request.Id);

            if (levelinfo == null)
            {
                //从数据库中获取
                using (var loker = _redis.Locker(KeyGenHelper
                                                 .GenUserKey(request.Id, LevelInfo.ClassName)))
                {
                    loker.Lock();
                    levelinfo = await _levelRepository.FindAndAdd(request.Id,
                                                                  new LevelInfo(request.Id, 1, 0, _levelManager.GetNeedExp(1)));

                    _ = _redis.SetLevelInfo(levelinfo);
                }
            }

            BodyResponse <LevelInfo> response = new BodyResponse <LevelInfo>(StatusCodeDefines.Success,
                                                                             null, levelinfo);

            return(response);
        }
        public async Task <BodyResponse <NullBody> > Handle(AgreeAddFriendCommand request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "FriendInfo")))
                using (var lockerFriend = _redis.Locker(KeyGenHelper.GenUserKey(request.FriendId, "FriendInfo")))
                {
                    await Task.WhenAll(locker.LockAsync(), lockerFriend.LockAsync());

                    var infos = await LoadToRedis(request.Id, request.FriendId);

                    bool isFriend = false;
                    if (infos[0] != null && infos[0]._friends.ContainsKey(request.FriendId))
                    {
                        isFriend = true;
                    }
                    if (isFriend)
                    {
                        return(new BodyResponse <NullBody>(StatusCodeDefines.IsAlreadyFriend));
                    }
                    bool isApplyed = await _redis.IsAlreadyApplyed(request.FriendId, request.Id);

                    if (!isApplyed)
                    {
                        return(new BodyResponse <NullBody>(StatusCodeDefines.IsNotApplyed));
                    }
                    await AddFriend(request.Id, request.FriendId, 0);

                    return(new BodyResponse <NullBody>(StatusCodeDefines.Success));
                }
        }
        public async Task <BodyResponse <GetFriendInfoMqResponse> > Handle(GetFriendInfoCommand request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "FriendInfo")))
            {
                await LoadToRedis(request.Id);

                var info = await _redis.GetOneFriendInfo(request.Id, request.OtherId);

                if (info != null)
                {
                    return(new BodyResponse <GetFriendInfoMqResponse>(StatusCodeDefines.Success, null,
                                                                      new GetFriendInfoMqResponse(info.Type)));
                }
                if (await _redis.IsAlreadyApplyed(request.Id, request.OtherId))
                {
                    return(new BodyResponse <GetFriendInfoMqResponse>(StatusCodeDefines.Success, null,
                                                                      new GetFriendInfoMqResponse(FriendTypes.Applyed)));
                }
                else
                {
                    return(new BodyResponse <GetFriendInfoMqResponse>(StatusCodeDefines.Success, null,
                                                                      new GetFriendInfoMqResponse(FriendTypes.None)));
                }
            }
        }
        public async Task <BodyResponse <NullBody> > OnUserApplySit(long id, string gameKey, Int64 blind, string roomId)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(id, UserRoomInfo.className)))
            {
                await locker.LockAsync();

                var userRoomInfo = await _redis.GetUserRoomInfo(id);

                if (userRoomInfo != null)
                {
                    return(new BodyResponse <NullBody>(StatusCodeDefines.Error, new List <string>()
                    {
                        "user already in room "
                    }));
                }
                if (!_roomManager.JoinOneRoom(gameKey, roomId))
                {
                    return(new BodyResponse <NullBody>(StatusCodeDefines.Error, new List <string>()
                    {
                        "room is full "
                    }));
                }
                _ = _redis.SetUserRoomInfo(new UserRoomInfo(id, roomId, gameKey, blind, MatchingStatus.Success));
                return(new BodyResponse <NullBody>(StatusCodeDefines.Success, null));
            }
        }
Beispiel #5
0
        public async Task <BodyResponse <MoneyInfo> > Handle(GetMoneyCommand request,
                                                             CancellationToken cancellationToken)
        {
            var moneyInfo = await _redis.GetMoney(request.Id);

            if (moneyInfo == null)
            {
                using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id,
                                                                          MoneyInfo.ClassName)))
                {
                    await locker.LockAsync();

                    moneyInfo = await _moneyRepository.FindAndAdd(request.Id,
                                                                  new MoneyInfo(request.Id, 0, 0, 0, 0, 0));

                    _ = _redis.SetMoney(request.Id, moneyInfo);
                }

                if (moneyInfo == null)
                {
                    return(new BodyResponse <MoneyInfo>(StatusCodeDefines.GetMoneyError, null, null));
                }
            }
            BodyResponse <MoneyInfo> response = new BodyResponse <MoneyInfo>
                                                    (StatusCodeDefines.Success, null, moneyInfo);

            Log.Debug($"GetMoneyCommand:{moneyInfo.CurCoins},{moneyInfo.Carry}");
            return(response);
        }
Beispiel #6
0
        public async Task <FriendInfo> GetFriendInfo(long id)
        {
            FriendInfo info = new FriendInfo(id, await _redis.GetHashAllAsync <long, OneFriendInfo>
                                                 (KeyGenHelper.GenUserKey(id, "FriendInfo")));

            return(info);
        }
Beispiel #7
0
 public Task AddApplyedFriend(long id, long friendId)
 {
     return(Task.WhenAll(_redis.AddZsetValueAsync(KeyGenHelper.GenUserKey(id, "ApplyedFriendInfo"), friendId.ToString(),
                                                  DateTime.Now.ToTimeStamp(), TimeSpan.FromDays(30)),
                         _redis.DeleteZsetValueRangeAsync(
                             KeyGenHelper.GenUserKey(id, "ApplyedFriendInfo"), 0, DateTime.Now.ToTimeStamp() - TimeSpan.FromDays(30).TotalSeconds)));
 }
        public async Task AddPlatformFriend(string platformAccount, int type, FriendInfo friendInfo)
        {
            //查询该玩家是否是注册玩家

            var response = await _mqGetIdCleint.GetResponseExt <GetIdByPlatformMqCommand, BodyResponse <GetIdByPlatformMqResponse> >
                               (new GetIdByPlatformMqCommand(platformAccount, type));

            if (response.Message.StatusCode != StatusCodeDefines.Success)
            {
                return;
            }
            long friendId = response.Message.Body.Id;

            if (friendInfo._friends == null || !friendInfo._friends.TryGetValue(friendId, out var info))
            {
                using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(friendId, "FriendInfo")))
                {
                    await AddFriend(friendInfo.Id, friendId, FriendTypes.PlatformFriend);

                    return;
                }
            }
            if (info.Type != FriendTypes.PlatformFriend)
            {
                using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(friendId, "FriendInfo")))
                {
                    await _redis.DeleteFriend(friendInfo.Id, friendId);

                    await Task.WhenAll(_redis.AddFriend(friendInfo.Id, friendId, FriendTypes.PlatformFriend),
                                       _friendRepository.UpdateFriend(friendInfo.Id, friendId, FriendTypes.PlatformFriend));
                }
            }
        }
        public async Task <BodyResponse <NullBody> > Handle(RecieveMsgReward request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "Msgs", MsgTypes.Reward.ToString())))
            {
                await locker.LockAsync();

                var msg = await _redis.GetMsgInfo(request.Id, MsgTypes.Reward, request.MsgId);

                if (msg != null && msg.MsgState != MsgStates.Recieved)
                {
                    List <Task> tasks = new List <Task>();
                    foreach (var one in msg.RewardInfo)
                    {
                        var rewardMethod = one.GenRewardMethod(one.RewardType);
                        if (rewardMethod == null)
                        {
                            continue;
                        }
                        tasks.Add(rewardMethod.AddReward(_mqBus, request.Id, one));
                    }
                    await Task.WhenAll(tasks);

                    msg.MsgState = MsgStates.Recieved;
                    await _redis.SetMsgInfo(request.Id, MsgTypes.Reward, msg);
                }
                return(new BodyResponse <NullBody>(StatusCodeDefines.Success));
            }
        }
        public async Task <BodyResponse <List <RewardInfo> > > Handle(RecieveAllMsgRewardCommand request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "Msgs", MsgTypes.Reward.ToString())))
            {
                await locker.LockAsync();

                var msgs = await _redis.GetAllMsg(request.Id, MsgTypes.Reward);

                List <Task>       tasks     = new List <Task>();
                List <RewardInfo> allreward = new List <RewardInfo>();
                foreach (var one in msgs)
                {
                    if (one.Value.MsgState != MsgStates.Recieved)
                    {
                        foreach (var oneRward in one.Value.RewardInfo)
                        {
                            var rewardMethod = oneRward.GenRewardMethod(oneRward.RewardType);
                            if (rewardMethod == null)
                            {
                                continue;
                            }
                            allreward.Add(oneRward);
                            tasks.Add(rewardMethod.AddReward(_mqBus, request.Id, oneRward));
                        }
                    }
                    one.Value.MsgState = MsgStates.Recieved;
                    tasks.Add(_redis.SetMsgInfo(request.Id, MsgTypes.Reward, one.Value));
                }
                await Task.WhenAll(tasks);

                return(new BodyResponse <List <RewardInfo> >(StatusCodeDefines.Success, null, allreward));
            }
        }
Beispiel #11
0
 public Task AddMsgInfo(long id, MsgTypes msgType, UserMsgInfo info)
 {
     return(Task.WhenAll(_redis.AddZsetValueAsync(KeyGenHelper.GenUserKey(id, "MsgIds", msgType.ToString()),
                                                  info.MsgId, DateTime.Now.ToTimeStamp(), TimeSpan.FromDays(30)),
                         _redis.AddHashValueAsync(KeyGenHelper.GenUserKey(id, "Msgs", msgType.ToString()),
                                                  info.MsgId, info, TimeSpan.FromDays(30)), DeleteExpiredMsgs(id, msgType)));
 }
Beispiel #12
0
        public async Task DeleteExpiredMsgs(long id, MsgTypes msgType)
        {
            string key         = KeyGenHelper.GenUserKey(id, "MsgIds", msgType.ToString());
            var    deletedKeys = await _redis.DeleteZsetReturnValueRangeAsync
                                     (key, 0, (DateTime.Now - TimeSpan.FromDays(30)).ToTimeStamp());

            await _redis.DeleteHashValuesAsync(KeyGenHelper.GenUserKey(id, "Msgs", msgType.ToString()), deletedKeys);
        }
        public async Task <BodyResponse <NullBody> > Handle(DeleteMsgCommand request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "Msgs", MsgTypes.Reward.ToString())))
            {
                await _redis.DeleteMsg(request.Id, request.MsgType, request.MsgId);

                return(new BodyResponse <NullBody>(StatusCodeDefines.Success));
            }
        }
        public async Task <BodyResponse <GameSampleMatchingResponseInfo> > MatchingRoom(long id, long blind, string curRoomId)
        {
            //获取这个玩家的redis锁
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(id, UserRoomInfo.className)))
            {
                if (!await locker.TryLockAsync())
                {
                    return(new BodyResponse <GameSampleMatchingResponseInfo>(StatusCodeDefines.IsMatching, null, null));
                }

                //查询redis是否有这个玩家
                RoomInfo roomInfo     = null;
                var      userRoomInfo = await _redis.GetUserRoomInfo(id);

                if (userRoomInfo != null)
                {
                    roomInfo = new RoomInfo(userRoomInfo.RoomId, 0, userRoomInfo.GameKey, userRoomInfo.Blind);
                }
                else
                {
                    roomInfo = await _roomManager.GetRoom(id, blind);
                }
                try
                {
                    BodyResponse <JoinGameRoomMqResponse> roomResponse =
                        await _roomManager.SendToGameRoom <JoinGameRoomMqCommand, BodyResponse <JoinGameRoomMqResponse> >
                            (roomInfo.GameKey, new JoinGameRoomMqCommand(id, roomInfo.RoomId, roomInfo.GameKey));

                    if (roomResponse.StatusCode == StatusCodeDefines.Success)
                    {
                        _ = _redis.SetUserRoomInfo(new UserRoomInfo(id, roomInfo.RoomId, roomInfo.GameKey, blind, MatchingStatus.Success));
                        return(new BodyResponse <GameSampleMatchingResponseInfo>(StatusCodeDefines.Success, null,
                                                                                 new GameSampleMatchingResponseInfo(id, roomInfo.RoomId, roomInfo.Blind, roomInfo.GameKey)));
                    }
                    else
                    {
                        if (roomResponse.Body != null)
                        {
                            RoomInfo newRoomInfo = new RoomInfo(roomResponse.Body.RoomId, roomResponse.Body.UserCount,
                                                                roomResponse.Body.GameKey, roomResponse.Body.Blind);
                            _roomManager.UpdateRoom(newRoomInfo);
                        }

                        _ = _redis.DeleteUserRoomInfo(id);
                        return(new BodyResponse <GameSampleMatchingResponseInfo>(roomResponse.StatusCode, roomResponse.ErrorInfos, null));
                    }
                }

                catch
                {
                    _ = _redis.DeleteUserRoomInfo(id);
                    Log.Error($"user {id} join room {roomInfo.RoomId} error");
                    return(new BodyResponse <GameSampleMatchingResponseInfo>(StatusCodeDefines.BusError, null, null));
                }
            }
        }
        public async Task <BodyResponse <NullBody> > Handle(DeleteFriendCommand request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "FriendInfo")))
                using (var lockerFriend = _redis.Locker(KeyGenHelper.GenUserKey(request.FriendId, "FriendInfo")))
                {
                    await Task.WhenAll(_redis.DeleteFriend(request.Id, request.FriendId), _friendRepository.DeleteFriend(request.Id, request.FriendId));

                    return(new BodyResponse <NullBody>(StatusCodeDefines.Success));
                }
        }
        public async Task <BodyResponse <NullBody> > Handle(IgnoreApplyCommand request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "FriendInfo")))
                using (var lockerFriend = _redis.Locker(KeyGenHelper.GenUserKey(request.FriendId, "FriendInfo")))
                {
                    await _redis.IgnoreApply(request.Id, request.FriendId);

                    return(new BodyResponse <NullBody>(StatusCodeDefines.Success));
                }
        }
Beispiel #17
0
        public async Task <BodyResponse <MoneyMqResponse> > Handle(AddMoneyCommand request,
                                                                   CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id,
                                                                      MoneyInfo.ClassName)))
            {
                await locker.LockAsync();

                Log.Debug($"AddMoneyCommand add begin:{request.AddCoins},{request.AddCarry} {request.AggregateId}");
                var moneyInfo = await _redis.GetMoney(request.Id);

                if (moneyInfo == null)
                {
                    moneyInfo = await _moneyRepository.FindAndAdd(request.Id,
                                                                  new MoneyInfo(request.Id, 0, 0, 0, 0, 0));
                }

                if (request.AddCoins < 0 &&
                    System.Math.Abs(request.AddCoins) >
                    moneyInfo.CurCoins)
                {
                    Log.Debug($"AddMoneyCommand add end:{request.AddCoins},{request.AddCarry} {request.AggregateId}--1");
                    return(new BodyResponse <MoneyMqResponse>
                               (StatusCodeDefines.NoEnoughMoney, null, null));
                }

                if (request.AddCarry < 0 &&
                    System.Math.Abs(request.AddCarry) > moneyInfo.Carry)
                {
                    Log.Debug($"AddMoneyCommand add end:{request.AddCoins},{request.AddCarry} {request.AggregateId}--2");
                    return(new BodyResponse <MoneyMqResponse>
                               (StatusCodeDefines.NoEnoughMoney, null, null));
                }
                moneyInfo.AddCoins(request.AddCoins);
                moneyInfo.AddCarry(request.AddCarry);
                moneyInfo.UpdateMaxCoins();
                long coinsChangedCount = request.AddCoins + request.AddCarry;

                var moneyevent = new MoneyChangedMqEvent
                                     (moneyInfo.Id, moneyInfo.CurCoins,
                                     moneyInfo.CurDiamonds, moneyInfo.MaxCoins,
                                     moneyInfo.MaxDiamonds, coinsChangedCount, 0);
                _busCtl.PublishExt(moneyevent);
                _busCtl.PublishServerReqExt(moneyInfo.Id, moneyevent);
                await Task.WhenAll(_redis.SetMoney(request.Id, moneyInfo),
                                   _moneyRepository.ReplaceAsync(moneyInfo));

                Log.Debug($"AddMoneyCommand add end:{request.AddCoins},{request.AddCarry} {request.AggregateId} curCoins:{moneyInfo.CurCoins} curCarry:{moneyInfo.Carry}--3");

                return(new BodyResponse <MoneyMqResponse>(StatusCodeDefines.Success, null,
                                                          new MoneyMqResponse(request.Id, moneyInfo.CurCoins, moneyInfo.CurDiamonds,
                                                                              moneyInfo.MaxCoins, moneyInfo.MaxDiamonds, moneyInfo.Carry)));
            }
        }
        public async Task <BodyResponse <NullBody> > Handle(ReadedAllCommand request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "Msgs", request.MsgType.ToString())))
            {
                await locker.LockAsync();

                await _redis.ReadedAllMsg(request.Id, request.MsgType);

                return(new BodyResponse <NullBody>(StatusCodeDefines.Success));
            }
        }
        public Task RemovInviteFriend(List <long> allInviter, string platform, int type)
        {
            List <Task> allTasks = new List <Task>();

            allTasks.Add(_redis.DeleteKeyAsync(KeyGenHelper.GenKey
                                                   (platform, "Invited")));
            foreach (var oneInviter in allInviter)
            {
                allTasks.Add(_redis.DeleteZsetValueAsync(KeyGenHelper.GenUserKey
                                                             (oneInviter, "Inviter"), platform));
            }
            return(Task.WhenAll(allTasks));
        }
        public async Task <BodyResponse <NullBody> > Handle(ApplyAddFriendCommand request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "FriendInfo")))
                using (var lockerFriend = _redis.Locker(KeyGenHelper.GenUserKey(request.FriendId, "FriendInfo")))
                {
                    await Task.WhenAll(locker.LockAsync(), lockerFriend.LockAsync());

                    var infos = await LoadToRedis(request.Id, request.FriendId);

                    var  selfInfo   = infos[0];
                    var  friendInfo = infos[1];
                    bool isFriend   = friendInfo._friends.ContainsKey(request.FriendId);
                    if (isFriend)
                    {
                        return(new BodyResponse <NullBody>(StatusCodeDefines.IsAlreadyFriend));
                    }
                    bool isApplyed = await _redis.IsAlreadyApplyed(request.FriendId, request.Id);

                    if (isApplyed)
                    {
                        bool isApplyedEachOther = await _redis.IsAlreadyApplyed(request.Id, request.FriendId);

                        if (isApplyedEachOther)
                        {
                            await AddFriend(request.FriendId, request.Id, 0);

                            return(new BodyResponse <NullBody>(StatusCodeDefines.Success));
                        }
                        return(new BodyResponse <NullBody>(StatusCodeDefines.IsAlreadyApplyed));
                    }

                    List <Task <long> > tFrindCounts = new List <Task <long> >
                    {
                        _redis.GetApplyedFriendCount(request.FriendId),
                        _redis.GetFriendCount(request.FriendId),
                        _redis.GetFriendCount(request.Id)
                    };
                    await Task.WhenAll(tFrindCounts);

                    if (tFrindCounts.Where(x => x.Result >= 1000).Count() > 0)
                    {
                        return(new BodyResponse <NullBody>(StatusCodeDefines.IsFull));
                    }

                    await _redis.AddApplyedFriend(request.FriendId, request.Id);

                    _mqBus.PublishServerReqExt(request.FriendId, new ApplyedAddFriendMqEvent(request.Id));
                    return(new BodyResponse <NullBody>(StatusCodeDefines.Success));
                }
        }
Beispiel #21
0
        public async Task AddFriend(long id, long friendId, FriendTypes type)
        {
            await Task.WhenAll(_redis.AddHashValueAsync(KeyGenHelper.GenUserKey(id, "FriendInfo"),
                                                        friendId.ToString(), new OneFriendInfo(friendId, type)),

                               _redis.DeleteSetValueAsync(KeyGenHelper.GenUserKey(id, "ApplyedFriendInfo"),
                                                          friendId.ToString()),

                               _redis.DeleteSetValueAsync(KeyGenHelper.GenUserKey(friendId, "ApplyedFriendInfo"),
                                                          friendId.ToString()),

                               _redis.AddHashValueAsync(KeyGenHelper.GenUserKey(friendId, "FriendInfo"), id.ToString(),
                                                        new OneFriendInfo(id, type)));
        }
        public async Task OnSiteFailed(long id, string gameKey, string roomId, string group)
        {
            if (group != matchingGroup)
            {
                return;
            }
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(id, UserRoomInfo.className)))
            {
                await locker.LockAsync();

                _ = _redis.DeleteUserRoomInfo(id);
                _roomManager.JoinOneRoomFailed(gameKey, roomId);
            }
        }
Beispiel #23
0
 public Task SetFriendInfo(FriendInfo info)
 {
     if (info == null || info._friends.Count == 0)
     {
         //如果好友为空,插一条数据进去,写入redis,这样可以防止redis没数据,每次都从数据库读取
         var dic = new Dictionary <long, OneFriendInfo>()
         {
             { -1, new OneFriendInfo(-1, FriendTypes.None) }
         };
         return(_redis.AddHashListAsync(KeyGenHelper.GenUserKey(info.Id, "FriendInfo"), dic, TimeSpan.FromDays(7)));
     }
     return(Task.WhenAll(_redis.AddHashListAsync(KeyGenHelper.GenUserKey(info.Id, "FriendInfo"),
                                                 info._friends, TimeSpan.FromDays(7))));
 }
        public async Task Handle(FinishRegisterRewardEvent notification, CancellationToken cancellationToken)
        {
            using (var loker = _redis.Locker(KeyGenHelper.GenUserKey(notification.Id, AccountInfo.className)))
            {
                loker.Lock();
                AccountInfo info = await _redis.GetAccountInfo(notification.Id);

                if (info == null)
                {
                    info = await _accountRepository.GetByIdAsync(notification.Id);
                }
                info.Flags |= GetAccountBaseInfoMqResponse.SomeFlags.RegisterReward;
                await Task.WhenAll(_accountRepository.UpdateAsync(info), _redis.SetAccountInfo(info));
            }
        }
        public Task SetInviteFriend(long id, string platform, int type)
        {
            var t1 = _redis.AddZsetValueAsync(KeyGenHelper.GenKey
                                                  (platform, "Invited"), id.ToString(),
                                              DateTime.Now.ToTimeStamp(), TimeSpan.FromDays(30));
            var t2 = _redis.DeleteZsetValueRangeAsync(KeyGenHelper.GenKey
                                                          (platform, "Invited"), 0, DateTime.Now.ToTimeStamp() - TimeSpan.FromDays(30).TotalSeconds);
            var t3 = _redis.AddZsetValueAsync(KeyGenHelper.GenUserKey
                                                  (id, "Inviter"), platform,
                                              DateTime.Now.ToTimeStamp(), TimeSpan.FromDays(30));
            var t4 = _redis.DeleteZsetValueRangeAsync(KeyGenHelper.GenUserKey
                                                          (id, "Inviter"), 0, DateTime.Now.ToTimeStamp() - TimeSpan.FromDays(30).TotalSeconds);

            return(Task.WhenAll(t1, t2, t3, t4));
        }
        public async Task OnLeaveGame(long id, string gameKey, string roomId, long blind, int userCount, string group)
        {
            if (group != matchingGroup)
            {
                return;
            }

            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(id, UserRoomInfo.className)))
            {
                await locker.LockAsync();

                _ = _redis.DeleteUserRoomInfo(id);
            }
            _roomManager.OnUserCountChange(gameKey, roomId, blind, -1);
        }
Beispiel #27
0
        public async Task <BodyResponse <MoneyMqResponse> > Handle(BuyInCommand request,
                                                                   CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id,
                                                                      MoneyInfo.ClassName)))
            {
                await locker.LockAsync();

                var moneyInfo = await _redis.GetMoney(request.Id);

                if (moneyInfo == null)
                {
                    moneyInfo = await _moneyRepository.FindAndAdd(request.Id,
                                                                  new MoneyInfo(request.Id, 0, 0, 0, 0, 0));
                }

                if (moneyInfo.CurCoins + moneyInfo.Carry < request.MinBuy)
                {
                    return(new BodyResponse <MoneyMqResponse>
                               (StatusCodeDefines.NoEnoughMoney, null, null));
                }
                long realBuy = 0;
                if (moneyInfo.CurCoins + moneyInfo.Carry >= request.MaxBuy)
                {
                    realBuy = request.MaxBuy;
                }
                else
                {
                    realBuy = moneyInfo.CurCoins + moneyInfo.Carry;
                }
                long left = moneyInfo.Carry - realBuy;
                if (left < 0)
                {
                    moneyInfo.AddCoins(left);
                    moneyInfo.AddCarry(realBuy);
                }
                var moneyevent = new MoneyChangedMqEvent
                                     (moneyInfo.Id, moneyInfo.CurCoins,
                                     moneyInfo.CurDiamonds, moneyInfo.MaxCoins,
                                     moneyInfo.MaxDiamonds, 0, 0);
                _busCtl.PublishExt(moneyevent);
                await Task.WhenAll(_redis.SetMoney(request.Id, moneyInfo), _moneyRepository.ReplaceAsync(moneyInfo));

                return(new BodyResponse <MoneyMqResponse>(StatusCodeDefines.Success, null,
                                                          new MoneyMqResponse(request.Id, moneyInfo.CurCoins, moneyInfo.CurDiamonds,
                                                                              moneyInfo.MaxCoins, moneyInfo.MaxDiamonds, moneyInfo.Carry)));
            }
        }
        public async Task <BodyResponse <FriendVM> > Handle(GetFriendsCommand request, CancellationToken cancellationToken)
        {
            var info = await _redis.GetFriendInfo(request.Id);

            if (info == null)
            {
                info = await _friendRepository.GetByIdAsync(request.Id);

                using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "FriendInfo")))
                {
                    await _redis.SetFriendInfo(info);
                }
            }
            if (info == null)
            {
                return(new BodyResponse <FriendVM>(StatusCodeDefines.Success));
            }

            List <Task <Response <BodyResponse <GetAccountBaseInfoMqResponse> > > > getTasks =
                new List <Task <Response <BodyResponse <GetAccountBaseInfoMqResponse> > > >();

            foreach (var oneFriend in info._friends)
            {
                //获取好友信息
                getTasks.Add(_mqCleint.GetResponseExt <GetAccountBaseInfoMqCommand, BodyResponse <GetAccountBaseInfoMqResponse> >
                                 (new GetAccountBaseInfoMqCommand(oneFriend.Key)));
            }
            var allTask = await Task.WhenAll(getTasks);

            FriendVM friendInfos = new FriendVM()
            {
                Friends = new List <OneFriendVM>()
            };

            foreach (var one in allTask)
            {
                var response = one.Message;
                if (response.StatusCode == StatusCodeDefines.Success)
                {
                    var friendType = info._friends[response.Body.Id].Type;
                    friendInfos.Friends.Add(new OneFriendVM(response.Body.Id, response.Body.PlatformAccount, response.Body.UserName,
                                                            response.Body.Sex, response.Body.HeadUrl, response.Body.Type, friendType));
                }
            }

            return(new BodyResponse <FriendVM>(StatusCodeDefines.Success, null, friendInfos));
        }
        public async Task <BodyResponse <NullBody> > Handle(UploadPlatformFriendsCommand request, CancellationToken cancellationToken)
        {
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id, "FriendInfo")))
            {
                await locker.LockAsync();

                var info = await LoadFriends(request.Id);

                List <Task> tasks = new List <Task>();
                foreach (var one in request.PlatformFriends)
                {
                    tasks.Add(AddPlatformFriend(one.PlatformAccount, one.Type, info));
                }
                await Task.WhenAll(tasks);

                return(new BodyResponse <NullBody>(StatusCodeDefines.Success));
            }
        }
       public async Task<BodyResponse<GameInfo>> Handle(GetGameInfoCommand request, 
           CancellationToken cancellationToken)
       {
 
           GameInfo gameinfo = await _redis.GetGameInfo(request.Id);
           if (gameinfo == null)
           {
               using (var loker = _redis.Locker(KeyGenHelper
               .GenUserKey(request.Id, GameInfo.ClassName)))
               {
                   loker.Lock();
                   gameinfo = await _gameRepository.FindAndAdd(request.Id, new GameInfo(request.Id, 0, 0, 0));
                   _ = _redis.SetGameInfo(gameinfo);
               }
           }
           
           BodyResponse<GameInfo> response = new BodyResponse<GameInfo>(StatusCodeDefines.Success,
               null, gameinfo);
           
           return response;
       }