コード例 #1
0
        public ProposedMatchup[] GetMatchups( Game game, User[] users )
        {
            if ( users.Length < 2 )
            {
                throw new ArgumentException( "At least two players are required" );
            }

            var team1 = new Team();
            var team2 = new Team();

            for ( int i = 0; i < users.Length; i++ )
            {
                if ( i % 2 == 0 )
                {
                    team1.Members.Add( users[i] );
                }
                else
                {
                    team2.Members.Add( users[i] );
                }
            }

            return new ProposedMatchup[]
            {
                new ProposedMatchup( game, team1, team2, winRatioRange.Next() ),
                new ProposedMatchup( game, team1, team2, winRatioRange.Next() ),
                new ProposedMatchup( game, team1, team2, winRatioRange.Next() ),
            };
        }
コード例 #2
0
ファイル: TestDatabase.cs プロジェクト: Breeto/MatchMaker
        public void AddUser( User user, UserCredentials credentials )
        {
            credentials.UserId = user.Id;

            this.users.Add( user );
            this.userCredentials.Add( credentials );
        }
コード例 #3
0
        public ProposedMatchup[] GetMatchups(Game game, User[] players)
        {
            var matchups = new List<ProposedMatchup>();
            var proposersMatchups = new ConcurrentQueue<ProposedMatchup[]>();
            Parallel.ForEach(proposers, proposer => proposersMatchups.Enqueue(proposer.GetMatchups(game, players).OrderBy(GetMatchupIdentifier).ToArray()));

            var proposersMatchupsArray = proposersMatchups.ToArray();
            int possibleMatchups =  0;
            for (int i = 0; i < proposersMatchupsArray.Length - 1; i++)
            {
                possibleMatchups = proposersMatchupsArray[i].Length;
                if(proposersMatchupsArray[i].Length != proposersMatchupsArray[i+1].Length)
                {
                    throw new Exception("Multiple Proposer Matchups do not Match");
                }
            }

            for (int i = 0; i < possibleMatchups; i++)
            {
                double team1Value = 0d;
                double team2Value = 0d;
                for (int j = 0; j < proposersMatchupsArray.Length; j++)
                {
                    team1Value += weights[j] * proposersMatchupsArray[j][i].Team1PredictedWinRatio;
                    team2Value += weights[j] * proposersMatchupsArray[j][i].Team2PredictedWinRatio;
                }

                var team1 = proposersMatchupsArray[0][i].Team1;
                var team2 = proposersMatchupsArray[0][i].Team2;
                matchups.Add(new ProposedMatchup(game, team1, team2, team1Value / (team1Value + team2Value)));

            }

            return matchups.ToArray();
        }
コード例 #4
0
ファイル: Database.cs プロジェクト: Breeto/MatchMaker
        public void AddUser( User user, UserCredentials credentials )
        {
            var users = database.GetCollection<User>( Collections.Users );
            users.Insert( user );

            credentials.UserId = user.Id;

            var userCredentials = database.GetCollection<UserCredentials>( Collections.UserCredentials );
            userCredentials.Insert( credentials );
        }
コード例 #5
0
        public void Populate()
        {
            CreateUsers();
            CreateGames();
            SelectGamesForUsers();
            CreateMatchHistory();
            CreatePlayerRatings();

            var zero = new User() {Id = Guid.NewGuid(), Name = "Zero"};
            database.AddUser(zero, new UserCredentials(zero));
            database.SetGamesPlayedByUser(zero.Id, database.GetGames().Select(g => g.Id).ToArray());
        }
コード例 #6
0
        public ProposedMatchup[] GetMatchups( Game game, User[] players )
        {
            Dictionary<Guid, Dictionary<Guid, Link>> links = GetLinks( database.GetMatchupResultsByGame( game.Id ), players );
            List<Tuple<Team, Team>> teams = TeamGenerator.GenerateTeams( players );

            var matchups = new List<ProposedMatchup>();

            foreach ( var teamPair in teams )
            {
                var team1Probability = CalculateTeamLinkAverage(links, teamPair.Item1);
                var team2Probability = CalculateTeamLinkAverage(links, teamPair.Item2);
                matchups.Add( new ProposedMatchup( game, teamPair.Item1, teamPair.Item2, team1Probability/(team1Probability + team2Probability) ) );
            }

            return matchups.ToArray();
        }
コード例 #7
0
ファイル: TeamGenerator.cs プロジェクト: Breeto/MatchMaker
        public static List<Tuple<Team, Team>> GenerateTeams(User[] Players)
        {
            var result = new ConcurrentQueue<Tuple<Team,Team>>();
            var powerOfTwo = 1 << (Players.Count() - 1);
            var numberOfPlayers = Players.Count();

            Parallel.For(0, (int)powerOfTwo, counter =>
            {
                var ones = GetOnes(counter, numberOfPlayers, 0);
                if (ones == numberOfPlayers / 2 || (numberOfPlayers % 2 == 1 && ones == (numberOfPlayers / 2) + 1))
                {
                    result.Enqueue(CreateTeamFromInt(counter, Players));
                }
            });

            return new List<Tuple<Team,Team>>(result);
        }
