Example #1
0
        public PlayerStats GetStats(int sportID, int gameID, int?playerID, int teamID, int leagueID, DataCache dataCache = null)
        {
            var stats  = GameDataProvider.GetInstance().GetStats(gameID, playerID: playerID, teamID: teamID, dataCache: dataCache);
            var player = PlayersService.GetInstance().GetPlayers(teamID: teamID, gameID: gameID, leagueID: leagueID, dataCache: dataCache).Where(p => p.ID == playerID).First();

            return(new PlayerStats()
            {
                PlayerName = player.Name,
                TeamID = teamID,
                GameID = gameID,
                PlayerID = player.ID,
                PlayerNumber = player.Number,
                Stats = stats.Select(s => new Stat()
                {
                    StatType = ConvertObjects.ConvertType(s),
                    GroupID = s.GroupID,
                    Value = s.Value
                }).ToList()
            });
        }
Example #2
0
        public PlayerStats GetPlayerStats(int playerID, DataCache dataCache = null)
        {
            var stats  = GameDataProvider.GetInstance().GetStats(playerID: playerID, dataCache: dataCache);
            var player = PlayersService.GetInstance().GetPlayer(playerID, dataCache: dataCache);

            return(new PlayerStats()
            {
                PlayerName = player.Name,
                PlayerID = player.ID,
                PlayerNumber = player.Number,
                Stats = stats.Select(s => new Stat()
                {
                    StatType = ConvertObjects.ConvertType(s),
                    GroupID = s.GroupID,
                    Value = s.Value,
                    LeagueID = s.LeagueID,
                    GameID = s.GameID,
                    TeamID = s.TeamID,
                    Sport = (SportsList)s.SportID
                }).ToList()
            });
        }
Example #3
0
        public Game GetGame(int gameID, DataCache dataCache = null)
        {
            var dtoGame    = GameDataProvider.GetInstance().GetGames(gameID: gameID, dataCache: dataCache).First();
            var game       = ConvertObjects.ConvertType(dtoGame);
            var validTeams = new List <int>()
            {
                game.Team1ID, game.Team2ID
            };
            var teams = TeamsService.GetInstance().GetTeams(validTeams: validTeams, dataCache: dataCache);

            if (UserHelper.HasGetPermissions(game))
            {
                var stats     = GameDataProvider.GetInstance().GetStats(gameID: gameID, dataCache: dataCache);
                var statTypes = GameDataProvider.GetInstance().GetStatTypes(game.SportID, dataCache: dataCache);
                var players   = PlayersService.GetInstance().GetPlayers(gameID: gameID, leagueID: game.LeagueID, dataCache: dataCache);
                var gameStats = stats.Select(s => ConvertObjects.ConvertType2(s)).ToList();

                var playerStats = new List <PlayerStats>();
                foreach (var player in players)
                {
                    var playerStat = new PlayerStats()
                    {
                        GameID     = gameID,
                        PlayerID   = player.ID,
                        PlayerName = player.Name,
                        TeamID     = player.TeamID,
                        Stats      = new List <Stat>()
                    };

                    foreach (var statType in statTypes.Where(st => !st.IsCalculated))
                    {
                        var existingStat = gameStats.FirstOrDefault(s =>
                                                                    s.StatType.ID == statType.StatTypeID &&
                                                                    s.PlayerID == player.ID &&
                                                                    s.TeamID == player.TeamID);

                        var stat = new GameStat()
                        {
                            GameID   = gameID,
                            PlayerID = player.ID,
                            TeamID   = player.TeamID,
                            Value    = 0,
                            StatType = ConvertObjects.ConvertType(statType)
                        };

                        if (existingStat != null)
                        {
                            stat.Value = gameStats.Where(s =>
                                                         s.StatType.ID == statType.StatTypeID &&
                                                         s.PlayerID == player.ID &&
                                                         s.TeamID == player.TeamID).Sum(s => s.Value);
                        }
                        stat.StatType.GridDisplayOrder      = statType.GridDisplayOrder;
                        stat.StatType.SelectionDisplayOrder = statType.SelectionDisplayOrder;
                        playerStat.Stats.Add(ConvertObjects.ConvertType(stat));
                    }

                    playerStats.Add(playerStat);
                }

                int team1score = 0;
                int team2score = 0;

                team1score = GetTeamScore(gameStats, game, true);
                team2score = GetTeamScore(gameStats, game, false);

                var selectedGame = new Game()
                {
                    ID          = game.ID,
                    GameDate    = game.GameDate,
                    Team1ID     = game.Team1ID,
                    Team1Score  = team1score,
                    Team1Name   = teams.First(t => t.ID == game.Team1ID).Name,
                    Team2ID     = game.Team2ID,
                    Team2Score  = team2score,
                    Team2Name   = teams.First(t => t.ID == game.Team2ID).Name,
                    PlayerStats = playerStats,
                    SportID     = game.SportID
                };

                foreach (var ps in selectedGame.PlayerStats)
                {
                    var playerGameStats = gameStats.Where(g => g.PlayerID == ps.PlayerID).ToList();
                    foreach (var calcStat in statTypes.Where(st => st.IsCalculated))
                    {
                        ps.Stats.Add(new Stat()
                        {
                            StatType = ConvertObjects.ConvertType(calcStat),
                            Value    = StatsCalculations.GetValue((CalculatedStatTypes)calcStat.StatTypeID, playerGameStats)
                        });
                    }
                }

                return(selectedGame);
            }
            else
            {
                return(null);
            }
        }
