Пример #1
0
        public async Task <TeamDto> UpdateTeam(Guid teamId, TeamRegistrationDto team)
        {
            ValidateTeam(team);
            Team teamToUpdate = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Id == teamId);

            if (teamToUpdate == null)
            {
                throw new HtfValidationException("The specified team is unknown!");
            }
            if (!Crypt.EnhancedVerify(team.Password, teamToUpdate.Password))
            {
                throw new HtfValidationException("The specified password is not correct!");
            }
            teamToUpdate.Name             = team.Name;
            teamToUpdate.FeedbackEndpoint = team.FeedbackEndpoint;
            teamToUpdate.Score--;
            await _dbContext.SaveChangesAsync();

            return(await _dbContext.Teams.Include(x => x.Location).Select(x => new TeamDto
            {
                Id = x.Id,
                Name = x.Name,
                FeedbackEndpoint = x.FeedbackEndpoint,
                Score = x.Score,
                LocationId = x.Location.Id,
                LocationName = x.Location.Name,
                TotalNumberOfAndroids = x.TotalNumberOfAndroids,
                NumberOfAndroidsAvailable = x.TotalNumberOfAndroids - x.Androids.Count,
                NumberOfAndroidsActive = x.Androids.Count(a => !a.Compromised),
                NumberOfAndroidsCompromised = x.Androids.Count(a => a.Compromised),
            }).SingleOrDefaultAsync(e => e.Id == teamId));
        }
Пример #2
0
        public bool Verify(string plainText, string hash, string salt)
        {
            var saltedText    = salt + plainText;
            var encryptedText = _encryptionService.Encrypt(saltedText);

            return(BCryptHasher.EnhancedVerify(encryptedText, hash));
        }
Пример #3
0
        public async Task <TeamDto> GetTeam(String name, String password)
        {
            var team = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Name == name);

            if (team != null && Crypt.EnhancedVerify(password, team.Password))
            {
                return(_teamMapper.Map(team));
            }
            return(null);
        }
Пример #4
0
        public async Task <ArenaDto> GetTeamArena(String teamName, String teamPassword)
        {
            var team = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Name == teamName);

            if (team != null && Crypt.EnhancedVerify(teamPassword, team.Password))
            {
                return(_arenaMapper.Map(
                           await _dbContext.Arenas.SingleOrDefaultAsync(x => x.Name == teamName)));
            }
            return(null);
        }
Пример #5
0
        public async Task <List <BotStatisticDto> > GetBotStatistics(String teamName, String teamPassword, String arenaName)
        {
            using (var sw = new SimpleStopwatch())
            {
                var botStatistics = new List <BotStatisticDto>();
                var team          = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Name.ToUpper() == teamName.ToUpper());

                if (team == null || !Crypt.EnhancedVerify(teamPassword, team.Password))
                {
                    return(null);
                }
                var arena = await _dbContext.Arenas.SingleOrDefaultAsync(x => x.Name.ToUpper() == arenaName.ToUpper());

                if (arena == null)
                {
                    return(null);
                }

                var bots = await _dbContext.Bots.Where(x => x.Deployments.Any(d => d.ArenaId == arena.Id)).ToListAsync();

                bots.ForEach(bot =>
                {
                    var botStatistic            = _botMapper.Map(bot);
                    botStatistic.PhysicalHealth = new HealthDto
                    {
                        Maximum = bot.MaximumPhysicalHealth,
                        Current = bot.CurrentPhysicalHealth,
                        Drain   = bot.PhysicalHealthDrain
                    };
                    botStatistic.Stamina = new HealthDto
                    {
                        Maximum = bot.MaximumStamina,
                        Current = bot.CurrentStamina,
                        Drain   = bot.StaminaDrain
                    };
                    botStatistic.Location = new PositionDto
                    {
                        X = bot.LocationX,
                        Y = bot.LocationY,
                    };
                    botStatistic.BotId     = bot.Id;
                    botStatistic.BotName   = bot.Name;
                    botStatistic.ArenaId   = arena.Id;
                    botStatistic.ArenaName = arena.Name;
                    botStatistic.TotalPhysicalDamageDone = bot.PhysicalDamageDone;
                    botStatistic.TotalNumberOfKills      = bot.Kills;
                    botStatistic.BotLife = DateTime.UtcNow - bot.TimeOfBirth;
                    botStatistics.Add(botStatistic);
                });
                Debug.WriteLine($"GetBotStatistics - {sw.ElapsedMilliseconds}ms");
                return(botStatistics);
            }
        }
