2021-08-05 13:35:43 +00:00
|
|
|
package quotegame
|
|
|
|
|
|
|
|
import (
|
2021-08-11 16:04:33 +00:00
|
|
|
"fmt"
|
2021-08-05 13:35:43 +00:00
|
|
|
"math/rand"
|
2021-08-05 14:47:34 +00:00
|
|
|
"regexp"
|
2021-08-11 16:04:33 +00:00
|
|
|
"strings"
|
2021-08-05 13:35:43 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2021-08-05 14:47:34 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-08-05 13:35:43 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type QuoteGame struct {
|
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
db *sqlx.DB
|
|
|
|
|
2021-08-05 14:47:34 +00:00
|
|
|
handlers []bot.HandlerSpec
|
|
|
|
|
2021-08-05 13:35:43 +00:00
|
|
|
currentGame *time.Timer
|
2021-08-11 16:04:33 +00:00
|
|
|
currentName string
|
2021-08-05 13:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *QuoteGame {
|
2021-08-05 14:47:34 +00:00
|
|
|
p := &QuoteGame{
|
2021-08-05 13:35:43 +00:00
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
currentGame: nil,
|
|
|
|
}
|
2021-08-05 14:47:34 +00:00
|
|
|
p.register()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *QuoteGame) register() {
|
|
|
|
log.Debug().Msg("registering quote handlers")
|
|
|
|
p.handlers = []bot.HandlerSpec{
|
|
|
|
{
|
2021-08-11 16:04:33 +00:00
|
|
|
Kind: bot.Message, IsCmd: false,
|
2021-08-05 14:47:34 +00:00
|
|
|
Regex: regexp.MustCompile(`(?i)^quote game$`),
|
|
|
|
HelpText: "Start a quote game",
|
|
|
|
Handler: p.startGame,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: false,
|
2021-08-11 16:04:33 +00:00
|
|
|
Regex: regexp.MustCompile(`(?i)^guess:\s?(?P<name>.+)$`),
|
|
|
|
Handler: p.guess,
|
2021-08-05 14:47:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
p.b.RegisterTable(p, p.handlers)
|
2021-08-05 13:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 16:04:33 +00:00
|
|
|
type quote struct {
|
|
|
|
Fact string
|
|
|
|
Tidbit string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *QuoteGame) getAllQuotes() ([]quote, error) {
|
2021-08-05 13:35:43 +00:00
|
|
|
threshold := p.c.GetInt("quotegame.threshold", 10)
|
2021-08-11 16:04:33 +00:00
|
|
|
q := `select fact, tidbit from factoid where fact like '%quotes' group by fact having count(fact) > ?`
|
|
|
|
quotes := []quote{}
|
2021-08-05 13:35:43 +00:00
|
|
|
err := p.db.Select("es, q, threshold)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return quotes, nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:04:33 +00:00
|
|
|
func (p *QuoteGame) getRandomquote() (string, string, error) {
|
2021-08-05 13:35:43 +00:00
|
|
|
quotes, err := p.getAllQuotes()
|
|
|
|
if err != nil {
|
2021-08-11 16:04:33 +00:00
|
|
|
return "", "", err
|
2021-08-05 13:35:43 +00:00
|
|
|
}
|
2021-08-11 16:04:33 +00:00
|
|
|
|
|
|
|
quote := quotes[rand.Intn(len(quotes))]
|
|
|
|
who := strings.ReplaceAll(quote.Fact, " quotes", "")
|
|
|
|
what := strings.ReplaceAll(quote.Tidbit, who, "")
|
|
|
|
what = strings.Trim(what, " <>")
|
|
|
|
|
|
|
|
return who, what, nil
|
2021-08-05 14:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *QuoteGame) startGame(r bot.Request) bool {
|
|
|
|
log.Debug().Msg("startGame called")
|
|
|
|
if p.currentGame != nil {
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "There is already a quote game running.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:04:33 +00:00
|
|
|
who, quote, err := p.getRandomquote()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("problem getting quote")
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Error: "+err.Error())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-08-05 14:47:34 +00:00
|
|
|
length := time.Duration(p.c.GetInt("quotegame.length", 120))
|
2021-08-11 16:04:33 +00:00
|
|
|
p.currentGame = time.AfterFunc(length*time.Second, func() {
|
2021-08-05 14:47:34 +00:00
|
|
|
p.currentGame = nil
|
2021-08-11 16:04:33 +00:00
|
|
|
p.currentName = ""
|
2021-08-11 16:40:15 +00:00
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel,
|
|
|
|
fmt.Sprintf("The quote game ended and nobody won. The answer was %s", who))
|
2021-08-05 14:47:34 +00:00
|
|
|
})
|
|
|
|
|
2021-08-11 16:04:33 +00:00
|
|
|
p.currentName = who
|
|
|
|
|
2021-08-11 16:22:19 +00:00
|
|
|
msg := fmt.Sprintf("Quote game: Who said \"%s\"?\nYou have %d seconds to guess.\nUse `guess: name` to guess who.", quote, length)
|
2021-08-11 16:04:33 +00:00
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
|
2021-08-05 14:47:34 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:04:33 +00:00
|
|
|
func (p *QuoteGame) guess(r bot.Request) bool {
|
|
|
|
log.Debug().Msg("quote game message check")
|
2021-08-05 14:47:34 +00:00
|
|
|
if p.currentGame == nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-08-11 16:04:33 +00:00
|
|
|
if r.Values["name"] == p.currentName {
|
|
|
|
msg := fmt.Sprintf("%s won the quote game!", r.Msg.User.Name)
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
|
|
|
|
p.currentName = ""
|
|
|
|
p.currentGame.Stop()
|
|
|
|
p.currentGame = nil
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel,
|
|
|
|
fmt.Sprintf("Sorry %s, that's not correct.", r.Msg.User.Name))
|
|
|
|
|
|
|
|
return true
|
2021-08-05 14:47:34 +00:00
|
|
|
}
|