2019-06-26 10:56:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
room "git.fromouter.space/mcg/cardgage/room/api"
|
|
|
|
)
|
|
|
|
|
2019-06-28 09:03:42 +00:00
|
|
|
// DraftBot is the functional part of draftbot
|
|
|
|
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-28 10:54:58 +00:00
|
|
|
Sessions map[string]*session
|
2019-06-26 10:56:02 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:03:42 +00:00
|
|
|
// NewDraftBot creates a new draft bot instance with the given name
|
|
|
|
// and communication interface
|
|
|
|
func NewDraftBot(botAPI BotInterface, name string) *DraftBot {
|
|
|
|
return &DraftBot{
|
2019-06-27 15:12:39 +00:00
|
|
|
API: botAPI,
|
2019-06-27 11:49:57 +00:00
|
|
|
Name: name,
|
2019-06-28 14:15:25 +00:00
|
|
|
Rooms: make(map[string]roomInfo),
|
2019-06-28 10:54:58 +00:00
|
|
|
Sessions: make(map[string]*session),
|
2019-06-26 10:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:03:42 +00:00
|
|
|
// OnMessage is the function to be called when messages are received
|
|
|
|
func (d *DraftBot) OnMessage(msg room.ServerMessage) {
|
2019-06-26 10:56:02 +00:00
|
|
|
switch msg.Type {
|
|
|
|
case room.MsgMessage:
|
2019-06-28 14:15:25 +00:00
|
|
|
if logAll {
|
2019-06-27 11:49:57 +00:00
|
|
|
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-28 14:15:25 +00:00
|
|
|
if logAll {
|
2019-06-27 11:49:57 +00:00
|
|
|
logger.Log("event", "event",
|
|
|
|
"roomid", msg.RoomID,
|
|
|
|
"content", msg.Event.Message)
|
|
|
|
}
|
2019-06-28 09:03:42 +00:00
|
|
|
d.handleEvent(msg.RoomID, *msg.Event)
|
2019-06-26 10:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 11:49:57 +00:00
|
|
|
|
2019-06-28 09:03:42 +00:00
|
|
|
func (d *DraftBot) sendMessage(roomid string, msg room.Message) {
|
2019-06-27 14:59:52 +00:00
|
|
|
d.API.Send(room.BotMessage{
|
|
|
|
RoomID: roomid,
|
|
|
|
Type: room.MsgMessage,
|
|
|
|
Message: &msg,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:03:42 +00:00
|
|
|
func (d *DraftBot) handleMessage(roomid string, msg room.Message) {
|
2019-06-27 14:59:52 +00:00
|
|
|
// 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
|
2019-06-28 09:03:42 +00:00
|
|
|
d.commands(commandMap{
|
|
|
|
// Owner wants to create a session
|
|
|
|
"create": d.cmdfnOnlyOwner(d.cmdCreateSession),
|
|
|
|
})(roomid, msg)
|
2019-06-27 16:14:17 +00:00
|
|
|
return
|
2019-06-27 14:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if player is in the draft
|
2019-06-28 09:03:42 +00:00
|
|
|
_, ok = session.Players[msg.From]
|
2019-06-27 14:59:52 +00:00
|
|
|
if !ok {
|
|
|
|
// Player not in draft, are they asking to join?
|
2019-06-28 09:03:42 +00:00
|
|
|
d.commands(commandMap{
|
|
|
|
// Player wants to join the session
|
|
|
|
"join": d.cmdJoinSession,
|
2019-06-28 10:54:58 +00:00
|
|
|
// Owner wants to start the session (but not partecipate)
|
|
|
|
"start": d.cmdfnOnlyOwner(d.cmdStartSession),
|
2019-06-28 09:03:42 +00:00
|
|
|
})(roomid, msg)
|
|
|
|
return
|
|
|
|
}
|
2019-06-27 16:14:17 +00:00
|
|
|
|
2019-06-28 09:03:42 +00:00
|
|
|
// Players in the draft session
|
|
|
|
d.commands(commandMap{
|
|
|
|
// Player has picked a card
|
|
|
|
"pick": d.cmdPickCard,
|
|
|
|
// Owner wants to start the session
|
|
|
|
"start": d.cmdfnOnlyOwner(d.cmdStartSession),
|
|
|
|
})(roomid, msg)
|
|
|
|
}
|
2019-06-27 16:14:17 +00:00
|
|
|
|
2019-06-28 09:03:42 +00:00
|
|
|
func (d *DraftBot) handleEvent(roomid string, evt room.Event) {
|
|
|
|
switch evt.Type {
|
2019-06-27 16:15:51 +00:00
|
|
|
|
2019-06-28 09:03:42 +00:00
|
|
|
// Got added to a new room
|
|
|
|
case room.EvtNewRoom:
|
|
|
|
// Add room to the list
|
|
|
|
d.Rooms[evt.Room.Id] = roomInfo{
|
|
|
|
Name: evt.Room.Name,
|
|
|
|
Owner: evt.Room.Creator,
|
2019-06-27 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 19:56:47 +00:00
|
|
|
// Someone left
|
|
|
|
case room.EvtLeft:
|
|
|
|
// Check if room has a running session
|
|
|
|
sess, ok := d.Sessions[roomid]
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Check if player is in that session
|
|
|
|
player, ok := sess.Players[evt.PlayerName]
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Replace player with bot
|
|
|
|
//TODO
|
|
|
|
_ = player
|
|
|
|
|
2019-06-28 09:03:42 +00:00
|
|
|
// A room got closed
|
|
|
|
case room.EvtRoomClosed:
|
|
|
|
// Check if there's a session there
|
|
|
|
session, ok := d.Sessions[roomid]
|
|
|
|
if ok {
|
|
|
|
// Close session
|
|
|
|
session.exit <- true
|
|
|
|
// Remove session from list
|
|
|
|
delete(d.Sessions, roomid)
|
2019-06-27 14:59:52 +00:00
|
|
|
}
|
2019-06-28 09:03:42 +00:00
|
|
|
// Remove room from list
|
|
|
|
delete(d.Rooms, roomid)
|
2019-06-27 14:59:52 +00:00
|
|
|
}
|
2019-06-27 11:49:57 +00:00
|
|
|
}
|
2019-06-28 10:54:58 +00:00
|
|
|
|
|
|
|
func (d *DraftBot) handleSessionMessages(roomid string, s *session) {
|
|
|
|
for msg := range s.messages {
|
|
|
|
d.sendMessage(roomid, msg)
|
|
|
|
}
|
|
|
|
}
|