172 lines
4.1 KiB
Go
172 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.fromouter.space/mcg/draft"
|
|
|
|
"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 {
|
|
case "draft-newpick":
|
|
cards := msg.Message.Data.(draft.Pack)
|
|
cardphotos := []tg.APIInputMediaPhoto{}
|
|
buttons := []tg.APIInlineKeyboardButton{}
|
|
for _, card := range cards {
|
|
cardphotos = append(cardphotos, tg.APIInputMediaPhoto{
|
|
Type: "photo",
|
|
Media: fmt.Sprintf("http://ponyhead.com/img/cards/%s.jpg", card.ID),
|
|
})
|
|
buttons = append(buttons, tg.APIInlineKeyboardButton{
|
|
Text: strings.ToUpper(card.ID),
|
|
CallbackData: fmt.Sprintf("PICK %s %s %s", msg.RoomID, msg.Message.To, card),
|
|
})
|
|
}
|
|
// If sending more than 10 cards, split in two messages
|
|
if len(cardphotos) > 10 {
|
|
half := len(cardphotos) / 2
|
|
tgi.client.SendAlbum(tg.ClientAlbumData{
|
|
ChatID: chatid,
|
|
Silent: true,
|
|
Media: cardphotos[:half],
|
|
})
|
|
tgi.client.SendAlbum(tg.ClientAlbumData{
|
|
ChatID: chatid,
|
|
Silent: true,
|
|
Media: cardphotos[half:],
|
|
})
|
|
} else {
|
|
tgi.client.SendAlbum(tg.ClientAlbumData{
|
|
ChatID: chatid,
|
|
Silent: true,
|
|
Media: cardphotos,
|
|
})
|
|
}
|
|
tgi.client.SendTextMessage(tg.ClientTextMessageData{
|
|
ChatID: chatid,
|
|
Text: "Choose your pick:",
|
|
ReplyMarkup: &tg.APIInlineKeyboardMarkup{
|
|
InlineKeyboard: rebalance(buttons),
|
|
},
|
|
})
|
|
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)
|
|
}
|
|
|
|
func rebalance(buttons []tg.APIInlineKeyboardButton) [][]tg.APIInlineKeyboardButton {
|
|
out := [][]tg.APIInlineKeyboardButton{}
|
|
currentRow := []tg.APIInlineKeyboardButton{}
|
|
var perRow int
|
|
switch {
|
|
case len(buttons) < 5:
|
|
perRow = 2
|
|
case len(buttons) < 10:
|
|
perRow = 3
|
|
default:
|
|
perRow = 4
|
|
}
|
|
for i := range buttons {
|
|
currentRow = append(currentRow, buttons[i])
|
|
if i%perRow == (perRow - 1) {
|
|
out = append(out, currentRow)
|
|
currentRow = []tg.APIInlineKeyboardButton{}
|
|
}
|
|
}
|
|
if len(currentRow) > 0 {
|
|
out = append(out, currentRow)
|
|
}
|
|
return out
|
|
}
|