Пример #6
0
        public async Task RemoveTeam(Guid teamId, String password)
        {
            Team teamToRemove = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Id == teamId);

            if (teamToRemove == null)
            {
                throw new BusinessException("");
            }
            if (!Crypt.EnhancedVerify(password, teamToRemove.Password))
            {
                throw new BusinessException("");
            }
            _dbContext.Teams.Remove(teamToRemove);
            await _dbContext.SaveChangesAsync();
        }
Пример #7
0
        public async Task <TeamDto> EditTeam(Guid teamId, String password, TeamRegistrationDto team)
        {
            Team teamToUpdate = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Id == teamId);

            if (teamToUpdate == null)
            {
                throw new BusinessException("");
            }
            if (!Crypt.EnhancedVerify(password, teamToUpdate.Password))
            {
                throw new BusinessException("");
            }
            teamToUpdate.Name     = team.Name;
            teamToUpdate.Password = Crypt.HashPassword(team.Password, 10, enhancedEntropy: true);
            await _dbContext.SaveChangesAsync();

            return(_teamMapper.Map(teamToUpdate));
        }
Пример #8
0
        public async Task <List <TeamStatisticDto> > GetTeamStatistics(String teamName, String teamPassword)
        {
            using (var sw = new SimpleStopwatch())
            {
                var teamStatistics = new List <TeamStatisticDto>();
                var team           = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Name.ToUpper() == teamName.ToUpper());

                if (team == null || !Crypt.EnhancedVerify(teamPassword, team.Password))
                {
                    return(null);
                }
                var arenas = await _dbContext.Arenas.Where(x => x.Active && (!x.Private || x.Name.ToUpper() == teamName.ToUpper())).ToListAsync();

                var arenaIds = arenas.Select(x => x.Id);
                var bots     = await _dbContext.Bots.Where(x => x.Deployments.Any(d => arenaIds.Contains(d.ArenaId)))
                               .Include(x => x.Deployments).ToListAsync();

                arenas.ForEach(arena =>
                {
                    var bots4Arena                    = bots.Where(x => x.Deployments.Any(d => d.TeamId == team.Id)).ToList();
                    var teamStatistic                 = _teamMapper.Map(team);
                    teamStatistic.ArenaId             = arena.Id;
                    teamStatistic.ArenaName           = arena.Name;
                    teamStatistic.TeamId              = team.Id;
                    teamStatistic.TeamName            = team.Name;
                    teamStatistic.NumberOfDeployments = arena.Deployments.Count(x => x.Team.Id == team.Id);
                    teamStatistic.NumberOfLiveBots    = bots4Arena.Count(x => x.CurrentPhysicalHealth > 0);
                    teamStatistic.NumberOfDeadBots    = bots4Arena.Count(x => x.CurrentPhysicalHealth == 0);
                    var averageBotLife                = bots.Where(x => x.TimeOfDeath.HasValue)
                                                        .Select(x => (x.TimeOfDeath.Value - x.TimeOfBirth).TotalMilliseconds)
                                                        .AverageOrDefault(Double.MaxValue);
                    teamStatistic.AverageBotLife = averageBotLife == Double.MaxValue
                        ? TimeSpan.MaxValue
                        : TimeSpan.FromMilliseconds(averageBotLife);
                    teamStatistic.TotalNumberOfKills      = bots4Arena.Select(x => x.Kills).Sum();
                    teamStatistic.TotalNumberOfDeaths     = bots4Arena.Count(x => x.CurrentPhysicalHealth == 0);
                    teamStatistic.TotalPhysicalDamageDone = bots4Arena.Select(x => x.PhysicalDamageDone).Sum();
                    teamStatistic.TotalStaminaConsumed    = bots4Arena.Select(x => x.MaximumStamina - x.CurrentStamina).Sum();
                    teamStatistics.Add(teamStatistic);
                });
                Debug.WriteLine($"GetTeamStatistics - {sw.ElapsedMilliseconds}ms");
                return(teamStatistics);
            }
        }
