Ejemplo n.º 1
0
 public void AddTeam(Team team)
 {
     if (UserHelper.HasUpdatePermissions(team.ID))
     {
         TeamDataProvider.GetInstance().AddTeam(ConvertObjects.ConvertType(team));
     }
 }
Ejemplo n.º 2
0
 public void AddStat(PlayerGameStat stat, BaseballGameState state = null)
 {
     if (UserHelper.HasUpdatePermissions())
     {
         stat.States = new List <int>();
         if (state != null)
         {
             if (state.PlayerOnFirst != null)
             {
                 stat.States.Add((int)StatStates.First);
             }
             if (state.PlayerOnSecond != null)
             {
                 stat.States.Add((int)StatStates.Second);
             }
             if (state.PlayerOnThird != null)
             {
                 stat.States.Add((int)StatStates.Third);
             }
             if (state.NumberOfOuts == 1)
             {
                 stat.States.Add((int)StatStates.Out1);
             }
             if (state.NumberOfOuts == 2)
             {
                 stat.States.Add((int)StatStates.Out2);
             }
         }
         var gameStat = ConvertObjects.ConvertType(stat);
         GameDataProvider.GetInstance().AddStat(gameStat);
     }
 }
Ejemplo n.º 3
0
        public List <Game> GetGames(int?leagueID = null, int?playerID = null, int?teamID = null, DataCache dataCache = null)
        {
            if (UserHelper.HasGetPermissions(teamID: teamID, leagueID: leagueID))
            {
                var games     = GameDataProvider.GetInstance().GetGames(leagueID: leagueID, playerID: playerID, teamID: teamID, dataCache: dataCache);
                var stats     = GameDataProvider.GetInstance().GetStats(leagueID: leagueID, playerID: playerID, dataCache: dataCache);
                var gameStats = stats.Select(s => ConvertObjects.ConvertType2(s)).ToList();

                return(games.Select(g =>
                {
                    var game = ConvertObjects.ConvertType(g);
                    return new Game()
                    {
                        ID = g.ID,
                        GameDate = g.GameDate,
                        Team1ID = g.Team1ID,
                        Team1Score = GetTeamScore(gameStats, game, true),
                        Team2ID = g.Team2ID,
                        Team2Score = GetTeamScore(gameStats, game, false)
                    };
                }).ToList());
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 4
0
 public void UpdateGame(Game game)
 {
     if (UserHelper.HasUpdatePermissions())
     {
         GameDataProvider.GetInstance().UpdateGame(ConvertObjects.ConvertType(game));
     }
 }
Ejemplo n.º 5
0
        public UserResult GetUser()
        {
            var loggedInUser = UserService.GetInstance().GetUser();

            if (loggedInUser == null)
            {
                var cookie = HttpContext.Current.Request.Cookies[_cookieName];
                if (cookie != null && !String.IsNullOrEmpty(cookie.Value))
                {
                    try
                    {
                        string     username = Encoding.UTF8.GetString(MachineKey.Unprotect(Convert.FromBase64String(cookie.Value)));
                        UserResult user     = new UserResult()
                        {
                            UserName = username
                        };
                        loggedInUser = UserService.GetInstance().GetUser(ConvertObjects.ConvertType(user), false);
                    }
                    catch
                    {
                        return(null);
                    }
                }
            }
            if (loggedInUser != null)
            {
                loggedInUser.HasOneTeam   = loggedInUser.Teams.Count == 1;
                loggedInUser.HasOneLeague = loggedInUser.Leagues.Count == 1;
            }
            return(ConvertObjects.ConvertType2(loggedInUser));
        }
Ejemplo n.º 6
0
        public void SaveOrder([FromBody] StatRequest statRequest)
        {
            var gameStat = ConvertObjects.ConvertType(statRequest);

            gameStat.Override = true;
            GamesService.GetInstance().AddStat(gameStat);
        }
Ejemplo n.º 7
0
        public PlayerResult Get(int id)
        {
            var dataCache = new DataCache();
            var player    = PlayersService.GetInstance().GetPlayer(id, dataCache);
            var sports    = SportsService.GetInstance().GetSports();
            var leagues   = LeaguesService.GetInstance().GetLeagues();
            var stats     = StatsService.GetInstance().GetAllStats(playerID: id, dataCache: dataCache);
            var statTypes = GamesService.GetInstance().GetStatTypes().OrderBy(s => s.GridDisplayOrder);

            var playerResult = new PlayerResult()
            {
                ID    = player.ID,
                Name  = player.Name,
                Games = player.Games?.Select(g => new PlayerGameResult()
                {
                    ID         = g.ID,
                    GameDate   = g.GameDate,
                    Team1Score = g.Team1Score,
                    Team2Score = g.Team2Score
                }).ToList(),
                Teams = player.Teams?.Select(t =>
                {
                    var league = leagues.First(l => l.ID == t.LeagueID);
                    var sport  = sports.First(s => s.ID == league.SportID);
                    return(new PlayerTeamResult()
                    {
                        ID = t.ID,
                        Name = t.Name,
                        LeagueID = t.LeagueID,
                        Number = t.PlayerNumber,
                        SportID = league.SportID,
                        LeagueName = string.Format("{0} - {1} {2} - {3}", sport.Name, league.StartDate.Year, league.Season.ToString(), league.Name)
                    });
                }).OrderBy(t => t.LeagueName).ToList(),
                HockeyStats = stats
                              .Select(s => ConvertObjects.ConvertType(s))
                              .Where(s => s.Sport == SportsList.Hockey && leagues.Exists(l => l.ID == s.LeagueID))
                              .OrderBy(s => leagues.First(l => l.ID == s.LeagueID).StartDate)
                              .ToList(),
                BaseballStats = stats
                                .Select(s => ConvertObjects.ConvertType(s))
                                .Where(s => s.Sport == SportsList.Baseball && leagues.Exists(l => l.ID == s.LeagueID))
                                .OrderBy(s => leagues.First(l => l.ID == s.LeagueID).StartDate)
                                .ToList(),
                BasketballStats = stats
                                  .Select(s => ConvertObjects.ConvertType(s))
                                  .Where(s => s.Sport == SportsList.Basketball && leagues.Exists(l => l.ID == s.LeagueID))
                                  .OrderBy(s => leagues.First(l => l.ID == s.LeagueID).StartDate)
                                  .ToList(),
                HockeyStatTypes     = statTypes.Select(s => ConvertObjects.ConvertType(s)).Where(st => st.Sport == SportsList.Hockey).ToList(),
                BaseballStatTypes   = statTypes.Select(s => ConvertObjects.ConvertType(s)).Where(st => st.Sport == SportsList.Baseball).ToList(),
                BasketballStatTypes = statTypes.Select(s => ConvertObjects.ConvertType(s)).Where(st => st.Sport == SportsList.Basketball).ToList(),
            };

            UpdateStatRow(playerResult.HockeyStats, player, leagues);
            UpdateStatRow(playerResult.BaseballStats, player, leagues);
            UpdateStatRow(playerResult.BasketballStats, player, leagues);

            return(playerResult);
        }
Ejemplo n.º 8
0
        public BaseballGameState GetExistingGameState(int gameID, int leagueID, DataCache dataCache = null)
        {
            var gameState = ConvertObjects.ConvertType(GameDataProvider.GetInstance().GetBaseballGameState(gameID, dataCache));

            if (gameState.Inning == 0)
            {
                gameState.Inning      = 1;
                gameState.TopOfInning = true;
            }

            var players = PlayersService.GetInstance().GetPlayers(gameID: gameID, leagueID: leagueID, dataCache: dataCache);

            if (gameState.PlayerOnFirst != null)
            {
                var player = players.First(p => p.ID == gameState.PlayerOnFirst.ID);
                gameState.PlayerOnFirst.Name   = player.Name;
                gameState.PlayerOnFirst.Number = player.Number;
            }
            if (gameState.PlayerOnSecond != null)
            {
                var player = players.First(p => p.ID == gameState.PlayerOnSecond.ID);
                gameState.PlayerOnSecond.Name   = player.Name;
                gameState.PlayerOnSecond.Number = player.Number;
            }
            if (gameState.PlayerOnThird != null)
            {
                var player = players.First(p => p.ID == gameState.PlayerOnThird.ID);
                gameState.PlayerOnThird.Name   = player.Name;
                gameState.PlayerOnThird.Number = player.Number;
            }
            return(gameState);
        }
Ejemplo n.º 9
0
 public int AddPlayer([FromBody] TeamPlayerSaveRequest saveRequest)
 {
     try
     {
         return(PlayersService.GetInstance().AddPlayer(ConvertObjects.ConvertType(saveRequest.Player), saveRequest.TeamID, saveRequest.LeagueID));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Ejemplo n.º 10
0
        public StatsResult GetStats(int sportID, int?playerID, int gameID, int teamID, int?groupID, int leagueID)
        {
            var statsResult = GetStatTypes(sportID, playerID, gameID, teamID, groupID, leagueID);

            if (sportID == (int)SportsList.Baseball)
            {
                statsResult.BaseballGameStateResult             = ConvertObjects.ConvertType(BaseballService.GetInstance().GetExistingGameState(gameID, statsResult.LeagueID));
                statsResult.BaseballGameStateResult.IsStatsPage = true;
            }
            return(statsResult);
        }
Ejemplo n.º 11
0
 public int AddPlayer(Player player, int?teamID = null, int?leagueID = null)
 {
     if (UserHelper.HasUpdatePermissions())
     {
         return(PlayerDataProvider.GetInstance().AddPlayer(ConvertObjects.ConvertType(player), teamID, leagueID));
     }
     else
     {
         throw (new UnauthorizedAccessException("nope"));
     }
 }
Ejemplo n.º 12
0
 public TeamResult AddGame([FromBody] GameResultBase game)
 {
     try
     {
         GamesService.GetInstance().AddGame(ConvertObjects.ConvertType(game));
         return(GetTeamResult(game.Team1ID, game.LeagueID));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Ejemplo n.º 13
0
 public List <GameLogResult> GetGameLog(int gameID)
 {
     try
     {
         var gamelogs = GamesService.GetInstance().GetGameLog(gameID);
         return(gamelogs.Select(g => ConvertObjects.ConvertType(g)).ToList());
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Ejemplo n.º 14
0
        public User GetUser(User user, bool checkPassword)
        {
            if (checkPassword && String.IsNullOrEmpty(user.Password))
            {
                return(null);
            }

            var result = UserDataProvider.GetInstance().GetUser(ConvertObjects.ConvertType(user));

            UserHelper.CurrentUser = ConvertObjects.ConvertType(result);
            return(UserHelper.CurrentUser);
        }
Ejemplo n.º 15
0
 public void SavePlayer(Player player)
 {
     if (UserHelper.HasUpdatePermissions())
     {
         var dtoPlayer = ConvertObjects.ConvertType(player);
         PlayerDataProvider.GetInstance().SavePlayer(dtoPlayer);
         foreach (Team t in player.Teams)
         {
             dtoPlayer.Number = t.PlayerNumber;
             dtoPlayer.TeamID = t.ID;
             PlayerDataProvider.GetInstance().SaveTeamPlayer(dtoPlayer);
         }
     }
 }
Ejemplo n.º 16
0
        public bool LoginUser([FromBody] UserResult user)
        {
            var  userResult = UserService.GetInstance().GetUser(ConvertObjects.ConvertType(user), true);
            bool validLogin = false;

            if (userResult.Role > 0)
            {
                validLogin = true;
                if (user.RememberMe)
                {
                    CreateUserCookie(user.UserName);
                }
            }
            return(validLogin);
        }
Ejemplo n.º 17
0
 public bool CreateNewStatGroup(int sportID, int gameID, int?playerID, int teamID)
 {
     if (UserHelper.HasUpdatePermissions())
     {
         var statGroup = new StatGroup();
         if (sportID == (int)SportsList.Baseball)
         {
             statGroup = ConvertObjects.ConvertType(GameDataProvider.GetInstance().GetStatGroup(gameID, playerID, teamID, true));
         }
         return(true);
     }
     else
     {
         throw new UnauthorizedAccessException("nope");
     }
 }
Ejemplo n.º 18
0
        public List <Player> GetPlayers(int?teamID = null, int?gameID = null, int?leagueID = null, DataCache dataCache = null)
        {
            var players = PlayerDataProvider.GetInstance().GetPlayers(teamID: teamID, gameID: gameID, leagueID: leagueID, dataCache: dataCache);

            if (gameID.HasValue)
            {
                var dtoGame    = GameDataProvider.GetInstance().GetGames(gameID: gameID, dataCache: dataCache).First();
                var game       = ConvertObjects.ConvertType(dtoGame);
                var validTeams = new List <int>()
                {
                    game.Team1ID, game.Team2ID
                };
                if (!players.Exists(p => p.TeamID == game.Team1ID))
                {
                    //ADD TEAM PLAYER
                    var dtoPlayer = new DTOPlayer()
                    {
                        Name   = "Team 1 Player",
                        TeamID = game.Team1ID
                    };
                    players.Add(dtoPlayer);
                }

                if (!players.Exists(p => p.TeamID == game.Team2ID))
                {
                    //ADD TEAM PLAYER
                    var dtoPlayer = new DTOPlayer()
                    {
                        Name   = "Team 2 Player",
                        TeamID = game.Team2ID
                    };
                    players.Add(dtoPlayer);
                }
            }

            return(players
                   .Where(p => UserHelper.HasGetPermissions(playerID: p.ID, gameID: gameID))
                   .Select(p => new Player()
            {
                ID = p.ID,
                Name = p.Name,
                Number = p.Number,
                TeamID = p.TeamID
            })
                   .ToList());
        }
Ejemplo n.º 19
0
        public NextBatterResult SaveNewGameState([FromBody] BaseballGameStateResult gameState)
        {
            var dataCache = new DataCache();

            if (gameState.GameStat != null)
            {
                var originalState = BaseballService.GetInstance().GetExistingGameState(gameID: gameState.GameID, leagueID: gameState.LeagueID, dataCache: dataCache);
                GamesService.GetInstance().AddStat(ConvertObjects.ConvertType(gameState.GameStat), originalState);
                int rbis = 0;
                foreach (var player in gameState.RunnersScored)
                {
                    rbis++;
                    GamesService.GetInstance().AddStat(ConvertObjects.ConvertType(new StatRequest()
                    {
                        GameID     = gameState.GameID,
                        PlayerID   = player.ID,
                        StatTypeID = (int)CalculatedStatTypes.Runs,
                        TeamID     = gameState.GameStat.TeamID,
                        Value      = 1,
                        GroupID    = gameState.GameStat.GroupID,
                    }));
                }

                if (rbis > 0)
                {
                    GamesService.GetInstance().AddStat(ConvertObjects.ConvertType(new StatRequest()
                    {
                        GameID     = gameState.GameID,
                        PlayerID   = gameState.GameStat.PlayerID,
                        StatTypeID = (int)CalculatedStatTypes.RBI,
                        TeamID     = gameState.GameStat.TeamID,
                        Value      = rbis,
                        GroupID    = gameState.GameStat.GroupID,
                    }), originalState);
                }
            }
            bool manualAdjustment = gameState.GameStat == null;
            var  result           = BaseballService.GetInstance().SaveNewGameState(ConvertObjects.ConvertType(gameState), manualAdjustment, dataCache: dataCache);

            if (gameState.GameStat != null)
            {
                //Create new at bat group for player
                StatsService.GetInstance().CreateNewStatGroup((int)SportsList.Baseball, gameState.GameID, gameState.GameStat.PlayerID, gameState.GameStat.TeamID);
            }
            return(ConvertObjects.ConvertType(result));
        }
Ejemplo n.º 20
0
        public BaseballGameStateResult AddStat([FromBody] StatRequest statRequest)
        {
            var dataCache = new DataCache();
            var gameStat  = ConvertObjects.ConvertType(statRequest);

            AtBatStates gameStates = null;

            if (statRequest.IsBaseball)
            {
                gameStates = BaseballService.GetInstance().GetGameStates(gameStat, statRequest.IsLatestGroup, dataCache);
            }

            NextBatterResult nextBatter = null;
            bool             dataSaved  = false;

            if (gameStates == null || (!gameStates.NewState.PotentialAdjustment && !gameStates.NewState.InningChanged))
            {
                dataSaved = true;
                GamesService.GetInstance().AddStat(gameStat, gameStates?.OriginalState);
                if (statRequest.IsBaseball && gameStates.NewState.ChangeState)
                {
                    nextBatter = ConvertObjects.ConvertType(BaseballService.GetInstance().SaveNewGameState(gameStates.NewState, false, dataCache: dataCache));
                    //Create new at bat group for player
                    StatsService.GetInstance().CreateNewStatGroup((int)SportsList.Baseball, gameStat.GameID, gameStat.PlayerID, gameStat.TeamID);
                }
            }

            if (statRequest.IsBaseball)
            {
                var gameStateResult = ConvertObjects.ConvertType(gameStates.NewState);
                gameStateResult.GameStat    = statRequest;
                gameStateResult.NextBatter  = nextBatter;
                gameStateResult.DataSaved   = dataSaved;
                gameStateResult.IsStatsPage = true;
                return(gameStateResult);
            }
            else
            {
                return(new BaseballGameStateResult()
                {
                    DataSaved = true
                });
            }
        }
Ejemplo n.º 21
0
 public GetTeamsResult AddTeam([FromBody] TeamsResult team)
 {
     try
     {
         if (team.ID == 0)
         {
             TeamsService.GetInstance().AddTeam(ConvertObjects.ConvertType(team));
         }
         else
         {
             TeamsService.GetInstance().AddLeagueTeam(team.ID, team.LeagueID);
         }
         return(GetTeamsResult(team.LeagueID, team.SportID));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Ejemplo n.º 22
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()
            });
        }
Ejemplo n.º 23
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()
            });
        }
Ejemplo n.º 24
0
        private TeamResult GetTeamResult(int teamID, int leagueID)
        {
            var dataCache   = new DataCache();
            var team        = TeamsService.GetInstance().GetTeam(teamID, leagueID, dataCache: dataCache);
            var teams       = TeamsService.GetInstance().GetTeams(leagueID: team.LeagueID, showAll: true, dataCache: dataCache);
            var players     = PlayersService.GetInstance().GetPlayers(dataCache: dataCache);
            var playerStats = StatsService.GetInstance().GetAllStats(teamID, team.SportID, leagueID, dataCache: dataCache);
            var statTypes   = GamesService.GetInstance().GetStatTypes(team.SportID).OrderBy(s => s.GridDisplayOrder);

            var teamResult = new TeamResult()
            {
                ID    = team.ID,
                Name  = team.Name,
                Games = team.Games.Select(g => new TeamGameResult()
                {
                    ID            = g.ID,
                    GameDate      = g.GameDate,
                    OtherTeamName = g.Team1ID == team.ID ? teams.First(t => t.ID == g.Team2ID).Name : teams.First(t => t.ID == g.Team1ID).Name,
                    DidWin        = DidWin(g.Team1ID == team.ID, g.Team1Score, g.Team2Score),
                    HighScore     = Math.Max(g.Team1Score, g.Team2Score),
                    LowScore      = Math.Min(g.Team1Score, g.Team2Score),
                }).ToList(),
                Teams = teams.Select(t => new TeamsResult()
                {
                    ID   = t.ID,
                    Name = t.Name
                }).ToList(),
                AvailablePlayers = players.Where(p => !team.Players.Exists(tp => tp.ID == p.ID)).Select(p => new PlayersResult()
                {
                    ID   = p.ID,
                    Name = p.Name,
                }).ToList(),
                TeamPlayerStats = playerStats.Select(s => ConvertObjects.ConvertType(s)).ToList(),
                StatTypes       = statTypes.Select(s => ConvertObjects.ConvertType(s)).ToList(),
            };

            return(teamResult);
        }
Ejemplo n.º 25
0
        private StatsResult GetStatTypes(int sportID, int?playerID, int gameID, int teamID, int?groupID, int leagueID)
        {
            var dataCache   = new DataCache();
            var statTypes   = GamesService.GetInstance().GetStatTypes(sportID, dataCache: dataCache);
            var playerStats = StatsService.GetInstance().GetStats(sportID, gameID, playerID, teamID, leagueID, dataCache: dataCache);
            var game        = GamesService.GetInstance().GetGame(gameID, dataCache: dataCache);

            int?statGroupID = null;

            if (groupID.HasValue)
            {
                statGroupID = groupID.Value;
            }
            else
            {
                statGroupID = StatsService.GetInstance().GetStatGroup(sportID, gameID, playerID, teamID)?.GroupID;
            }
            var statGroups = StatsService.GetInstance().GetStatGroups(sportID, gameID, playerID);
            var states     = StatsService.GetInstance().GetStates(sportID);

            var statTypesResult = statTypes
                                  .Where(st => !st.IsCalculated)
                                  .Select(t => new StatTypesResult()
            {
                ID           = t.ID,
                Name         = t.Name,
                DisplayName  = t.DisplayName,
                DefaultShow  = t.DefaultShow,
                CurrentValue = playerStats.Stats
                               .Where(ps => ps.StatType?.ID == t.ID && IsValidStatWithGroup(ps.StatType, statGroupID, ps.GroupID))
                               .Sum(ps => ps.Value),
                GridDisplayOrder      = t.GridDisplayOrder,
                SelectionDisplayOrder = t.SelectionDisplayOrder,
                AutoGenerated         = t.AutoGenerated
            })
                                  .OrderBy(s => s.SelectionDisplayOrder);

            var statGroupsResult = statGroups.Select(s => new StatGroupResult()
            {
                GroupID = s.GroupID
            }).ToList();

            int i = 1;

            foreach (var result in statGroupsResult)
            {
                result.GroupName = "AB " + i++;
            }

            return(new StatsResult()
            {
                GroupID = statGroupID,
                PlayerID = playerStats.PlayerID,
                PlayerNumber = playerStats.PlayerNumber,
                PlayerName = playerStats.PlayerName,
                StatTypes = statTypesResult.ToList(),
                StatGroups = statGroupsResult,
                GameResult = ConvertObjects.ConvertType2(game),
                LeagueID = game.LeagueID
            });
        }
Ejemplo n.º 26
0
 public GameResult UpdateGame([FromBody] GameResultBase game)
 {
     GamesService.GetInstance().UpdateGame(ConvertObjects.ConvertType(game));
     return(GetGameResult(game.LeagueID, game.ID));
 }
Ejemplo n.º 27
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.º 28
0
        private GameResult GetGameResult(int leagueID, int gameID)
        {
            try
            {
                var dataCache = new DataCache();
                var game      = GamesService.GetInstance().GetGame(gameID, dataCache: dataCache);
                var teams     = new List <int>()
                {
                    game.Team1ID, game.Team2ID
                };
                var allTeams  = TeamsService.GetInstance().GetTeams(leagueID: leagueID, validTeams: teams, dataCache: dataCache);
                var players   = PlayersService.GetInstance().GetPlayers(gameID: gameID, leagueID: leagueID, dataCache: dataCache);
                var statTypes = GamesService.GetInstance().GetStatTypes(game.SportID, dataCache: dataCache).OrderBy(s => s.GridDisplayOrder);
                var gameState = BaseballService.GetInstance().GetExistingGameState(gameID, leagueID, dataCache: dataCache);

                int?playerAtBat = gameState.TopOfInning ? gameState.Team1Player?.ID : gameState.Team2Player?.ID;

                var gameResult = new GameResult()
                {
                    ID             = game.ID,
                    GameDate       = game.GameDate,
                    StatTypes      = statTypes.Select(s => ConvertObjects.ConvertType(s)).ToList(),
                    QuickStatTypes = statTypes
                                     .Where(st => st.QuickButtonOrder > 0)
                                     .Select(s => ConvertObjects.ConvertType(s))
                                     .OrderBy(s => s.QuickButtonOrder)
                                     .ToList(),
                    Team1ID          = game.Team1ID,
                    Team1Score       = game.Team1Score,
                    Team1Name        = game.Team1Name,
                    Team2ID          = game.Team2ID,
                    Team2Score       = game.Team2Score,
                    Team2Name        = game.Team2Name,
                    Team1PlayerStats = game.PlayerStats
                                       .Where(p => p.TeamID == game.Team1ID)
                                       .Select(g =>
                    {
                        var order = Convert.ToInt16(g.Stats.Where(s => s.StatType.ID == (int)CalculatedStatTypes.BattingOrder).Sum(s => s.Value));
                        return(new PlayerStatsResult()
                        {
                            PlayerID = g.PlayerID,
                            PlayerName = players.FirstOrDefault(p => p.ID == g.PlayerID)?.Name,
                            PlayerNumber = players.FirstOrDefault(p => p.ID == g.PlayerID)?.Number ?? 0,
                            Order = order == 0 ? (int?)null : order,
                            AtBat = game.SportID == (int)SportsList.Baseball && g.PlayerID == playerAtBat,
                            PlayerStats = g.Stats.OrderBy(s => s.StatType.GridDisplayOrder)
                                          .Select(s => new StatResult()
                            {
                                StatTypeID = s.StatType.ID,
                                Name = s.StatType.Name,
                                DefaultShow = s.StatType.DefaultShow,
                                Value = s.Value
                            }).ToList()
                        });
                    })
                                       .ToList(),
                    Team2PlayerStats = game.PlayerStats
                                       .Where(p => p.TeamID == game.Team2ID)
                                       .Select(g =>
                    {
                        var order = Convert.ToInt16(g.Stats.Where(s => s.StatType.ID == (int)CalculatedStatTypes.BattingOrder).Sum(s => s.Value));
                        return(new PlayerStatsResult()
                        {
                            PlayerID = g.PlayerID,
                            PlayerName = players.FirstOrDefault(p => p.ID == g.PlayerID)?.Name,
                            PlayerNumber = players.FirstOrDefault(p => p.ID == g.PlayerID)?.Number ?? 0,
                            Order = order == 0 ? (int?)null : order,
                            AtBat = game.SportID == (int)SportsList.Baseball && g.PlayerID == playerAtBat,
                            PlayerStats = g.Stats.OrderBy(s => s.StatType.GridDisplayOrder)
                                          .Select(s => new StatResult()
                            {
                                StatTypeID = s.StatType.ID,
                                Name = s.StatType.Name,
                                DefaultShow = s.StatType.DefaultShow,
                                Value = s.Value
                            }).ToList()
                        });
                    })
                                       .ToList(),
                    Teams = allTeams.Select(t => new TeamsResult()
                    {
                        ID   = t.ID,
                        Name = t.Name
                    })
                            .ToList()
                };

                if (game.SportID == (int)SportsList.Baseball)
                {
                    gameResult.Team1PlayerStats        = gameResult.Team1PlayerStats.OrderBy(p => p.Order == 0 || p.Order == null ? 99 : p.Order).ToList();
                    gameResult.Team2PlayerStats        = gameResult.Team2PlayerStats.OrderBy(p => p.Order == 0 || p.Order == null ? 99 : p.Order).ToList();
                    gameResult.BaseballGameStateResult = ConvertObjects.ConvertType(BaseballService.GetInstance().GetExistingGameState(gameID, leagueID));
                }

                return(gameResult);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 29
0
        public List <State> GetStates(int sportID)
        {
            var statStates = GameDataProvider.GetInstance().GetStates(sportID);

            return(statStates.Select(s => ConvertObjects.ConvertType(s)).ToList());
        }
Ejemplo n.º 30
0
 public List <PlayersResult> AddPlayer([FromBody] PlayersResult player)
 {
     PlayersService.GetInstance().AddPlayer(ConvertObjects.ConvertType(player));
     return(GetPlayers());
 }