2019-06-26 10:56:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-06-27 14:59:52 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-06-26 10:56:02 +00:00
|
|
|
room "git.fromouter.space/mcg/cardgage/room/api"
|
2019-06-27 14:59:52 +00:00
|
|
|
"git.fromouter.space/mcg/draft"
|
2019-06-26 10:56:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type draftBot struct {
|
2019-06-27 14:59:52 +00:00
|
|
|
API BotInterface
|
2019-06-27 11:49:57 +00:00
|
|
|
Name string
|
2019-06-27 14:59:52 +00:00
|
|
|
Rooms map[string]roomInfo
|
2019-06-26 10:56:02 +00:00
|
|
|
Sessions map[string]session
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:59:52 +00:00
|
|
|
type roomInfo struct {
|
|
|
|
Name string
|
|
|
|
Owner string
|
|
|
|
}
|
|
|
|
|
|
|
|
// BotInterface is the interface needed by draftbot for working in cardgage
|
|
|
|
// This exists so that draftbot can be attached to a mocked API for testing
|
|
|
|
type BotInterface interface {
|
|
|
|
Send(room.BotMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDraftBot(botAPI BotInterface, name string) *draftBot {
|
2019-06-26 10:56:02 +00:00
|
|
|
return &draftBot{
|
2019-06-27 11:49:57 +00:00
|
|
|
Name: name,
|
2019-06-26 10:56:02 +00:00
|
|
|
Sessions: make(map[string]session),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *draftBot) onMessage(msg room.ServerMessage) {
|
|
|
|
switch msg.Type {
|
|
|
|
case room.MsgMessage:
|
2019-06-27 11:49:57 +00:00
|
|
|
if *logAll {
|
|
|
|
logger.Log("event", "message",
|
|
|
|
"roomid", msg.RoomID,
|
|
|
|
"from", msg.Message.From,
|
|
|
|
"to", msg.Message.To,
|
|
|
|
"content", msg.Message.Message)
|
|
|
|
}
|
|
|
|
// Only consider messages that speak directly to me
|
|
|
|
if msg.Message.To == d.Name {
|
2019-06-27 14:59:52 +00:00
|
|
|
d.handleMessage(msg.RoomID, *msg.Message)
|
2019-06-27 11:49:57 +00:00
|
|
|
}
|
2019-06-26 10:56:02 +00:00
|
|
|
case room.MsgEvent:
|
2019-06-27 11:49:57 +00:00
|
|
|
if *logAll {
|
|
|
|
logger.Log("event", "event",
|
|
|
|
"roomid", msg.RoomID,
|
|
|
|
"content", msg.Event.Message)
|
|
|
|
}
|
2019-06-26 10:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 11:49:57 +00:00
|
|
|
|
2019-06-27 14:59:52 +00:00
|
|
|
func (d *draftBot) sendMessage(roomid string, msg room.Message) {
|
|
|
|
d.API.Send(room.BotMessage{
|
|
|
|
RoomID: roomid,
|
|
|
|
Type: room.MsgMessage,
|
|
|
|
Message: &msg,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *draftBot) handleMessage(roomid string, msg room.Message) {
|
|
|
|
// Get session in room
|
|
|
|
session, ok := d.Sessions[roomid]
|
|
|
|
if !ok {
|
|
|
|
// Room does not have a currently running session, ignore unless it's the owner asking for specific stuff
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if player is in the draft
|
|
|
|
player, ok := session.Players[msg.From]
|
|
|
|
if !ok {
|
|
|
|
// Player not in draft, are they asking to join?
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msg.Type {
|
|
|
|
// Player has picked a card
|
|
|
|
case "pick":
|
|
|
|
// Get picked card
|
|
|
|
picked := msg.Data.(string)
|
|
|
|
|
|
|
|
// Try to pick on player struct
|
|
|
|
err := player.Pick(draft.Card{ID: picked})
|
|
|
|
if err != nil {
|
|
|
|
if err == draft.ErrNotInPack {
|
|
|
|
d.sendMessage(roomid, room.Message{
|
|
|
|
To: msg.From,
|
|
|
|
Type: "invalid-pick",
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Technically not needed, as Pick can only throw ErrNotInPack right now
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.sendMessage(roomid, room.Message{
|
|
|
|
Channel: "draft",
|
|
|
|
Type: "card-picked",
|
|
|
|
Data: struct {
|
|
|
|
Player string
|
|
|
|
}{
|
|
|
|
msg.From,
|
|
|
|
},
|
|
|
|
Message: fmt.Sprintf("%s picked a card from his pack", msg.From),
|
|
|
|
})
|
|
|
|
}
|
2019-06-27 11:49:57 +00:00
|
|
|
}
|