Пример #9
0
        public async Task <List <ArenaDto> > GetTeamArenas(String teamName, String teamPassword)
        {
            var team = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Name == teamName);

            if (team != null && Crypt.EnhancedVerify(teamPassword, team.Password))
            {
                var arenas = _arenaMapper.Map(
                    await _dbContext.Arenas.Where(x => x.Active && (!x.Private || x.Name == teamName)).ToListAsync());
                foreach (var arena in arenas)
                {
                    var lastDeployment = await _dbContext.Deployments.Where(x => x.Arena.Id == arena.Id && x.Team.Id == team.Id)
                                         .OrderByDescending(x => x.DeploymentDateTime)
                                         .FirstOrDefaultAsync();

                    arena.LastDeploymentDateTime = lastDeployment?.DeploymentDateTime;
                }
                return(arenas);
            }
            return(null);
        }
Пример #10
0
        public async Task <(bool, UserDTO?)> VerifyUserLoginAsync(LoginPayload payload, CancellationToken ct = default)
        {
            var result = await _users.FindAsync(user => user.Email !.Equals(payload.Email), null, ct);

            var user = result.FirstOrDefault(ct);

            if (user is null)
            {
                return(false, null);
            }
            return(
                BCryptNet.EnhancedVerify(payload.Password, user.Password),
                new UserDTO
            {
                Email = user.Email,
                LastName = user.LastName,
                Name = user.Name,
                Id = user.Id
            }
                );
        }
Пример #11
0
        /// <summary>
        /// Deploys a new android in the field.
        /// </summary>
        /// <param name="teamId">The unique identifier of the registered team that is sending this request.</param>
        /// <param name="androidId">The unique identifier of the deployed android this reuest is meant for.</param>
        /// <param name="request">The actual request.</param>
        /// <returns>The android the request is for.</returns>
        public async Task <AndroidDto> SendRequest(Guid teamId, Guid androidId, AndroidRequestDto request)
        {
            Team teamToCheck = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Id == teamId);

            if (teamToCheck == null)
            {
                throw new HtfValidationException("The specified team is unknown!");
            }
            if (!Crypt.EnhancedVerify(request.Password, teamToCheck.Password))
            {
                throw new HtfValidationException("The specified password is not correct!");
            }
            Android androidToRequest = await _dbContext.Androids.SingleOrDefaultAsync(x => x.Id == androidId);

            if (androidToRequest == null)
            {
                throw new HtfValidationException("The specified android is unknown!");
            }
            if (androidToRequest.Compromised)
            {
                throw new HtfValidationException("The specified android is compromised!");
            }
            if (androidToRequest.AutoPilot == AutoPilot.Level1)
            {
                throw new HtfValidationException("The specified level-1 android does not support manual requests!");
            }

            SensoryDataRequest requestToCreate = _requestMapper.Map(request);

            requestToCreate.AndroidId = androidId;
            requestToCreate.TimeStamp = DateTime.UtcNow;
            await _dbContext.SensoryDataRequests.AddAsync(requestToCreate);

            await _dbContext.SaveChangesAsync();

            return(_androidMapper.Map(androidToRequest));
        }
