public TeamPlayerPerformanceCalculator(string connString)
        {
            _connectionString = connString;
            SqlConnection connection = new SqlConnection(_connectionString);
            connection.Open();
            SqlTransaction transaction = connection.BeginTransaction();

            // get bouts
            var bouts = new BoutGateway(connection, transaction).GetBouts().OrderBy(b => b.BoutDate);
            IList<Jam> jams = new JamGateway(connection, transaction).GetAllJams();
            IList<JamPlayer> jamPlayers = new JamPlayerGateway(connection, transaction).GetJamPlayers();

            var jamPlayerMap = jamPlayers.GroupBy(jp => jp.JamID).ToDictionary(g => g.Key, g => g.ToList());

            foreach(Bout bout in bouts)
            {
                var boutJams = jams.Where(j => j.BoutID == bout.ID);

                // get teamplayerperformance values
                // scrape FTS?
                // get FTS data for this bout (input ratings, differential, ratings change)

                // Calculate an estimated Team Str based on play rate of players
                double homeTeamStrength, awayTeamStrength;
                CalculateEstimatedTeamStrength(boutJams, bout, jamPlayerMap, out homeTeamStrength, out awayTeamStrength);

                // Calculate Expected Ratio from Team Str comparison
                CalculateExpectedHomeTeamShare(bout, homeTeamStrength, awayTeamStrength);

                // Calculate Expected Delta (Modify both sides scores equally?)
                // Convert expected delta to PerfAgainstAverage
                // ??? Compare Sum of PerfAgainstAverage to Actual Delta
                // ??? Adjust player Ratings to align Team Str with results
            }
        }
        public IList<Dictionary<int, PlayerPerformance>> GetAllPlayerPointPerformances(int numberOfIterations)
        {
            // pull data
            SqlConnection connection = new SqlConnection(_connectionString);
            connection.Open();
            SqlTransaction transaction = connection.BeginTransaction();
            var players = new PlayerGateway(connection, transaction).GetAllPlayers().GroupBy(p => p.ID).ToDictionary(g => g.Key, g => g.ToDictionary(g2 => g2.TeamID));
            var jams = new JamGateway(connection, transaction).GetAllJams().OrderBy(j => j.ID);
            var jamBoutMap = jams.ToDictionary(j => j.ID, j => j.BoutID);
            var jpe = new JamPlayerEffectivenessGateway(connection, transaction).GetAllJamPlayerEffectiveness();
            var jamData = new JamDataGateway(connection, transaction).GetAllJamData()
                .GroupBy(jd => jd.JamID)
                .ToDictionary(g => g.Key, g => g.ToDictionary(g2 => g2.TeamID));
            var teams = new TeamGateway(connection, transaction).GetAllTeams().ToDictionary(t => t.ID);
            var bouts = new BoutGateway(connection, transaction).GetBouts().ToDictionary(t => t.ID);
            var penalties = new PenaltyGateway(connection, transaction).GetAllPenalties()
                .GroupBy(p => p.JamID)
                .ToDictionary(
                    g => g.Key,
                    g => g.GroupBy(g2 => g2.PlayerID).ToDictionary(g3 => g3.Key, g3 => g3.ToList()));
            var pgs = new PenaltyGroupGateway(connection, transaction).GetAllPenaltyGroups();
            Dictionary<int, int> boxTimeEstimates = new BoxTimeEstimateGateway(connection, transaction).GetAllBoxTimeEstimates();
            Dictionary<FoulComparison, Dictionary<int, float>> sss = new SituationalScoreGateway(connection, transaction).GetAllSituationalScores();
            AveragePenaltyCostPerJam avgPenCost = new AveragePenaltyCostGateway(connection, transaction).GetAveragePenaltyCost();
            Dictionary<int, Dictionary<int, JamPlayer>> jamPlayerMap = new JamPlayerGateway(connection, transaction).GetJamPlayers()
                .GroupBy(jp => jp.JamID)
                .ToDictionary(g => g.Key, g => g.ToDictionary(g2 => g2.PlayerID));
            transaction.Commit();
            connection.Close();

            Dictionary<FoulComparison, float> medians = CalculateMedianScores(sss);
            PenaltyCostCalculator ppcCalc = new PenaltyCostCalculator(_connectionString);
            Dictionary<int, double> groupPenaltyCostMap = PenaltyCostCalculator.CalculatePointCosts(jamData, jamPlayerMap, pgs, boxTimeEstimates, sss);
            Dictionary<int, double> jamTotalPortionMap = new Dictionary<int, double>(300);

            var previousResult = GenerateInitialPlayerPerformance(players, jams, jpe, jamData, teams, bouts, penalties, pgs, avgPenCost, medians, groupPenaltyCostMap, jamTotalPortionMap);

            List<Dictionary<int, PlayerPerformance>> result = new List<Dictionary<int, PlayerPerformance>>(numberOfIterations);
            result.Add(previousResult);
            int i = 0;
            while(i < numberOfIterations)
            {
                previousResult = GeneratePlayerPerformanceIteration(previousResult, jams, jamData, jamTotalPortionMap, jpe, avgPenCost);
                result.Add(previousResult);
                i++;
            }

            return result;
        }
