Пример #1
0
        public async Task <Unit> Handle(LoginCommand command, CancellationToken cancellationToken)
        {
            var email    = command.Email;
            var password = command.Password;

            var user = await _userDomainService.Get(p => p.Email == email && p.HasVerifiedEmail);

            if (user == null)
            {
                await _bus.RaiseEvent(new DomainNotification("用户不存在"));

                return(Unit.Value);
            }

            if (user.Password != password.ToMd5())
            {
                await _bus.RaiseEvent(new DomainNotification("密码错误"));

                return(Unit.Value);
            }

            if (user.Status != UserStatusEnum.正常)
            {
                await _bus.RaiseEvent(new DomainNotification($"账号为“{user.Status}”状态"));

                return(Unit.Value);
            }

            string ip = _httpAccessor.HttpContext.GetUserIp();

            user.LastDate = DateTime.Now;
            user.LastIp   = ip;

            await _userDomainService.Update(user);

            var jwtAccount = new JwtAccount
            {
                UserId = user.Id,
                Email  = user.Email
            };

            await _httpAccessor.HttpContext.SignIn("user", jwtAccount);

            if (await Commit())
            {
                await _bus.RaiseEvent(new SignInEvent(user)).ConfigureAwait(false);
            }

            return(Unit.Value);
        }
Пример #2
0
        public async Task <Unit> Handle(JoinGameCommand command, CancellationToken cancellationToken)
        {
            _logger.LogDebug($"Handle JoinGameCommand:{JsonConvert.SerializeObject(command)}");
            var userId   = command.UserId;
            var playerId = command.PlayerId;

            var player = await _playerDomainService.Get(playerId);

            if (player == null)
            {
                await _bus.RaiseEvent(new DomainNotification($"角色不存在!"));

                return(Unit.Value);
            }

            if (player.UserId != userId)
            {
                await _bus.RaiseEvent(new DomainNotification("该角色不存在!"));

                return(Unit.Value);
            }

            if (_account == null)
            {
                await _bus.RaiseEvent(new DomainNotification("未登录!"));

                return(Unit.Value);
            }

            var jwtAccount = new JwtAccount
            {
                UserId     = userId,
                Email      = _account.Email,
                PlayerId   = playerId,
                PlayerName = player.Name
            };

            await _httpAccessor.HttpContext.SignIn("user", jwtAccount);

            if (await Commit())
            {
                await _bus.RaiseEvent(new JoinedGameEvent(player)).ConfigureAwait(false);
            }

            return(Unit.Value);
        }
Пример #3
0
        public async Task <Unit> Handle(RegCommand command, CancellationToken cancellationToken)
        {
            var email    = command.Email.Trim().ToLower();
            var password = command.Password;
            var code     = command.Code;

            var user = await _userDomainService.Get(p => p.Email == email && p.HasVerifiedEmail);

            if (user != null)
            {
                await _bus.RaiseEvent(new DomainNotification("邮箱已被注册,请更改!"));

                return(Unit.Value);
            }

            string key = string.Format(RedisKey.RegEmail, email);// $"regemail_{email}";
            long   ttl = await _redisDb.KeyTimeToLive(key);

            if (ttl < 0)
            {
                await _bus.RaiseEvent(new DomainNotification($"注册验证码已超时,请重试"));

                return(Unit.Value);
            }

            string emailCode = await _redisDb.StringGet <string>(key);

            if (string.Compare(emailCode, code, true) != 0)
            {
                await _bus.RaiseEvent(new DomainNotification($"注册验证码已失效,请重试"));

                return(Unit.Value);
            }



            string ip = _httpAccessor.HttpContext.GetUserIp();

            user = new UserEntity
            {
                Email            = email,
                LastDate         = DateTime.Now,
                Password         = password.ToMd5(),
                Status           = UserStatusEnum.正常,
                RegDate          = DateTime.Now,
                UserName         = "",
                RegIp            = ip,
                LastIp           = ip,
                HasVerifiedEmail = true
            };

            await _userDomainService.Add(user);

            var jwtAccount = new JwtAccount
            {
                UserId = user.Id,
                Email  = user.Email
            };

            await _httpAccessor.HttpContext.SignIn("user", jwtAccount);

            await _redisDb.KeyDelete(key);

            if (await Commit())
            {
                await _bus.RaiseEvent(new SignUpEvent(user)).ConfigureAwait(false);
            }

            return(Unit.Value);
        }
Пример #4
0
        public async Task <Unit> Handle(CreateCommand command, CancellationToken cancellationToken)
        {
            var name   = command.Name;
            var gender = command.Gender;
            var userId = command.UserId;
            var str    = command.Str;
            var @int   = command.Int;
            var dex    = command.Dex;
            var con    = command.Con;

            var player = await _playerDomainService.Get(p => p.Name == name);

            if (player != null)
            {
                await _bus.RaiseEvent(new DomainNotification("角色名已被使用,请更改!"));

                return(Unit.Value);
            }

            player = await _playerDomainService.Get(x => x.UserId == userId);

            if (player != null)
            {
                await _bus.RaiseEvent(new DomainNotification("已经超过最大可创建角色数!"));

                return(Unit.Value);
            }

            if (str + @int + dex + con != 80)
            {
                await _bus.RaiseEvent(new DomainNotification("所有先天属性之和必须为80!"));

                return(Unit.Value);
            }

            var roomId = _appConfig.Site.BornRoomId;

            if (roomId <= 0)
            {
                await _bus.RaiseEvent(new DomainNotification("未设置出生地点!"));

                return(Unit.Value);
            }

            var room = await _roomDomainService.Get(roomId);

            if (room == null)
            {
                await _bus.RaiseEvent(new DomainNotification("设置的出生地点不存在!"));

                return(Unit.Value);
            }

            Random random = new Random();

            player = new PlayerEntity
            {
                CreateDate = DateTime.Now,
                LastDate   = DateTime.Now,
                Level      = 1,
                Name       = name,
                UserId     = userId,
                Status     = PlayerStatusEnum.空闲,
                Gender     = gender,
                Age        = 14 * 12,
                ConAdd     = 0,
                DexAdd     = 0,
                FactionId  = 0,
                IntAdd     = 0,
                Money      = 0,
                RoomId     = roomId,
                Title      = "",
                StrAdd     = 0,
                Atk        = 0,
                Str        = str,
                Con        = con,
                Int        = @int,
                Dex        = dex,
                Exp        = 0,
                Cor        = 20,
                Cps        = 20,


                Pot     = 0,
                Kar     = random.Next(1, 100),
                Def     = 0,
                Hp      = 0,
                LimitMp = 0,
                MaxHp   = 0,
                MaxMp   = 0,
                Mp      = 0,

                Hit   = 0,
                Parry = 0,
                Flee  = 0,
                Per   = random.Next(10, 50),
                Nrg   = 0
            };

            player.Computed();

            await _playerDomainService.Add(player);

            var jwtAccount = new JwtAccount
            {
                UserId     = userId,
                Email      = _account.Email,
                PlayerId   = player.Id,
                PlayerName = player.Name
            };

            await _httpAccessor.HttpContext.SignIn("user", jwtAccount);

            if (await Commit())
            {
                await _bus.RaiseEvent(new CreatedEvent(player)).ConfigureAwait(false);
            }


            return(Unit.Value);
        }