protected override ValidationResult IsValid(object value, ValidationContext validationContext) { using (var repoProvider = new RepoProvider()) { string newName = value.ToString().Trim(); var playerFound = repoProvider.PlayerRepo.Get(newName); if(playerFound != null) { return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); } } return null; }
public async Task<int> GetHandicap(RepoProvider repoProvider, WeekModel week, Guid playerId) { var result = 0; if (week.BeenPlayed) { result = repoProvider.RoundRepo.GetWeeklyRound(playerId, week.WeekId).Handicap; } else { result = await ServiceProvider.HandicapInstance.CalculateHandicap(playerId); } return result; }
public async Task<AddPlayerMatchupViewModel> Get() { AddPlayerMatchupViewModel vm = new AddPlayerMatchupViewModel(); using (var repoProvider = new RepoProvider()) { var highestWeekNbr = repoProvider.WeekRepo.GetHighestWeekNumber(); vm.NextWeek = ++highestWeekNbr; vm.Players = new List<BasicPlayerViewModel>(); foreach (var player in await repoProvider.PlayerRepo.GetAll()) { vm.Players.Add(new BasicPlayerViewModel() { Name = player.Name, Id = player.PlayerId }); } } return vm; }
private async Task<LeaderboardViewModel> getModelData() { var playerSummaryVM = new LeaderboardViewModel(); var firstHalfPlayerColl = new List<PlayerLeaderboardViewModel>(); var secondHalfPlayerColl = new List<PlayerLeaderboardViewModel>(); using (var repoProvider = new RepoProvider()) { playerSummaryVM.WeeksPlayed = (await repoProvider.WeekRepo.GetPlayedWeeks()).Count(); //first half foreach (var player in await repoProvider.PlayerRepo.GetAll()) { var playerVM = new PlayerLeaderboardViewModel() { Name = player.Name }; playerVM.CurrentHandicap = await ServiceProvider.HandicapInstance.CalculateHandicap(player.PlayerId); playerVM.ScoreAvg = repoProvider.RoundRepo.GetPlayerScoreAverage(player.PlayerId, true); playerVM.TotalPoints = repoProvider.RoundRepo.GetPlayerTotalPoints(player.PlayerId, true); playerVM.Birds = repoProvider.RoundRepo.GetPlayerBirieTotal(player.PlayerId, true); playerVM.Pars = repoProvider.RoundRepo.GetPlayerParTotal(player.PlayerId, true); playerVM.Bogeys = repoProvider.RoundRepo.GetPlayerBogeyTotal(player.PlayerId, true); firstHalfPlayerColl.Add(playerVM); } //second half foreach (var player in await repoProvider.PlayerRepo.GetAll()) { var playerVM = new PlayerLeaderboardViewModel() { Name = player.Name }; playerVM.CurrentHandicap = await ServiceProvider.HandicapInstance.CalculateHandicap(player.PlayerId); playerVM.ScoreAvg = repoProvider.RoundRepo.GetPlayerScoreAverage(player.PlayerId, false); playerVM.TotalPoints = repoProvider.RoundRepo.GetPlayerTotalPoints(player.PlayerId, false); playerVM.Birds = repoProvider.RoundRepo.GetPlayerBirieTotal(player.PlayerId, false); playerVM.Pars = repoProvider.RoundRepo.GetPlayerParTotal(player.PlayerId, false); playerVM.Bogeys = repoProvider.RoundRepo.GetPlayerBogeyTotal(player.PlayerId, false); secondHalfPlayerColl.Add(playerVM); } } playerSummaryVM.FirstHalfPlayerSummary = firstHalfPlayerColl.OrderByDescending(t => t.TotalPoints).ToList(); playerSummaryVM.SecondHalfPlayerSummary = secondHalfPlayerColl.OrderByDescending(t => t.TotalPoints).ToList(); return playerSummaryVM; }
public async Task<IHttpActionResult> Post(AddNewPlayerMatchupViewModel vm) { if (ModelState.IsValid && vm.NewPlayerMatchups.Count > 0) { //Todo:Validate that each player is selected only once. using (var repoProvider = new RepoProvider()) { WeekModel weekObj = new WeekModel() { WeekId = Guid.NewGuid(), BeenPlayed = false, WeekNbr = vm.WeekNbr, ScoreCreateDate = DateTime.Now }; repoProvider.WeekRepo.Add(weekObj); for (int i = 0; i < vm.NewPlayerMatchups.Count; i++) { MatchupModel matchupObj = new MatchupModel() { MatchupId = Guid.NewGuid(), WeekId = weekObj.WeekId, Player1 = vm.NewPlayerMatchups[i].Player1Id, Player2 = vm.NewPlayerMatchups[i].Player2Id }; repoProvider.MatchupRepo.Add(matchupObj); } await repoProvider.SaveAllRepoChangesAsync(); } return Ok(); } return BadRequest(); }
public async Task<MatchupWeekViewModel> Get(int weekNbr = 0) { var globalExistingMatchups = new List<MatchupModel>(); var vm = new MatchupWeekViewModel(); vm.WeekNbr = weekNbr; using (var repoProvider = new RepoProvider()) { var players = await repoProvider.PlayerRepo.GetAll(); var weeksUnordered = await repoProvider.WeekRepo.GetAll(); var weeks = weeksUnordered.OrderBy(w => w.WeekNbr); foreach (var week in weeks.Where(w => w.WeekNbr > weekNbr)) { var matchups = await repoProvider.MatchupRepo.GetAllWeeklyMatchups(week.WeekId); globalExistingMatchups.AddRange(matchups); } List<MatchupModel> newMatchups = new List<MatchupModel>(); var nextPlayer = getNextPlayerDoesNotHaveMatchup(players, newMatchups); while (nextPlayer != null) { addNewRandomMatchupForPlayer(nextPlayer, players, newMatchups, globalExistingMatchups); nextPlayer = getNextPlayerDoesNotHaveMatchup(players, newMatchups); } vm.Matchups = new List<MatchupViewModel>(); foreach (var m in newMatchups) { var p1 = await repoProvider.PlayerRepo.Get(m.Player1); var p2 = await repoProvider.PlayerRepo.Get(m.Player2); vm.Matchups.Add(new MatchupViewModel() { Player1Name = p1.Name, Player2Name = p2.Name }); } //hydrate names into a viewmodel } return vm; }
private async Task<IEnumerable<WeekResultsViewModel>> getModelData() { var weekColl = new List<WeekResultsViewModel>(); using (var repoProvider = new RepoProvider()) { foreach (var week in await repoProvider.WeekRepo.GetPlayedWeeks()) { var weekResultsVM = new WeekResultsViewModel() { WeekNbr = week.WeekNbr, ScoreCreateDate = week.ScoreCreateDate, PlayerRounds = new List<PlayerRoundViewModel>() }; foreach (var round in await repoProvider.RoundRepo.GetWeeklyRounds(week.WeekId)) { var player = await repoProvider.PlayerRepo.Get(round.PlayerRefId); var playerRoundVM = new PlayerRoundViewModel() { TotalPoints = round.TotalPoints, TotalScore = round.TotalScore, Name = player.Name, Birdies = round.BirdieCnt, Pars = round.ParCnt, Bogeys = round.BogeyCnt, PlayerId = round.PlayerRefId.ToString() }; weekResultsVM.PlayerRounds.Add(playerRoundVM); } weekColl.Add(weekResultsVM); } } return weekColl.OrderByDescending(w => w.ScoreCreateDate); }
private async Task<PlayercardViewModel> getModelData(string id) { PlayercardViewModel vm = new PlayercardViewModel(); using (var repoProvider = new RepoProvider()) { if (!string.IsNullOrEmpty(id)) { Guid pGuid = new Guid(id); var player = await repoProvider.PlayerRepo.Get(pGuid); vm.Name = player.Name; vm.Handicap = await ServiceProvider.HandicapInstance.CalculateHandicap(player.PlayerId); vm.AvgScore = repoProvider.RoundRepo.GetPlayerScoreAverage(player.PlayerId); vm.SeasonLow = repoProvider.RoundRepo.GetPlayerLowRound(player.PlayerId); vm.SeasonHigh = repoProvider.RoundRepo.GetPlayerHighRound(player.PlayerId); vm.WeeklyRounds = new List<WeeklyRoundViewModel>(); var pRound = await repoProvider.RoundRepo.GetPlayerRounds(pGuid); foreach (var round in pRound) { var weekModel = await repoProvider.WeekRepo.Get(round.WeekId); vm.WeeklyRounds.Add(new WeeklyRoundViewModel() { Birdies = round.BirdieCnt, Pars = round.ParCnt, Bogeys = round.BogeyCnt, Points = round.TotalPoints, Score = round.TotalScore, WeekNbr = weekModel.WeekNbr }); } vm.WeeklyRounds = vm.WeeklyRounds.OrderBy(r => r.WeekNbr).ToList(); } } return vm; }
public async Task<AddRoundWeekViewModel> Get() { AddRoundWeekViewModel vm = new AddRoundWeekViewModel() { PlayerRounds = new List<AddRoundViewModel>() }; using (var repoProvider = new RepoProvider()) { var week = repoProvider.WeekRepo.GetFirstUnplayedWeek(); vm.HasUnplayedWeeks = false; if (week != null) { vm.HasUnplayedWeeks = true; vm.WeekNbr = week.WeekNbr; vm.WeekId = week.WeekId; foreach (var player in await repoProvider.PlayerRepo.GetAll()) { var roundScores = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; vm.PlayerRounds.Add(new AddRoundViewModel() { PlayerId = player.PlayerId, PlayerName = player.Name, Scores = roundScores }); } } } return vm; }
public async Task<int> CalculateHandicap(Guid playerId) { //HttpContext.Current.Application.Lock(); //HttpContext.Current.Application["Name"] = "Value"; //HttpContext.Current.Application.UnLock(); var retVal = 0; using(var repoProvider = new RepoProvider()) { var weeksUnsorted = await repoProvider.WeekRepo.GetPlayedWeeks(); var weeks = weeksUnsorted.OrderByDescending(w => w.WeekNbr).ToList(); var runningTotal = 0.0; var nbrRoundsWithScore = 0; var parTotal = 0; var overParTotal = 0.0; var player = await repoProvider.PlayerRepo.Get(playerId); for (int i = 0; i < _handicapWeeks && i < weeks.Count(); i++) { var round = repoProvider.RoundRepo.GetWeeklyRound(playerId, weeks[i].WeekId); runningTotal += (round.TotalScore * _roundAdjustment); if (round.TotalScore != 0) { nbrRoundsWithScore++; var thisPar = round.Front?_roundParFront:_roundParBack; parTotal += thisPar; var diff = (round.TotalScore - thisPar) * _roundAdjustment; overParTotal += diff; } } if (player.HandicapRound1 > 0) { runningTotal += (player.HandicapRound1 * _roundAdjustment); nbrRoundsWithScore++; parTotal += _roundParFront; var diff = (player.HandicapRound1 - _roundParFront) * _roundAdjustment; overParTotal += diff; } if (player.HandicapRound2 > 0) { runningTotal += (player.HandicapRound2 * _roundAdjustment); nbrRoundsWithScore++; parTotal += _roundParBack; var diff = (player.HandicapRound2 - _roundParBack) * _roundAdjustment; overParTotal += diff; } if(nbrRoundsWithScore > 0 && runningTotal > 0.0) { //Third Scoring Method double avg = overParTotal / nbrRoundsWithScore; //Second Version //double totalOverPar = runningTotal - parTotal; //double avg = totalOverPar / nbrRoundsWithScore; retVal = (int)Math.Round(avg, 0, MidpointRounding.AwayFromZero); //First version //double avg = runningTotal / nbrRoundsWithScore; //retVal = (int)Math.Round(avg, 0, MidpointRounding.AwayFromZero) - _roundPar; } } return retVal; }
public async Task<IHttpActionResult> Post(AddRoundWeekViewModel vm) { if (ModelState.IsValid) { using (var repoProvider = new RepoProvider()) { var week = await repoProvider.WeekRepo.Get(vm.WeekId); week.BeenPlayed = true; week.ScoreCreateDate = DateTime.Now; bool front = vm.FrontBack.Equals("Front"); var course = repoProvider.CourseRepo.Get(); var pars = front ? HttpContext.Current.Application["FrontPars"] as List<Par> : HttpContext.Current.Application["BackPars"] as List<Par>; var parList = pars.Select(p => p.ParValue); bool half = vm.FirstHalf.Equals("First"); var matchups = await repoProvider.MatchupRepo.GetAllWeeklyMatchups(vm.WeekId); foreach (var matchup in matchups) { var postedRoundPlayer1 = vm.PlayerRounds.First(r => r.PlayerId.CompareTo(matchup.Player1) == 0); var postedRoundPlayer2 = vm.PlayerRounds.First(r => r.PlayerId.CompareTo(matchup.Player2) == 0); var p1Handicap = await ServiceProvider.HandicapInstance.CalculateHandicap(matchup.Player1); var p2Handicap = await ServiceProvider.HandicapInstance.CalculateHandicap(matchup.Player2); List<ScoringResultModel> scoringResults = ServiceProvider.ScoringInstance.ScoreMatchup(parList, postedRoundPlayer1.Scores, postedRoundPlayer2.Scores, p1Handicap, p2Handicap); var p1NewRound = new RoundModel() { PlayerRefId = postedRoundPlayer1.PlayerId, RoundId = Guid.NewGuid(), WeekId = vm.WeekId, Scores = ServiceProvider.ScoringInstance.ExtractScores(postedRoundPlayer1.Scores), Front = front, SeasonFirstHalf = half, Handicap = p1Handicap, TotalScore = ServiceProvider.ScoringInstance.RoundTotalScore(postedRoundPlayer1.Scores), TotalPoints = scoringResults[0].Points, BirdieCnt = scoringResults[0].Birdies, ParCnt = scoringResults[0].Pars, EagleCnt = scoringResults[0].Eagles, BogeyCnt = scoringResults[0].Bogeys }; var p2NewRound = new RoundModel() { PlayerRefId = postedRoundPlayer2.PlayerId, RoundId = Guid.NewGuid(), WeekId = vm.WeekId, Scores = ServiceProvider.ScoringInstance.ExtractScores(postedRoundPlayer2.Scores), Front = front, SeasonFirstHalf = half, Handicap = p2Handicap, TotalScore = ServiceProvider.ScoringInstance.RoundTotalScore(postedRoundPlayer2.Scores), TotalPoints = scoringResults[1].Points, BirdieCnt = scoringResults[1].Birdies, ParCnt = scoringResults[1].Pars, EagleCnt = scoringResults[1].Eagles, BogeyCnt = scoringResults[1].Bogeys }; repoProvider.RoundRepo.Add(p1NewRound); repoProvider.RoundRepo.Add(p2NewRound); } await repoProvider.SaveAllRepoChangesAsync(); } return Ok(); } return BadRequest(); }
private async Task<MatchupSummaryViewModel> getModelData() { MatchupSummaryViewModel vm = new MatchupSummaryViewModel(); var weekColl = new List<MatchupWeekViewModel>(); using (var repoProvider = new RepoProvider()) { foreach (var week in await repoProvider.WeekRepo.GetAll()) { var matchupWeekVM = new MatchupWeekViewModel() { WeekNbr = week.WeekNbr, Matchups = new List<MatchupViewModel>() }; foreach (var matchup in await repoProvider.MatchupRepo.GetAllWeeklyMatchups(week.WeekId)) { var p1 = await repoProvider.PlayerRepo.Get(matchup.Player1); var p2 = await repoProvider.PlayerRepo.Get(matchup.Player2); var matchupVM = new MatchupViewModel() { Player1Name = p1.Name, Player2Name = p2.Name }; matchupVM.Player1Handicap = await ServiceProvider.HandicapInstance.GetHandicap(repoProvider, week, matchup.Player1); matchupVM.Player2Handicap = await ServiceProvider.HandicapInstance.GetHandicap(repoProvider, week, matchup.Player2); matchupWeekVM.Matchups.Add(matchupVM); } weekColl.Add(matchupWeekVM); } var highestWeekNbr = repoProvider.WeekRepo.GetHighestWeekNumber(); vm.NextWeek = ++highestWeekNbr; vm.Players = new List<BasicPlayerViewModel>(); foreach (var player in await repoProvider.PlayerRepo.GetAll()) { vm.Players.Add(new BasicPlayerViewModel() { Name = player.Name, Id = player.PlayerId }); } } vm.Weeks = weekColl.OrderByDescending(w=>w.WeekNbr).ToList(); return vm; }