diff --git a/main.go b/main.go index 19fbe54..8884589 100644 --- a/main.go +++ b/main.go @@ -438,20 +438,78 @@ func main() { header := []string{"Rank", "Team", "Games", "Goals", "Diff", "Points"} data := [][]string{} - for _, standing := range leagueTable.Standing { + // cast standing items to a new array with a more generic type + var standing []interface{} + if leagueTable.Standing != nil { + standing = make([]interface{}, len(leagueTable.Standing)) + for i, v := range leagueTable.Standing { + standing[i] = v + } + } + + if standing == nil { + if leagueTable.Standings == nil { + conn.Noticef(sender, "Sorry, can't display league table at this moment.") + return + } + + // expect a standing name to be input + if len(cmd.Arguments) < 1 { + conn.Noticef(sender, "You need to type in which standing you want to view the table of.") + return + } + + // find matching standing (case-insensitive) + ok := false + for key, val := range leagueTable.Standings { + if strings.EqualFold(key, cmd.Arguments[0]) { + ok = true + standing = make([]interface{}, len(val)) + for i, v := range val { + standing[i] = v + } + break + } + } + if !ok { + conn.Noticef(sender, "Can not find requested standing for league table.") + return + } + } + + for _, standing := range standing { + // HACK + var actualStanding footballdata.TeamLeagueStatistics + switch v := standing.(type) { + case footballdata.TeamLeagueStatisticsInStanding: + actualStanding = v.TeamLeagueStatistics + case footballdata.TeamLeagueStatisticsInStandings: + actualStanding = v.TeamLeagueStatistics + } + goalDiffPrefix := "" - if standing.GoalDifference > 0 { + if actualStanding.GoalDifference > 0 { goalDiffPrefix = "+" } else { goalDiffPrefix = "" } + + // HACK - get team name from two diff fields + var teamName string + switch v := standing.(type) { + case footballdata.TeamLeagueStatisticsInStanding: + teamName = v.TeamName + case footballdata.TeamLeagueStatisticsInStandings: + teamName = v.Team + } + data = append(data, []string{ - fmt.Sprintf("%d", standing.Position), - standing.TeamName, - fmt.Sprintf("%d", standing.PlayedGames), - fmt.Sprintf("%d:%d", standing.Goals, standing.GoalsAgainst), - fmt.Sprintf("%s%d", goalDiffPrefix, standing.GoalDifference), - fmt.Sprintf("%d", standing.Points), + fmt.Sprintf("%d", actualStanding.Position), + teamName, + fmt.Sprintf("%d", actualStanding.PlayedGames), + fmt.Sprintf("%d:%d", actualStanding.Goals, actualStanding.GoalsAgainst), + fmt.Sprintf("%s%d", goalDiffPrefix, actualStanding.GoalDifference), + fmt.Sprintf("%d", actualStanding.Points), }) }