public async Task <BodyResponse <RewardInfoVM> > Handle(GetLoginRewardCommand request, CancellationToken cancellationToken)
        {
            DateTime tnow      = DateTime.Now;
            int      dayOfWeek = (int)tnow.DayOfWeek;

            using (var locker = _redis.Locker(KeyGenHelper.GenUserWeekKey(tnow, request.Id, LoginRewardInfo.className)))
            {
                var rewardInfo = await _redis.GetLoginReward(tnow, request.Id);

                long rewardCoins = 0;
                if (rewardInfo == null || !rewardInfo.GettedDays.Contains(dayOfWeek))
                {
                    rewardCoins = _loginConfig.DayRewards[dayOfWeek];
                    if (rewardInfo == null)
                    {
                        rewardInfo = new LoginRewardInfo(request.Id, new List <int>());
                    }
                    rewardInfo.GettedDays.Add(dayOfWeek);
                    await _redis.SetUserLoginReward(tnow, rewardInfo);

                    _ = _mqBus.Publish(new AddMoneyMqCommand(request.Id, rewardCoins, 0, AddReason.LoginReward));
                }
                else
                {
                    return(new BodyResponse <RewardInfoVM>(StatusCodeDefines.RewardGetted));
                }

                return(new BodyResponse <RewardInfoVM>(StatusCodeDefines.Success, null, new RewardInfoVM(rewardCoins)));
            }
        }
Beispiel #2
0
        public async Task <WrappedResponse <RewardInfoVm> > Handle(GetLoginRewardCommand request, CancellationToken cancellationToken)
        {
            DateTime tnow      = DateTime.Now;
            int      dayOfWeek = (int)tnow.DayOfWeek;

            using var locker = _redis.Locker(KeyGenTool.GenUserWeekKey(tnow, request.Id, nameof(LoginRewardInfo)));

            var rewardInfo = await _redis.GetLoginReward(tnow, request.Id);

            long rewardCoins = 0;

            if (rewardInfo == null || !rewardInfo.GettedDays.Contains(dayOfWeek))
            {
                rewardCoins = RewardManager.LoginRewardConf.DayRewards[dayOfWeek];
                if (rewardInfo == null)
                {
                    rewardInfo = new LoginRewardInfo(request.Id, new List <int>());
                }
                rewardInfo.GettedDays.Add(dayOfWeek);
                await _redis.SetUserLoginReward(tnow, rewardInfo);

                _ = _mqBus.Publish(new AddMoneyMqCmd(request.Id, rewardCoins, 0, AddReason.LoginReward));
            }
            else
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.RewardGetted));
            }

            return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Success, null, new RewardInfoVm(rewardCoins)));
        }
Beispiel #3
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(GetGameActRewardCommand request,
                                                                   CancellationToken cancellationToken)
        {
            DateTime tnow       = DateTime.Now;
            var      roomConfig = RewardManager.GameActConf.AllGameConfigs
                                  .Find(x => x.ActivityId == request.ActId).RoomConfigs
                                  .Find(x => x.SubId == request.SubId);

            using var locker = _redis.Locker(KeyGenTool.GenUserDayKey(tnow, request.Id, "GameActivity", request.ActId));

            await locker.LockAsync();

            var subAct = await _redis.GetGameActProgress(tnow, request.Id, request.ActId, request.SubId);

            if (subAct == null)
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Error));
            }

            long rewardCoins = 0;

            if (subAct.State == 1)
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Error));
            }
            if (subAct.CurCount >= roomConfig.NeedCount)
            {
                rewardCoins  = roomConfig.RewardCoins;
                _            = _mqBus.Publish(new AddMoneyMqCmd(request.Id, rewardCoins, 0, AddReason.GameAct));
                subAct.State = 1;
                await _redis.SetGameActProgress(tnow, request.Id, request.ActId, request.SubId, subAct);

                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Success,
                                                          null, new RewardInfoVm(rewardCoins)));
            }
            else
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Error));
            }
        }
        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)));
        }
        public async Task <WrappedResponse <RewardInfoVm> > Handle(GetRegisterRewardCommand request, CancellationToken cancellationToken)
        {
            var accountResponse = await _accountClient.GetResponseExt <GetAccountBaseInfoMqCmd, WrappedResponse <GetAccountBaseInfoMqResponse> >
                                      (new GetAccountBaseInfoMqCmd(request.Id));

            var accountInfo = accountResponse.Message;

            if (accountInfo.ResponseStatus != ResponseStatus.Success)
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Error));
            }
            if ((accountInfo.Body.Flags & GetAccountBaseInfoMqResponse.SomeFlags.RegisterReward) ==
                GetAccountBaseInfoMqResponse.SomeFlags.RegisterReward)
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Error));
            }
            if (RewardManager.RegisterRewardConf.DayRewards.Count == 0)
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Error));
            }
            DateTime registerDate = accountInfo.Body.RegisterDate.DateOfDayBegin();
            DateTime nowDate      = DateTime.Now.DateOfDayBegin();

            if (registerDate == nowDate)
            {
                return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Error));
            }
            int  dayIndex    = 0;
            long rewardCoins = 0;

            using (var locker = _redis.Locker(KeyGenTool.GenUserKey(request.Id, nameof(RegisterRewardInfo))))
            {
                await locker.LockAsync();

                var rewardInfo = await _redis.GetUserRegiserReward(request.Id);

                if (rewardInfo == null)
                {
                    rewardInfo = await _registerRepository.GetByIdAsync(request.Id);
                }
                if (rewardInfo == null)
                {
                    rewardCoins = RewardManager.RegisterRewardConf.DayRewards[dayIndex];
                    dayIndex    = 0;
                }
                else
                {
                    if (rewardInfo.DayIndex >= RewardManager.RegisterRewardConf.DayRewards.Count - 1)
                    {
                        return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Error));
                    }
                    else if (rewardInfo.GetDate.DateOfDayBegin() == nowDate)
                    {
                        return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Error));
                    }
                    else
                    {
                        dayIndex    = rewardInfo.DayIndex + 1;
                        rewardCoins = RewardManager.RegisterRewardConf.DayRewards[dayIndex];
                    }
                }

                rewardInfo = new RegisterRewardInfo(request.Id, dayIndex, DateTime.Now);
                await Task.WhenAll(_redis.SetUserRegiserReward(rewardInfo), _registerRepository.ReplaceAndAddAsync(rewardInfo));
            }
            _ = _mqBus.Publish(new AddMoneyMqCmd(request.Id, rewardCoins, 0, AddReason.RegisterReward));
            if (dayIndex >= RewardManager.RegisterRewardConf.DayRewards.Count - 1)
            {
                _ = _mqBus.Publish(new FinishedRegisterRewardMqEvent(request.Id));
            }
            return(new WrappedResponse <RewardInfoVm>(ResponseStatus.Success, null,
                                                      new RewardInfoVm(rewardCoins)));
        }