Пример #12
0
        /// <summary>
        /// Deploys a new android in the field.
        /// </summary>
        /// <param name="teamId">The unique identifier of the registered team to deploy an android for.</param>
        /// <param name="android">The android settings to use for deployment.</param>
        /// <returns>The deployed android.</returns>
        public async Task <AndroidDto> DeployAndroid(Guid teamId, DeployAndroidDto android)
        {
            Team teamToCheck = await _dbContext.Teams.SingleOrDefaultAsync(x => x.Id == teamId);

            if (teamToCheck == null)
            {
                throw new HtfValidationException("The specified team is unknown!");
            }
            if (!Crypt.EnhancedVerify(android.Password, teamToCheck.Password))
            {
                throw new HtfValidationException("The specified password is not correct!");
            }

            Android androidToDeploy = _deploymentMapper.Map(android);

            if (!Enum.IsDefined(typeof(AutoPilot), androidToDeploy.AutoPilot))
            {
                throw new HtfValidationException("The specified value for 'AutoPilot' is not within the expected range!");
            }
            if (!Enum.IsDefined(typeof(SensorAccuracy), androidToDeploy.LocationSensorAccuracy))
            {
                throw new HtfValidationException("The specified value for 'CrowdSensorAccuracy' is not within the expected range!");
            }
            if (!Enum.IsDefined(typeof(SensorAccuracy), androidToDeploy.CrowdSensorAccuracy))
            {
                throw new HtfValidationException("The specified value for 'LocationSensorAccuracy' is not within the expected range!");
            }
            if (!Enum.IsDefined(typeof(SensorAccuracy), androidToDeploy.MoodSensorAccuracy))
            {
                throw new HtfValidationException("The specified value for 'MoodSensorAccuracy' is not within the expected range!");
            }
            if (!Enum.IsDefined(typeof(SensorAccuracy), androidToDeploy.RelationshipSensorAccuracy))
            {
                throw new HtfValidationException("The specified value for 'RelationshipSensorAccuracy' is not within the expected range!");
            }

            Int32 existingAndroids = await _dbContext.Androids.CountAsync(
                x => x.TeamId == teamId && !x.Compromised && x.AutoPilot == androidToDeploy.AutoPilot);

            switch (androidToDeploy.AutoPilot)
            {
            case AutoPilot.Level1:
                if (existingAndroids >= 500)
                {
                    throw new HtfValidationException("This many level-1 androids in the field is getting suspicious!");
                }
                break;

            case AutoPilot.Level2:
                if (existingAndroids >= 50)
                {
                    throw new HtfValidationException("This many level-2 androids in the field is getting suspicious!");
                }
                break;

            case AutoPilot.Level3:
                if (existingAndroids >= 5)
                {
                    throw new HtfValidationException("This many level-3 androids in the field is getting suspicious!");
                }
                break;
            }

            androidToDeploy.TeamId = teamId;
            if (androidToDeploy.AutoPilot == AutoPilot.Level1)
            {
                androidToDeploy.LocationSensorAccuracy     = SensorAccuracy.LowAccuracySensor;
                androidToDeploy.CrowdSensorAccuracy        = SensorAccuracy.LowAccuracySensor;
                androidToDeploy.MoodSensorAccuracy         = SensorAccuracy.LowAccuracySensor;
                androidToDeploy.RelationshipSensorAccuracy = SensorAccuracy.LowAccuracySensor;
            }
            if (androidToDeploy.AutoPilot == AutoPilot.Level2)
            {
                androidToDeploy.LocationSensorAccuracy     = SensorAccuracy.MediumAccuracySensor;
                androidToDeploy.CrowdSensorAccuracy        = SensorAccuracy.MediumAccuracySensor;
                androidToDeploy.MoodSensorAccuracy         = SensorAccuracy.MediumAccuracySensor;
                androidToDeploy.RelationshipSensorAccuracy = SensorAccuracy.MediumAccuracySensor;
            }
            switch (androidToDeploy.AutoPilot)
            {
            case AutoPilot.Level1:
                teamToCheck.Score += 10;
                break;

            case AutoPilot.Level2:
                teamToCheck.Score += 100;
                break;

            case AutoPilot.Level3:
                teamToCheck.Score += 1000;
                break;
            }
            await _dbContext.Androids.AddAsync(androidToDeploy);

            await _dbContext.SaveChangesAsync();

            return(_androidMapper.Map(androidToDeploy));
        }
Пример #13
0
 public static bool Validate(string password, string enhancedPassword)
 {
     return(Crypt.EnhancedVerify(password, enhancedPassword));
 }
Пример #14
0
 public static bool Validate(string password, string passwordHash)
 {
     return(BCr.EnhancedVerify(password, passwordHash));
 }