diff --git a/SMB3Explorer/Models/Exports/SeasonSchedule.cs b/SMB3Explorer/Models/Exports/SeasonSchedule.cs index 7881a12..f6a075d 100644 --- a/SMB3Explorer/Models/Exports/SeasonSchedule.cs +++ b/SMB3Explorer/Models/Exports/SeasonSchedule.cs @@ -21,4 +21,16 @@ public class SeasonSchedule [Name("Away Team"), Index(4)] public string AwayTeam { get; set; } = string.Empty; + + [Name("Home Score"), Index(5)] + public int? HomeRunsScored { get; set; } + + [Name("Away Score"), Index(6)] + public int? AwayRunsScored { get; set; } + + [Name("Home Pitcher"), Index(7)] + public string? HomePitcherName { get; set; } = string.Empty; + + [Name("Away Pitcher"), Index(8)] + public string? AwayPitcherName { get; set; } = string.Empty; } \ No newline at end of file diff --git a/SMB3Explorer/Resources/Sql/MostRecentSeasonSchedule.sql b/SMB3Explorer/Resources/Sql/MostRecentSeasonSchedule.sql index 17e7f29..79a52a7 100644 --- a/SMB3Explorer/Resources/Sql/MostRecentSeasonSchedule.sql +++ b/SMB3Explorer/Resources/Sql/MostRecentSeasonSchedule.sql @@ -1,5 +1,3 @@ --- The t_season_schedule table apparently only tracks the current season for the given league, --- so the usage of this will be restricted to the current season WITH teams AS (SELECT ttli.GUID AS teamGUID, ttli.localID, tt.teamName FROM t_team_local_ids ttli @@ -10,20 +8,56 @@ WITH teams AS JOIN t_leagues l on c.leagueGUID = l.GUID JOIN t_franchise tf ON l.GUID = tf.leagueGUID WHERE l.GUID = CAST(@leagueId AS BLOB)), - seasons AS (SELECT id AS seasonID, RANK() OVER (ORDER BY id) AS seasonNum - FROM t_seasons - JOIN t_leagues ON t_seasons.historicalLeagueGUID = t_leagues.GUID - JOIN t_franchise tf ON t_leagues.GUID = tf.leagueGUID - WHERE t_leagues.GUID = CAST(@leagueId AS BLOB)) -SELECT tss.seasonID, - s.seasonNum, - RANK() OVER (PARTITION BY tss.seasonID ORDER BY rowid) as gameNumber, - (ROW_NUMBER() OVER (PARTITION BY tss.seasonID ORDER BY rowid) - 1) / - ((SELECT COUNT(*) FROM teams) / 2) + 1 AS day, - homeTeams.teamName AS homeTeam, - awayTeams.teamName AS awayTeam -FROM t_season_schedule tss - JOIN seasons s ON tss.seasonID = s.seasonID - JOIN teams homeTeams ON tss.homeTeamID = homeTeams.localID - JOIN teams awayTeams ON tss.awayTeamID = awayTeams.localID -ORDER BY tss.seasonID, gameNumber; + mostRecentSeason AS (SELECT id AS seasonID, + RANK() OVER (ORDER BY id) AS seasonNum + FROM t_seasons + JOIN t_leagues ON t_seasons.historicalLeagueGUID = t_leagues.GUID + JOIN t_franchise tf ON t_leagues.GUID = tf.leagueGUID + WHERE t_leagues.GUID = CAST(@leagueId AS BLOB) + ORDER BY ID DESC + LIMIT 1), + gameResults AS (SELECT (ROW_NUMBER() OVER (PARTITION BY tsg.seasonID ORDER BY tgr.ID) - 1) / + ((SELECT COUNT(*) FROM teams) / 2) + 1 AS day, + tgr.* + FROM t_game_results tgr + JOIN t_season_games tsg on tgr.ID = tsg.gameID + JOIN mostRecentSeason mrs on tsg.seasonID = mrs.seasonID + JOIN t_team_local_ids t_local_away on tgr.awayTeamLocalID = t_local_away.localID + JOIN t_team_local_ids t_local_home on tgr.homeTeamLocalID = t_local_home.localID + ORDER BY tgr.ID), + seasonSchedule AS (SELECT tss.seasonID, + mrs.seasonNum, + RANK() OVER (PARTITION BY tss.seasonID ORDER BY rowid) as gameNumber, + (ROW_NUMBER() OVER (PARTITION BY tss.seasonID ORDER BY rowid) - 1) / + ((SELECT COUNT(*) FROM teams) / 2) + 1 AS day, + tss.homeTeamID, + tss.awayTeamID + FROM t_season_schedule tss + JOIN mostRecentSeason mrs ON tss.seasonID = mrs.seasonID) +SELECT ss.seasonID, + ss.seasonNum, + ss.gameNumber, + ss.day, + ss.homeTeamID, + homeTeams.teamName, + ss.awayTeamID, + awayTeams.teamName, + gr.homeRunsScored, + gr.awayRunsScored, + gr.homePitcherLocalID, + vbpi_home.firstName || ' ' || vbpi_home.lastName AS homePitcherName, + gr.awayPitcherLocalID, + vbpi_away.firstName || ' ' || vbpi_away.lastName AS awayPitcherName +FROM seasonSchedule ss + LEFT JOIN gameResults gr + ON gr.homeTeamLocalID = ss.homeTeamID AND gr.awayTeamLocalID = ss.awayTeamID AND ss.day = gr.day + + JOIN teams homeTeams ON ss.homeTeamID = homeTeams.localID + JOIN teams awayTeams ON ss.awayTeamID = awayTeams.localID + + LEFT JOIN t_baseball_player_local_ids tbpli_home ON gr.homePitcherLocalID = tbpli_home.localID + LEFT JOIN t_baseball_player_local_ids tbpli_away ON gr.awayPitcherLocalID = tbpli_away.localID + + LEFT JOIN v_baseball_player_info vbpi_home ON tbpli_home.GUID = vbpi_home.baseballPlayerGUID + LEFT JOIN v_baseball_player_info vbpi_away ON tbpli_away.GUID = vbpi_away.baseballPlayerGUID +ORDER BY gameNumber; diff --git a/SMB3Explorer/Services/DataService/DataServiceMostRecentSeason.cs b/SMB3Explorer/Services/DataService/DataServiceMostRecentSeason.cs index 6fc1369..f1e6965 100644 --- a/SMB3Explorer/Services/DataService/DataServiceMostRecentSeason.cs +++ b/SMB3Explorer/Services/DataService/DataServiceMostRecentSeason.cs @@ -16,7 +16,7 @@ public async IAsyncEnumerable GetMostRecentSea bool isRookies = false) { var seasonAverageOps = await GetAverageSeasonOps(); - + var command = Connection!.CreateCommand(); var sqlFile = isRookies ? SqlFile.TopPerformersRookiesBatting : SqlFile.TopPerformersBatting; @@ -28,7 +28,7 @@ public async IAsyncEnumerable GetMostRecentSea { Value = _applicationContext.SelectedFranchise!.LeagueId.ToBlob() }); - + command.Parameters.Add(new SqliteParameter("@leagueOps", SqliteType.Real) { Value = seasonAverageOps @@ -80,7 +80,7 @@ public async IAsyncEnumerable GetMostRecentSe bool isRookies = false) { var seasonAveragePitcherStats = await GetAverageSeasonPitcherStats(); - + var command = Connection!.CreateCommand(); var sqlFile = isRookies ? SqlFile.TopPerformersRookiesPitching : SqlFile.TopPerformersPitching; @@ -92,12 +92,12 @@ public async IAsyncEnumerable GetMostRecentSe { Value = _applicationContext.SelectedFranchise!.LeagueId.ToBlob() }); - + command.Parameters.Add(new SqliteParameter("@leagueEra", SqliteType.Real) { Value = seasonAveragePitcherStats.Era }); - + command.Parameters.Add(new SqliteParameter("@leagueFip", SqliteType.Real) { Value = seasonAveragePitcherStats.Fip @@ -203,15 +203,15 @@ public async IAsyncEnumerable GetMostRecentSeasonPlayers() public async IAsyncEnumerable GetMostRecentSeasonTeams() { var command = Connection!.CreateCommand(); - + var commandText = SqlRunner.GetSqlCommand(SqlFile.MostRecentSeasonTeams); command.CommandText = commandText; - + command.Parameters.Add(new SqliteParameter("@leagueId", SqliteType.Blob) { Value = _applicationContext.SelectedFranchise!.LeagueId.ToBlob() }); - + var reader = await command.ExecuteReaderAsync(); while (reader.Read()) @@ -242,7 +242,7 @@ public async IAsyncEnumerable GetMostRecentSeasonTeams() seasonTeam.Velocity = reader.GetInt32(23); seasonTeam.Junk = reader.GetInt32(24); seasonTeam.Accuracy = reader.GetInt32(25); - + yield return seasonTeam; } } @@ -250,28 +250,32 @@ public async IAsyncEnumerable GetMostRecentSeasonTeams() public async IAsyncEnumerable GetMostRecentSeasonSchedule() { var command = Connection!.CreateCommand(); - + var commandText = SqlRunner.GetSqlCommand(SqlFile.MostRecentSeasonSchedule); command.CommandText = commandText; - + command.Parameters.Add(new SqliteParameter("@leagueId", SqliteType.Blob) { Value = _applicationContext.SelectedFranchise!.LeagueId.ToBlob() }); - + var reader = await command.ExecuteReaderAsync(); while (reader.Read()) { var seasonSchedule = new SeasonSchedule(); - + seasonSchedule.SeasonId = reader.GetInt32(0); seasonSchedule.SeasonNum = reader.GetInt32(1); seasonSchedule.GameNum = reader.GetInt32(2); seasonSchedule.Day = reader.GetInt32(3); - seasonSchedule.HomeTeam = reader.GetString(4); - seasonSchedule.AwayTeam = reader.GetString(5); - + seasonSchedule.HomeTeam = reader.GetString(5); + seasonSchedule.AwayTeam = reader.GetString(7); + seasonSchedule.HomeRunsScored = reader.IsDBNull(8) ? null : reader.GetInt32(8); + seasonSchedule.AwayRunsScored = reader.IsDBNull(9) ? null : reader.GetInt32(9); + seasonSchedule.HomePitcherName = reader.IsDBNull(11) ? null : reader.GetString(11); + seasonSchedule.AwayPitcherName = reader.IsDBNull(13) ? null : reader.GetString(13); + yield return seasonSchedule; } } @@ -282,15 +286,15 @@ private async Task GetAverageSeasonOps() var commandText = SqlRunner.GetSqlCommand(SqlFile.SeasonAverageBatterStats); command.CommandText = commandText; - + command.Parameters.Add(new SqliteParameter("@leagueId", SqliteType.Blob) { Value = _applicationContext.SelectedFranchise!.LeagueId.ToBlob() }); - + var reader = await command.ExecuteReaderAsync(); reader.Read(); - + var opsOrdinal = reader.GetDouble(0); return opsOrdinal; } @@ -301,15 +305,15 @@ private async Task GetAverageSeasonPitcherStats() var commandText = SqlRunner.GetSqlCommand(SqlFile.SeasonAveragePitcherStats); command.CommandText = commandText; - + command.Parameters.Add(new SqliteParameter("@leagueId", SqliteType.Blob) { Value = _applicationContext.SelectedFranchise!.LeagueId.ToBlob() }); - + var reader = await command.ExecuteReaderAsync(); reader.Read(); - + var eraOrdinal = reader.GetDouble(0); var fipOrdinal = reader.GetDouble(1); return new AverageSeasonPitcherStats(eraOrdinal, fipOrdinal);