Example #1
0
        public async Task <BodyResponse <RewardInfoVM> > Handle(GetBankruptcyRewardCommand request, CancellationToken cancellationToken)
        {
            DateTime tnow = DateTime.Now;

            using (var locker = _redis.Locker(KeyGenHelper.GenUserDayKey(tnow, request.Id, BankruptcyInfo.className)))
            {
                await locker.LockAsync();

                var bankruptcyInfo = await _redis.GetBankruptcyInfo(tnow, request.Id);

                int totalTimes = _bankruptcyConfig.BankruptcyRewards.Count;
                if (bankruptcyInfo == null)
                {
                    bankruptcyInfo = new BankruptcyInfo(0);
                }
                if (bankruptcyInfo.CurTimes >= 2)
                {
                    return(new BodyResponse <RewardInfoVM>(StatusCodeDefines.RewardNotAvailable));
                }
                long rewardCoins = _bankruptcyConfig.BankruptcyRewards[bankruptcyInfo.CurTimes];
                ++bankruptcyInfo.CurTimes;
                await _redis.SetBankruptcyInfo(tnow, bankruptcyInfo);

                _ = _mqBus.Publish(new AddMoneyMqCommand(request.Id, rewardCoins, 0, AddReason.Bankruptcy));
                return(new BodyResponse <RewardInfoVM>(StatusCodeDefines.Success, null, new RewardInfoVM(rewardCoins)));
            }
        }
        public async Task <WrappedResponse <RewardInfoVm> > Handle(GetBankruptcyRewardCommand request, CancellationToken cancellationToken)
        {
            var getMoneyResponse = await _getMoneyClient.
                                   GetResponseExt <GetMoneyMqCmd, WrappedResponse <MoneyMqResponse> >
                                       (new GetMoneyMqCmd(request.Id));

            var moneyInfo = getMoneyResponse.Message;

            if (moneyInfo.ResponseStatus != ResponseStatus.Success)
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.RewardNotAvailable));
            }
            if (moneyInfo.Body.CurCoins > RewardManager.BankruptcyConf.BankruptcyLimit)
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.RewardNotAvailable));
            }

            DateTime tnow = DateTime.Now;

            using var locker = _rewardRedis.Locker(KeyGenTool.GenUserDayKey(tnow, request.Id, nameof(BankruptcyInfo)));

            await locker.LockAsync();

            var bankruptcyInfo = await _rewardRedis.GetBankruptcyInfo(tnow, request.Id);

            int totalTimes = RewardManager.BankruptcyConf.BankruptcyRewards.Count;

            if (bankruptcyInfo == null)
            {
                bankruptcyInfo = new BankruptcyInfo(0);
            }
            if (bankruptcyInfo.CurTimes >= 2)
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.RewardNotAvailable));
            }
            long rewardCoins = RewardManager.BankruptcyConf.BankruptcyRewards[bankruptcyInfo.CurTimes];

            ++bankruptcyInfo.CurTimes;
            await _rewardRedis.SetBankruptcyInfo(tnow, bankruptcyInfo);

            _ = _mqBus.Publish(new AddMoneyMqCmd(request.Id, rewardCoins, 0, AddReason.Bankruptcy));
            return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Success, null, new RewardInfoVm(rewardCoins)));
        }