private UserBetViewModel CreateUserBetViewModel(Game todaysGame, List <ScoreBasis> scoreBasis, Bet currentGamesResultBet, Bet currentGamesBet, string userNameForUserBet) { int?points = null; if (currentGamesResultBet != null) { points = _gameScoreCalculator.GetScoreForUserOnGame(currentGamesResultBet, currentGamesBet, todaysGame, scoreBasis); } return(new UserBetViewModel { Score = points, UserName = userNameForUserBet }); }
public string UpdateUserScores(UserBet referenceUserBet, int sportsEventId = 1) { var invalidUserBets = ""; var gameIdsToUsersWithMaxScore = new Dictionary <int, List <string> >(); var userBets = _userBetRepository.GetAllUserBetsBySportsEventIdWithoutResultBet(sportsEventId); var groupGames = _gameRepository.GetGroupGamesBySportsEventId(sportsEventId); var scoreBases = _scoreBasisRepository.GetScoreBasisesBySportsEventId(sportsEventId); var userScores = _userScoreRepository.GetAllUserScoresBySportsEventId(sportsEventId); var maxGroupGameScore = scoreBases.Where(x => x.GameType == (int)GameType.GroupGame).Max(x => x.Points); AddNewUserScoresBasedOnUserBets(sportsEventId, userBets, userScores); foreach (var userBet in userBets) { if (UserBetIsInvalid(userBet)) { invalidUserBets += " " + userBet.UserId; continue; } var referenceBets = referenceUserBet.Bets; var referencePlayoffBets = referenceUserBet.PlayoffBets; var currentUserScore = userScores.First(x => x.UserId == userBet.UserId); currentUserScore.Score = 0; foreach (var referenceBet in referenceBets) { var currentBet = userBet.Bets.Single(x => x.GameId == referenceBet.GameId); var currentGame = groupGames.First(x => x.Id == referenceBet.GameId); var scoreForGame = _gameScoreCalculator.GetScoreForUserOnGame(referenceBet, currentBet, currentGame, scoreBases); if (scoreForGame == maxGroupGameScore) { if (gameIdsToUsersWithMaxScore.ContainsKey(currentGame.Id)) { gameIdsToUsersWithMaxScore[currentGame.Id].Add(userBet.UserId); } else { gameIdsToUsersWithMaxScore.Add(currentGame.Id, new List <string> { userBet.UserId }); } } currentUserScore.Score += scoreForGame; } currentUserScore.TopScorerScore = _topScorerScoreCalculator.Calculate(referenceUserBet, userBet, scoreBases); currentUserScore.PlayoffScore = AddScoresForPlayoffGames(referencePlayoffBets, userBet, scoreBases); currentUserScore.BonusScore = 0; } AddBonusPoints(userScores, userBets, gameIdsToUsersWithMaxScore); _userScoreRepository.SaveOrUpdateUserScores(userScores); return(invalidUserBets); }