From b6a0896de879adffb34137335a9edccf242018b8 Mon Sep 17 00:00:00 2001 From: potassium5703 Date: Thu, 26 Dec 2024 20:04:36 +0300 Subject: [PATCH] sync --- go.mod | 8 +++ go.sum | 4 ++ market.go | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 market.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..188fb68 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module paxpamir + +go 1.23.1 + +require ( + git.niplace.ru/XoxJlopeZi4BB/imageutils v0.0.0-20241224134015-2709034fc322 + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ccbdedf --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +git.niplace.ru/XoxJlopeZi4BB/imageutils v0.0.0-20241224134015-2709034fc322 h1:jefEdqKZ/JKnmky9LipaS/UH0phAhz/56ORHIjmHREk= +git.niplace.ru/XoxJlopeZi4BB/imageutils v0.0.0-20241224134015-2709034fc322/go.mod h1:QieEgSGk9ZYISDv5XyMMktQXi/2qlTmdHuxTlt1Xfq8= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= diff --git a/market.go b/market.go new file mode 100644 index 0000000..d302938 --- /dev/null +++ b/market.go @@ -0,0 +1,152 @@ +package market + +import ( + "bytes" + "fmt" + "image" + "io" + "log" + "math/rand" + "net/http" + "time" + + "git.niplace.ru/XoxJlopeZi4BB/imageutils" + tg "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +func shuffle[S ~[]E, E any](a S) { + rand.Seed(time.Now().UnixNano()) + for i := len(a) - 1; i > 0; i-- { + j := rand.Intn(i + 1) + a[i], a[j] = a[j], a[i] + } +} + +func takeOne[S ~[]T, T any](s S) T { + v := s[0] + s = s[1:] + return v +} + +var ShuffledCardPool []int = func(n int) []int { + arr := make([]int, n, n) + for i := 0; i < n; i++ { + arr[i] = i + 1 + } + shuffle(arr) + return arr +}(116) + +var RupeesOnCards []int = func() (ns []int) { + rand.Seed(time.Now().UnixNano()) + for i := 0; i < 2; i++ { + ns = append(ns, rand.Intn(4)) + } + return +}() + +var LinkFormat string = "https://rally-the-troops.com/" + + "pax-pamir/cards/card_%d.jpg" + +type Button struct { + Text string `json:"text"` + Data string `json:"data"` +} + +type ListEntry struct { + Images []string + Buttons []Button +} + +func Buttons(b []Button) tg.InlineKeyboardMarkup { + return tg.NewInlineKeyboardMarkup( + func() []tg.InlineKeyboardButton { + var row []tg.InlineKeyboardButton + for _, Button := range b { + row = append(row, + tg.NewInlineKeyboardButtonData( + Button.Text, + Button.Data, + ), + ) + } + return row + }(), + ) +} + +func HandleCommand(update tg.Update, bot *tg.BotAPI) { + maxCost := 5 + for i := 0; i <= maxCost; i++ { + var ( + msg tg.PhotoConfig + buf *bytes.Buffer + img image.Image + ) + + for j := 0; j < 2; j++ { + cardNum := ShuffledCardPool[0] + ShuffledCardPool = ShuffledCardPool[1:] + resp, err := http.Get(fmt.Sprintf(LinkFormat, + cardNum)) + if err != nil { + log.Println("http GET error:", err) + } + defer resp.Body.Close() + + io.Copy(buf, resp.Body) + } + msg = tg.NewPhoto( + update.Message.Chat.ID, + tg.FileBytes(buf.Bytes()), + ) + buttons := []Button{} + for j := 0; j < 2; j++ { + text := func() (s string) { + s = fmt.Sprintf("buy for %d", i) + if RupeesOnCard[j] >= i { + s += fmt.Sprintf(", get 1") + } + return + }() + buttons := append(buttons, Button{Text: text}) + } + _, 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) + } +} + +func HandleUpdate(update tg.Update, bot *tg.BotAPI) { + switch { + case update.Message != nil: + log.Print("new message. id:", update.Message.MessageID) + if update.Message.IsCommand() { + HandleCommand(update, bot) + } else { + log.Println("message is not a command:\n", + update.Message.Text) + } + case update.CallbackQuery != nil: + ConsumeCallback(update, bot) + } + return +} + +func ConsumeCallback(update tg.Update, bot *tg.BotAPI) { + updateMsg := update.CallbackQuery.Message + log.Print("new CallbackQuery. id:", updateMsg.MessageID) + callback := tg.NewCallback( + update.CallbackQuery.ID, + update.CallbackQuery.Data) + + // Recieve callback + req, err := bot.Request(callback) + if err != nil { + log.Println("failed on request:", err) + } + log.Print("request sent. response:", req.Description) +}