Telegram bot for drafting MLP:CCG (@mlpdraftbot)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

109 lines
2.5 KiB

package main
import (
"bytes"
"strings"
"text/template"
"git.fromouter.space/mcg/draft"
"git.fromouter.space/mcg/draftbot"
"github.com/Masterminds/sprig"
)
var sessionCreatedTplText = `<b>Draft session open!</b>
Join with "/join".
Start with "/startdraft".
<b>Draft type</b>: {{ .Options.Options.Type }}
<b>Players</b>: {{ .Players | join " "}} ({{ .PlayerCount }}/{{ .Options.Players }})`
var sessionOrderTplText = `<b>Here's how everyone is positioned:</b>
{{ .Players | join "\n" }}`
var newPickTplText = `Pack <b>{{ .CurrentPack }}</b> - Pick <b>{{ .PickNum }}</b>
Waiting: {{ .PickWaiting | join " " }}
Done: {{ .PickDone | join " " }}`
var yourPicksTplText = `Draft finished!
Here are your picks: {{ .PonyheadURL }}`
var templateMap = map[string]string{
"session-created": sessionCreatedTplText,
"session-order": sessionOrderTplText,
"new-pick": newPickTplText,
"your-picks": yourPicksTplText,
}
var templates = map[string]*template.Template{}
func initTemplates() {
for k, v := range templateMap {
templates[k] = template.Must(template.New("base").Funcs(sprig.TxtFuncMap()).Parse(v))
}
}
var sessionCreatedTpl = (sessionCreatedTplText)
func txtDraftSessionCreated(opt draftbot.SessionOptions, players map[string]*draft.Player) string {
var tpl bytes.Buffer
names := playerNames(players)
templates["session-created"].Execute(&tpl, struct {
Players []string
PlayerCount int
Options draftbot.SessionOptions
}{
names,
len(names),
opt,
})
return tpl.String()
}
func txtDraftSessionOrder(players []string) string {
var tpl bytes.Buffer
for i := range players {
if players[i] == "bot" {
players[i] = "🤖 Bot"
} else {
playerName := players[i][strings.IndexRune(players[i], ':')+1:]
players[i] = "👤 @" + playerName
}
}
templates["session-order"].Execute(&tpl, struct {
Players []string
}{
players,
})
return tpl.String()
}
func txtCurrentPick(sess *draftSession) string {
var tpl bytes.Buffer
templates["new-pick"].Execute(&tpl, sess)
return tpl.String()
}
func txtYourPicks(picks []string) string {
var tpl bytes.Buffer
templates["your-picks"].Execute(&tpl, struct {
PonyheadURL string
}{
ponyheadURL(picks),
})
return tpl.String()
}
func playerNames(lst map[string]*draft.Player) (out []string) {
for key := range lst {
out = append(out, "@"+key)
}
return
}
func ponyheadURL(picks []string) string {
base := "https://ponyhead.com/deckbuilder?v1code="
for _, pick := range picks {
base += pick + "x1-"
}
return strings.TrimRight(base, "-")
}