aoc: refactor

This commit is contained in:
Chris Sexton 2021-02-01 07:11:02 -05:00 committed by Chris Sexton
parent 951c345666
commit dfbcfafab9
1 changed files with 72 additions and 81 deletions

View File

@ -2,6 +2,7 @@ package aoc
import ( import (
"fmt" "fmt"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -10,7 +11,6 @@ import (
"code.chrissexton.org/cws/getaoc" "code.chrissexton.org/cws/getaoc"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config" "github.com/velour/catbase/config"
) )
@ -24,42 +24,35 @@ func New(b bot.Bot) *AOC {
b: b, b: b,
c: b.Config(), c: b.Config(),
} }
b.Register(aoc, bot.Message, aoc.message) b.RegisterRegexCmd(aoc, bot.Message, aocRegex, aoc.aocCmd)
return aoc return aoc
} }
func (p *AOC) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { var aocRegex = regexp.MustCompile(`(?i)^aoc\s?(?P<year>\S+)?$`)
if !message.Command {
return false func (p *AOC) aocCmd(r bot.Request) bool {
}
cleaned := strings.TrimSpace(strings.ToLower(message.Body))
fields := strings.Split(cleaned, " ")
if strings.HasPrefix(cleaned, "aoc") {
year := time.Now().Year() year := time.Now().Year()
if len(fields) > 1 { if time.Now().Month() < 11 {
year, _ = strconv.Atoi(fields[1]) year--
}
if r.Values["year"] != "" {
year, _ = strconv.Atoi(r.Values["year"])
} }
boardId := p.c.GetInt("aoc.board", 0) boardId := p.c.GetInt("aoc.board", 0)
var err error var err error
if len(fields) > 2 {
boardId, err = strconv.Atoi(fields[2])
if err != nil {
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Error getting leaderboard: %s", err))
return true
}
}
session := p.c.Get("aoc.session", "") session := p.c.Get("aoc.session", "")
if session == "" { if session == "" {
p.b.Send(c, bot.Message, message.Channel, "Error getting leaderboard: must have a session ID.") p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Error getting leaderboard: must have a session ID.")
return true return true
} }
if boardId == 0 { if boardId == 0 {
p.b.Send(c, bot.Message, message.Channel, "Error getting leaderboard: must have a board ID.") p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Error getting leaderboard: must have a board ID.")
return true return true
} }
board, err := getaoc.GetLeaderboard(session, year, boardId) board, err := getaoc.GetLeaderboard(session, year, boardId)
if err != nil { if err != nil {
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Error getting leaderboard: %s", err)) p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Error getting leaderboard: %s", err))
return true return true
} }
@ -88,7 +81,7 @@ func (p *AOC) message(c bot.Connector, kind bot.Kind, message msg.Message, args
} }
} }
link := c.URLFormat("leaderboard", fmt.Sprintf("https://adventofcode.com/%d/leaderboard/private/view/%d", year, boardId)) link := r.Conn.URLFormat("leaderboard", fmt.Sprintf("https://adventofcode.com/%d/leaderboard/private/view/%d", year, boardId))
msg := fmt.Sprintf("AoC %s:\n", link) msg := fmt.Sprintf("AoC %s:\n", link)
for _, m := range members { for _, m := range members {
if m.Stars == 0 { if m.Stars == 0 {
@ -97,18 +90,16 @@ func (p *AOC) message(c bot.Connector, kind bot.Kind, message msg.Message, args
trophy := "" trophy := ""
switch m.ID { switch m.ID {
case goldID: case goldID:
trophy = c.Emojy(":gold-trophy:") trophy = r.Conn.Emojy(":gold-trophy:")
case silverID: case silverID:
trophy = c.Emojy(":silver-trophy:") trophy = r.Conn.Emojy(":silver-trophy:")
case bronzeID: case bronzeID:
trophy = c.Emojy(":bronze-trophy:") trophy = r.Conn.Emojy(":bronze-trophy:")
} }
msg += fmt.Sprintf("%s has %d :star: for a score of %d%s\n", m.Name, m.Stars, m.LocalScore, trophy) msg += fmt.Sprintf("%s has %d :star: for a score of %d%s\n", m.Name, m.Stars, m.LocalScore, trophy)
} }
msg = strings.TrimSpace(msg) msg = strings.TrimSpace(msg)
p.b.Send(c, bot.Message, message.Channel, msg) p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
return true return true
}
return false
} }