mlpdraftbot/tginterface.go

102 lines
2.3 KiB
Go

package main
import (
"log"
"strconv"
"git.fromouter.space/mcg/draft/mlp"
"git.fromouter.space/hamcha/tg"
lobby "git.fromouter.space/mcg/cardgage/lobby/proto"
room "git.fromouter.space/mcg/cardgage/room/api"
"git.fromouter.space/mcg/mlp-server-tools/draftbot"
)
type tgInterface struct {
client *tg.Telegram
bot *draftbot.DraftBot
usermap map[string]int64
}
func (tgi *tgInterface) Send(msg room.BotMessage) {
// Get room ID
chatid, err := strconv.ParseInt(msg.RoomID, 10, 64)
if err != nil {
log.Printf("[WARN] Trying to send message to weird place: \"%s\". Dropped.\n", msg.RoomID)
return
}
// For now, skip non-messages
if msg.Type != room.MsgMessage {
//TODO
return
}
// Are we sending a message to a player in particular?
if msg.Message.To != "" {
chatid = tgi.usermap[msg.Message.To]
}
switch msg.Message.Type {
default:
tgi.client.SendTextMessage(tg.ClientTextMessageData{
ChatID: chatid,
Text: msg.Message.Message,
})
}
}
func (tgi *tgInterface) Bind(bot *draftbot.DraftBot) {
tgi.bot = bot
}
func (tgi *tgInterface) message(user tg.APIUser, session string, typ string, data interface{}) {
tgi.usermap[user.Username] = user.UserID
tgi.bot.OnMessage(room.ServerMessage{
RoomID: session,
Type: room.MsgMessage,
Message: &room.Message{
From: user.Username,
To: botname,
Type: typ,
Data: data,
},
})
}
func (tgi *tgInterface) NewDraft(chat *tg.APIChat, author tg.APIUser) {
chatid := strconv.FormatInt(chat.ChatID, 10)
tgi.bot.OnMessage(room.ServerMessage{
RoomID: chatid,
Type: room.MsgEvent,
Event: &room.Event{
Type: room.EvtNewRoom,
Room: &lobby.Room{
Id: chatid,
Name: *chat.Title,
Creator: author.Username,
},
},
})
tgi.message(author, chatid, "create", draftbot.SessionOptions{
Players: 8,
Options: draftbot.DraftOptions{
Type: draftbot.DraftSet,
Positioning: draftbot.PosEven,
Set: mlp.SetAbsoluteDiscord,
PackCount: 4,
},
})
}
func (tgi *tgInterface) JoinDraft(chat *tg.APIChat, usr tg.APIUser) {
chatid := strconv.FormatInt(chat.ChatID, 10)
tgi.message(usr, chatid, "join", nil)
}
func (tgi *tgInterface) StartDraft(chat *tg.APIChat, usr tg.APIUser) {
chatid := strconv.FormatInt(chat.ChatID, 10)
tgi.message(usr, chatid, "start", nil)
}