Fix bug where api retrurns team which player first recorded a stat for as opposed to current team
This commit is contained in:
parent
71e7c7fcf7
commit
5cadb7f661
@ -95,7 +95,6 @@ export async function fetchFieldingStatsBySeason(seasonNumber: number, isRegular
|
||||
return []
|
||||
}
|
||||
|
||||
// TODO might want to make this playerpositionteam grouping (currently does not exist)
|
||||
const response = await fetch(`${SITE_URL}/api/v3/plays/fielding?season=${seasonNumber}&limit=10000&group_by=playerposition&s_type=${isRegularSeason ? 'regular' : 'post'}`)
|
||||
|
||||
const fieldingStatsResponse: {
|
||||
|
||||
@ -31,3 +31,19 @@ export async function fetchActiveTeamByOwnerId(ownerId: string): Promise<Team |
|
||||
|
||||
return teamResponse.teams[0]
|
||||
}
|
||||
|
||||
export async function fetchTeamsBySeason(season: number): Promise<Team[]> {
|
||||
const response = await fetch(`${SITE_URL}/api/v3/teams?season=${season}&short_output=False`)
|
||||
|
||||
const teamResponse: {
|
||||
count: number
|
||||
teams: Team[]
|
||||
} = await response.json()
|
||||
|
||||
if (teamResponse.count === 0) {
|
||||
console.warn('teamServices.fetchTeamsBySeason - Received 0 teams for season, did you provide the correct season number?')
|
||||
return []
|
||||
}
|
||||
|
||||
return teamResponse.teams
|
||||
}
|
||||
|
||||
@ -62,6 +62,7 @@ import { type LeagueInfo, fetchCurrentLeagueInfo } from '@/services/currentServi
|
||||
import { fetchFieldingStatsBySeason, type FieldingStat } from '@/services/fieldingStatsService'
|
||||
import { fetchPitchingStatsBySeason, type PitchingStat } from '@/services/pitchingStatsService'
|
||||
import type { Player } from '@/services/playersService'
|
||||
import { fetchTeamsBySeason } from '@/services/teamsService'
|
||||
import { CURRENT_SEASON, GAMES_PER_WEEK, WEEKS_PER_SEASON } from '@/services/utilities'
|
||||
|
||||
interface LeaderboardTableData<T> {
|
||||
@ -109,6 +110,7 @@ export default {
|
||||
allPlayersBattingStats: [] as BattingStat[],
|
||||
allPlayersFieldingStats: [] as FlatFieldingStat[],
|
||||
allPlayersPitchingStats: [] as PitchingStat[],
|
||||
teamsById: {} as Map<number, Team>,
|
||||
currentSeasonNumber: CURRENT_SEASON,
|
||||
seasonNumber: CURRENT_SEASON,
|
||||
statType: 'Batting' as 'Batting' | 'Pitching' | 'Fielding',
|
||||
@ -327,20 +329,33 @@ export default {
|
||||
const leagueInfo: LeagueInfo = await fetchCurrentLeagueInfo()
|
||||
this.weekNumber = leagueInfo.week
|
||||
|
||||
if (!this.teamsById.size) {
|
||||
const teams = await fetchTeamsBySeason(this.seasonNumber)
|
||||
this.teamsById = new Map(teams.map(team => [team.id, team]))
|
||||
}
|
||||
|
||||
if (this.statType == 'Batting') {
|
||||
this.allPlayersBattingStats = await fetchBattingStatsBySeason(this.seasonNumber, true)
|
||||
this.allPlayersBattingStats = (await fetchBattingStatsBySeason(this.seasonNumber, true))
|
||||
.map(stat => {
|
||||
return { ...stat, team: this.teamsById.get(stat.player.team as unknown as number) ?? stat.team }
|
||||
})
|
||||
}
|
||||
if (this.statType == 'Pitching') {
|
||||
this.allPlayersPitchingStats = await fetchPitchingStatsBySeason(this.seasonNumber, true)
|
||||
this.allPlayersPitchingStats = (await fetchPitchingStatsBySeason(this.seasonNumber, true))
|
||||
.map(stat => {
|
||||
return { ...stat, team: this.teamsById.get(stat.player.team as unknown as number) ?? stat.team }
|
||||
})
|
||||
}
|
||||
if (this.statType == 'Fielding') {
|
||||
const allPlayerPositionFieldingStats: FieldingStat[] = await fetchFieldingStatsBySeason(this.seasonNumber, true)
|
||||
const aggregatedFieldingStatsByPlayer: { [playerName: string]: FlatFieldingStat } = {}
|
||||
|
||||
allPlayerPositionFieldingStats.forEach(stat => {
|
||||
if (!aggregatedFieldingStatsByPlayer[stat.player.name]) {
|
||||
aggregatedFieldingStatsByPlayer[stat.player.name] = {
|
||||
player: stat.player,
|
||||
team: stat.team,
|
||||
// looking up team as api provides first team with stat so traded players are incorrect
|
||||
team: this.teamsById.get(stat.player.team as unknown as number) ?? stat.team,
|
||||
xCheckCount: 0,
|
||||
hit: 0,
|
||||
error: 0,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user