update: 重写[mcfish]交易检测逻辑 (#1070)

This commit is contained in:
vatebur 2024-11-28 23:41:19 +08:00 committed by GitHub
parent 6a2c7e8740
commit 83037f621c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 58 additions and 81 deletions

View File

@ -6,6 +6,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
@ -28,7 +29,7 @@ type fishdb struct {
const FishLimit = 50 const FishLimit = 50
// version 规则版本号 // version 规则版本号
const version = "5.5.8" const version = "5.5.9"
// 各物品信息 // 各物品信息
type jsonInfo struct { type jsonInfo struct {
@ -147,7 +148,7 @@ var (
"8.合成:\n-> 铁竿 : 3x木竿\n-> 金竿 : 3x铁竿\n-> 钻石竿 : 3x金竿\n-> 下界合金竿 : 3x钻石竿\n-> 三叉戟 : 3x下界合金竿\n注:合成成功率90%(包括梭哈),合成鱼竿的附魔等级=(附魔等级合/合成鱼竿数量)\n" + "8.合成:\n-> 铁竿 : 3x木竿\n-> 金竿 : 3x铁竿\n-> 钻石竿 : 3x金竿\n-> 下界合金竿 : 3x钻石竿\n-> 三叉戟 : 3x下界合金竿\n注:合成成功率90%(包括梭哈),合成鱼竿的附魔等级=(附魔等级合/合成鱼竿数量)\n" +
"9.杂项:\n-> 无装备的情况下,每人最多可以购买3次100块钱的鱼竿\n-> 默认状态钓鱼上钩概率为60%(理论值!!!)\n-> 附魔的鱼竿会因附魔变得昂贵,每个附魔最高3级\n-> 三叉戟不算鱼竿,修复时可直接满耐久\n" + "9.杂项:\n-> 无装备的情况下,每人最多可以购买3次100块钱的鱼竿\n-> 默认状态钓鱼上钩概率为60%(理论值!!!)\n-> 附魔的鱼竿会因附魔变得昂贵,每个附魔最高3级\n-> 三叉戟不算鱼竿,修复时可直接满耐久\n" +
"-> 鱼竿数量大于50的不能买东西;\n 鱼竿数量大于30的不能钓鱼;\n 每购/售10次鱼竿获得1层宝藏诅咒;\n 每购买20次物品将获得3次价格减半福利;\n 每钓鱼75次获得1本净化书;\n" + "-> 鱼竿数量大于50的不能买东西;\n 鱼竿数量大于30的不能钓鱼;\n 每购/售10次鱼竿获得1层宝藏诅咒;\n 每购买20次物品将获得3次价格减半福利;\n 每钓鱼75次获得1本净化书;\n" +
" 每天最多只可出售5个鱼竿和购买15次物品;鱼类交易每天最多100条.", " 每天可交易鱼竿10个物品200件.",
PublicDataFolder: "McFish", PublicDataFolder: "McFish",
}).ApplySingle(ctxext.DefaultSingle) }).ApplySingle(ctxext.DefaultSingle)
getdb = fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool { getdb = fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool {
@ -344,7 +345,7 @@ func (sql *fishdb) setEquipFor(uid int64) (err error) {
if err != nil { if err != nil {
return err return err
} }
_ = sql.db.Find("fishState", &userInfo, "WHERE ID = ?", uid) err = sql.db.Find("fishState", &userInfo, "WHERE ID = ?", uid)
if err != nil { if err != nil {
return err return err
} }
@ -608,7 +609,7 @@ func (sql *fishdb) refreshStroeInfo() (ok bool, err error) {
} }
} }
thing := store{} thing := store{}
oldThing := []store{} var oldThing []store
_ = sql.db.FindFor("stroeDiscount", &thing, "WHERE type = 'pole'", func() error { _ = sql.db.FindFor("stroeDiscount", &thing, "WHERE type = 'pole'", func() error {
if time.Since(time.Unix(thing.Duration, 0)) > 24 { if time.Since(time.Unix(thing.Duration, 0)) > 24 {
oldThing = append(oldThing, thing) oldThing = append(oldThing, thing)
@ -628,7 +629,7 @@ func (sql *fishdb) refreshStroeInfo() (ok bool, err error) {
Price: priceList[fish] * discountList[fish] / 100, Price: priceList[fish] * discountList[fish] / 100,
} }
_ = sql.db.Find("store", &thingInfo, "WHERE Name = ?", fish) _ = sql.db.Find("store", &thingInfo, "WHERE Name = ?", fish)
thingInfo.Number += (100 - discountList[fish]) thingInfo.Number += 100 - discountList[fish]
if thingInfo.Number < 1 { if thingInfo.Number < 1 {
thingInfo.Number = 100 thingInfo.Number = 100
} }
@ -774,15 +775,14 @@ func (sql *fishdb) useCouponAt(uid int64, times int) (int, error) {
return useTimes, sql.db.Insert("buff", &userInfo) return useTimes, sql.db.Insert("buff", &userInfo)
} }
// 检测卖鱼竿上限 // 买卖上限检测
func (sql *fishdb) checkCanSalesFor(uid int64, sales bool) (int, error) { func (sql *fishdb) checkCanSalesFor(uid int64, saleType string, salesNum int) (int, error) {
residue := 0
sql.Lock() sql.Lock()
defer sql.Unlock() defer sql.Unlock()
userInfo := buffInfo{ID: uid} userInfo := buffInfo{ID: uid}
err := sql.db.Create("buff", &userInfo) err := sql.db.Create("buff", &userInfo)
if err != nil { if err != nil {
return residue, err return salesNum, err
} }
_ = sql.db.Find("buff", &userInfo, "WHERE ID = ?", uid) _ = sql.db.Find("buff", &userInfo, "WHERE ID = ?", uid)
if time.Now().Day() != time.Unix(userInfo.Duration, 0).Day() { if time.Now().Day() != time.Unix(userInfo.Duration, 0).Day() {
@ -791,14 +791,22 @@ func (sql *fishdb) checkCanSalesFor(uid int64, sales bool) (int, error) {
userInfo.BuyTing = 0 userInfo.BuyTing = 0
userInfo.SalesFish = 0 userInfo.SalesFish = 0
} }
if sales && userInfo.SalesPole < 5 { if strings.Contains(saleType, "竿") {
residue = 5 - userInfo.SalesPole if userInfo.SalesPole >= 10 {
salesNum = -1
}
userInfo.SalesPole++ userInfo.SalesPole++
} else if userInfo.BuyTing < 15 { } else {
residue = 15 - userInfo.BuyTing maxSales := 200 - userInfo.SalesFish
userInfo.BuyTing++ if maxSales < 0 {
salesNum = 0
}
if salesNum > maxSales {
salesNum = maxSales
}
} }
return residue, sql.db.Insert("buff", &userInfo)
return salesNum, sql.db.Insert("buff", &userInfo)
} }
// 检测物品是否是鱼 // 检测物品是否是鱼
@ -811,38 +819,6 @@ func checkIsFish(thing string) bool {
return false return false
} }
// 查询能交易鱼类的数量
func (sql *fishdb) selectCanSalesFishFor(uid int64, sales int) int {
residue := 0
sql.Lock()
defer sql.Unlock()
userInfo := buffInfo{ID: uid}
err := sql.db.Create("buff", &userInfo)
if err != nil {
return residue
}
_ = sql.db.Find("buff", &userInfo, "WHERE ID = ?", uid)
if time.Now().Day() != time.Unix(userInfo.Duration, 0).Day() {
userInfo.Duration = time.Now().Unix()
// 在 checkCanSalesFor 也有更新buff时间TODO重构 *CanSalesFishFor 俩个函数
userInfo.SalesPole = 0
userInfo.BuyTing = 0
userInfo.SalesFish = 0
err := sql.db.Insert("buff", &userInfo)
if err != nil {
return residue
}
}
maxSales := 100 - userInfo.SalesFish
if maxSales < 0 {
maxSales = 0
}
if sales > maxSales {
sales = maxSales
}
return sales
}
// 更新买卖鱼上限假定sales变量已经在 selectCanSalesFishFor 进行了防护 // 更新买卖鱼上限假定sales变量已经在 selectCanSalesFishFor 进行了防护
func (sql *fishdb) updateCanSalesFishFor(uid int64, sales int) error { func (sql *fishdb) updateCanSalesFishFor(uid int64, sales int) error {
sql.Lock() sql.Lock()

View File

@ -70,29 +70,28 @@ func init() {
engine.OnRegex(`^出售(`+strings.Join(thingList, "|")+`)\s*(\d*)$`, getdb, refreshFish).SetBlock(true).Limit(limitSet).Handle(func(ctx *zero.Ctx) { engine.OnRegex(`^出售(`+strings.Join(thingList, "|")+`)\s*(\d*)$`, getdb, refreshFish).SetBlock(true).Limit(limitSet).Handle(func(ctx *zero.Ctx) {
uid := ctx.Event.UserID uid := ctx.Event.UserID
thingName := ctx.State["regex_matched"].([]string)[1] thingName := ctx.State["regex_matched"].([]string)[1]
if strings.Contains(thingName, "竿") {
times, err := dbdata.checkCanSalesFor(uid, true)
if err != nil {
ctx.SendChain(message.Text("[ERROR at store.go.75]:", err))
return
}
if times <= 0 {
ctx.SendChain(message.Text("出售次数已达到上限,明天再来售卖吧"))
return
}
}
number, _ := strconv.Atoi(ctx.State["regex_matched"].([]string)[2]) number, _ := strconv.Atoi(ctx.State["regex_matched"].([]string)[2])
if number == 0 || strings.Contains(thingName, "竿") { if number == 0 || strings.Contains(thingName, "竿") {
number = 1 number = 1
} }
if checkIsFish(thingName) {
residue := dbdata.selectCanSalesFishFor(uid, number) // 检测物品交易次数
if residue <= 0 { number, err := dbdata.checkCanSalesFor(uid, thingName, number)
ctx.SendChain(message.Text("一天只能交易100条鱼明天再来卖鱼吧")) if err != nil {
return ctx.SendChain(message.Text("[ERROR at store.go.75]:", err))
} return
number = residue
} }
if number <= 0 {
var msg string
if strings.Contains(thingName, "竿") {
msg = "一天只能交易10把鱼竿,明天再来售卖吧"
} else {
msg = "一天只能交易200次物品明天再来吧~"
}
ctx.SendChain(message.Text(msg))
return
}
articles, err := dbdata.getUserThingInfo(uid, thingName) articles, err := dbdata.getUserThingInfo(uid, thingName)
if err != nil { if err != nil {
ctx.SendChain(message.Text("[ERROR at store.go.5]:", err)) ctx.SendChain(message.Text("[ERROR at store.go.5]:", err))
@ -402,6 +401,12 @@ func init() {
}) })
engine.OnRegex(`^购买(`+strings.Join(thingList, "|")+`)\s*(\d*)$`, getdb, refreshFish).SetBlock(true).Limit(limitSet).Handle(func(ctx *zero.Ctx) { engine.OnRegex(`^购买(`+strings.Join(thingList, "|")+`)\s*(\d*)$`, getdb, refreshFish).SetBlock(true).Limit(limitSet).Handle(func(ctx *zero.Ctx) {
uid := ctx.Event.UserID uid := ctx.Event.UserID
thingName := ctx.State["regex_matched"].([]string)[1]
number, _ := strconv.Atoi(ctx.State["regex_matched"].([]string)[2])
if number == 0 || strings.Contains(thingName, "竿") {
number = 1
}
numberOfPole, err := dbdata.getNumberFor(uid, "竿") numberOfPole, err := dbdata.getNumberFor(uid, "竿")
if err != nil { if err != nil {
ctx.SendChain(message.Text("[ERROR at store.go.9.3]:", err)) ctx.SendChain(message.Text("[ERROR at store.go.9.3]:", err))
@ -411,28 +416,24 @@ func init() {
ctx.SendChain(message.Text("你有", numberOfPole, "支鱼竿,大于50支的玩家不允许购买东西")) ctx.SendChain(message.Text("你有", numberOfPole, "支鱼竿,大于50支的玩家不允许购买东西"))
return return
} }
buytimes, err := dbdata.checkCanSalesFor(uid, false)
// 检测物品交易次数
number, err = dbdata.checkCanSalesFor(uid, thingName, number)
if err != nil { if err != nil {
ctx.SendChain(message.Text("[ERROR at store.go.75]:", err)) ctx.SendChain(message.Text("[ERROR at store.go.75]:", err))
return return
} }
if buytimes <= 0 { if number <= 0 {
ctx.SendChain(message.Text("购买次数已达到上限,明天再来购买吧")) var msg string
if strings.Contains(thingName, "竿") {
msg = "一天只能交易10把鱼竿,明天再来售卖吧"
} else {
msg = "一天只能交易200次物品明天再来吧~"
}
ctx.SendChain(message.Text(msg))
return return
} }
thingName := ctx.State["regex_matched"].([]string)[1]
number, _ := strconv.Atoi(ctx.State["regex_matched"].([]string)[2])
if number == 0 {
number = 1
}
if checkIsFish(thingName) {
residue := dbdata.selectCanSalesFishFor(uid, number)
if residue <= 0 {
ctx.SendChain(message.Text("一天只能交易100条鱼明天再来买鱼吧"))
return
}
number = residue
}
thingInfos, err := dbdata.getStoreThingInfo(thingName) thingInfos, err := dbdata.getStoreThingInfo(thingName)
if err != nil { if err != nil {
ctx.SendChain(message.Text("[ERROR at store.go.11]:", err)) ctx.SendChain(message.Text("[ERROR at store.go.11]:", err))