mlp-server-tools/rulebot/rulebot.go

79 lines
1.8 KiB
Go
Raw Permalink Normal View History

2019-07-01 10:55:38 +00:00
package main
import (
room "git.fromouter.space/mcg/cardgage/room/api"
)
// RuleBot is the functional part of RuleBot
type RuleBot struct {
API BotInterface
Name string
Rooms map[string]roomInfo
Games map[string]*engine.Game
}
type roomInfo struct {
Name string
Owner string
}
// BotInterface is the interface needed by RuleBot for working in cardgage
// This exists so that RuleBot can be attached to a mocked API for testing
type BotInterface interface {
Send(room.BotMessage)
}
// NewRuleBot creates a new rule engine instance with the given name
// and communication interface
func NewRuleBot(botAPI BotInterface, name string) *RuleBot {
return &RuleBot{
API: botAPI,
Name: name,
Rooms: make(map[string]roomInfo),
Sessions: make(map[string]*session),
}
}
// OnMessage is the function to be called when messages are received
func (d *RuleBot) OnMessage(msg room.ServerMessage) {
switch msg.Type {
case room.MsgMessage:
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 {
d.handleMessage(msg.RoomID, *msg.Message)
}
case room.MsgEvent:
if logAll {
logger.Log("event", "event",
"roomid", msg.RoomID,
"content", msg.Event.Message)
}
d.handleEvent(msg.RoomID, *msg.Event)
}
}
func (d *RuleBot) sendMessage(roomid string, msg room.Message) {
d.API.Send(room.BotMessage{
RoomID: roomid,
Type: room.MsgMessage,
Message: &msg,
})
}
func (d *RuleBot) handleMessage(roomid string, msg room.Message) {
//TODO
}
func (d *RuleBot) handleSessionMessages(roomid string, s *session) {
for msg := range s.messages {
d.sendMessage(roomid, msg)
}
}