Implement !table command to print league table and use league 453 for testing.
parent
46a35c746c
commit
4565d7deaa
65
main.go
65
main.go
|
@ -1,15 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/thoj/go-ircevent"
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
|
||||
|
@ -18,12 +21,17 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
SoccerSeason_EuropeanChampionShipsFrance2016 = 444
|
||||
Competition_Testing = 453
|
||||
Competition_WorldChampionShip2018 = -1
|
||||
|
||||
day = 24 * time.Hour
|
||||
week = 7 * day
|
||||
)
|
||||
|
||||
var (
|
||||
rDiff = regexp.MustCompile("[\\+\\-]\\d+")
|
||||
)
|
||||
|
||||
type versus struct {
|
||||
HomeTeam, AwayTeam string
|
||||
}
|
||||
|
@ -103,7 +111,7 @@ func main() {
|
|||
|
||||
updateTopics := func() {
|
||||
// Get football data
|
||||
if r, err := footballData.FixturesOfSoccerSeason(SoccerSeason_EuropeanChampionShipsFrance2016).Do(); err != nil {
|
||||
if r, err := footballData.FixturesOfSoccerSeason(Competition_Testing).Do(); err != nil {
|
||||
log.Print(err)
|
||||
} else {
|
||||
currentMatches := []*footballdata.Fixture{}
|
||||
|
@ -400,6 +408,59 @@ func main() {
|
|||
switch {
|
||||
case !isChannel && strings.HasPrefix(msg, "updatetopics"):
|
||||
updateTopicsChan <- nil
|
||||
case strings.HasPrefix(msg, "!table"):
|
||||
if leagueTable, err := footballData.LeagueTableOfCompetition(Competition_Testing).Do(); err != nil {
|
||||
if s, err := tplString("error", err); err != nil {
|
||||
log.Print(err)
|
||||
} else {
|
||||
conn.Privmsg(target, s)
|
||||
}
|
||||
} else {
|
||||
log.Printf("%+v", leagueTable)
|
||||
|
||||
header := []string{"Rank", "Team", "Games", "Goals", "Diff", "Points"}
|
||||
data := [][]string{}
|
||||
|
||||
for _, standing := range leagueTable.Standing {
|
||||
goalDiffPrefix := ""
|
||||
if standing.GoalDifference > 0 {
|
||||
goalDiffPrefix = "+"
|
||||
} else {
|
||||
goalDiffPrefix = ""
|
||||
}
|
||||
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),
|
||||
})
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
table := tablewriter.NewWriter(buf)
|
||||
table.SetHeader(header)
|
||||
table.SetAlignment(tablewriter.ALIGN_CENTER)
|
||||
table.SetNewLine("\n")
|
||||
table.SetBorder(true)
|
||||
table.AppendBulk(data)
|
||||
table.Render()
|
||||
|
||||
tableStr := rDiff.ReplaceAllStringFunc(buf.String(), func(s string) string {
|
||||
switch s[0] {
|
||||
case '+':
|
||||
return fmt.Sprintf("\x0303%s\x03", s)
|
||||
}
|
||||
return fmt.Sprintf("\x0304%s\x03", s)
|
||||
})
|
||||
|
||||
lines := strings.Split(tableStr, "\n")
|
||||
for _, line := range lines {
|
||||
conn.Privmsg(target, line)
|
||||
}
|
||||
}
|
||||
case strings.HasPrefix(msg, "!match"):
|
||||
/*if r, err := footballData.FixturesOfSoccerSeason(SoccerSeason_EuropeanChampionShipsFrance2016).TimeFrame(1 * day).Do(); err != nil {
|
||||
if s, err := tplString("error", err); err != nil {
|
||||
|
|
Loading…
Reference in New Issue