diff --git a/README.md b/README.md index 0c88052daa..10d6bf6ee4 100644 --- a/README.md +++ b/README.md @@ -1053,6 +1053,8 @@ print("run[CQ:image,file="+j["img"]+"]") - [x] 赎牛牛 +- [x] 出售牛牛 + - [x] 牛牛商店 - [x] 牛牛背包 diff --git a/plugin/niuniu/main.go b/plugin/niuniu/main.go index d843b303f3..b4c60fcee0 100644 --- a/plugin/niuniu/main.go +++ b/plugin/niuniu/main.go @@ -38,6 +38,7 @@ var ( "- 使用[道具名称]jj@xxx\n" + "- 注册牛牛\n" + "- 赎牛牛(cd:60分钟)\n" + + "- 出售牛牛\n" + "- 牛牛商店\n" + "- 牛牛背包\n" + "- 注销牛牛\n" + @@ -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 @@ -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 } @@ -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币")) + 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 @@ -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 } @@ -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 } @@ -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, "次注销,注销成功,你已经没有牛牛了")) }) } diff --git a/plugin/niuniu/model.go b/plugin/niuniu/model.go index eb65ba608d..b980c419c0 100644 --- a/plugin/niuniu/model.go +++ b/plugin/niuniu/model.go @@ -142,8 +142,14 @@ func (u *userInfo) processNiuNiuAction(t string, props string) (string, error) { 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 @@ -221,8 +227,11 @@ func (u *userInfo) processJJuAction(adduserniuniu *userInfo, t string, props str v, ok := prop.Load(t) info = *u if props != "" { - if contains(t, jjProp) { - return "", errors.New("道具不存在") + if props != "击剑神器" && props != "击剑神稽" { + err = errors.New("道具不存在") + } + if props == "伟哥" || props == "媚药" { + err = errors.New("道具不能混着用哦") } if err = u.createUserInfoByProps(props); err != nil { return "", err @@ -347,12 +356,12 @@ func (db *model) createGIDTable(gid int64) error { } // 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 更新一个用户的牛牛信息 diff --git a/plugin/niuniu/utils.go b/plugin/niuniu/utils.go index 4c9fe3c957..8bd83337a6 100644 --- a/plugin/niuniu/utils.go +++ b/plugin/niuniu/utils.go @@ -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))] } @@ -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), + 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))