Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(niuniu): 出售牛牛 #1047

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,8 @@ print("run[CQ:image,file="+j["img"]+"]")

- [x] 赎牛牛

- [x] 出售牛牛

- [x] 牛牛商店

- [x] 牛牛背包
Expand Down
133 changes: 101 additions & 32 deletions plugin/niuniu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
"- 使用[道具名称]jj@xxx\n" +
"- 注册牛牛\n" +
"- 赎牛牛(cd:60分钟)\n" +
"- 出售牛牛\n" +
"- 牛牛商店\n" +
"- 牛牛背包\n" +
"- 注销牛牛\n" +
Expand All @@ -46,13 +47,36 @@ var (
"- 牛子深度排行\n",
PrivateDataFolder: "niuniu",
})
dajiaoLimiter = rate.NewManager[string](time.Second*90, 1)
jjLimiter = rate.NewManager[string](time.Second*150, 1)
jjCount = syncx.Map[string, *lastLength]{}
prop = syncx.Map[string, *propsCount]{}
dajiaoLimiter = rate.NewManager[string](time.Second*90, 1)
jjLimiter = rate.NewManager[string](time.Second*150, 1)
jjCount = syncx.Map[string, *lastLength]{}
prop = syncx.Map[string, *propsCount]{}
countDeleteNiuNiu = syncx.Map[string, *propsCount]{} // 结构一样所以用同一个结构体
)

func init() {
en.OnFullMatch("出售牛牛", zero.OnlyGroup, getdb).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
uid := ctx.Event.UserID
info, err := db.findNiuNiu(gid, uid)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}

money, msg := niuNiuProfit(info.Length)
if money == 0 {
ctx.SendChain(message.Text(msg))
return
}

if err = wallet.InsertWalletOf(uid, money); err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}

ctx.SendChain(message.Text(msg))
})
en.OnFullMatch("牛牛背包", zero.OnlyGroup, getdb).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
uid := ctx.Event.UserID
Expand Down Expand Up @@ -137,7 +161,7 @@ func init() {
return
}

if err = db.insertNiuNiu(&info, gid); err != nil {
if err = db.insertNiuNiu(info, gid); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
Expand Down Expand Up @@ -167,33 +191,52 @@ func init() {
ctx.SendChain(message.Text("你还没有被厥够4次呢,不能赎牛牛"))
return
}
ctx.SendChain(message.Text("再次确认一下哦,这次赎牛牛,牛牛长度将会变成", last.Length, "cm\n还需要嘛【是|否】"))
recv, cancel := zero.NewFutureEvent("message", 999, false, zero.CheckUser(uid), zero.CheckGroup(gid), zero.RegexRule(`^(是|否)$`)).Repeat()
defer cancel()
timer := time.NewTimer(2 * time.Minute)
defer timer.Stop()
for {
select {
case <-timer.C:
ctx.SendChain(message.Text("操作超时,已自动取消"))
return
case c := <-recv:
answer := c.Event.Message.String()
if answer == "否" {
ctx.SendChain(message.Text("取消成功!"))
return
}
money := wallet.GetWalletOf(uid)
if money < 150 {
ctx.SendChain(message.Text("赎牛牛需要150ATRI币,快去赚钱吧,目前仅有:", money, "个ATRI币"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ATRI币不建议硬编码进去
可以用wallet.GetWalletName()

return
}

money := wallet.GetWalletOf(uid)
if money < 150 {
ctx.SendChain(message.Text("赎牛牛需要150ATRI币,快去赚钱吧"))
return
}
if err := wallet.InsertWalletOf(uid, -150); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}

if err := wallet.InsertWalletOf(uid, -150); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
niuniu, err := db.findNiuNiu(gid, uid)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}

niuniu, err := db.findNiuNiu(gid, uid)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
niuniu.Length = last.Length

niuniu.Length = last.Length
if err = db.insertNiuNiu(niuniu, gid); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}

if err = db.insertNiuNiu(&niuniu, gid); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
jjCount.Delete(fmt.Sprintf("%d_%d", gid, uid))

jjCount.Delete(fmt.Sprintf("%d_%d", gid, uid))
ctx.SendChain(message.At(uid), message.Text(fmt.Sprintf("恭喜你!成功赎回牛牛,当前长度为:%.2fcm", last.Length)))
ctx.SendChain(message.At(uid), message.Text(fmt.Sprintf("恭喜你!成功赎回牛牛,当前长度为:%.2fcm", last.Length)))
return
}
}
})
en.OnFullMatch("牛子长度排行", zero.OnlyGroup, getdb).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
Expand Down Expand Up @@ -289,13 +332,14 @@ func init() {
dajiaoLimiter.Delete(fmt.Sprintf("%d_%d", gid, uid))
return
}