Esempio n. 3
0
 private List<JamPlayer> AddJamPlayers(Dictionary<string, Player> playerMap, Jam jam, IList<PlayerLineupModel> lineups, JamPlayerGateway gateway)
 {
     List<JamPlayer> list = new List<JamPlayer>();
     foreach (PlayerLineupModel lineup in lineups)
     {
         if(lineup == null)
         {
             // TODO: handle null players
             Console.WriteLine(jam.ToString() + ": empty player spot");
             continue;
         }
         Player player = playerMap[lineup.PlayerNumber];
         list.Add(gateway.AddJamPlayer(jam.ID, player.ID, player.TeamID, lineup.IsJammer, lineup.IsPivot));
     }
     return list;
 }
Esempio n. 4
0
        private List<JamPlayer> AddJamPlayers(Dictionary<string, Player> playerMap, Jam jam, IList<PlayerLineupModel> lineups, JamPlayerGateway gateway)
        {
            List<JamPlayer> list = new List<JamPlayer>();
            List<string> duplicateCheckList = new List<string>();
            foreach (PlayerLineupModel lineup in lineups)
            {
                if(lineup == null)
                {
                    // TODO: handle null players
                    Console.WriteLine(jam.ToString() + ": empty player spot");
                    continue;
                }
                if (duplicateCheckList.Contains(lineup.PlayerNumber))
                {
                    throw new InvalidOperationException(string.Format("{0}: #{1} in lineup multiple times.", jam, lineup.PlayerNumber));
                }

                duplicateCheckList.Add(lineup.PlayerNumber);
                Player player = playerMap[lineup.PlayerNumber];
                list.Add(gateway.AddJamPlayer(jam.ID, player.ID, player.TeamID, lineup.IsJammer, lineup.IsPivot));
            }
            return list;
        }
Esempio n. 5
0
        private Dictionary<int, List<JamPlayer>> CreateJamPlayerMap(Dictionary<string, Player> homePlayerMap, Dictionary<string, Player> awayPlayerMap, 
                                                                    IList<Jam> jamList, IList<JamLineupModel> lineups)
        {
            Dictionary<int, List<JamPlayer>> map = new Dictionary<int, List<JamPlayer>>();
            JamPlayerGateway jamPlayerGateway = new JamPlayerGateway(_connection, _transaction);

            foreach(JamLineupModel jamLineup in lineups)
            {
                Jam jam = jamList.First(j => j.IsFirstHalf == jamLineup.IsFirstHalf && j.JamNumber == jamLineup.JamNumber);
                List<JamPlayer> list = AddJamPlayers(homePlayerMap, jam, jamLineup.HomeLineup, jamPlayerGateway);
                list.AddRange(AddJamPlayers(awayPlayerMap, jam, jamLineup.AwayLineup, jamPlayerGateway));
                map[jam.ID] = list;
            }
            return map;
        }