Ejemplo n.º 1
0
        public List <PlayerStats> GetAllStats(int?teamID = null, int?sportID = null, int?leagueID = null, int?playerID = null, DataCache dataCache = null)
        {
            var         stats    = GameDataProvider.GetInstance().GetStats(teamID: teamID, leagueID: leagueID, playerID: playerID, dataCache: dataCache);
            var         players  = PlayerDataProvider.GetInstance().GetPlayers(teamID: teamID, leagueID: leagueID, playerID: playerID, dataCache: dataCache);
            List <Team> teamList = new List <Team>();

            if (teamID.HasValue)
            {
                teamList.Add(new Team()
                {
                    ID       = teamID.Value,
                    LeagueID = leagueID.Value,
                    SportID  = sportID.Value
                });
            }
            else
            {
                teamList = TeamsService.GetInstance().GetTeams(leagueID: leagueID, playerID: playerID, showAll: true, dataCache: dataCache);
            }
            var statTypes = GameDataProvider.GetInstance().GetStatTypes(sportID, dataCache: dataCache);
            var gameStats = stats.Select(s => ConvertObjects.ConvertType2(s)).ToList();

            var playerStats = new List <PlayerStats>();

            foreach (var player in players)
            {
                foreach (var team in teamList)
                {
                    var playerStat = new PlayerStats()
                    {
                        TeamID       = team.ID,
                        LeagueID     = team.LeagueID,
                        SportID      = team.SportID,
                        PlayerName   = player.Name,
                        PlayerNumber = teamID.HasValue ? player.Number : team.PlayerNumber,
                        GameID       = 0,
                        PlayerID     = player.ID,
                        Stats        = new List <Stat>()
                    };

                    foreach (var statType in statTypes.Where(st => !st.IsCalculated && st.SportID == team.SportID))
                    {
                        var stat = stats.Where(s => s.StatTypeID == statType.StatTypeID && s.PlayerID == player.ID && s.LeagueID == team.LeagueID).Sum(s => s.Value);
                        playerStat.Stats.Add(new Stat()
                        {
                            Value    = stat,
                            StatType = ConvertObjects.ConvertType(statType)
                        });
                    }

                    playerStats.Add(playerStat);
                }
            }

            foreach (var playerStat in playerStats)
            {
                var playerGameStats = gameStats.Where(g => g.PlayerID == playerStat.PlayerID && g.LeagueID == playerStat.LeagueID).ToList();
                foreach (var calcStat in statTypes.Where(st => st.IsCalculated && st.SportID == playerStat.SportID))
                {
                    playerStat.Stats.Add(new Stat()
                    {
                        StatType = ConvertObjects.ConvertType(calcStat),
                        Value    = StatsCalculations.GetValue((CalculatedStatTypes)calcStat.StatTypeID, playerGameStats)
                    });
                }
            }
            return(playerStats);
        }
Ejemplo n.º 2
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);
            }
        }