Example #1
0
        public void Import(string connectionString, StatbookModel statbook, bool assumeATeams)
        {
            _connection = new SqlConnection(connectionString);
            try
            {
                _connection.Open();
                _transaction = _connection.BeginTransaction();

                // insert leagues
                LeagueGateway leagueGateway = new LeagueGateway(_connection, _transaction);
                var leagues = leagueGateway.GetAllLeagues();
                League homeLeague = leagues.FirstOrDefault(l => l.Name.ToLower() == statbook.HomeTeam.LeagueName.ToLower());
                League awayLeague = leagues.FirstOrDefault(l => l.Name.ToLower() == statbook.AwayTeam.LeagueName.ToLower());
                int maxID = leagues.Select(l => l.ID).Max();
                if(homeLeague == null)
                {
                    homeLeague = leagueGateway.GetLeague(maxID + 1, statbook.HomeTeam.LeagueName, statbook.Date, false);
                    maxID++;
                }
                if(awayLeague == null)
                {
                    awayLeague = leagueGateway.GetLeague(maxID + 1, statbook.AwayTeam.LeagueName, statbook.Date, false);
                    maxID++;
                }

                // insert teams
                TeamGateway teamGateway = new TeamGateway(_connection, _transaction);
                Team homeTeam, awayTeam;
                if (assumeATeams)
                {
                    homeTeam = teamGateway.GetATeam(homeLeague.ID);
                    awayTeam = teamGateway.GetATeam(awayLeague.ID);

                }
                else
                {
                    homeTeam = teamGateway.GetTeam(statbook.HomeTeam.Name, homeLeague.ID, "A", false);
                    awayTeam = teamGateway.GetTeam(statbook.AwayTeam.Name, awayLeague.ID, "A", false);
                }

                // insert bout
                BoutGateway boutGateway = new BoutGateway(_connection, _transaction);
                if(!boutGateway.DoesBoutExist(homeTeam.ID, awayTeam.ID, statbook.Date))
                {
                    Bout bout = boutGateway.GetBout(homeTeam.ID, awayTeam.ID, statbook.Date);
                    BoutDataImport(statbook, bout, homeTeam, awayTeam);
                }
                else
                {
                    // bout already exists
                    Console.WriteLine(string.Format("Bout between {0} and {1} on {2} already exists.", homeTeam.Name, awayTeam.Name, statbook.Date));
                }

                _transaction.Commit();
            }
            finally
            {
                _connection.Close();
            }
        }