コード例 #8
0
        public ProposedMatchup[] GetMatchups(Game game, User[] players)
        {
            var averageRatings = ratingsHelper.CalculateAverageRatings(game, players);

            var teams = TeamGenerator.GenerateTeams(players);
            var matchups = new List<ProposedMatchup>();

            foreach (var teamPair in teams)
            {
            var team1 = teamPair.Item1;
                var team2 = teamPair.Item2;
                var team1Ratings = GetTeamRating(team1, averageRatings);
                var team2Ratings = GetTeamRating(team2, averageRatings);
                matchups.Add(new ProposedMatchup(game, team1, team2, team1Ratings / (team1Ratings + team2Ratings)));
            }

            return matchups.ToArray();
        }
コード例 #9
0
ファイル: TeamGenerator.cs プロジェクト: Breeto/MatchMaker
        private static Tuple<Team, Team> CreateTeamFromInt(int TeamNumber, User[] Players)
        {
            var currentPlayerMask = 1;
            var team1 = new Team();
            var team2 = new Team();
            for (int currentPlayer = 0; currentPlayer < Players.Count(); currentPlayer++)
            {
                if ((currentPlayerMask & TeamNumber) != 0)
                {
                    team1.Members.Add(Players[currentPlayer]);
                }
                else
                {
                    team2.Members.Add(Players[currentPlayer]);
                }
                currentPlayerMask = currentPlayerMask << 1;
            }

            return  new Tuple<Team, Team>(team1, team2);
        }
コード例 #10
0
        public ProposedMatchup[] GetMatchups(Game game, User[] players)
        {
            var winLossRecords = new Dictionary<Guid, WinLossRecord>();
            foreach (var player in players)
            {
                winLossRecords[player.Id] = database.GetWinLossRecord(game.Id, player.Id);
            }

            var teams = TeamGenerator.GenerateTeams(players);
            var matchups = new List<ProposedMatchup>();
            foreach (var teamPair in teams)
            {
                var team1 = teamPair.Item1;
                var team2 = teamPair.Item2;
                var team1WinPercentage = GetTeamWinPercentage(team1, winLossRecords);
                var team2WinPercentage = GetTeamWinPercentage(team2, winLossRecords);
                matchups.Add(new ProposedMatchup(game, team1, team2, team1WinPercentage / (team1WinPercentage + team2WinPercentage)));
            }

            return matchups.ToArray();
        }
コード例 #11
0
        private Dictionary<Guid, Dictionary<Guid, Link>> GetLinks( MatchupResult[] PreviousGames, User[] players )
        {
            var result = new Dictionary<Guid, Dictionary<Guid, Link>>();
            var playerDictionary = new Dictionary<Guid, User>();
            foreach ( User user in players )
            {
                result.Add( user.Id, new Dictionary<Guid, Link>() );
                playerDictionary.Add( user.Id, user );
            }

            foreach ( MatchupResult matchup in PreviousGames )
            {
                foreach ( Guid id in matchup.Team1UserIds )
                {
                    if ( !playerDictionary.Keys.Contains( id ) )
                    {
                        continue;
                    }
                    foreach ( Guid otherId in matchup.Team1UserIds )
                    {
                        if ( !playerDictionary.Keys.Contains( otherId ) || id == otherId || result[id] == null )
                        {
                            continue;
                        }
                        if ( !result[id].ContainsKey( otherId ) )
                        {
                            result[id].Add( otherId, new Link( playerDictionary[otherId] ) );
                        }
                        result[id][otherId].AddGame( matchup );
                    }
                }
                foreach ( Guid id in matchup.Team2UserIds )
                {
                    if ( !playerDictionary.Keys.Contains( id ) )
                    {
                        continue;
                    }
                    foreach ( Guid otherId in matchup.Team2UserIds )
                    {
                        if ( !playerDictionary.Keys.Contains( otherId ) || id == otherId || result[id] == null )
                        {
                            continue;
                        }
                        if ( !result[id].ContainsKey( otherId ) )
                        {
                            result[id].Add( otherId, new Link( playerDictionary[otherId] ) );
                        }
                        result[id][otherId].AddGame( matchup );
                    }
                }
            }

            foreach(var player in result.Keys)
            {
                foreach(var otherPlayer in result.Keys)
                {
                    if (player.Equals(otherPlayer) || !result[player].Keys.Contains(otherPlayer))
                        continue;
                    if(result[player][otherPlayer].GetGameCount()>maxLinks)
                    {
                        result[player][otherPlayer].RemoveOldGames(maxLinks);
                    }
                    if (result[player][otherPlayer].GetGameCount() < minLinks)
                    {
                        result[player][otherPlayer].BackfillOldGames(minLinks);
                    }
                }
            }

            return result;
        }
