Skip to content

Commit dd328af

Browse files
authored
fix: wifegame (#1225)
1 parent d304cda commit dd328af

File tree

2 files changed

+46
-86
lines changed

2 files changed

+46
-86
lines changed

plugin/wife/main.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,7 @@ var (
2525
Brief: "从老婆库抽每日老婆",
2626
PublicDataFolder: "Wife",
2727
}).ApplySingle(ctxext.DefaultSingle)
28-
)
29-
30-
func card2name(card string) (string, string) {
31-
match := re.FindStringSubmatch(card)
32-
if len(match) >= 3 {
33-
return match[1], match[2]
34-
}
35-
return "", ""
36-
}
37-
38-
func init() {
39-
_ = os.MkdirAll(engine.DataFolder()+"wives", 0755)
40-
engine.OnFullMatch("抽老婆", fcext.DoOnceOnSuccess(
28+
getJSON = fcext.DoOnceOnSuccess(
4129
func(ctx *zero.Ctx) bool {
4230
data, err := engine.GetLazyData("wife.json", true)
4331
if err != nil {
@@ -52,7 +40,20 @@ func init() {
5240
logrus.Infof("[wife]加载%d个老婆", len(cards))
5341
return true
5442
},
55-
)).SetBlock(true).
43+
)
44+
)
45+
46+
func card2name(card string) (string, string) {
47+
match := re.FindStringSubmatch(card)
48+
if len(match) >= 3 {
49+
return match[1], match[2]
50+
}
51+
return "", ""
52+
}
53+
54+
func init() {
55+
_ = os.MkdirAll(engine.DataFolder()+"wives", 0755)
56+
engine.OnFullMatch("抽老婆", getJSON).SetBlock(true).
5657
Handle(func(ctx *zero.Ctx) {
5758
card := cards[fcext.RandSenderPerDayN(ctx.Event.UserID, len(cards))]
5859
data, err := engine.GetLazyData("wives/"+card, true)

plugin/wife/wifegame.go

Lines changed: 31 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22
package wife
33

44
import (
5-
"errors"
5+
"bytes"
6+
"image"
67
"image/color"
7-
"io/fs"
88
"math/rand"
9-
"os"
109
"strings"
1110
"time"
1211

13-
"github.com/FloatTech/floatbox/file"
14-
"github.com/FloatTech/gg"
1512
ctrl "github.com/FloatTech/zbpctrl"
1613
"github.com/FloatTech/zbputils/control"
1714
"github.com/FloatTech/zbputils/ctxext"
@@ -32,25 +29,18 @@ var (
3229
)
3330

3431
func init() {
35-
// _ = os.MkdirAll(engine.DataFolder()+"wives", 0755)
36-
enguess.OnFullMatch("猜老婆").SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
37-
var err error
32+
enguess.OnFullMatch("猜老婆", getJSON).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
3833
class := 3
3934

40-
fileName, err := lottery()
35+
card := cards[rand.Intn(len(cards))]
36+
pic, err := engine.GetLazyData("wives/"+card, true)
4137
if err != nil {
4238
ctx.SendChain(message.Text("[猜老婆]error:\n", err))
4339
return
4440
}
45-
46-
work, name := card2name(fileName)
47-
picFile := file.BOTPATH + "/" + engine.DataFolder() + "wives/" + fileName
48-
pic, err := os.ReadFile(picFile)
49-
if err != nil {
50-
ctx.SendChain(message.Text("[猜老婆]error:\n", err))
51-
return
52-
}
53-
img, err := gg.LoadImage(picFile)
41+
work, name := card2name(card)
42+
name = strings.ToLower(name)
43+
img, _, err := image.Decode(bytes.NewReader(pic))
5444
if err != nil {
5545
ctx.SendChain(message.Text("[猜老婆]error:\n", err))
5646
return
@@ -67,13 +57,13 @@ func init() {
6757
if id := ctx.SendChain(
6858
message.ImageBytes(q),
6959
); id.ID() != 0 {
70-
ctx.SendChain(message.Text("请回答该二次元角色名字\n以“xxx酱”格式回答"))
60+
ctx.SendChain(message.Text("请回答该二次元角色名字\n以“xxx酱”格式回答\n发送“跳过”结束猜题"))
7161
}
7262
var next *zero.FutureEvent
7363
if ctx.Event.GroupID == 0 {
74-
next = zero.NewFutureEvent("message", 999, false, zero.RegexRule(`(·)?.+酱$`), ctx.CheckSession())
64+
next = zero.NewFutureEvent("message", 999, false, zero.RegexRule(`^(·)?[^酱]+酱|^跳过$`), ctx.CheckSession())
7565
} else {
76-
next = zero.NewFutureEvent("message", 999, false, zero.RegexRule(`(·)?.+酱$`), zero.CheckGroup(ctx.Event.GroupID))
66+
next = zero.NewFutureEvent("message", 999, false, zero.RegexRule(`^(·)?[^酱]+酱|^跳过$`), zero.CheckGroup(ctx.Event.GroupID))
7767
}
7868
recv, cancel := next.Repeat()
7969
defer cancel()
@@ -92,12 +82,22 @@ func init() {
9282
)
9383
return
9484
case c := <-recv:
95-
tick.Reset(105 * time.Second)
96-
after.Reset(120 * time.Second)
97-
msg := c.Event.Message.String()
98-
msg, _, _ = strings.Cut(msg, "酱")
85+
// tick.Reset(105 * time.Second)
86+
// after.Reset(120 * time.Second)
87+
msg := strings.ReplaceAll(c.Event.Message.String(), "酱", "")
88+
if msg == "" {
89+
continue
90+
}
91+
if msg == "跳过" {
92+
if msgID := ctx.Send(message.ReplyWithMessage(c.Event.MessageID,
93+
message.Text("已跳过猜题\n角色是:\n", name, "\n出自《", work, "》\n"),
94+
message.ImageBytes(pic))); msgID.ID() == 0 {
95+
ctx.SendChain(message.Text("太棒了,你猜对了!\n图片发送失败,可能被风控\n角色是:\n", name, "\n出自《", work, "》"))
96+
}
97+
return
98+
}
9999
class--
100-
if strings.Contains(name, msg) {
100+
if strings.Contains(name, strings.ToLower(msg)) {
101101
if msgID := ctx.Send(message.ReplyWithMessage(c.Event.MessageID,
102102
message.Text("太棒了,你猜对了!\n角色是:\n", name, "\n出自《", work, "》\n"),
103103
message.ImageBytes(pic))); msgID.ID() == 0 {
@@ -120,8 +120,12 @@ func init() {
120120
)
121121
continue
122122
}
123+
msg = ""
124+
if class == 2 {
125+
msg = "(提示:" + work + ")\n"
126+
}
123127
ctx.SendChain(
124-
message.Text("回答错误,你还有", class, "次机会\n请继续作答(难度降低)\n"),
128+
message.Text("回答错误,你还有", class, "次机会\n", msg, "请继续作答(难度降低)\n"),
125129
message.ImageBytes(q),
126130
)
127131
continue
@@ -130,51 +134,6 @@ func init() {
130134
})
131135
}
132136

133-
// 从本地图库随机抽取,规避网络问题
134-
func lottery() (fileName string, err error) {
135-
path := engine.DataFolder() + "wives" + "/"
136-
if file.IsNotExist(path) {
137-
err = errors.New("图库文件夹不存在,请先发送“抽老婆”扩展图库")
138-
return
139-
}
140-
files, err := os.ReadDir(path)
141-
if err != nil {
142-
return
143-
}
144-
// 如果本地列表为空
145-
if len(files) == 0 {
146-
err = errors.New("本地数据为0,请先发送“抽老婆”扩展图库")
147-
return
148-
}
149-
fileName = randPicture(files, 10)
150-
if fileName == "" {
151-
err = errors.New("抽取图库轮空了,请重试")
152-
}
153-
return
154-
}
155-
156-
func randPicture(files []fs.DirEntry, indexMax int) (fileName string) {
157-
if len(files) > 1 {
158-
picture := files[rand.Intn(len(files))]
159-
// 如果是文件夹就递归
160-
if picture.IsDir() {
161-
indexMax--
162-
if indexMax <= 0 {
163-
return
164-
}
165-
fileName = randPicture(files, indexMax)
166-
} else {
167-
fileName = picture.Name()
168-
}
169-
} else {
170-
music := files[0]
171-
if !music.IsDir() {
172-
fileName = files[0].Name()
173-
}
174-
}
175-
return
176-
}
177-
178137
// 马赛克生成
179138
func mosaic(dst *imgfactory.Factory, level int) ([]byte, error) {
180139
b := dst.Image().Bounds()

0 commit comments

Comments
 (0)