mirror of https://github.com/velour/catbase.git
quotegame: add game
This commit is contained in:
parent
99abd9bb2f
commit
24668cf774
|
@ -1,8 +1,10 @@
|
||||||
package quotegame
|
package quotegame
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
@ -19,6 +21,7 @@ type QuoteGame struct {
|
||||||
handlers []bot.HandlerSpec
|
handlers []bot.HandlerSpec
|
||||||
|
|
||||||
currentGame *time.Timer
|
currentGame *time.Timer
|
||||||
|
currentName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(b bot.Bot) *QuoteGame {
|
func New(b bot.Bot) *QuoteGame {
|
||||||
|
@ -36,28 +39,29 @@ func (p *QuoteGame) register() {
|
||||||
log.Debug().Msg("registering quote handlers")
|
log.Debug().Msg("registering quote handlers")
|
||||||
p.handlers = []bot.HandlerSpec{
|
p.handlers = []bot.HandlerSpec{
|
||||||
{
|
{
|
||||||
Kind: bot.Message, IsCmd: true,
|
Kind: bot.Message, IsCmd: false,
|
||||||
Regex: regexp.MustCompile(`(?i)^quote game$`),
|
Regex: regexp.MustCompile(`(?i)^quote game$`),
|
||||||
HelpText: "Start a quote game",
|
HelpText: "Start a quote game",
|
||||||
Handler: p.startGame,
|
Handler: p.startGame,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Kind: bot.Message, IsCmd: false,
|
Kind: bot.Message, IsCmd: false,
|
||||||
Regex: regexp.MustCompile(`.*`),
|
Regex: regexp.MustCompile(`(?i)^guess:\s?(?P<name>.+)$`),
|
||||||
Handler: p.message,
|
Handler: p.guess,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
p.b.RegisterTable(p, p.handlers)
|
p.b.RegisterTable(p, p.handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *QuoteGame) getAllQuotes() ([]string, error) {
|
type quote struct {
|
||||||
|
Fact string
|
||||||
|
Tidbit string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *QuoteGame) getAllQuotes() ([]quote, error) {
|
||||||
threshold := p.c.GetInt("quotegame.threshold", 10)
|
threshold := p.c.GetInt("quotegame.threshold", 10)
|
||||||
q := `
|
q := `select fact, tidbit from factoid where fact like '%quotes' group by fact having count(fact) > ?`
|
||||||
select tidbit from fact where fact in (
|
quotes := []quote{}
|
||||||
select fact, verb, tidbit from fact where fact like '%quotes' group by fact having count(fact) > ?
|
|
||||||
)
|
|
||||||
`
|
|
||||||
quotes := []string{}
|
|
||||||
err := p.db.Select("es, q, threshold)
|
err := p.db.Select("es, q, threshold)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -65,12 +69,18 @@ func (p *QuoteGame) getAllQuotes() ([]string, error) {
|
||||||
return quotes, nil
|
return quotes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *QuoteGame) getRandomquote() (string, error) {
|
func (p *QuoteGame) getRandomquote() (string, string, error) {
|
||||||
quotes, err := p.getAllQuotes()
|
quotes, err := p.getAllQuotes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
return quotes[rand.Intn(len(quotes))], nil
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *QuoteGame) startGame(r bot.Request) bool {
|
func (p *QuoteGame) startGame(r bot.Request) bool {
|
||||||
|
@ -80,20 +90,46 @@ func (p *QuoteGame) startGame(r bot.Request) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Debug: starting game with %s having said %s", who, quote))
|
||||||
|
|
||||||
length := time.Duration(p.c.GetInt("quotegame.length", 120))
|
length := time.Duration(p.c.GetInt("quotegame.length", 120))
|
||||||
p.currentGame = time.AfterFunc(length*time.Second, func() {
|
p.currentGame = time.AfterFunc(length*time.Second, func() {
|
||||||
p.currentGame = nil
|
p.currentGame = nil
|
||||||
|
p.currentName = ""
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Game ended.")
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Game ended.")
|
||||||
})
|
})
|
||||||
|
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Game started.")
|
p.currentName = who
|
||||||
|
|
||||||
|
msg := fmt.Sprintf("Quote game: Who said \"%s\"?\n\nUse `guess: name` to guess who", quote)
|
||||||
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *QuoteGame) message(r bot.Request) bool {
|
func (p *QuoteGame) guess(r bot.Request) bool {
|
||||||
|
log.Debug().Msg("quote game message check")
|
||||||
if p.currentGame == nil {
|
if p.currentGame == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return false
|
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue