diff --git a/market.go b/market.go index 422ec8f..6659e7d 100644 --- a/market.go +++ b/market.go @@ -9,8 +9,8 @@ import ( "log" "math/rand" "net/http" - "time" "slices" + "time" "git.niplace.ru/XoxJlopeZi4BB/imageutils" "git.niplace.ru/XoxJlopeZi4BB/paxpamir/token" @@ -35,7 +35,7 @@ func shuffle[S ~[]E, E any](a S) { func takeOne[S ~[]T, T any](s *S) T { var new = slices.Clone(*s) v := new[0] - *s = new[1:] + *s = new[1:] return v } @@ -69,6 +69,59 @@ type ListEntry struct { Buttons []Button } +var Commands = map[string](func(tg.Update, *tg.BotAPI)){ + "market": func(update tg.Update, bot *tg.BotAPI) { + maxCost := 5 + var err error + for i := 0; i <= maxCost; i++ { + buf := new(bytes.Buffer) + var img = imageutils.Concat( + Must(GetImage(fmt.Sprintf(LinkFormat, + takeOne(&ShuffledCardPool)))), + Must(GetImage(fmt.Sprintf(LinkFormat, + takeOne(&ShuffledCardPool)))), + imageutils.Right, + ) + err = png.Encode(buf, img) + if err != nil { + log.Println("error on decode:", err) + } + + buttons := []Button{} + for j := 0; j < 2; j++ { + text := func() (s string) { + s = fmt.Sprintf("buy for %d", i) + if RupeesOnCards[j] >= i { + s += fmt.Sprintf(", get 1") + } + return + }() + buttons = append(buttons, Button{Text: text, Data: "somethin"}) + } + + msg := tg.NewPhoto( + update.Message.Chat.ID, + tg.FileBytes{Name: "cards", Bytes: buf.Bytes()}, + ) + fmt.Println(Buttons(buttons)) + msg.ReplyMarkup = Buttons(buttons) + + responseMsg, err := bot.Send(msg) // get response message + if err != nil { + log.Println("failed to send message on command:", + err) + } + + log.Print("message sent. id:", responseMsg.MessageID) + } + }, + // todo + "hand": func(update tg.Update, bot *tg.BotAPI) { + }, + "court": func(update tg.Update, bot *tg.BotAPI) { + }, +} + func Buttons(b []Button) tg.InlineKeyboardMarkup { return tg.NewInlineKeyboardMarkup( func() []tg.InlineKeyboardButton { @@ -108,6 +161,7 @@ func GetImage(link string) (image.Image, error) { } func HandleCommand(update tg.Update, bot *tg.BotAPI) { +<<<<<<< HEAD maxCost := 5 var err error for i := 0; i <= maxCost; i++ { @@ -150,6 +204,9 @@ func HandleCommand(update tg.Update, bot *tg.BotAPI) { log.Print("message sent. id:", responseMsg.MessageID) } +======= + Commands[update.Message.Command()](update, bot) +>>>>>>> 5d7fe44 (separated commands) } func HandleUpdate(update tg.Update, bot *tg.BotAPI) {