Compare commits

...

7 Commits

7 changed files with 88 additions and 21 deletions

View File

@ -1,12 +1,13 @@
FROM golang:1.8 FROM golang:1.10
RUN mkdir -p /go/src/app RUN mkdir -p /go/src/app
WORKDIR /go/src/app WORKDIR /go/src/app
ARG ROOT_IMPORT_PATH=github.com/icedream/embot
COPY . /go/src/app COPY . /go/src/app
RUN \ RUN \
mkdir -p "$GOPATH/src/github.com/icedream" &&\ mkdir -p "$GOPATH/src/$(dirname "$ROOT_IMPORT_PATH")" &&\
ln -sf /go/src/app "$GOPATH/src/github.com/icedream/embot" &&\ ln -sf /go/src/app "$GOPATH/src/$ROOT_IMPORT_PATH" &&\
go-wrapper download &&\ go-wrapper download &&\
go-wrapper install go-wrapper install

View File

@ -50,7 +50,7 @@ func (m *AntiSpam) init() {
m.userTrackingMap = map[string][]time.Time{} m.userTrackingMap = map[string][]time.Time{}
m.commandTrackingMap = map[string][]time.Time{} m.commandTrackingMap = map[string][]time.Time{}
m.CommandCooldownDuration = 15 * time.Second m.CommandCooldownDuration = 10 * time.Second
m.CommandRateLimitCount = 2 m.CommandRateLimitCount = 2
m.CommandRateLimitDuration = 1 * time.Minute m.CommandRateLimitDuration = 1 * time.Minute

92
main.go
View File

@ -23,8 +23,11 @@ import (
) )
const ( const (
Competition_Testing = 453 Competition_Testing_GermanLeague2 = 453
Competition_WorldChampionShip2018 = -1 Competition_Testing = 464
Competition_WorldChampionShip2018 = 467
Competition = Competition_WorldChampionShip2018
day = 24 * time.Hour day = 24 * time.Hour
week = 7 * day week = 7 * day
@ -115,7 +118,7 @@ func main() {
updateTopics := func() { updateTopics := func() {
// Get football data // Get football data
if r, err := footballData.FixturesOfSoccerSeason(Competition_Testing).Do(); err != nil { if r, err := footballData.FixturesOfCompetition(Competition).Do(); err != nil {
log.Print(err) log.Print(err)
} else { } else {
currentMatches := []*footballdata.Fixture{} currentMatches := []*footballdata.Fixture{}
@ -411,6 +414,11 @@ func main() {
log.Printf("<%s @ %s> %s", event.Nick, target, msg) log.Printf("<%s @ %s> %s", event.Nick, target, msg)
cmd := parseCommand(msg) cmd := parseCommand(msg)
isCommand := !isChannel || strings.HasPrefix(cmd.Name, "!")
if !isCommand {
return
}
log.Printf("Antispam check: %s, %s", event.Source, cmd.Name) log.Printf("Antispam check: %s, %s", event.Source, cmd.Name)
if ok, duration := antispamInstance.Check(event.Source, cmd.Name); !ok { if ok, duration := antispamInstance.Check(event.Source, cmd.Name); !ok {
conn.Noticef(sender, "Sorry, please try again %s.", humanize.Time(time.Now().Add(duration))) conn.Noticef(sender, "Sorry, please try again %s.", humanize.Time(time.Now().Add(duration)))
@ -423,7 +431,7 @@ func main() {
updateTopicsChan <- nil updateTopicsChan <- nil
case strings.EqualFold(cmd.Name, "!table"): case strings.EqualFold(cmd.Name, "!table"):
if leagueTable, err := footballData.LeagueTableOfCompetition(Competition_Testing).Do(); err != nil { if leagueTable, err := footballData.LeagueTableOfCompetition(Competition).Do(); err != nil {
if s, err := tplString("error", err); err != nil { if s, err := tplString("error", err); err != nil {
log.Print(err) log.Print(err)
} else { } else {
@ -435,20 +443,78 @@ func main() {
header := []string{"Rank", "Team", "Games", "Goals", "Diff", "Points"} header := []string{"Rank", "Team", "Games", "Goals", "Diff", "Points"}
data := [][]string{} 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 := "" goalDiffPrefix := ""
if standing.GoalDifference > 0 { if actualStanding.GoalDifference > 0 {
goalDiffPrefix = "+" goalDiffPrefix = "+"
} else { } else {
goalDiffPrefix = "" 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{ data = append(data, []string{
fmt.Sprintf("%d", standing.Position), fmt.Sprintf("%d", actualStanding.Position),
standing.TeamName, teamName,
fmt.Sprintf("%d", standing.PlayedGames), fmt.Sprintf("%d", actualStanding.PlayedGames),
fmt.Sprintf("%d:%d", standing.Goals, standing.GoalsAgainst), fmt.Sprintf("%d:%d", actualStanding.Goals, actualStanding.GoalsAgainst),
fmt.Sprintf("%s%d", goalDiffPrefix, standing.GoalDifference), fmt.Sprintf("%s%d", goalDiffPrefix, actualStanding.GoalDifference),
fmt.Sprintf("%d", standing.Points), fmt.Sprintf("%d", actualStanding.Points),
}) })
} }
@ -476,7 +542,7 @@ func main() {
} }
} }
case strings.EqualFold(cmd.Name, "!match"): case strings.EqualFold(cmd.Name, "!match"):
/*if r, err := footballData.FixturesOfSoccerSeason(SoccerSeason_EuropeanChampionShipsFrance2016).TimeFrame(1 * day).Do(); err != nil { /*if r, err := footballData.FixturesOfCompetition(SoccerSeason_EuropeanChampionShipsFrance2016).TimeFrame(1 * day).Do(); err != nil {
if s, err := tplString("error", err); err != nil { if s, err := tplString("error", err); err != nil {
log.Print(err) log.Print(err)
} else { } else {

@ -1 +1 @@
Subproject commit 48a484dfcebd2d72bd73f76202b0f2393ccf5162 Subproject commit 12f2557ea80f4bbb1fee8642991e1af49901f1b1

@ -1 +1 @@
Subproject commit a3647f8e31d79543b2d0f0ae2fe5c379d72cedc0 Subproject commit 9f6ff22cfff829561052f5886ec80a2ac148b4eb

2
vendor/github.com/stretchr/testify generated vendored

@ -1 +1 @@
Subproject commit 05e8a0eda380579888eb53c394909df027f06991 Subproject commit c679ae2cc0cb27ec3293fea7e254e47386f05d69

2
vendor/github.com/thoj/go-ircevent generated vendored

@ -1 +1 @@
Subproject commit 1b0acb5f2f1b615cfbd4b9f91abb14cb39a18769 Subproject commit 5063e5f26097f6423c9de665079347c9473788da