From 4565d7deaa31203d1433cdbb0921daf3aa348a25 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Sat, 12 Aug 2017 00:16:47 +0200 Subject: [PATCH] Implement !table command to print league table and use league 453 for testing. --- main.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 53d4930..e44f742 100644 --- a/main.go +++ b/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 {