Example #1
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(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));
                }
        }