예제 #1
0
        /// <summary>
        /// Handles the use case for adding a new user.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>If the user is created succesfully.</returns>
        public async Task <bool> Handle(AddUser request, CancellationToken cancellationToken)
        {
            var(username, email, password) = request;

            if (await this.userManager.FindByEmailAsync(email) != null)
            {
                throw new EmailAlreadyTakenException();
            }

            if (await this.userManager.FindByNameAsync(username) != null)
            {
                throw new UsernameAlreadyTakenException();
            }

            var user = new KwetterUser
            {
                Id       = Guid.NewGuid(),
                Email    = email,
                UserName = username,
            };

            var result = await this.userManager.CreateAsync(user, password);

            if (!result.Succeeded)
            {
                throw new Exception(result.Errors.First().Description);
            }

            var userRole = await this.roleManager.FindByNameAsync("user");

            if (userRole == null)
            {
                await this.roleManager.CreateAsync(new KwetterRole()
                {
                    Name = "user", Description = "The user role."
                });
            }

            await this.userManager.AddToRoleAsync(user, "user");

            await PubSubService.Publish("user-created", new
            {
                UserId   = user.Id,
                Username = user.UserName,
                Email    = user.Email,
            });

            return(result.Succeeded);
        }
예제 #2
0
        /// <summary>
        /// Handles the use case for deleteing an existing user.
        /// Handles the use case for deleteing an existing user.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>If the user is created succesfully.</returns>
        public async Task <bool> Handle(DeleteUser request, CancellationToken cancellationToken)
        {
            var foundUser = await this.userManager.FindByIdAsync(request.UserId.ToString());

            if (foundUser == null)
            {
                throw new UserNotFoundException();
            }

            var deleteResult = await this.userManager.DeleteAsync(foundUser);

            await PubSubService.Publish("user-deleted", new
            {
                UserId   = foundUser.Id,
                Username = foundUser.UserName,
                Email    = foundUser.Email,
            });

            return(deleteResult.Succeeded);
        }
        IEnumerator SearchForTargets(float delay)
        {
            while (true)
            {
                Collider[] targets = Physics.OverlapSphere(transform.position, ViewRadius, TargetMask);

                var lostTargets = targets
                                  .Select(t => t.GetComponent <Collider>())
                                  .Where(t => !visibleTargets.Contains(t.transform));

                lostTargets.ToList().ForEach(col => PubSubService.Publish(new TargetLostEvent(col)));

                visibleTargets.Clear();

                foreach (Collider target in targets)
                {
                    Vector3 directionToTarget = (target.transform.position - transform.position).normalized;

                    // CurrentTarget is within view angle
                    if (Vector3.Angle(transform.forward, directionToTarget) < ViewAngle / 2f)
                    {
                        float distanceToTarget = Vector3.Distance(transform.position, target.transform.position);

                        // check for obstacles in a way
                        if (!Physics.Raycast(transform.position, directionToTarget, distanceToTarget, ObstacleMask))
                        {
                            visibleTargets.Add(target.transform);

                            PubSubService.Publish(new TargetSpottedEvent(target, VisionOwner.transform));
                        }
                    }
                }

                yield return(new WaitForSeconds(delay));
            }
        }