Send picks as gallery
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Hamcha 2019-08-22 18:03:54 +02:00
parent 5e7513fd42
commit d9617ff1af
Signed by: hamcha
GPG key ID: 44AD3571EB09A39E
3 changed files with 72 additions and 1 deletions

1
go.sum
View file

@ -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/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-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-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= 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/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=

View file

@ -97,7 +97,7 @@ func webhook(update tg.APIUpdate) {
return return
} }
case tg.ChatTypePrivate: case tg.ChatTypePrivate:
//todo //TODO
} }
} }
} }

View file

@ -1,8 +1,12 @@
package main package main
import ( import (
"fmt"
"log" "log"
"strconv" "strconv"
"strings"
"git.fromouter.space/mcg/draft"
"git.fromouter.space/mcg/draft/mlp" "git.fromouter.space/mcg/draft/mlp"
@ -38,6 +42,47 @@ func (tgi *tgInterface) Send(msg room.BotMessage) {
} }
switch msg.Message.Type { 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: default:
tgi.client.SendTextMessage(tg.ClientTextMessageData{ tgi.client.SendTextMessage(tg.ClientTextMessageData{
ChatID: chatid, ChatID: chatid,
@ -99,3 +144,28 @@ func (tgi *tgInterface) StartDraft(chat *tg.APIChat, usr tg.APIUser) {
chatid := strconv.FormatInt(chat.ChatID, 10) chatid := strconv.FormatInt(chat.ChatID, 10)
tgi.message(usr, chatid, "start", nil) 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
}