sokobass.

This commit is contained in:
skkiesel 2017-11-02 16:32:02 -04:00
parent 54b83cd444
commit 1ac566c2c2
2 changed files with 129 additions and 24 deletions

View File

@ -39,8 +39,12 @@ func (mb *MockBot) SendAction(ch string, msg string) string {
mb.Actions = append(mb.Actions, msg)
return fmt.Sprintf("a-%d", len(mb.Actions)-1)
}
func (mb *MockBot) ReplyToMessageIdentifier(channel, message, identifier string) (string, bool) { return "", false }
func (mb *MockBot) ReplyToMessage(channel, message string, replyTo msg.Message) (string, bool) { return "", false }
func (mb *MockBot) ReplyToMessageIdentifier(channel, message, identifier string) (string, bool) {
return "", false
}
func (mb *MockBot) ReplyToMessage(channel, message string, replyTo msg.Message) (string, bool) {
return "", false
}
func (mb *MockBot) MsgReceived(msg msg.Message) {}
func (mb *MockBot) EventReceived(msg msg.Message) {}
func (mb *MockBot) Filter(msg msg.Message, s string) string { return "" }
@ -77,6 +81,11 @@ func (mb *MockBot) Edit(channel, newMessage, identifier string) bool {
}
return true
}
func (mb *MockBot) ReplyMsgReceived(msg.Message, string) {
}
func (mb *MockBot) GetEmojiList() map[string]string { return make(map[string]string) }
func (mb *MockBot) RegisterFilter(s string, f func(string) string) {}

View File

@ -1,42 +1,115 @@
package rpgORdie
import (
"fmt"
"strings"
"time"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
)
const (
DUDE = ":lion_face:"
BOULDER = ":full_moon:"
HOLE = ":new_moon:"
EMPTY = ":white_large_square:"
OK = iota
INVALID = iota
WIN = iota
)
type RPGPlugin struct {
Bot bot.Bot
listenFor map[string]bool
Bot bot.Bot
listenFor map[string]*board
}
type board struct {
state [][]string
x, y int
}
func NewRandomBoard() *board {
boardSize := 5
b := board{
state: make([][]string, boardSize),
x: boardSize - 1,
y: boardSize - 1,
}
for i := 0; i < boardSize; i++ {
b.state[i] = make([]string, boardSize)
for j := 0; j < boardSize; j++ {
b.state[i][j] = ":white_large_square:"
}
}
b.state[boardSize-1][boardSize-1] = DUDE
b.state[boardSize/2][boardSize/2] = BOULDER
b.state[0][0] = HOLE
return &b
}
func (b *board) toMessageString() string {
lines := make([]string, len(b.state))
for i := 0; i < len(b.state); i++ {
lines[i] = strings.Join(b.state[i], "")
}
return strings.Join(lines, "\n")
}
func (b *board) checkAndMove(dx, dy int) int {
newX := b.x + dx
newY := b.y + dy
if newX < 0 || newY < 0 || newX >= len(b.state) || newY >= len(b.state) {
return INVALID
}
if b.state[newY][newX] == HOLE {
return INVALID
}
win := false
if b.state[newY][newX] == BOULDER {
newBoulderX := newX + dx
newBoulderY := newY + dy
if newBoulderX < 0 || newBoulderY < 0 || newBoulderX >= len(b.state) || newBoulderY >= len(b.state) {
return INVALID
}
if b.state[newBoulderY][newBoulderX] != HOLE {
b.state[newBoulderY][newBoulderX] = BOULDER
} else {
win = true
}
}
b.state[newY][newX] = DUDE
b.state[b.y][b.x] = EMPTY
b.x = newX
b.y = newY
if win {
return WIN
}
return OK
}
func New(b bot.Bot) *RPGPlugin {
return &RPGPlugin{
Bot: b,
listenFor: map[string]bool{},
Bot: b,
listenFor: map[string]*board{},
}
}
func (p *RPGPlugin) Message(message msg.Message) bool {
if strings.ToLower(message.Body) == "start rpg" {
ts := p.Bot.SendMessage(message.Channel, "I'll edit this.")
p.listenFor[ts] = true
time.Sleep(2 * time.Second)
edited := ""
for i := 0; i <= 5; i++ {
p.Bot.Edit(message.Channel, edited, ts)
edited += ":fire:"
time.Sleep(500 * time.Millisecond)
}
p.Bot.Edit(message.Channel, "HECK YES", ts)
p.Bot.ReplyToMessageIdentifier(message.Channel, "How's this reply?", ts)
b := NewRandomBoard()
ts := p.Bot.SendMessage(message.Channel, b.toMessageString())
p.listenFor[ts] = b
p.Bot.ReplyToMessageIdentifier(message.Channel, "Over here.", ts)
return true
}
return false
@ -64,8 +137,31 @@ func (p *RPGPlugin) RegisterWeb() *string {
func (p *RPGPlugin) ReplyMessage(message msg.Message, identifier string) bool {
if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Nick) {
if _, ok := p.listenFor[identifier]; ok {
p.Bot.ReplyToMessageIdentifier(message.Channel, "Pong", identifier)
if b, ok := p.listenFor[identifier]; ok {
var res int
if message.Body == "left" {
res = b.checkAndMove(-1, 0)
} else if message.Body == "right" {
res = b.checkAndMove(1, 0)
} else if message.Body == "up" {
res = b.checkAndMove(0, -1)
} else if message.Body == "down" {
res = b.checkAndMove(0, 1)
} else {
return false
}
switch res {
case OK:
p.Bot.Edit(message.Channel, b.toMessageString(), identifier)
case WIN:
p.Bot.Edit(message.Channel, b.toMessageString(), identifier)
p.Bot.ReplyToMessageIdentifier(message.Channel, "congratulations, you beat the easiest level imaginable.", identifier)
case INVALID:
p.Bot.ReplyToMessageIdentifier(message.Channel, fmt.Sprintf("you can't move %s", message.Body), identifier)
}
return true
}
}