not working concept
This commit is contained in:
109
market.go
109
market.go
@@ -1,19 +1,28 @@
|
||||
package market
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
_ "image/jpeg"
|
||||
"image/png"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.niplace.ru/XoxJlopeZi4BB/imageutils"
|
||||
"git.niplace.ru/XoxJlopeZi4BB/paxpamir/token"
|
||||
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
const (
|
||||
CardNum = 116
|
||||
DominanceCheck = 4
|
||||
EventCards = 12
|
||||
SpecialCards = DominanceCheck + EventCards
|
||||
)
|
||||
|
||||
func shuffle[S ~[]E, E any](a S) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
for i := len(a) - 1; i > 0; i-- {
|
||||
@@ -35,7 +44,7 @@ var ShuffledCardPool []int = func(n int) []int {
|
||||
}
|
||||
shuffle(arr)
|
||||
return arr
|
||||
}(116)
|
||||
}(CardNum)
|
||||
|
||||
var RupeesOnCards []int = func() (ns []int) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
@@ -45,8 +54,8 @@ var RupeesOnCards []int = func() (ns []int) {
|
||||
return
|
||||
}()
|
||||
|
||||
var LinkFormat string = "https://rally-the-troops.com/" +
|
||||
"pax-pamir/cards/card_%d.jpg"
|
||||
var LinkFormat string = "https://rally-the-troops.com" +
|
||||
"/pax-pamir/cards/card_%d.jpg"
|
||||
|
||||
type Button struct {
|
||||
Text string `json:"text"`
|
||||
@@ -75,47 +84,78 @@ func Buttons(b []Button) tg.InlineKeyboardMarkup {
|
||||
)
|
||||
}
|
||||
|
||||
func Must[T any](v T, err error) T {
|
||||
if err != nil {
|
||||
log.Println("ERROR:", err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func GetImage(link string) (image.Image, error) {
|
||||
resp, err := http.Get(link)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
img, _, err := image.Decode(resp.Body)
|
||||
if img == nil {
|
||||
log.Fatal("img is nil")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func HandleCommand(update tg.Update, bot *tg.BotAPI) {
|
||||
maxCost := 5
|
||||
var err error
|
||||
for i := 0; i <= maxCost; i++ {
|
||||
var (
|
||||
msg tg.PhotoConfig
|
||||
buf *bytes.Buffer
|
||||
img image.Image
|
||||
var buf *bytes.Buffer
|
||||
var img = imageutils.Concat(
|
||||
Must(GetImage(fmt.Sprintf(LinkFormat,
|
||||
takeOne(ShuffledCardPool)))),
|
||||
Must(GetImage(fmt.Sprintf(LinkFormat,
|
||||
takeOne(ShuffledCardPool)))),
|
||||
imageutils.Right,
|
||||
)
|
||||
|
||||
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)
|
||||
if img == nil {
|
||||
log.Fatal("pizdec")
|
||||
}
|
||||
msg = tg.NewPhoto(
|
||||
update.Message.Chat.ID,
|
||||
tg.FileBytes(buf.Bytes()),
|
||||
|
||||
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 RupeesOnCard[j] >= i {
|
||||
if RupeesOnCards[j] >= i {
|
||||
s += fmt.Sprintf(", get 1")
|
||||
}
|
||||
return
|
||||
}()
|
||||
buttons := append(buttons, Button{Text: text})
|
||||
buttons = append(buttons, Button{Text: text})
|
||||
}
|
||||
_, err := bot.Send(msg) // get response message
|
||||
|
||||
msg := tg.NewPhoto(
|
||||
update.Message.Chat.ID,
|
||||
tg.FileBytes{Name: "cards", Bytes: buf.Bytes()},
|
||||
)
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -150,3 +190,18 @@ func ConsumeCallback(update tg.Update, bot *tg.BotAPI) {
|
||||
}
|
||||
log.Print("request sent. response:", req.Description)
|
||||
}
|
||||
|
||||
func main() {
|
||||
bot := Must(tg.NewBotAPI(token.Token()))
|
||||
{
|
||||
log.Printf("authorized on account %s", bot.Self.UserName)
|
||||
u := tg.NewUpdate(0)
|
||||
u.Timeout = 60
|
||||
|
||||
updates := bot.GetUpdatesChan(u)
|
||||
|
||||
for update := range updates {
|
||||
HandleUpdate(update, bot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user