Beispiel #1
0
        public bool Register(RegistrationRequest request)
        {
            User currentUser = _context.Users.FirstOrDefault(u => u.Username == request.Username);

            if (currentUser != null)
            {
                throw new Exception("Not a valid username.");
            }
            string registrationCode = _context.ApplicationSettings.FirstOrDefault(s => s.ApplicationSettingCode == "URC").Value;
            string adminCode        = _context.ApplicationSettings.FirstOrDefault(s => s.ApplicationSettingCode == "ARC").Value;
            bool   isAdmin          = false;

            if (request.RegistrationCode == adminCode)
            {
                isAdmin = true;
            }
            else if (request.RegistrationCode != registrationCode)
            {
                throw new Exception("Not a valid registration code.");
            }
            User newUser = new User {
                Username = request.Username, Password = _passwordHasher.HashPassword(request.Password), FirstName = request.FirstName, LastName = request.LastName, IsAdmin = isAdmin
            };

            _context.Users.Add(newUser);
            _context.SaveChanges();
            return(true);
        }
Beispiel #2
0
        public ApplicationSetting UpdateSetting(ApplicationSetting setting)
        {
            ApplicationSetting settingToUpdate = _context.ApplicationSettings.FirstOrDefault(s => s.ApplicationSettingCode == setting.ApplicationSettingCode);

            settingToUpdate.Value = setting.Value;
            _context.SaveChanges();
            return(settingToUpdate);
        }
Beispiel #3
0
        public GoalAttempt RecordGoalAttempt(RecordGoalAttemptRequest request)
        {
            Round round = _context.Rounds.FirstOrDefault(r => r.RoundId == request.GoalAttempt.RoundId);

            round.SecondsRemaining = request.SecondsRemaining;
            RfCountToRouteAndGame currentRfCountToRouteAndGame = _context.RfToRouteAndGameList.FirstOrDefault(rf => rf.RouteId == request.GoalAttempt.RouteId && rf.GameId == round.GameId);

            currentRfCountToRouteAndGame.Value += 1;

            if (!round.Practice)
            {
                List <RfCountToRouteAndGame> otherRfCountToRouteAndGameList = _context.RfToRouteAndGameList.Where(rf => rf.GameId == round.GameId && rf.RouteId != request.GoalAttempt.RouteId).ToList();
                double sum = otherRfCountToRouteAndGameList.Sum(rf => rf.Value) + currentRfCountToRouteAndGame.Value;

                double newRf = currentRfCountToRouteAndGame.Value / sum;
                if (newRf <= 0.0825)
                {
                    //Score a goal!
                    request.GoalAttempt.ScoredGoal      = true;
                    currentRfCountToRouteAndGame.Value *= 0.95;
                    otherRfCountToRouteAndGameList.ForEach(rf => rf.Value *= 0.95);
                }
                else
                {
                    //Did not score a goal
                    request.GoalAttempt.ScoredGoal = false;
                }
            }
            else
            {
                request.GoalAttempt.ScoredGoal = new Random().Next(100) < 50;
            }

            _context.GoalAttempts.Add(request.GoalAttempt);
            _context.SaveChanges();
            return(request.GoalAttempt);
        }