2
2
package niuniu
3
3
4
4
import (
5
+ "errors"
5
6
"fmt"
6
7
"math/rand"
7
8
"strconv"
9
+ "strings"
8
10
"time"
9
11
10
12
"github.com/FloatTech/AnimeAPI/niu"
@@ -18,12 +20,6 @@ import (
18
20
"github.com/wdvxdr1123/ZeroBot/message"
19
21
)
20
22
21
- type lastLength struct {
22
- TimeLimit time.Time
23
- Count int
24
- Length float64
25
- }
26
-
27
23
var (
28
24
en = control .AutoRegister (& ctrl.Options [* zero.Ctx ]{
29
25
DisableOnDefault : false ,
47
43
})
48
44
dajiaoLimiter = rate .NewManager [string ](time .Second * 90 , 1 )
49
45
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 ]{}
52
48
)
53
49
54
50
func init () {
@@ -103,15 +99,19 @@ func init() {
103
99
en .OnFullMatch ("出售牛牛" , zero .OnlyGroup ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
104
100
gid := ctx .Event .GroupID
105
101
uid := ctx .Event .UserID
102
+ key := fmt .Sprintf ("%d_%d" , gid , uid )
106
103
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 {
108
109
ctx .SendChain (message .Text ("ERROR:" , err ))
109
110
return
110
111
}
112
+
111
113
// 数据库操作成功之后,及时删除残留的缓存
112
- key := fmt .Sprintf ("%d_%d" , gid , uid )
113
- _ , ok := jjCount .Load (key )
114
- if ok {
114
+ if _ , ok := jjCount .Load (key ); ok {
115
115
jjCount .Delete (key )
116
116
}
117
117
ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text (sell ))
@@ -140,29 +140,33 @@ func init() {
140
140
cost int
141
141
scope string
142
142
description string
143
- count int
144
143
}{
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" , "可以让你每次击剑都失败" },
149
148
}
150
149
151
150
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" )))
152
158
messages = append (messages , ctxext .FakeSenderForwardNode (ctx , message .Text ("牛牛商店当前售卖的物品如下" )))
153
159
for id := 1 ; id <= len (propMap ); id ++ {
154
160
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 )
157
163
messages = append (messages , ctxext .FakeSenderForwardNode (ctx , message .Text (productInfo )))
158
164
}
159
165
if id := ctx .Send (messages ).ID (); id == 0 {
160
166
ctx .Send (message .Text ("发送商店失败" ))
161
167
return
162
168
}
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 ()
166
170
defer cancel ()
167
171
timer := time .NewTimer (120 * time .Second )
168
172
answer := ""
@@ -174,13 +178,13 @@ func init() {
174
178
return
175
179
case r := <- recv :
176
180
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
- }
182
181
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 {
184
188
ctx .SendChain (message .Text ("ERROR: " , err ))
185
189
return
186
190
}
@@ -227,7 +231,7 @@ func init() {
227
231
return
228
232
}
229
233
230
- if err := niu .Redeem (gid , uid , last . Length ); err != nil {
234
+ if err := niu .Redeem (gid , uid , * last ); err != nil {
231
235
ctx .SendChain (message .Text ("ERROR:" , err ))
232
236
return
233
237
}
@@ -337,7 +341,7 @@ func init() {
337
341
}
338
342
uid := ctx .Event .UserID
339
343
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 ])
341
345
if err != nil {
342
346
ctx .SendChain (message .Text ("ERROR: " , err ))
343
347
jjLimiter .Delete (fmt .Sprintf ("%d_%d" , ctx .Event .GroupID , ctx .Event .UserID ))
@@ -346,24 +350,27 @@ func init() {
346
350
ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text (msg ))
347
351
j := fmt .Sprintf ("%d_%d" , gid , adduser )
348
352
count , ok := jjCount .Load (j )
349
- var c lastLength
353
+ var c niu. PKRecord
350
354
// 按照最后一次被 jj 时的时间计算,超过60分钟则重置
351
355
if ! ok {
352
356
// 第一次被 jj
353
- c = lastLength {
357
+ c = niu.PKRecord {
358
+ NiuID : niuID ,
354
359
TimeLimit : time .Now (),
355
360
Count : 1 ,
356
361
Length : length ,
357
362
}
358
363
} else {
359
- c = lastLength {
364
+ c = niu.PKRecord {
365
+ NiuID : niuID ,
360
366
TimeLimit : time .Now (),
361
367
Count : count .Count + 1 ,
362
368
Length : count .Length ,
363
369
}
364
370
// 超时了,重置
365
371
if time .Since (c .TimeLimit ) > time .Hour {
366
- c = lastLength {
372
+ c = niu.PKRecord {
373
+ NiuID : niuID ,
367
374
TimeLimit : time .Now (),
368
375
Count : 1 ,
369
376
Length : length ,
@@ -397,7 +404,7 @@ func init() {
397
404
data , ok := register .Load (key )
398
405
switch {
399
406
case ! ok || time .Since (data .TimeLimit ) > time .Hour * 24 :
400
- data = & lastLength {
407
+ data = & niu. PKRecord {
401
408
TimeLimit : time .Now (),
402
409
Count : 1 ,
403
410
}
0 commit comments