Fix table command to make use of multiple standings.

master
Icedream 2018-06-03 00:47:46 +02:00
parent 5a250b538c
commit e72ec5f3e4
Signed by: icedream
GPG Key ID: 1573F6D8EFE4D0CF
1 changed files with 66 additions and 8 deletions

74
main.go
View File

@ -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),
})
}