コード例 #12
0
        private void AddUserFor( RegistrationRequest request )
        {
            var user = new User();
            user.Name = request.UserName;

            var credentials = new UserCredentials
            {
                EmailAddress = request.EmailAddress,
                EncryptedPassword = passwordEncryptor.Encrypt( request.Password1 ),
                AuthId = Guid.NewGuid()
            };
            database.AddUser( user, credentials );
        }
コード例 #13
0
        private List<int> GetRecentWinLosses(MatchupResult[] results, User user)
        {
            var winLosses = new List<int>();
            var streak = 0;
            foreach(var result in results)
            {
                var onTeam1 = result.Team1UserIds.Contains(user.Id);
                var onTeam2 = result.Team2UserIds.Contains(user.Id);
                if(!onTeam1 && !onTeam2)
                {
                    continue;
                }
                var won = result.Winner == MatchupWinner.Team1 && onTeam1 || result.Winner == MatchupWinner.Team2 && onTeam2;
                if(streak>=0)
                {
                    if(won)
                    {
                        streak++;
                    }
                    else
                    {
                        streak = -1;
                    }
                }
                else
                {
                    if(!won)
                    {
                        streak --;
                    }
                    else
                    {
                        streak = 1;
                    }
                }

                winLosses.Add(streak);

            }

            if(winLosses.Count<=20)
                return winLosses;
            winLosses.RemoveRange(0,winLosses.Count-20);
            return winLosses;
        }
コード例 #14
0
        private List<MapWinLossInformation> GetMapWinPercentages(MatchupResult[] results, User user, List<Map> maps)
        {
            var countDictonary = maps.ToDictionary(map => map.Id, map => new MapWinLossInformation(){Map = map});
            foreach (var result in results)
            {
                var onTeam1 = result.Team1UserIds.Contains(user.Id);
                var onTeam2 = result.Team2UserIds.Contains(user.Id);
                if (!onTeam1 && !onTeam2 || !countDictonary.Keys.Contains(result.MapId))
                {
                    continue;
                }
                var won = result.Winner == MatchupWinner.Team1 && onTeam1 || result.Winner == MatchupWinner.Team2 && onTeam2;
                countDictonary[result.MapId].TimesPlayed++;
                if (won)
                {
                    countDictonary[result.MapId].Wins++;
                }

            }

            foreach (var key in countDictonary.Keys)
            {
                countDictonary[key].Percentage = countDictonary[key].TimesPlayed == 0 ? .5 :  countDictonary[key].Wins/countDictonary[key].TimesPlayed;
            }

            return (from kvpair in countDictonary select kvpair.Value).ToList();
        }
コード例 #15
0
        private Dictionary<Guid, Dictionary<Guid, Link>> GetLinks( MatchupResult[] PreviousGames, User[] players )
        {
            var result = new Dictionary<Guid, Dictionary<Guid, Link>>();
            var playerDictionary = new Dictionary<Guid, User>();
            foreach ( User user in players )
            {
                result.Add( user.Id, new Dictionary<Guid, Link>() );
                playerDictionary.Add( user.Id, user );
            }

            foreach ( MatchupResult matchup in PreviousGames )
            {
                foreach ( Guid id in matchup.Team1UserIds )
                {
                    if ( !playerDictionary.Keys.Contains( id ) )
                    {
                        continue;
                    }
                    foreach ( Guid otherId in matchup.Team1UserIds )
                    {
                        if ( !playerDictionary.Keys.Contains( otherId ) || id == otherId || result[id] == null )
                        {
                            continue;
                        }
                        if ( !result[id].ContainsKey( otherId ) )
                        {
                            result[id].Add( otherId, new Link( playerDictionary[otherId] ) );
                        }
                        result[id][otherId].AddGame( matchup );
                    }
                }
                foreach ( Guid id in matchup.Team2UserIds )
                {
                    if ( !playerDictionary.Keys.Contains( id ) )
                    {
                        continue;
                    }
                    foreach ( Guid otherId in matchup.Team2UserIds )
                    {
                        if ( !playerDictionary.Keys.Contains( otherId ) || id == otherId || result[id] == null )
                        {
                            continue;
                        }
                        if ( !result[id].ContainsKey( otherId ) )
                        {
                            result[id].Add( otherId, new Link( playerDictionary[otherId] ) );
                        }
                        result[id][otherId].AddGame( matchup );
                    }
                }
            }

            return result;
        }
コード例 #16
0
 private UserCredentials CreateCredentialsFor( User user )
 {
     return new UserCredentials()
     {
         AuthId = Guid.NewGuid(),
         EmailAddress = string.Format( "{0}@example.com", user.Name ),
         EncryptedPassword = passwordEncryptor.Encrypt( user.Name.ToLower() ),
     };
 }
コード例 #17
0
ファイル: UserCredentials.cs プロジェクト: Breeto/MatchMaker
 public UserCredentials( User user )
     : this()
 {
     UserId = user.Id;
 }
コード例 #18
0
ファイル: Link.cs プロジェクト: Breeto/MatchMaker
 public Link(User otherPlayer)
 {
     OtherPlayer = otherPlayer;
     Games = new List<MatchupResult>();
     paddedGames = 0;
 }