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); } }
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); } }
public void UpdateGame(Game game) { if (UserHelper.HasUpdatePermissions()) { GameDataProvider.GetInstance().UpdateGame(ConvertObjects.ConvertType(game)); } }
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); }
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"); } }
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()); }
public List <StatGroup> GetStatGroups(int sportID, int gameID, int?playerID) { if (UserHelper.HasGetPermissions(gameID: gameID)) { var statGroups = new List <StatGroup>(); if (sportID == (int)SportsList.Baseball) { var dtoStatGroups = GameDataProvider.GetInstance().GetStatGroups(gameID, playerID); statGroups = dtoStatGroups.Select(sg => new StatGroup() { GroupID = sg.GroupID }).ToList(); } return(statGroups); } else { throw new UnauthorizedAccessException("nope"); } }
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() }); }
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() }); }
public List <State> GetStates(int sportID) { var statStates = GameDataProvider.GetInstance().GetStates(sportID); return(statStates.Select(s => ConvertObjects.ConvertType(s)).ToList()); }
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); }
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); } }
public List <StatType> GetStatTypes(int?sportID = null, DataCache dataCache = null) { return(GameDataProvider.GetInstance().GetStatTypes(sportID, dataCache) .Select(s => ConvertObjects.ConvertType(s)).ToList()); }
public List <GameLog> GetGameLog(int gameID, DataCache dataCache = null) { var game = GameDataProvider.GetInstance().GetGameLog(gameID: gameID); return(game.Select(g => ConvertObjects.ConvertType(g)).ToList()); }
public NextBatter SaveNewGameState(BaseballGameState state, bool manualAdjustment, DataCache dataCache = null) { var nextBatterResult = new NextBatter(); if (UserHelper.HasUpdatePermissions()) { var game = GamesService.GetInstance().GetGame(state.GameID, dataCache: dataCache); if (!manualAdjustment) { var atBatTeamID = state.TopOfInning ? game.Team1ID : game.Team2ID; var atBatPlayerID = state.TopOfInning ? state.Team1Player?.ID : state.Team2Player?.ID; var battingOrder = GameDataProvider.GetInstance().GetBattingOrder(state.GameID, atBatTeamID); if (battingOrder.Count == 0) { battingOrder.Add(new DTOBattingOrder() { BattingOrder = 1 }); } var currentBatter = battingOrder.First(bo => bo.PlayerID == atBatPlayerID); var nextBatter = battingOrder.FirstOrDefault(bo => bo.BattingOrder == currentBatter.BattingOrder + 1); if (nextBatter == null) { nextBatter = battingOrder.First(); } if (state.TopOfInning) { state.Team1Player = new PlayerBase() { ID = nextBatter.PlayerID }; } else { state.Team2Player = new PlayerBase() { ID = nextBatter.PlayerID }; } nextBatterResult.PlayerID = nextBatter.PlayerID; nextBatterResult.TeamID = atBatTeamID; } //Change Teams if (state.NumberOfOuts == 3) { state.NumberOfOuts = 0; state.TopOfInning = !state.TopOfInning; if (state.TopOfInning) { state.Inning++; } state.PlayerOnFirst = null; state.PlayerOnSecond = null; state.PlayerOnThird = null; var nextAtBatTeamID = state.TopOfInning ? game.Team1ID : game.Team2ID; var battingOrderNextTeam = GameDataProvider.GetInstance().GetBattingOrder(state.GameID, nextAtBatTeamID); nextBatterResult.TeamID = nextAtBatTeamID; var nextAtBatPlayer = state.TopOfInning ? state.Team1Player : state.Team2Player; if (nextAtBatPlayer == null) { if (battingOrderNextTeam.Any()) { nextBatterResult.PlayerID = battingOrderNextTeam.First().PlayerID; } else { nextBatterResult.PlayerID = null; } } else { nextBatterResult.PlayerID = battingOrderNextTeam.FirstOrDefault(bo => bo.PlayerID == nextAtBatPlayer.ID).PlayerID; } } GameDataProvider.GetInstance().SaveBaseballGameState(ConvertObjects.ConvertType2(state)); } return(nextBatterResult); }