messages, err := niuniu.processNiuNiuAction(t, fiancee[1])
if err != nil {
ctx.SendChain(message.Text(err))
dajiaoLimiter.Delete(fmt.Sprintf("%d_%d", gid, uid))
return
}
if err = db.insertNiuNiu(&niuniu, gid); err != nil {
if err = db.insertNiuNiu(niuniu, gid); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
Expand Down Expand Up @@ -372,19 +416,18 @@ func init() {
jjLimiter.Delete(t)
return
}
fencingResult, err := myniuniu.processJJuAction(&adduserniuniu, t, fiancee[1])
fencingResult, err := myniuniu.processJJuAction(adduserniuniu, t, fiancee[1])
if err != nil {
ctx.SendChain(message.Text(err))
jjLimiter.Delete(t)
return
}

if err = db.insertNiuNiu(&myniuniu, gid); err != nil {
if err = db.insertNiuNiu(myniuniu, gid); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}

if err = db.insertNiuNiu(&adduserniuniu, gid); err != nil {
if err = db.insertNiuNiu(adduserniuniu, gid); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
Expand Down Expand Up @@ -437,11 +480,37 @@ func init() {
ctx.SendChain(message.Text("你还没有牛牛呢,咋的你想凭空造一个啊"))
return
}
t := fmt.Sprintf("%d_%d", gid, uid)
value, ok := countDeleteNiuNiu.Load(t)
if !ok {
countDeleteNiuNiu.Store(t, &propsCount{
Count: 1,
TimeLimit: time.Now(),
})
value = &propsCount{}
}
if value.TimeLimit.IsZero() && time.Since(value.TimeLimit) < 24*time.Hour {
money := getMoneyForNumber(value.Count)
walletOf := wallet.GetWalletOf(uid)
if walletOf < money {
ctx.SendChain(message.Text(fmt.Sprintf("你今天已经注销了%d次了,此次注销需要%d个ATRI币,没钱就等待明天重置吧", value.Count+1, money)))
return
}
if err = wallet.InsertWalletOf(uid, -money); err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
countDeleteNiuNiu.Store(t, &propsCount{
Count: value.Count + 1,
})
} else {
countDeleteNiuNiu.Delete(t)
}
err = db.deleteniuniu(gid, uid)
if err != nil {
ctx.SendChain(message.Text("注销失败"))
return
}
ctx.SendChain(message.Text("注销成功,你已经没有牛牛了"))
ctx.SendChain(message.Text("这是你今天第", value.Count+1, "次注销,注销成功,你已经没有牛牛了"))
})
}
21 changes: 15 additions & 6 deletions plugin/niuniu/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,14 @@
load, ok := prop.Load(t)
info = *u
if props != "" {
if contains(t, dajiaoProp) {
return "", errors.New("道具不存在")
if props != "伟哥" && props != "媚药" {
err = errors.New("道具不存在")
}
if props == "击剑神器" || props == "击剑神稽" {
err = errors.New("道具不能混着用哦")
}
if err != nil {
return "", err
}
if err = u.createUserInfoByProps(props); err != nil {
return "", err
Expand Down Expand Up @@ -221,8 +227,11 @@
v, ok := prop.Load(t)
info = *u
if props != "" {
if contains(t, jjProp) {
return "", errors.New("道具不存在")
if props != "击剑神器" && props != "击剑神稽" {
err = errors.New("道具不存在")

Check failure on line 231 in plugin/niuniu/model.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

Check failure on line 231 in plugin/niuniu/model.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
}
if props == "伟哥" || props == "媚药" {
err = errors.New("道具不能混着用哦")

Check failure on line 234 in plugin/niuniu/model.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

Check failure on line 234 in plugin/niuniu/model.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得你直接return比较好🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没必要专门给第二个if判断return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你err最后不都被下面这个err = u.createUserInfoByProps(props);覆盖了吗,那你上面这两个err的赋值不就没意义了
主要你ci没过

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

}
if err = u.createUserInfoByProps(props); err != nil {
return "", err
Expand Down Expand Up @@ -347,12 +356,12 @@
}

// findNiuNiu 返回一个用户的牛牛信息
func (db *model) findNiuNiu(gid, uid int64) (userInfo, error) {
func (db *model) findNiuNiu(gid, uid int64) (*userInfo, error) {
db.RLock()
defer db.RUnlock()
u := userInfo{}
err := db.sql.Find(strconv.FormatInt(gid, 10), &u, "where UID = "+strconv.FormatInt(uid, 10))
return u, err
return &u, err
}

// insertNiuNiu 更新一个用户的牛牛信息
Expand Down
56 changes: 40 additions & 16 deletions plugin/niuniu/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,9 @@ import (
"fmt"
"math"
"math/rand"
"strings"
"time"
)

var (
jjProp = []string{"击剑神器", "击剑神稽"}
dajiaoProp = []string{"伟哥", "媚药"}
)

// 检查字符串是否在切片中
func contains(s string, array []string) bool {
for _, item := range array {
if strings.EqualFold(item, s) {
return true
}
}
return false
}

func randomChoice(options []string) string {
return options[rand.Intn(len(options))]
}
Expand Down Expand Up @@ -56,6 +40,46 @@ func updateMap(t string, d bool) {
}
}

func getMoneyForNumber(n int) (money int) {
switch n {
case 1:
money = 5
case 2:
money = 10
default:
money = n * 10
}
return
}

func niuNiuProfit(niuniu float64) (money int, message string) {
switch {
case 0 < niuniu && niuniu <= 15:
message = randomChoice([]string{
"你的牛牛太小啦",
"这么小的牛牛就要肩负起这么大的责任吗?快去打胶吧!",
})
case niuniu > 15:
money = int(niuniu * 10)
message = randomChoice([]string{
fmt.Sprintf("你的牛牛已经离你而去了,你赚取了%d个ATRI币", money),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ATRI币不建议硬编码进去
可以用wallet.GetWalletName()

fmt.Sprintf("啊!你的牛☞已经没啦🤣,为了这点钱就出卖你的牛牛可真不值,你赚取了%d个ATRI币", money),
})
case niuniu <= 0 && niuniu >= -15:
message = randomChoice([]string{
"你的牛牛太小啦",
"这么小的牛牛就要肩负起这么大的责任吗?快去找别人玩吧!",
})
case niuniu < -15:
money = int(math.Abs(niuniu * 10))
message = randomChoice([]string{
fmt.Sprintf("此世做了女孩子来世来当男孩子(bushi),你赚取了%d个ATRI币", money),
fmt.Sprintf("呜呜呜,不哭不哭当女孩子不委屈的,你赚取了%d个ATRI币", money),
})
}
return
}

func generateRandomStingTwo(niuniu float64) (string, float64) {
probability := rand.Intn(100 + 1)
reduce := math.Abs(hitGlue(niuniu))
Expand Down
Loading