Skip to content

Commit

Permalink
draw a Poker card
Browse files Browse the repository at this point in the history
  • Loading branch information
aimerneige committed May 9, 2024
1 parent f2fa4b3 commit 5c5f93f
Show file tree
Hide file tree
Showing 57 changed files with 65 additions and 2 deletions.
Binary file added internal/plugin/random/assets/back.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/10club.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/10diamond.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/10heart.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/10spade.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/2club.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/2diamond.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/2heart.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/2spade.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/3club.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/3diamond.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/3heart.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/3spade.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/4club.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/4diamond.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/4heart.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/4spade.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/5club.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/5diamond.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/5heart.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/5spade.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/6club.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/6diamond.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/6heart.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/6spade.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added internal/plugin/random/assets/card/7club.jpg
Binary file added internal/plugin/random/assets/card/7diamond.jpg
Binary file added internal/plugin/random/assets/card/7heart.jpg
Binary file added internal/plugin/random/assets/card/7spade.jpg
Binary file added internal/plugin/random/assets/card/8club.jpg
Binary file added internal/plugin/random/assets/card/8diamond.jpg
Binary file added internal/plugin/random/assets/card/8heart.jpg
Binary file added internal/plugin/random/assets/card/8spade.jpg
Binary file added internal/plugin/random/assets/card/9club.jpg
Binary file added internal/plugin/random/assets/card/9diamond.jpg
Binary file added internal/plugin/random/assets/card/9heart.jpg
Binary file added internal/plugin/random/assets/card/9spade.jpg
Binary file added internal/plugin/random/assets/card/Aclub.jpg
Binary file added internal/plugin/random/assets/card/Adiamond.jpg
Binary file added internal/plugin/random/assets/card/Aheart.jpg
Binary file added internal/plugin/random/assets/card/Aspade.jpg
Binary file added internal/plugin/random/assets/card/Jclub.jpg
Binary file added internal/plugin/random/assets/card/Jdiamond.jpg
Binary file added internal/plugin/random/assets/card/Jheart.jpg
Binary file added internal/plugin/random/assets/card/Jspade.jpg
Binary file added internal/plugin/random/assets/card/Kclub.jpg
Binary file added internal/plugin/random/assets/card/Kdiamond.jpg
Binary file added internal/plugin/random/assets/card/Kheart.jpg
Binary file added internal/plugin/random/assets/card/Kspade.jpg
Binary file added internal/plugin/random/assets/card/Lynette.jpg
Binary file added internal/plugin/random/assets/card/Lyney.jpg
Binary file added internal/plugin/random/assets/card/Qclub.jpg
Binary file added internal/plugin/random/assets/card/Qdiamond.jpg
Binary file added internal/plugin/random/assets/card/Qheart.jpg
Binary file added internal/plugin/random/assets/card/Qspade.jpg
63 changes: 62 additions & 1 deletion internal/plugin/random/random.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package random

import (
_ "embed"
"embed"
b64 "encoding/base64"
"fmt"
"io/fs"
"math/rand"
"path"

log "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)

const CardImgPath = "assets/card"

//go:embed assets/cxk.mp3
var cxkBytes []byte

//go:embed assets/card
var cardEmbedFS embed.FS

func init() {
engine := zero.New()
engine.OnFullMatchGroup([]string{"掷硬币", "/coin"}).
Expand All @@ -38,4 +46,57 @@ func init() {
replyTextMessage := fmt.Sprintf("掷出了 %d 点。", (rand.Int()%6)+1)
ctx.SendChain(message.At(ctx.Event.UserID), message.Text(replyTextMessage))
})
engine.OnFullMatchGroup([]string{"抽扑克", "/card"}).
SetPriority(6).
SetBlock(true).
Handle(func(ctx *zero.Ctx) {
ctx.Send(drawCard(1))
})
}

func drawCard(number int) message.Message {
cardImages, err := fs.ReadDir(cardEmbedFS, CardImgPath)
log.Errorln(cardImages)
if err != nil {
log.Errorln("[random]", "Fail to read card images", err)
return []message.MessageSegment{message.Text("发生错误,无法读取扑克图片")}
}
cardImagePaths := make([]string, 0, 78)
for _, card := range cardImages {
if !card.IsDir() {
_imgPath := path.Join(CardImgPath, card.Name())
cardImagePaths = append(cardImagePaths, _imgPath)
}
}
cardResult := randomDraw(cardImagePaths, number)
log.Debugln("[tarot]", fmt.Sprintf("cards: %v", cardResult))
imgs := make([]message.MessageSegment, number)
for i, imgPath := range cardResult {
imgData, err := fs.ReadFile(cardEmbedFS, imgPath)
// 读取图片
if err != nil {
log.Errorln("[tarot]", "Fail to read card image", err)
imgs[i] = message.Text("[ERROR] 读取图片失败,请查阅后台日志。\n")
continue
}
imgs[i] = message.Image("base64://" + b64.StdEncoding.EncodeToString(imgData))
}
return imgs
}

// randomDraw 随机不放回抽取
func randomDraw(s []string, k int) []string {
n := len(s)
if k > n {
k = n
}

result := make([]string, k)
for i := 0; i < k; i++ {
j := rand.Intn(n-i) + i
result[i] = s[j]
s[i], s[j] = s[j], s[i]
}

return result
}
4 changes: 3 additions & 1 deletion internal/plugin/tarot/tarot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/wdvxdr1123/ZeroBot/message"
)

const DeckImgPath = "assets/deck"

//go:embed assets/deck
var deckEmbedFS embed.FS

Expand Down Expand Up @@ -76,7 +78,7 @@ func drawCard(number int) message.Message {
if (rand.Int() % 3) == 0 {
theme = "bilibili"
}
deckPath := path.Join("./assets/deck/", theme)
deckPath := path.Join(DeckImgPath, theme)
cardImages, err := fs.ReadDir(deckEmbedFS, deckPath)
if err != nil {
log.Errorln("[tarot]", "Fail to read card images.", err)
Expand Down

0 comments on commit 5c5f93f

Please sign in to comment.