feat(niuniu): 添加可自定义购买商品数量,调整商品单价 (#1189)

This commit is contained in:
宇~ 2025-09-10 10:33:21 +08:00 committed by GitHub
parent 08e02ab730
commit b012df4c23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 39 deletions

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.20
require ( require (
github.com/Baidu-AIP/golang-sdk v1.1.1 github.com/Baidu-AIP/golang-sdk v1.1.1
github.com/FloatTech/AnimeAPI v1.7.1-0.20250717123723-d300df538b46 github.com/FloatTech/AnimeAPI v1.7.1-0.20250901143505-180d33844860
github.com/FloatTech/floatbox v0.0.0-20250513111443-adba80e84e80 github.com/FloatTech/floatbox v0.0.0-20250513111443-adba80e84e80
github.com/FloatTech/gg v1.1.3 github.com/FloatTech/gg v1.1.3
github.com/FloatTech/imgfactory v0.2.2-0.20230413152719-e101cc3606ef github.com/FloatTech/imgfactory v0.2.2-0.20230413152719-e101cc3606ef

6
go.sum
View File

@ -1,8 +1,8 @@
github.com/Baidu-AIP/golang-sdk v1.1.1 h1:RQsAmgDSAkiq22I6n7XJ2t3afgzFeqjY46FGhvrx4cw= github.com/Baidu-AIP/golang-sdk v1.1.1 h1:RQsAmgDSAkiq22I6n7XJ2t3afgzFeqjY46FGhvrx4cw=
github.com/Baidu-AIP/golang-sdk v1.1.1/go.mod h1:bXnGw7xPeKt8aF7UCELKrV6UZ/46spItONK1RQBQj1Y= github.com/Baidu-AIP/golang-sdk v1.1.1/go.mod h1:bXnGw7xPeKt8aF7UCELKrV6UZ/46spItONK1RQBQj1Y=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/FloatTech/AnimeAPI v1.7.1-0.20250717123723-d300df538b46 h1:X6ZbOWoZJIoHCin+CeU92Q3EwpvglyQ4gc5BZhOtAwo= github.com/FloatTech/AnimeAPI v1.7.1-0.20250901143505-180d33844860 h1:ddthsMzYC2LZ517/71W//9VsXT82CSBALVt3sQY5vfA=
github.com/FloatTech/AnimeAPI v1.7.1-0.20250717123723-d300df538b46/go.mod h1:XXG1eBJf+eeWacQx5azsQKL5Gg7jDYTFyyZGIa/56js= github.com/FloatTech/AnimeAPI v1.7.1-0.20250901143505-180d33844860/go.mod h1:CzpSeo5Pvslnq7Ho14E438Yn/flFMKzjGeX2nbC1mzk=
github.com/FloatTech/floatbox v0.0.0-20250513111443-adba80e84e80 h1:lFD1pd8NkYCrw0QpTX/T5pJ67I7AL5eGxQ4v0r9f81Q= github.com/FloatTech/floatbox v0.0.0-20250513111443-adba80e84e80 h1:lFD1pd8NkYCrw0QpTX/T5pJ67I7AL5eGxQ4v0r9f81Q=
github.com/FloatTech/floatbox v0.0.0-20250513111443-adba80e84e80/go.mod h1:IWoFFqu+0FeaHHQdddyiTRL5z7gJME6qHC96qh0R2sc= github.com/FloatTech/floatbox v0.0.0-20250513111443-adba80e84e80/go.mod h1:IWoFFqu+0FeaHHQdddyiTRL5z7gJME6qHC96qh0R2sc=
github.com/FloatTech/gg v1.1.3 h1:+GlL02lTKsxJQr4WCuNwVxC1/eBZrCvypCIBtxuOFb4= github.com/FloatTech/gg v1.1.3 h1:+GlL02lTKsxJQr4WCuNwVxC1/eBZrCvypCIBtxuOFb4=
@ -124,8 +124,8 @@ github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o=
github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jozsefsallai/gophersauce v1.0.1 h1:BA3ovtQRrAb1qYU9JoRLbDHpxnDunlNcEkEfhCvDDCM= github.com/jozsefsallai/gophersauce v1.0.1 h1:BA3ovtQRrAb1qYU9JoRLbDHpxnDunlNcEkEfhCvDDCM=
github.com/jozsefsallai/gophersauce v1.0.1/go.mod h1:YVEI7djliMTmZ1Vh01YPF8bUHi+oKhe3yXgKf1T49vg= github.com/jozsefsallai/gophersauce v1.0.1/go.mod h1:YVEI7djliMTmZ1Vh01YPF8bUHi+oKhe3yXgKf1T49vg=
github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5 h1:BXnB1Gz4y/zwQh+ZFNy7rgd+ZfMOrwRr4uZSHEI+ieY= github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5 h1:BXnB1Gz4y/zwQh+ZFNy7rgd+ZfMOrwRr4uZSHEI+ieY=

View File

@ -2,9 +2,11 @@
package niuniu package niuniu
import ( import (
"errors"
"fmt" "fmt"
"math/rand" "math/rand"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/FloatTech/AnimeAPI/niu" "github.com/FloatTech/AnimeAPI/niu"
@ -18,12 +20,6 @@ import (
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
) )
type lastLength struct {
TimeLimit time.Time
Count int
Length float64
}
var ( var (
en = control.AutoRegister(&ctrl.Options[*zero.Ctx]{ en = control.AutoRegister(&ctrl.Options[*zero.Ctx]{
DisableOnDefault: false, DisableOnDefault: false,
@ -47,8 +43,8 @@ var (
}) })
dajiaoLimiter = rate.NewManager[string](time.Second*90, 1) dajiaoLimiter = rate.NewManager[string](time.Second*90, 1)
jjLimiter = rate.NewManager[string](time.Second*150, 1) jjLimiter = rate.NewManager[string](time.Second*150, 1)
jjCount = syncx.Map[string, *lastLength]{} jjCount = syncx.Map[string, *niu.PKRecord]{}
register = syncx.Map[string, *lastLength]{} register = syncx.Map[string, *niu.PKRecord]{}
) )
func init() { func init() {
@ -103,15 +99,19 @@ func init() {
en.OnFullMatch("出售牛牛", zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) { en.OnFullMatch("出售牛牛", zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID gid := ctx.Event.GroupID
uid := ctx.Event.UserID uid := ctx.Event.UserID
key := fmt.Sprintf("%d_%d", gid, uid)
sell, err := niu.Sell(gid, uid) sell, err := niu.Sell(gid, uid)
if err != nil { if errors.Is(err, niu.ErrCanceled) || errors.Is(err, niu.ErrNoNiuNiu) {
ctx.SendChain(message.Text(err))
jjCount.Delete(key)
return
} else if err != nil {
ctx.SendChain(message.Text("ERROR:", err)) ctx.SendChain(message.Text("ERROR:", err))
return return
} }
// 数据库操作成功之后,及时删除残留的缓存 // 数据库操作成功之后,及时删除残留的缓存
key := fmt.Sprintf("%d_%d", gid, uid) if _, ok := jjCount.Load(key); ok {
_, ok := jjCount.Load(key)
if ok {
jjCount.Delete(key) jjCount.Delete(key)
} }
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(sell)) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(sell))
@ -140,29 +140,33 @@ func init() {
cost int cost int
scope string scope string
description string description string
count int
}{ }{
1: {"伟哥", 300, "打胶", "可以让你打胶每次都增长", 5}, 1: {"伟哥", 100, "打胶", "可以让你打胶每次都增长"},
2: {"媚药", 300, "打胶", "可以让你打胶每次都减少", 5}, 2: {"媚药", 100, "打胶", "可以让你打胶每次都减少"},
3: {"击剑神器", 500, "jj", "可以让你每次击剑都立于不败之地", 2}, 3: {"击剑神器", 300, "jj", "可以让你每次击剑都立于不败之地"},
4: {"击剑神稽", 500, "jj", "可以让你每次击剑都失败", 2}, 4: {"击剑神稽", 300, "jj", "可以让你每次击剑都失败"},
} }
var messages message.Message var messages message.Message
messages = append(messages, ctxext.FakeSenderForwardNode(ctx,
message.Text("输入对应序号进行购买商品"),
message.Text(
"使用说明:\n"+
"商品id-商品数量\n"+
"如想购买10个伟哥\n"+
"即:1-10")))
messages = append(messages, ctxext.FakeSenderForwardNode(ctx, message.Text("牛牛商店当前售卖的物品如下"))) messages = append(messages, ctxext.FakeSenderForwardNode(ctx, message.Text("牛牛商店当前售卖的物品如下")))
for id := 1; id <= len(propMap); id++ { for id := 1; id <= len(propMap); id++ {
product := propMap[id] product := propMap[id]
productInfo := fmt.Sprintf("商品%d\n商品名: %s\n商品价格: %dATRI币\n商品作用域: %s\n商品描述: %s\n使用次数:%d", productInfo := fmt.Sprintf("商品%d\n商品名: %s\n商品价格: %dATRI币\n商品作用域: %s\n商品描述: %s",
id, product.name, product.cost, product.scope, product.description, product.count) id, product.name, product.cost, product.scope, product.description)
messages = append(messages, ctxext.FakeSenderForwardNode(ctx, message.Text(productInfo))) messages = append(messages, ctxext.FakeSenderForwardNode(ctx, message.Text(productInfo)))
} }
if id := ctx.Send(messages).ID(); id == 0 { if id := ctx.Send(messages).ID(); id == 0 {
ctx.Send(message.Text("发送商店失败")) ctx.Send(message.Text("发送商店失败"))
return return
} }
recv, cancel := zero.NewFutureEvent("message", 999, false, zero.CheckUser(uid), zero.CheckGroup(gid), zero.RegexRule(`^(\d+)-(\d+)$`)).Repeat()
ctx.SendChain(message.Text("输入对应序号进行购买商品"))
recv, cancel := zero.NewFutureEvent("message", 999, false, zero.CheckUser(uid), zero.CheckGroup(gid), zero.RegexRule(`^(\d+)$`)).Repeat()
defer cancel() defer cancel()
timer := time.NewTimer(120 * time.Second) timer := time.NewTimer(120 * time.Second)
answer := "" answer := ""
@ -174,13 +178,13 @@ func init() {
return return
case r := <-recv: case r := <-recv:
answer = r.Event.Message.String() answer = r.Event.Message.String()
n, err := strconv.Atoi(answer)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
if err = niu.Store(gid, uid, n); err != nil { // 解析输入的商品ID和数量
parts := strings.Split(answer, "-")
productID, _ := strconv.Atoi(parts[0])
quantity, _ := strconv.Atoi(parts[1])
if err := niu.Store(gid, uid, productID, quantity); err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
} }
@ -227,7 +231,7 @@ func init() {
return return
} }
if err := niu.Redeem(gid, uid, last.Length); err != nil { if err := niu.Redeem(gid, uid, *last); err != nil {
ctx.SendChain(message.Text("ERROR:", err)) ctx.SendChain(message.Text("ERROR:", err))
return return
} }
@ -337,7 +341,7 @@ func init() {
} }
uid := ctx.Event.UserID uid := ctx.Event.UserID
gid := ctx.Event.GroupID gid := ctx.Event.GroupID
msg, length, err := niu.JJ(gid, uid, adduser, patternParsed[0].Text()[1]) msg, length, niuID, err := niu.JJ(gid, uid, adduser, patternParsed[0].Text()[1])
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
jjLimiter.Delete(fmt.Sprintf("%d_%d", ctx.Event.GroupID, ctx.Event.UserID)) jjLimiter.Delete(fmt.Sprintf("%d_%d", ctx.Event.GroupID, ctx.Event.UserID))
@ -346,24 +350,27 @@ func init() {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(msg)) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(msg))
j := fmt.Sprintf("%d_%d", gid, adduser) j := fmt.Sprintf("%d_%d", gid, adduser)
count, ok := jjCount.Load(j) count, ok := jjCount.Load(j)
var c lastLength var c niu.PKRecord
// 按照最后一次被 jj 时的时间计算超过60分钟则重置 // 按照最后一次被 jj 时的时间计算超过60分钟则重置
if !ok { if !ok {
// 第一次被 jj // 第一次被 jj
c = lastLength{ c = niu.PKRecord{
NiuID: niuID,
TimeLimit: time.Now(), TimeLimit: time.Now(),
Count: 1, Count: 1,
Length: length, Length: length,
} }
} else { } else {
c = lastLength{ c = niu.PKRecord{
NiuID: niuID,
TimeLimit: time.Now(), TimeLimit: time.Now(),
Count: count.Count + 1, Count: count.Count + 1,
Length: count.Length, Length: count.Length,
} }
// 超时了,重置 // 超时了,重置
if time.Since(c.TimeLimit) > time.Hour { if time.Since(c.TimeLimit) > time.Hour {
c = lastLength{ c = niu.PKRecord{
NiuID: niuID,
TimeLimit: time.Now(), TimeLimit: time.Now(),
Count: 1, Count: 1,
Length: length, Length: length,
@ -397,7 +404,7 @@ func init() {
data, ok := register.Load(key) data, ok := register.Load(key)
switch { switch {
case !ok || time.Since(data.TimeLimit) > time.Hour*24: case !ok || time.Since(data.TimeLimit) > time.Hour*24:
data = &lastLength{ data = &niu.PKRecord{
TimeLimit: time.Now(), TimeLimit: time.Now(),
Count: 1, Count: 1,
} }