public async Task <WrappedResponse <MoneyInfo> > Handle(GetMoneyCommand request, CancellationToken cancellationToken)
        {
            var moneyInfo = await _moneyRedisRep.GetMoneyInfo(request.Id);

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

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

                    _ = _moneyRedisRep.SetMoneyInfo(moneyInfo);
                }

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

            Log.Debug($"GetMoneyCommand:{moneyInfo.CurCoins},{moneyInfo.Carry}");
            return(response);
        }
        public static async Task <WrappedResponse <NullBody> > OnUserApplySit(long id, string gameKey, Int64 blind, string roomId)
        {
            using var locker = _redis.Locker(KeyGenTool.GenUserKey(id, nameof(UserRoomInfo)));

            await locker.LockAsync();

            var userRoomInfo = await _redis.GetUserRoomInfo(id);

            if (userRoomInfo != null)
            {
                return(new WrappedResponse <NullBody>(ResponseStatus.Error, new List <string>()
                {
                    "user already in room "
                }));
            }
            if (!RoomManager.JoinOneRoom(gameKey, roomId))
            {
                return(new WrappedResponse <NullBody>(ResponseStatus.Error, new List <string>()
                {
                    "room is full "
                }));
            }
            _ = _redis.SetUserRoomInfo(new UserRoomInfo(id, roomId, gameKey, blind, MatchingStatus.Success));
            return(new WrappedResponse <NullBody>(ResponseStatus.Success, null));
        }
        public static async Task <WrappedResponse <RoomMatchResponseVm> > MatchRoom(long id, long blind, string curRoomId)
        {
            //获取这个玩家的redis锁
            using var locker = _redis.Locker(KeyGenTool.GenUserKey(id, nameof(UserRoomInfo)));

            if (!await locker.TryLockAsync())
            {
                return(new WrappedResponse <RoomMatchResponseVm>(ResponseStatus.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(blind);
            }
            try
            {
                WrappedResponse <JoinGameRoomMqResponse> roomResponse =
                    await RoomManager.SendToGameRoom <JoinGameRoomMqCmd, WrappedResponse <JoinGameRoomMqResponse> >
                        (roomInfo.GameKey, new JoinGameRoomMqCmd(id, roomInfo.RoomId, roomInfo.GameKey));

                if (roomResponse.ResponseStatus == ResponseStatus.Success)
                {
                    _ = _redis.SetUserRoomInfo(new UserRoomInfo(id, roomInfo.RoomId, roomInfo.GameKey, blind, MatchingStatus.Success));
                    return(new WrappedResponse <RoomMatchResponseVm>(ResponseStatus.Success, null,
                                                                     new RoomMatchResponseVm(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 WrappedResponse <RoomMatchResponseVm>(roomResponse.ResponseStatus, roomResponse.ErrorInfos, null));
                }
            }

            catch
            {
                _ = _redis.DeleteUserRoomInfo(id);
                Log.Error($"user {id} join room {roomInfo.RoomId} error");
                return(new WrappedResponse <RoomMatchResponseVm>(ResponseStatus.BusError, null, null));
            }
        }
        public static async Task OnSiteFailed(long id, string gameKey, string roomId, string group)
        {
            if (group != matchingGroup)
            {
                return;
            }
            using var locker = _redis.Locker(KeyGenTool.GenUserKey(id, nameof(UserRoomInfo)));
            await locker.LockAsync();

            _ = _redis.DeleteUserRoomInfo(id);
            RoomManager.JoinOneRoomFailed(gameKey, roomId);
        }
Esempio n. 5
0
        public async Task Handle(FinishRegisterRewardEvent notification, CancellationToken cancellationToken)
        {
            using var loker = _redisRep.Locker(KeyGenTool.GenUserKey(notification.Id, nameof(AccountInfo)));
            loker.Lock();
            AccountInfo info = await _redisRep.GetAccountInfo(notification.Id);

            if (info == null)
            {
                info = await _accountRep.GetByIdAsync(notification.Id);
            }
            info.FinishRegister();
            await Task.WhenAll(_accountRep.UpdateAsync(info), _redisRep.SetAccountInfo(info));
        }
        public async Task <WrappedResponse <MoneyInfo> > Handle(AddMoneyCommand request, CancellationToken cancellationToken)
        {
            using var locker = _moneyRedisRep.Locker(KeyGenTool.GenUserKey(request.Id, MoneyInfo.ClassName));
            await locker.LockAsync();

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

            if (moneyInfo == null)
            {
                moneyInfo = await _moneyRep.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 WrappedResponse <MoneyInfo>
                           (ResponseStatus.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 WrappedResponse <MoneyInfo>
                           (ResponseStatus.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, request.Reason);

            _ = _busCtl.Publish(moneyevent);
            // _busCtl.PublishServerReqExt(moneyInfo.Id, moneyevent);
            await Task.WhenAll(_moneyRedisRep.SetMoneyInfo(moneyInfo),
                               _moneyRep.ReplaceAsync(moneyInfo));

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

            return(new WrappedResponse <MoneyInfo>(ResponseStatus.Success, null,
                                                   new MoneyInfo(request.Id, moneyInfo.CurCoins, moneyInfo.CurDiamonds,
                                                                 moneyInfo.MaxCoins, moneyInfo.MaxDiamonds, moneyInfo.Carry)));
        }
        /*public static Task OnJoinGame(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.SetUserRoomInfo(new UserRoomInfo(id, roomId, gameKey, blind, MatchingStatus.Success));
         *
         *  }
         *
         *  _roomManager.OnUserCountChange(gameKey, roomId, blind, userCount);
         *  return Task.CompletedTask;
         * }*/

        public static async Task OnLeaveGame(long id, string gameKey, string roomId, long blind, int userCount, string group)
        {
            if (group != matchingGroup)
            {
                return;
            }

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

                _ = _redis.DeleteUserRoomInfo(id);
            }
            RoomManager.OnUserCountChange(gameKey, roomId, -1);
        }
        public Task RemovInviteFriend(IEnumerable <long> allInviter, string platform)
        {
            List <Task> allTasks = new List <Task>
            {
                RedisOpt.DeleteKeyAsync(KeyGenTool.GenKey
                                            (platform, "Invited"))
            };

            foreach (var oneInviter in allInviter)
            {
                allTasks.Add(RedisOpt.DeleteZsetValueAsync(KeyGenTool.GenUserKey
                                                               (oneInviter, "Inviter"), platform));
            }
            return(Task.WhenAll(allTasks));
        }
        public async Task <WrappedResponse <MoneyInfo> > Handle(BuyInCommand request, CancellationToken cancellationToken)
        {
            using var locker = _moneyRedisRep.Locker(KeyGenTool.GenUserKey(request.Id, MoneyInfo.ClassName));
            await locker.LockAsync();

            Log.Debug($"AddMoneyCommand add begin:{request.MinBuy},{request.MaxBuy}, {request.AggregateId}");
            var moneyInfo = await _moneyRedisRep.GetMoneyInfo(request.Id);

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

            if (moneyInfo.CurCoins + moneyInfo.Carry < request.MinBuy)
            {
                return(new WrappedResponse <MoneyInfo>
                           (ResponseStatus.NoEnoughMoney, null, null));
            }
            long realBuy;

            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, request.Reason);

            _ = _busCtl.Publish(moneyevent);
            await Task.WhenAll(_moneyRedisRep.SetMoneyInfo(moneyInfo), _moneyRep.ReplaceAsync(moneyInfo));

            return(new WrappedResponse <MoneyInfo>(ResponseStatus.Success, null,
                                                   new MoneyInfo(request.Id, moneyInfo.CurCoins, moneyInfo.CurDiamonds,
                                                                 moneyInfo.MaxCoins, moneyInfo.MaxDiamonds, moneyInfo.Carry)));
        }
        public async Task SetInviteFriend(long id, string platform)
        {
            string keyInvited = KeyGenTool.GenKey(platform, "Invited");
            string keyInviter = KeyGenTool.GenUserKey(id, "Inviter");
            var    t1         = RedisOpt.AddZsetValueAsync(keyInvited, id.ToString(),
                                                           DateTime.Now.ToTimeStamp());
            var t2 = RedisOpt.AddZsetValueAsync(keyInviter, platform,
                                                DateTime.Now.ToTimeStamp());
            await Task.WhenAll(t1, t2);

            var t3 = RedisOpt.ExpiryAsync(keyInvited, TimeSpan.FromDays(30));
            var t4 = RedisOpt.ExpiryAsync(keyInviter, TimeSpan.FromDays(30));
            var t5 = RedisOpt.DeleteZsetValueRangeAsync(keyInvited, 0, DateTime.Now.ToTimeStamp() - TimeSpan.FromDays(30).TotalSeconds);
            var t6 = RedisOpt.DeleteZsetValueRangeAsync(keyInviter, 0, DateTime.Now.ToTimeStamp() - TimeSpan.FromDays(30).TotalSeconds);
            await Task.WhenAll(t3, t4, t5, t6);
        }
Esempio n. 11
0
        public async Task <WrappedResponse <GameInfo> > Handle(GetGameInfoCommand request, CancellationToken cancellationToken)
        {
            GameInfo gameinfo = await _redisRepository.GetGameInfo(request.Id);

            if (gameinfo == null)
            {
                using var loker = _redisRepository.Locker(KeyGenTool.GenUserKey(request.Id, GameInfo.ClassName));
                loker.Lock();
                gameinfo = await _gameRepository.FindAndAdd(request.Id, new GameInfo(request.Id, 0, 0, 0));

                _ = _redisRepository.SetGameInfo(gameinfo);
            }

            WrappedResponse <GameInfo> response = new WrappedResponse <GameInfo>(ResponseStatus.Success,
                                                                                 null, gameinfo);

            return(response);
        }
Esempio n. 12
0
        public async Task <WrappedResponse <LevelInfo> > Handle(GetLevelInfoCommand request, CancellationToken cancellationToken)
        {
            LevelInfo levelinfo = await _redisRepository.GetLevelInfo(request.Id);

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

                _ = _redisRepository.SetLevelInfo(levelinfo);
            }

            WrappedResponse <LevelInfo> response = new WrappedResponse <LevelInfo>(ResponseStatus.Success,
                                                                                   null, levelinfo);

            return(response);
        }
Esempio n. 13
0
 public Task <GameInfo> GetGameInfo(long id)
 {
     return(RedisOpt.GetStringAsync <GameInfo>(KeyGenTool.GenUserKey(id, GameInfo.ClassName)));
 }
Esempio n. 14
0
 public Task SetGameInfo(GameInfo info)
 {
     RedisOpt.SetStringNoWait(KeyGenTool
                              .GenUserKey(info.Id, GameInfo.ClassName), info, TimeSpan.FromDays(7));
     return(Task.CompletedTask);
 }
Esempio n. 15
0
 public Task <AccountInfo> GetAccountInfo(long id)
 {
     return(RedisOpt.GetStringAsync <AccountInfo>(KeyGenTool.GenUserKey(id,
                                                                        nameof(AccountInfo))));
 }
Esempio n. 16
0
 public Task SetAccountInfo(AccountInfo info)
 {
     return(RedisOpt.SetStringAsync(KeyGenTool.GenUserKey(info.Id,
                                                          nameof(AccountInfo)), info, TimeSpan.FromDays(7)));
 }
 public Task SetUserRoomInfo(UserRoomInfo info)
 {
     return(RedisOpt.SetStringAsync <UserRoomInfo>(KeyGenTool.GenUserKey(info.Id, nameof(UserRoomInfo)),
                                                   info, TimeSpan.FromHours(1)));
 }
 public Task DeleteUserRoomInfo(long id)
 {
     return(RedisOpt.DeleteKeyAsync(KeyGenTool.GenUserKey(id, nameof(UserRoomInfo))));
 }
Esempio n. 19
0
 public Task SetServiceTemplateInfo(ServiceTemplateInfo info)
 {
     return(RedisOpt.SetStringAsync(KeyGenTool.GenUserKey(info.Id,
                                                          nameof(ServiceTemplateInfo)), info, TimeSpan.FromDays(7)));
 }
 public Task <UserRoomInfo> GetUserRoomInfo(long id)
 {
     return(RedisOpt.GetStringAsync <UserRoomInfo>(KeyGenTool.GenUserKey(id, nameof(UserRoomInfo))));
 }
Esempio n. 21
0
 public Task <ServiceTemplateInfo> GetServiceTemplateInfo(long id)
 {
     return(RedisOpt.GetStringAsync <ServiceTemplateInfo>(KeyGenTool.GenUserKey(id,
                                                                                nameof(ServiceTemplateInfo))));
 }
Esempio n. 22
0
 public Task <GameInfo> GetGameInfo(long id)
 {
     return(RedisOpt.GetStringAsync <GameInfo>(KeyGenTool.GenUserKey(id,
                                                                     nameof(GameInfo))));
 }
 public Task <RegisterRewardInfo> GetUserRegiserReward(long id)
 {
     return(RedisOpt.GetStringAsync <RegisterRewardInfo>(KeyGenTool.GenUserKey(id, nameof(RegisterRewardInfo))));
 }
        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)));
        }
 public Task SetUserRegiserReward(RegisterRewardInfo info)
 {
     return(RedisOpt.SetStringAsync(KeyGenTool.GenUserKey(info.Id, nameof(RegisterRewardInfo)), info, TimeSpan.FromDays(5)));
 }