Example #1
0
        public async Task <int> RecordMultiPlayerGame(MultiPlayerMatchRequest payload)
        {
            var   winningTeam      = new List <Rating>();
            var   losingTeam       = new List <Rating>();
            float winningTeamScore = 0;
            float losingTeamScore  = 0;

            //create the match because we need the match id for later use
            var match = new Match()
            {
                GameId = payload.GameId,
                Date   = DateTime.Now
            };

            _dbContext.Matches.Add(match);
            _dbContext.SaveChanges();

            var multiPlayerTable = new List <MultiPlayerMatch>();

            foreach (var player in payload.Players)
            {
                var currentPlayer = _dbContext.Ratings.FirstOrDefault(p => p.PlayerId == player.PlayerId && p.GameId == payload.GameId);
                //Player never played this game before
                if (currentPlayer == null)
                {
                    currentPlayer = new Rating {
                        PlayerId = player.PlayerId, Score = 2400, GameId = payload.GameId, LastUpdateDate = DateTime.Now
                    };
                    _dbContext.Ratings.Add(currentPlayer);
                    _dbContext.SaveChanges();
                }
                if (player.IsWinner)
                {
                    winningTeam.Add(currentPlayer);
                    winningTeamScore += currentPlayer.Score;
                }
                else
                {
                    losingTeam.Add(currentPlayer);
                    losingTeamScore += currentPlayer.Score;
                }

                var multiPlayObject = new MultiPlayerMatch()
                {
                    MatchId  = match.Id,
                    PlayerId = player.PlayerId,
                    IsWinner = player.IsWinner
                };
                multiPlayObject.LastUpdateDate = DateTime.Now;
                multiPlayerTable.Add(multiPlayObject);
            }

            //Average team score
            if (winningTeam.Any())
            {
                winningTeamScore = winningTeamScore / winningTeam.Count;
            }
            else
            {
                winningTeamScore = 0;
            }
            if (losingTeam.Any())
            {
                losingTeamScore = losingTeamScore / losingTeam.Count;
            }
            else
            {
                losingTeamScore = 0;
            }

            float playerScoreImpact  = 0;
            var   kFactor            = _dbContext.Games.First(g => g.Id == payload.GameId).KFactor;
            var   newScores          = _matchService.CalculateNewEloScore(Convert.ToDecimal(winningTeamScore), Convert.ToDecimal(losingTeamScore), payload.GameId, kFactor);
            float newWinningEloScore = float.Parse(newScores.WinningNewEloScore.ToString(),
                                                   CultureInfo.InvariantCulture.NumberFormat);

            playerScoreImpact = newWinningEloScore - winningTeamScore;
            foreach (var player in winningTeam)
            {
                //var pointsDifference = Math.Abs(float.Parse(newScores.WinningNewEloScore.ToString(), CultureInfo.InvariantCulture.NumberFormat) - player.Score);
                player.Score         += playerScoreImpact;
                player.LastUpdateDate = DateTime.Now;
            }
            foreach (var player in losingTeam)
            {
                //var pointsDifference = Math.Abs(player.Score - float.Parse(newScores.LosingNewEloScore.ToString(), CultureInfo.InvariantCulture.NumberFormat));
                player.Score         -= playerScoreImpact;
                player.LastUpdateDate = DateTime.Now;
            }

            _dbContext.Ratings.UpdateRange(winningTeam);
            _dbContext.Ratings.UpdateRange(losingTeam);
            await _dbContext.MultiPlayerMatches.AddRangeAsync(multiPlayerTable);

            await _dbContext.SaveChangesAsync();

            return((int)Math.Round(playerScoreImpact));
        }