Example #2
0
        public void Import(string connectionString, StatbookModel statbook)
        {
            _connection = new SqlConnection(connectionString);
            try
            {
                _connection.Open();
                _transaction = _connection.BeginTransaction();

                // insert leagues
                LeagueGateway leagueGateway = new LeagueGateway(_connection, _transaction);
                var leagues = leagueGateway.GetAllLeagues();
                League homeLeague = leagues.FirstOrDefault(l => l.Name.ToLower() == statbook.HomeTeam.LeagueName.ToLower());
                League awayLeague = leagues.FirstOrDefault(l => l.Name.ToLower() == statbook.AwayTeam.LeagueName.ToLower());
                int maxID = leagues.Select(l => l.ID).Max();
                if(homeLeague == null || awayLeague == null)
                {
                    throw new InvalidOperationException("Bad league name");
                }

                // for the basic importer, we'll just take whatever A team this league has, rather than worrying about validating the team name
                TeamGateway teamGateway = new TeamGateway(_connection, _transaction);
                Team homeTeam = teamGateway.GetATeam(homeLeague.ID);
                Team awayTeam = teamGateway.GetATeam(awayLeague.ID);

                // insert bout
                BoutGateway boutGateway = new BoutGateway(_connection, _transaction);
                if(!boutGateway.DoesBoutExist(homeTeam.ID, awayTeam.ID, statbook.Date))
                {
                    Bout bout = boutGateway.GetBout(homeTeam.ID, awayTeam.ID, statbook.Date);
                    BoutDataImport(statbook, bout, homeTeam, awayTeam);
                }
                else
                {
                    // bout already exists
                    Console.WriteLine(string.Format("Bout between {0} and {1} on {2} already exists.", homeTeam.Name, awayTeam.Name, statbook.Date));
                }

                _transaction.Commit();
            }
            finally
            {
                _connection.Close();
            }
        }
        public IList<TeamRating> GetTeamRatings()
        {
            SqlConnection connection = new SqlConnection(_connectionString);
            connection.Open();
            SqlTransaction transaction = connection.BeginTransaction();

            // attempt to get the rankings data from the db
            TeamRatingGateway trGateway = new TeamRatingGateway(connection, transaction);
            TeamGateway teamGateway = new TeamGateway(connection, transaction);
            LeagueGateway leagueGateway = new LeagueGateway(connection, transaction);
            var teamRankings = trGateway.GetCurrentTeamRatings();
            if (!teamRankings.Any() || DateTime.Now.Subtract(teamRankings[0].AddedDate).TotalDays > 30 )
            {
                var teams = teamGateway.GetAllWftdaTeams();
                var leagues = leagueGateway.GetAllLeagues();
                var teamMapper = new TeamMapperGateway(connection, transaction).GetAllTeamMappers();
                var wftdaData = GetWftdaRankingsData();
                var ftsData = GetFtsRankingsData();
                teamRankings = new List<TeamRating>(250);
                var leftOut = new List<WftdaRankingData>();
                foreach(WftdaRankingData wftda in wftdaData)
                {
                    var fts = ftsData.FirstOrDefault(f => string.Equals(wftda.TeamName.Substring(0, 10), f.TeamName.Substring(0, 10), StringComparison.OrdinalIgnoreCase));
                    if(fts != null)
                    {
                        teamRankings.Add(new TeamRating
                            {
                                FtsRank = fts.Rank,
                                FtsScore = fts.Rating,
                                TeamID = 0,
                                TeamName = wftda.TeamName,
                                WftdaRank = wftda.Rank,
                                WftdaScore = wftda.RatingScore,
                                WftdaStrength = wftda.Strength
                            });
                    }
                    else
                    {
                        // try the team mapper?
                        TeamMapper map = teamMapper.FirstOrDefault(tm => string.Equals(tm.TeamSpelling, wftda.TeamName, StringComparison.OrdinalIgnoreCase));
                        if(map != null)
                        {
                            var otherMaps = teamMapper.Where(tm => tm.TeamID == map.TeamID).Select(tm => tm.TeamSpelling);
                            fts = ftsData.FirstOrDefault(f => otherMaps.Contains(f.TeamName));
                            if (fts != null)
                            {
                                teamRankings.Add(new TeamRating
                                {
                                    FtsRank = fts.Rank,
                                    FtsScore = fts.Rating,
                                    TeamID = map.TeamID,
                                    TeamName = wftda.TeamName,
                                    WftdaRank = wftda.Rank,
                                    WftdaScore = wftda.RatingScore,
                                    WftdaStrength = wftda.Strength
                                });
                            }
                            else
                            {
                                leftOut.Add(wftda);
                            }
                        }

                    }
                }

                List<TeamRating> leftOvers = new List<TeamRating>();
                foreach(TeamRating teamRating in teamRankings)
                {
                    if (teamRating.TeamID > 0) continue;
                    var team = teams.FirstOrDefault(t => string.Equals(t.Name,teamRating.TeamName, StringComparison.OrdinalIgnoreCase));
                    if(team != null)
                    {
                        teamRating.TeamID = team.ID;
                    }
                    else
                    {
                        var league = leagues.FirstOrDefault(l => string.Equals(l.Name, teamRating.TeamName, StringComparison.OrdinalIgnoreCase));
                        if (league != null)
                        {
                            team = teams.First(t => t.LeagueID == league.ID);
                            teamRating.TeamID = team.ID;
                        }
                        else
                        {
                            // try the team mapper?
                            TeamMapper map = teamMapper.FirstOrDefault(tm => string.Equals(tm.TeamSpelling, teamRating.TeamName, StringComparison.OrdinalIgnoreCase));
                            if (map != null)
                            {
                                teamRating.TeamID = map.TeamID;
                            }
                            // TODO: else, create the League and the team? The nature of leagueID makes that tough...
                        }
                    }
                }
                trGateway.InsertTeamRatings(teamRankings.Where(tr => tr.TeamID != 0).ToList());
            }
            transaction.Commit();
            connection.Close();
            return teamRankings;
        }