Example #4
0
        public AtBatStates GetGameStates(PlayerGameStat stat, bool isLatestGroup, DataCache dataCache = null)
        {
            AtBatStates states  = new AtBatStates();
            var         players = PlayersService.GetInstance().GetPlayers(stat.TeamID, gameID: stat.GameID, dataCache: dataCache);
            var         player  = players.First(p => p.ID == stat.PlayerID);

            states.OriginalState        = GetExistingGameState(gameID: stat.GameID, leagueID: stat.LeagueID, dataCache: dataCache);
            states.OriginalState.GameID = stat.GameID;
            if (states.OriginalState.Inning == 0)
            {
                states.OriginalState.Inning      = 1;
                states.OriginalState.TopOfInning = true;
            }
            BaseballGameState newState = new BaseballGameState()
            {
                GameID        = states.OriginalState.GameID,
                NumberOfOuts  = states.OriginalState.NumberOfOuts,
                Inning        = states.OriginalState.Inning,
                TopOfInning   = states.OriginalState.TopOfInning,
                RunnersScored = new List <PlayerBase>(),
                RunnersOut    = new List <PlayerBase>(),
                Team1Player   = states.OriginalState.TopOfInning ? new PlayerBase()
                {
                    ID = stat.PlayerID
                } : states.OriginalState.Team1Player,
                Team2Player = !states.OriginalState.TopOfInning ? new PlayerBase()
                {
                    ID = stat.PlayerID
                } : states.OriginalState.Team2Player,
            };

            bool changeState         = true;
            bool nextAtBat           = false;
            bool potentialAdjustment = false;

            if (stat.Value == -1 || !isLatestGroup)
            {
                newState.PlayerOnFirst  = states.OriginalState.PlayerOnFirst;
                newState.PlayerOnSecond = states.OriginalState.PlayerOnSecond;
                newState.PlayerOnThird  = states.OriginalState.PlayerOnThird;
                changeState             = false;
                potentialAdjustment     = false;
            }
            else
            {
                if (isLatestGroup)
                {
                    if (stat.StatTypeID == (int)CalculatedStatTypes.Single || stat.StatTypeID == (int)CalculatedStatTypes.FC)
                    {
                        newState.PlayerOnFirst = player;
                        if (states.OriginalState.PlayerOnThird != null)
                        {
                            potentialAdjustment = true;
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnThird);
                        }
                        if (states.OriginalState.PlayerOnSecond != null)
                        {
                            potentialAdjustment    = true;
                            newState.PlayerOnThird = states.OriginalState.PlayerOnSecond;
                        }
                        if (states.OriginalState.PlayerOnFirst != null)
                        {
                            potentialAdjustment     = true;
                            newState.PlayerOnSecond = states.OriginalState.PlayerOnFirst;
                        }
                        nextAtBat = true;
                    }
                    else if (stat.StatTypeID == (int)CalculatedStatTypes.Double)
                    {
                        newState.PlayerOnSecond = player;
                        if (states.OriginalState.PlayerOnThird != null)
                        {
                            potentialAdjustment = true;
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnThird);
                        }
                        if (states.OriginalState.PlayerOnSecond != null)
                        {
                            potentialAdjustment = true;
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnSecond);
                        }
                        if (states.OriginalState.PlayerOnFirst != null)
                        {
                            potentialAdjustment    = true;
                            newState.PlayerOnThird = states.OriginalState.PlayerOnFirst;
                        }
                        nextAtBat = true;
                    }
                    else if (stat.StatTypeID == (int)CalculatedStatTypes.Triple)
                    {
                        newState.PlayerOnThird = player;
                        if (states.OriginalState.PlayerOnFirst != null)
                        {
                            potentialAdjustment = true;
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnFirst);
                        }
                        if (states.OriginalState.PlayerOnSecond != null)
                        {
                            potentialAdjustment = true;
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnSecond);
                        }
                        if (states.OriginalState.PlayerOnThird != null)
                        {
                            potentialAdjustment = true;
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnThird);
                        }
                        nextAtBat = true;
                    }
                    else if (stat.StatTypeID == (int)CalculatedStatTypes.HomeRun)
                    {
                        newState.RunnersScored.Add(player);
                        potentialAdjustment = true;
                        if (states.OriginalState.PlayerOnFirst != null)
                        {
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnFirst);
                        }
                        if (states.OriginalState.PlayerOnSecond != null)
                        {
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnSecond);
                        }
                        if (states.OriginalState.PlayerOnThird != null)
                        {
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnThird);
                        }
                        nextAtBat = true;
                    }
                    else if (stat.StatTypeID == (int)CalculatedStatTypes.GroundOut)
                    {
                        newState.NumberOfOuts++;
                        if (states.OriginalState.PlayerOnFirst != null)
                        {
                            potentialAdjustment     = true;
                            newState.PlayerOnSecond = states.OriginalState.PlayerOnFirst;
                        }
                        if (states.OriginalState.PlayerOnSecond != null)
                        {
                            potentialAdjustment    = true;
                            newState.PlayerOnThird = states.OriginalState.PlayerOnSecond;
                        }
                        if (states.OriginalState.PlayerOnThird != null && newState.NumberOfOuts < 3)
                        {
                            potentialAdjustment = true;
                            newState.RunnersScored.Add(states.OriginalState.PlayerOnThird);
                        }
                        nextAtBat = true;
                    }
                    else if (stat.StatTypeID == (int)CalculatedStatTypes.FlyOut || stat.StatTypeID == (int)CalculatedStatTypes.SF)
                    {
                        potentialAdjustment     = true;
                        newState.PlayerOnFirst  = states.OriginalState.PlayerOnFirst;
                        newState.PlayerOnSecond = states.OriginalState.PlayerOnSecond;
                        newState.PlayerOnThird  = states.OriginalState.PlayerOnThird;
                        newState.NumberOfOuts++;
                        nextAtBat = true;
                    }
                    else if (stat.StatTypeID == (int)CalculatedStatTypes.Strikeout)
                    {
                        newState.PlayerOnFirst  = states.OriginalState.PlayerOnFirst;
                        newState.PlayerOnSecond = states.OriginalState.PlayerOnSecond;
                        newState.PlayerOnThird  = states.OriginalState.PlayerOnThird;
                        newState.NumberOfOuts++;
                        nextAtBat = true;
                    }
                    else
                    {
                        newState.PlayerOnFirst  = states.OriginalState.PlayerOnFirst;
                        newState.PlayerOnSecond = states.OriginalState.PlayerOnSecond;
                        newState.PlayerOnThird  = states.OriginalState.PlayerOnThird;
                        changeState             = false;
                    }
                }
            }
            bool inningChange = false;

            if (newState.NumberOfOuts == 3)
            {
                inningChange = true;
            }

            //Modifying old at-bat, done mess with the state
            if (!isLatestGroup)
            {
                inningChange        = false;
                nextAtBat           = false;
                potentialAdjustment = false;
                changeState         = false;
            }

            newState.InningChanged       = inningChange;
            newState.PotentialAdjustment = potentialAdjustment;
            newState.NextAtBat           = nextAtBat;
            newState.ChangeState         = changeState;

            states.NewState = newState;

            return(states);
        }