Implement !table command to print league table and use league 453 for testing.

master
Icedream 2017-08-12 00:16:47 +02:00
parent 46a35c746c
commit 4565d7deaa
Signed by: icedream
GPG Key ID: 1573F6D8EFE4D0CF
1 changed files with 63 additions and 2 deletions

65
main.go
View File

@ -1,15 +1,18 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"log" "log"
"math" "math"
"regexp"
"sort" "sort"
"strings" "strings"
"time" "time"
"net/http" "net/http"
"github.com/olekukonko/tablewriter"
"github.com/thoj/go-ircevent" "github.com/thoj/go-ircevent"
"gopkg.in/alecthomas/kingpin.v2" "gopkg.in/alecthomas/kingpin.v2"
@ -18,12 +21,17 @@ import (
) )
const ( const (
SoccerSeason_EuropeanChampionShipsFrance2016 = 444 Competition_Testing = 453
Competition_WorldChampionShip2018 = -1
day = 24 * time.Hour day = 24 * time.Hour
week = 7 * day week = 7 * day
) )
var (
rDiff = regexp.MustCompile("[\\+\\-]\\d+")
)
type versus struct { type versus struct {
HomeTeam, AwayTeam string HomeTeam, AwayTeam string
} }
@ -103,7 +111,7 @@ func main() {
updateTopics := func() { updateTopics := func() {
// Get football data // 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) log.Print(err)
} else { } else {
currentMatches := []*footballdata.Fixture{} currentMatches := []*footballdata.Fixture{}
@ -400,6 +408,59 @@ func main() {
switch { switch {
case !isChannel && strings.HasPrefix(msg, "updatetopics"): case !isChannel && strings.HasPrefix(msg, "updatetopics"):
updateTopicsChan <- nil 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"): case strings.HasPrefix(msg, "!match"):
/*if r, err := footballData.FixturesOfSoccerSeason(SoccerSeason_EuropeanChampionShipsFrance2016).TimeFrame(1 * day).Do(); err != nil { /*if r, err := footballData.FixturesOfSoccerSeason(SoccerSeason_EuropeanChampionShipsFrance2016).TimeFrame(1 * day).Do(); err != nil {
if s, err := tplString("error", err); err != nil { if s, err := tplString("error", err); err != nil {