Example #1
0
        public void Play(ColdBot.Models.Magic.Command command, String channel)
        {
            var teams          = new List <Team>();
            var decks          = command.Decks;
            var gameMode       = command.GameMode;
            var ratings        = getRatings(decks, gameMode, channel);
            int playersPerTeam = command.GameMode.ShortName.Equals("ffa") ? 1 : 2;

            if (ratings == null)
            {
                slackService.SendMessage("Rating were not properly fetched. The game was not recorded.", channel);
                return;
            }

            if (decks.Count % playersPerTeam != 0)
            {
                slackService.SendMessage("Actual numbers players are not divisible by players per team. The game was not recorded.", channel);
                return;
            }

            var playersWithRating = new List <Tuple <Player, Rating> >();

            // Player names and their rating from the database to TrueSkill objects
            for (int i = 0; i < decks.Count; i++)
            {
                var player = new Player(decks[i].Player.Name);
                var rating = new Rating(ratings[i].Mean, ratings[i].StandardDeviation);
                playersWithRating.Add((new Tuple <Player, Rating>(player, rating)));
            }

            // Divide players to teams
            while (playersWithRating.Count > 0)
            {
                var playersInTeam = new List <Tuple <Player, Rating> >();

                for (int i = 0; i < playersPerTeam; i++)
                {
                    playersInTeam.Add(new Tuple <Player, Rating>(playersWithRating[0].Item1, playersWithRating[0].Item2));
                    playersWithRating.RemoveAt(0);
                }

                var team = new Team();

                foreach (var player in playersInTeam)
                {
                    team.AddPlayer(player.Item1, player.Item2);
                }

                teams.Add(team);
            }

            // Calculate new rating
            var newRatings = TrueSkillCalculator.CalculateNewRatings(GameInfo.DefaultGameInfo, Teams.Concat(teams.ToArray()), generateRanking(teams.Count).ToArray());

            // For each new rating match with fetched ratings and update values in DB
            foreach (var playerWithRating in newRatings)
            {
                var rating = ratings
                             .Where(x => x.Player.Name.ToLower().Equals(playerWithRating.Key.Id.ToString().ToLower()))
                             .Where(x => x.GameMode.Equals(gameMode))
                             .FirstOrDefault();

                rating.Mean = playerWithRating.Value.Mean;
                rating.StandardDeviation  = playerWithRating.Value.StandardDeviation;
                rating.ConservativeRating = playerWithRating.Value.ConservativeRating;
            }

            context.SaveChanges();
        }
Example #2
0
 public void Play2HG(ColdBot.Models.Magic.Command command, String channel)
 {
     // TODO: Verify data.
     Play(command, channel);
 }