From 7eb81969a3c54e446c9f309fbd8a846357eab6b2 Mon Sep 17 00:00:00 2001 From: Aimer Neige Date: Fri, 10 May 2024 15:18:39 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E6=8A=BD=E6=89=91=E5=85=8B=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 1 + plugin/poker/poker.go | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 plugin/poker/poker.go diff --git a/main.go b/main.go index e1881754d4..208914ae0d 100644 --- a/main.go +++ b/main.go @@ -115,6 +115,7 @@ import ( _ "github.com/FloatTech/ZeroBot-Plugin/plugin/nsfw" // nsfw图片识别 _ "github.com/FloatTech/ZeroBot-Plugin/plugin/nwife" // 本地老婆 _ "github.com/FloatTech/ZeroBot-Plugin/plugin/omikuji" // 浅草寺求签 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin/poker" // 抽扑克 _ "github.com/FloatTech/ZeroBot-Plugin/plugin/qqwife" // 一群一天一夫一妻制群老婆 _ "github.com/FloatTech/ZeroBot-Plugin/plugin/qzone" // qq空间表白墙 _ "github.com/FloatTech/ZeroBot-Plugin/plugin/realcugan" // realcugan清晰术 diff --git a/plugin/poker/poker.go b/plugin/poker/poker.go new file mode 100644 index 0000000000..5b49d31557 --- /dev/null +++ b/plugin/poker/poker.go @@ -0,0 +1,54 @@ +package poker + +import ( + "math/rand" + "os" + "path" + + ctrl "github.com/FloatTech/zbpctrl" + "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/ctxext" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" +) + +// 图片来源 https://www.bilibili.com/opus/834601953403076633 + +var cardImgPathList []string + +func init() { + engine := control.AutoRegister(&ctrl.Options[*zero.Ctx]{ + DisableOnDefault: false, + Brief: "抽扑克牌", + Help: "- 抽扑克\n- poker", + PublicDataFolder: "Poker", + }).ApplySingle(ctxext.DefaultSingle) + + // 初始化扑克牌图片文件路径 + entries, _ := os.ReadDir(engine.DataFolder()) + for _, entry := range entries { + imgPath := path.Join(engine.DataFolder(), entry.Name()) + cardImgPathList = append(cardImgPathList, imgPath) + } + + engine.OnFullMatchGroup([]string{"抽扑克", "poker"}).SetBlock(true). + Handle(func(ctx *zero.Ctx) { + imgMsg, err := drawPoker() + if err != nil { + ctx.Send("[poker]读取扑克图片失败") + return + } + ctx.Send(imgMsg) + }) +} + +func drawPoker() (msg message.MessageSegment, err error) { + randomIndex := rand.Intn(len(cardImgPathList)) + randomImgPath := cardImgPathList[randomIndex] + imgData, err := os.ReadFile(randomImgPath) + if err != nil { + return + } + msg = message.ImageBytes(imgData) + return msg, nil +} From 9a2d5e21acf16ddfc2c873f4a69cb147f1ca4ae3 Mon Sep 17 00:00:00 2001 From: Aimer Neige Date: Sat, 11 May 2024 17:19:45 +0000 Subject: [PATCH 2/4] use GetLazyData --- plugin/poker/poker.go | 44 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/plugin/poker/poker.go b/plugin/poker/poker.go index 5b49d31557..9f0288271b 100644 --- a/plugin/poker/poker.go +++ b/plugin/poker/poker.go @@ -1,10 +1,10 @@ package poker import ( + "encoding/json" "math/rand" - "os" - "path" + fcext "github.com/FloatTech/floatbox/ctxext" ctrl "github.com/FloatTech/zbpctrl" "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" @@ -24,31 +24,29 @@ func init() { PublicDataFolder: "Poker", }).ApplySingle(ctxext.DefaultSingle) - // 初始化扑克牌图片文件路径 - entries, _ := os.ReadDir(engine.DataFolder()) - for _, entry := range entries { - imgPath := path.Join(engine.DataFolder(), entry.Name()) - cardImgPathList = append(cardImgPathList, imgPath) - } - - engine.OnFullMatchGroup([]string{"抽扑克", "poker"}).SetBlock(true). + getImg := fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool { + data, err := engine.GetLazyData("imgdata.json", true) + if err != nil { + ctx.SendChain(message.Text("ERROR:", err)) + return false + } + err = json.Unmarshal(data, &cardImgPathList) + if err != nil { + ctx.SendChain(message.Text("ERROR:", err)) + return false + } + return true + }) + + engine.OnFullMatchGroup([]string{"抽扑克", "poker"}, getImg).SetBlock(true). Handle(func(ctx *zero.Ctx) { - imgMsg, err := drawPoker() + randomIndex := rand.Intn(len(cardImgPathList)) + randomImgPath := cardImgPathList[randomIndex] + imgData, err := engine.GetLazyData(randomImgPath, true) if err != nil { ctx.Send("[poker]读取扑克图片失败") return } - ctx.Send(imgMsg) + ctx.Send(message.ImageBytes(imgData)) }) } - -func drawPoker() (msg message.MessageSegment, err error) { - randomIndex := rand.Intn(len(cardImgPathList)) - randomImgPath := cardImgPathList[randomIndex] - imgData, err := os.ReadFile(randomImgPath) - if err != nil { - return - } - msg = message.ImageBytes(imgData) - return msg, nil -} From 3f64b2fe09608da74b41ae325a51692a10e6637d Mon Sep 17 00:00:00 2001 From: Aimer Neige Date: Sun, 12 May 2024 15:32:51 +0000 Subject: [PATCH 3/4] update readme --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9dcef358bd..978846eadd 100644 --- a/README.md +++ b/README.md @@ -1411,7 +1411,7 @@ print("run[CQ:image,file="+j["img"]+"]") 一些游戏王插件 `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/ygo"` - + ##### 白鸽API卡查 ###### `"github.com/FloatTech/ZeroBot-Plugin/plugin/ygo/ygocdb.go"` @@ -1483,6 +1483,15 @@ print("run[CQ:image,file="+j["img"]+"]") +
+ 抽扑克 + + `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/yujn"` + + - [x] 抽扑克牌 + +
+ ### *低优先级*
From 870e276ca9d416fc33f90e94bc853b5d83dd50e4 Mon Sep 17 00:00:00 2001 From: Aimer Neige Date: Sun, 12 May 2024 15:33:50 +0000 Subject: [PATCH 4/4] wtf --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 978846eadd..2dbdbe1aa5 100644 --- a/README.md +++ b/README.md @@ -1411,7 +1411,7 @@ print("run[CQ:image,file="+j["img"]+"]") 一些游戏王插件 `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/ygo"` - + ##### 白鸽API卡查 ###### `"github.com/FloatTech/ZeroBot-Plugin/plugin/ygo/ygocdb.go"`