Skip to content

Commit b012df4

Browse files
authored
feat(niuniu): 添加可自定义购买商品数量,调整商品单价 (FloatTech#1189)
1 parent 08e02ab commit b012df4

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.20
44

55
require (
66
github.com/Baidu-AIP/golang-sdk v1.1.1
7-
github.com/FloatTech/AnimeAPI v1.7.1-0.20250717123723-d300df538b46
7+
github.com/FloatTech/AnimeAPI v1.7.1-0.20250901143505-180d33844860
88
github.com/FloatTech/floatbox v0.0.0-20250513111443-adba80e84e80
99
github.com/FloatTech/gg v1.1.3
1010
github.com/FloatTech/imgfactory v0.2.2-0.20230413152719-e101cc3606ef

go.sum

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
github.com/Baidu-AIP/golang-sdk v1.1.1 h1:RQsAmgDSAkiq22I6n7XJ2t3afgzFeqjY46FGhvrx4cw=
22
github.com/Baidu-AIP/golang-sdk v1.1.1/go.mod h1:bXnGw7xPeKt8aF7UCELKrV6UZ/46spItONK1RQBQj1Y=
33
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
4-
github.com/FloatTech/AnimeAPI v1.7.1-0.20250717123723-d300df538b46 h1:X6ZbOWoZJIoHCin+CeU92Q3EwpvglyQ4gc5BZhOtAwo=
5-
github.com/FloatTech/AnimeAPI v1.7.1-0.20250717123723-d300df538b46/go.mod h1:XXG1eBJf+eeWacQx5azsQKL5Gg7jDYTFyyZGIa/56js=
4+
github.com/FloatTech/AnimeAPI v1.7.1-0.20250901143505-180d33844860 h1:ddthsMzYC2LZ517/71W//9VsXT82CSBALVt3sQY5vfA=
5+
github.com/FloatTech/AnimeAPI v1.7.1-0.20250901143505-180d33844860/go.mod h1:CzpSeo5Pvslnq7Ho14E438Yn/flFMKzjGeX2nbC1mzk=
66
github.com/FloatTech/floatbox v0.0.0-20250513111443-adba80e84e80 h1:lFD1pd8NkYCrw0QpTX/T5pJ67I7AL5eGxQ4v0r9f81Q=
77
github.com/FloatTech/floatbox v0.0.0-20250513111443-adba80e84e80/go.mod h1:IWoFFqu+0FeaHHQdddyiTRL5z7gJME6qHC96qh0R2sc=
88
github.com/FloatTech/gg v1.1.3 h1:+GlL02lTKsxJQr4WCuNwVxC1/eBZrCvypCIBtxuOFb4=
@@ -124,8 +124,8 @@ github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o=
124124
github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs=
125125
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
126126
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
127-
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
128127
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
128+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
129129
github.com/jozsefsallai/gophersauce v1.0.1 h1:BA3ovtQRrAb1qYU9JoRLbDHpxnDunlNcEkEfhCvDDCM=
130130
github.com/jozsefsallai/gophersauce v1.0.1/go.mod h1:YVEI7djliMTmZ1Vh01YPF8bUHi+oKhe3yXgKf1T49vg=
131131
github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5 h1:BXnB1Gz4y/zwQh+ZFNy7rgd+ZfMOrwRr4uZSHEI+ieY=

plugin/niuniu/main.go

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
package niuniu
33

44
import (
5+
"errors"
56
"fmt"
67
"math/rand"
78
"strconv"
9+
"strings"
810
"time"
911

1012
"github.com/FloatTech/AnimeAPI/niu"
@@ -18,12 +20,6 @@ import (
1820
"github.com/wdvxdr1123/ZeroBot/message"
1921
)
2022

21-
type lastLength struct {
22-
TimeLimit time.Time
23-
Count int
24-
Length float64
25-
}
26-
2723
var (
2824
en = control.AutoRegister(&ctrl.Options[*zero.Ctx]{
2925
DisableOnDefault: false,
@@ -47,8 +43,8 @@ var (
4743
})
4844
dajiaoLimiter = rate.NewManager[string](time.Second*90, 1)
4945
jjLimiter = rate.NewManager[string](time.Second*150, 1)
50-
jjCount = syncx.Map[string, *lastLength]{}
51-
register = syncx.Map[string, *lastLength]{}
46+
jjCount = syncx.Map[string, *niu.PKRecord]{}
47+
register = syncx.Map[string, *niu.PKRecord]{}
5248
)
5349

5450
func init() {
@@ -103,15 +99,19 @@ func init() {
10399
en.OnFullMatch("出售牛牛", zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) {
104100
gid := ctx.Event.GroupID
105101
uid := ctx.Event.UserID
102+
key := fmt.Sprintf("%d_%d", gid, uid)
106103
sell, err := niu.Sell(gid, uid)
107-
if err != nil {
104+
if errors.Is(err, niu.ErrCanceled) || errors.Is(err, niu.ErrNoNiuNiu) {
105+
ctx.SendChain(message.Text(err))
106+
jjCount.Delete(key)
107+
return
108+
} else if err != nil {
108109
ctx.SendChain(message.Text("ERROR:", err))
109110
return
110111
}
112+
111113
// 数据库操作成功之后,及时删除残留的缓存
112-
key := fmt.Sprintf("%d_%d", gid, uid)
113-
_, ok := jjCount.Load(key)
114-
if ok {
114+
if _, ok := jjCount.Load(key); ok {
115115
jjCount.Delete(key)
116116
}
117117
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(sell))
@@ -140,29 +140,33 @@ func init() {
140140
cost int
141141
scope string
142142
description string
143-
count int
144143
}{
145-
1: {"伟哥", 300, "打胶", "可以让你打胶每次都增长", 5},
146-
2: {"媚药", 300, "打胶", "可以让你打胶每次都减少", 5},
147-
3: {"击剑神器", 500, "jj", "可以让你每次击剑都立于不败之地", 2},
148-
4: {"击剑神稽", 500, "jj", "可以让你每次击剑都失败", 2},
144+
1: {"伟哥", 100, "打胶", "可以让你打胶每次都增长"},
145+
2: {"媚药", 100, "打胶", "可以让你打胶每次都减少"},
146+
3: {"击剑神器", 300, "jj", "可以让你每次击剑都立于不败之地"},
147+
4: {"击剑神稽", 300, "jj", "可以让你每次击剑都失败"},
149148
}
150149

151150
var messages message.Message
151+
messages = append(messages, ctxext.FakeSenderForwardNode(ctx,
152+
message.Text("输入对应序号进行购买商品"),
153+
message.Text(
154+
"使用说明:\n"+
155+
"商品id-商品数量\n"+
156+
"如想购买10个伟哥\n"+
157+
"即:1-10")))
152158
messages = append(messages, ctxext.FakeSenderForwardNode(ctx, message.Text("牛牛商店当前售卖的物品如下")))
153159
for id := 1; id <= len(propMap); id++ {
154160
product := propMap[id]
155-
productInfo := fmt.Sprintf("商品%d\n商品名: %s\n商品价格: %dATRI币\n商品作用域: %s\n商品描述: %s\n使用次数:%d",
156-
id, product.name, product.cost, product.scope, product.description, product.count)
161+
productInfo := fmt.Sprintf("商品%d\n商品名: %s\n商品价格: %dATRI币\n商品作用域: %s\n商品描述: %s",
162+
id, product.name, product.cost, product.scope, product.description)
157163
messages = append(messages, ctxext.FakeSenderForwardNode(ctx, message.Text(productInfo)))
158164
}
159165
if id := ctx.Send(messages).ID(); id == 0 {
160166
ctx.Send(message.Text("发送商店失败"))
161167
return
162168
}
163-
164-
ctx.SendChain(message.Text("输入对应序号进行购买商品"))
165-
recv, cancel := zero.NewFutureEvent("message", 999, false, zero.CheckUser(uid), zero.CheckGroup(gid), zero.RegexRule(`^(\d+)$`)).Repeat()
169+
recv, cancel := zero.NewFutureEvent("message", 999, false, zero.CheckUser(uid), zero.CheckGroup(gid), zero.RegexRule(`^(\d+)-(\d+)$`)).Repeat()
166170
defer cancel()
167171
timer := time.NewTimer(120 * time.Second)
168172
answer := ""
@@ -174,13 +178,13 @@ func init() {
174178
return
175179
case r := <-recv:
176180
answer = r.Event.Message.String()
177-
n, err := strconv.Atoi(answer)
178-
if err != nil {
179-
ctx.SendChain(message.Text("ERROR: ", err))
180-
return
181-
}
182181

183-
if err = niu.Store(gid, uid, n); err != nil {
182+
// 解析输入的商品ID和数量
183+
parts := strings.Split(answer, "-")
184+
productID, _ := strconv.Atoi(parts[0])
185+
quantity, _ := strconv.Atoi(parts[1])
186+
187+
if err := niu.Store(gid, uid, productID, quantity); err != nil {
184188
ctx.SendChain(message.Text("ERROR: ", err))
185189
return
186190
}
@@ -227,7 +231,7 @@ func init() {
227231
return
228232
}
229233

230-
if err := niu.Redeem(gid, uid, last.Length); err != nil {
234+
if err := niu.Redeem(gid, uid, *last); err != nil {
231235
ctx.SendChain(message.Text("ERROR:", err))
232236
return
233237
}
@@ -337,7 +341,7 @@ func init() {
337341
}
338342
uid := ctx.Event.UserID
339343
gid := ctx.Event.GroupID
340-
msg, length, err := niu.JJ(gid, uid, adduser, patternParsed[0].Text()[1])
344+
msg, length, niuID, err := niu.JJ(gid, uid, adduser, patternParsed[0].Text()[1])
341345
if err != nil {
342346
ctx.SendChain(message.Text("ERROR: ", err))
343347
jjLimiter.Delete(fmt.Sprintf("%d_%d", ctx.Event.GroupID, ctx.Event.UserID))
@@ -346,24 +350,27 @@ func init() {
346350
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(msg))
347351
j := fmt.Sprintf("%d_%d", gid, adduser)
348352
count, ok := jjCount.Load(j)
349-
var c lastLength
353+
var c niu.PKRecord
350354
// 按照最后一次被 jj 时的时间计算,超过60分钟则重置
351355
if !ok {
352356
// 第一次被 jj
353-
c = lastLength{
357+
c = niu.PKRecord{
358+
NiuID: niuID,
354359
TimeLimit: time.Now(),
355360
Count: 1,
356361
Length: length,
357362
}
358363
} else {
359-
c = lastLength{
364+
c = niu.PKRecord{
365+
NiuID: niuID,
360366
TimeLimit: time.Now(),
361367
Count: count.Count + 1,
362368
Length: count.Length,
363369
}
364370
// 超时了,重置
365371
if time.Since(c.TimeLimit) > time.Hour {
366-
c = lastLength{
372+
c = niu.PKRecord{
373+
NiuID: niuID,
367374
TimeLimit: time.Now(),
368375
Count: 1,
369376
Length: length,
@@ -397,7 +404,7 @@ func init() {
397404
data, ok := register.Load(key)
398405
switch {
399406
case !ok || time.Since(data.TimeLimit) > time.Hour*24:
400-
data = &lastLength{
407+
data = &niu.PKRecord{
401408
TimeLimit: time.Now(),
402409
Count: 1,
403410
}

0 commit comments

Comments
 (0)