From d9617ff1af02f9796d222058b59d0714573fc804 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 22 Aug 2019 18:03:54 +0200 Subject: [PATCH] Send picks as gallery --- go.sum | 1 + main.go | 2 +- tginterface.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/go.sum b/go.sum index 44fd493..ead7780 100644 --- a/go.sum +++ b/go.sum @@ -268,6 +268,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd h1:/e+gpKk9r3dJobndpTytxS2gOy6m5uvpg+ISQoEcusQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc= diff --git a/main.go b/main.go index 8201613..9787b74 100644 --- a/main.go +++ b/main.go @@ -97,7 +97,7 @@ func webhook(update tg.APIUpdate) { return } case tg.ChatTypePrivate: - //todo + //TODO } } } diff --git a/tginterface.go b/tginterface.go index 38dfb5c..6661b43 100644 --- a/tginterface.go +++ b/tginterface.go @@ -1,8 +1,12 @@ package main import ( + "fmt" "log" "strconv" + "strings" + + "git.fromouter.space/mcg/draft" "git.fromouter.space/mcg/draft/mlp" @@ -38,6 +42,47 @@ func (tgi *tgInterface) Send(msg room.BotMessage) { } 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, @@ -99,3 +144,28 @@ 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 +}