2017-11-03 18:01:49 +00:00
|
|
|
package sisyphus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2017-11-03 18:01:49 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BOULDER = ":full_moon:"
|
|
|
|
MOUNTAIN = ":new_moon:"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SisyphusPlugin struct {
|
2019-05-27 23:21:53 +00:00
|
|
|
bot bot.Bot
|
2017-11-03 18:01:49 +00:00
|
|
|
listenFor map[string]*game
|
|
|
|
}
|
|
|
|
|
|
|
|
type game struct {
|
|
|
|
id string
|
|
|
|
channel string
|
|
|
|
bot bot.Bot
|
|
|
|
who string
|
|
|
|
start time.Time
|
|
|
|
size int
|
|
|
|
current int
|
|
|
|
nextPush time.Time
|
|
|
|
nextDec time.Time
|
|
|
|
timers [2]*time.Timer
|
|
|
|
ended bool
|
|
|
|
nextAns int
|
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func NewRandomGame(c bot.Connector, b bot.Bot, channel, who string) *game {
|
2017-11-03 18:01:49 +00:00
|
|
|
size := rand.Intn(9) + 2
|
|
|
|
g := game{
|
2017-11-06 19:32:49 +00:00
|
|
|
channel: channel,
|
2019-02-05 15:54:13 +00:00
|
|
|
bot: b,
|
2017-11-06 19:32:49 +00:00
|
|
|
who: who,
|
|
|
|
start: time.Now(),
|
|
|
|
size: size,
|
|
|
|
current: size / 2,
|
2017-11-03 18:01:49 +00:00
|
|
|
}
|
2019-05-27 23:21:53 +00:00
|
|
|
g.id, _ = b.Send(c, bot.Message, channel, g.toMessageString())
|
2017-11-03 18:01:49 +00:00
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
g.schedulePush(c)
|
|
|
|
g.scheduleDecrement(c)
|
2017-11-03 18:01:49 +00:00
|
|
|
|
|
|
|
return &g
|
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (g *game) scheduleDecrement(c bot.Connector) {
|
2017-11-03 18:01:49 +00:00
|
|
|
if g.timers[0] != nil {
|
|
|
|
g.timers[0].Stop()
|
|
|
|
}
|
2019-01-22 00:16:57 +00:00
|
|
|
minDec := g.bot.Config().GetInt("Sisyphus.MinDecrement", 10)
|
|
|
|
maxDec := g.bot.Config().GetInt("Sisyphus.MaxDecrement", 30)
|
2019-05-27 23:21:53 +00:00
|
|
|
g.nextDec = time.Now().Add(time.Duration(minDec+rand.Intn(maxDec)) * time.Minute)
|
2017-11-03 18:01:49 +00:00
|
|
|
go func() {
|
|
|
|
t := time.NewTimer(g.nextDec.Sub(time.Now()))
|
|
|
|
g.timers[0] = t
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
2019-05-27 23:21:53 +00:00
|
|
|
g.handleDecrement(c)
|
2017-11-03 18:01:49 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (g *game) schedulePush(c bot.Connector) {
|
2017-11-03 18:01:49 +00:00
|
|
|
if g.timers[1] != nil {
|
|
|
|
g.timers[1].Stop()
|
|
|
|
}
|
2019-01-22 00:16:57 +00:00
|
|
|
minPush := g.bot.Config().GetInt("Sisyphus.MinPush", 1)
|
|
|
|
maxPush := g.bot.Config().GetInt("Sisyphus.MaxPush", 10)
|
2017-11-06 19:40:43 +00:00
|
|
|
g.nextPush = time.Now().Add(time.Duration(rand.Intn(maxPush)+minPush) * time.Minute)
|
2017-11-03 18:01:49 +00:00
|
|
|
go func() {
|
|
|
|
t := time.NewTimer(g.nextPush.Sub(time.Now()))
|
|
|
|
g.timers[1] = t
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
2019-05-27 23:21:53 +00:00
|
|
|
g.handleNotify(c)
|
2017-11-03 18:01:49 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) endGame() {
|
|
|
|
for _, t := range g.timers {
|
|
|
|
t.Stop()
|
|
|
|
}
|
|
|
|
g.ended = true
|
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (g *game) handleDecrement(c bot.Connector) {
|
2017-11-03 18:01:49 +00:00
|
|
|
g.current++
|
2019-05-27 23:21:53 +00:00
|
|
|
g.bot.Send(c, bot.Edit, g.channel, g.toMessageString(), g.id)
|
2017-11-03 18:01:49 +00:00
|
|
|
if g.current > g.size-2 {
|
2019-05-27 23:21:53 +00:00
|
|
|
g.bot.Send(c, bot.Reply, g.channel, "you lose", g.id)
|
2017-11-03 18:01:49 +00:00
|
|
|
msg := fmt.Sprintf("%s just lost the game after %s", g.who, time.Now().Sub(g.start))
|
2019-05-27 23:21:53 +00:00
|
|
|
g.bot.Send(c, bot.Message, g.channel, msg)
|
2017-11-03 18:01:49 +00:00
|
|
|
g.endGame()
|
|
|
|
} else {
|
2019-05-27 23:21:53 +00:00
|
|
|
g.scheduleDecrement(c)
|
2017-11-03 18:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (g *game) handleNotify(c bot.Connector) {
|
|
|
|
g.bot.Send(c, bot.Reply, g.channel, "You can push now.\n"+g.generateQuestion(), g.id)
|
2017-11-03 18:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) generateQuestion() string {
|
|
|
|
n1 := rand.Intn(99) + (rand.Intn(9)+1)*100
|
|
|
|
n2 := rand.Intn(99) + (rand.Intn(9)+1)*100
|
|
|
|
var op string
|
|
|
|
switch i := rand.Intn(3); i {
|
|
|
|
case 0:
|
|
|
|
// times
|
|
|
|
g.nextAns = n1 * n2
|
|
|
|
op = "*"
|
|
|
|
case 1:
|
|
|
|
// plus
|
|
|
|
g.nextAns = n1 + n2
|
|
|
|
op = "+"
|
|
|
|
case 2:
|
|
|
|
// minus
|
|
|
|
g.nextAns = n1 - n2
|
|
|
|
op = "-"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("What is %d %s %d?", n1, op, n2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) checkAnswer(ans string) bool {
|
|
|
|
if strings.Contains(ans, strconv.Itoa(g.nextAns)) {
|
|
|
|
g.current--
|
|
|
|
if g.current < 0 {
|
|
|
|
g.current = 0
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) toMessageString() string {
|
|
|
|
out := ""
|
|
|
|
for i := 0; i < g.size; i++ {
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
out = out + MOUNTAIN
|
|
|
|
}
|
|
|
|
if i == g.current {
|
|
|
|
out = out + BOULDER
|
|
|
|
} else if i == g.current+1 {
|
|
|
|
out = out + ":" + g.who + ":"
|
|
|
|
}
|
|
|
|
out = out + "\n"
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *SisyphusPlugin {
|
2019-02-05 19:41:38 +00:00
|
|
|
sp := &SisyphusPlugin{
|
2019-05-27 23:21:53 +00:00
|
|
|
bot: b,
|
2017-11-03 18:01:49 +00:00
|
|
|
listenFor: map[string]*game{},
|
|
|
|
}
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(sp, bot.Message, sp.message)
|
|
|
|
b.Register(sp, bot.Reply, sp.replyMessage)
|
|
|
|
b.Register(sp, bot.Help, sp.help)
|
|
|
|
return sp
|
2017-11-03 18:01:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *SisyphusPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2017-11-03 18:01:49 +00:00
|
|
|
if strings.ToLower(message.Body) == "start sisyphus" {
|
2019-05-27 23:21:53 +00:00
|
|
|
b := NewRandomGame(c, p.bot, message.Channel, message.User.Name)
|
2017-11-03 18:01:49 +00:00
|
|
|
p.listenFor[b.id] = b
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Reply, message.Channel, "Over here.", b.id)
|
2017-11-03 18:01:49 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *SisyphusPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
|
|
p.bot.Send(c, bot.Message, message.Channel, "https://en.wikipedia.org/wiki/Sisyphus")
|
2019-02-05 19:41:38 +00:00
|
|
|
return true
|
2017-11-03 18:01:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *SisyphusPlugin) replyMessage(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2019-02-05 19:41:38 +00:00
|
|
|
identifier := args[0].(string)
|
2019-05-27 23:21:53 +00:00
|
|
|
if strings.ToLower(message.User.Name) != strings.ToLower(p.bot.Config().Get("Nick", "bot")) {
|
2017-11-03 18:01:49 +00:00
|
|
|
if g, ok := p.listenFor[identifier]; ok {
|
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("got message on %s: %+v", identifier, message)
|
2017-11-03 18:01:49 +00:00
|
|
|
|
|
|
|
if g.ended {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.ToLower(message.Body) == "end game" {
|
|
|
|
g.endGame()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().After(g.nextPush) {
|
|
|
|
if g.checkAnswer(message.Body) {
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Edit, message.Channel, g.toMessageString(), identifier)
|
|
|
|
g.schedulePush(c)
|
2017-11-03 18:01:49 +00:00
|
|
|
msg := fmt.Sprintf("Ok. You can push again in %s", g.nextPush.Sub(time.Now()))
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Reply, message.Channel, msg, identifier)
|
2017-11-03 18:01:49 +00:00
|
|
|
} else {
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Reply, message.Channel, "you lose", identifier)
|
2017-11-03 18:01:49 +00:00
|
|
|
msg := fmt.Sprintf("%s just lost the sisyphus game after %s", g.who, time.Now().Sub(g.start))
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Message, message.Channel, msg)
|
2017-11-03 18:01:49 +00:00
|
|
|
g.endGame()
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Reply, message.Channel, "you cannot push yet", identifier)
|
2017-11-03 18:01:49 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|