Skip to content

Commit 83037f6

Browse files
authored
update: 重写[mcfish]交易检测逻辑 (#1070)
1 parent 6a2c7e8 commit 83037f6

File tree

2 files changed

+57
-80
lines changed

2 files changed

+57
-80
lines changed

plugin/mcfish/main.go

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"math/rand"
77
"os"
88
"strconv"
9+
"strings"
910
"sync"
1011
"time"
1112

@@ -28,7 +29,7 @@ type fishdb struct {
2829
const FishLimit = 50
2930

3031
// version 规则版本号
31-
const version = "5.5.8"
32+
const version = "5.5.9"
3233

3334
// 各物品信息
3435
type jsonInfo struct {
@@ -147,7 +148,7 @@ var (
147148
"8.合成:\n-> 铁竿 : 3x木竿\n-> 金竿 : 3x铁竿\n-> 钻石竿 : 3x金竿\n-> 下界合金竿 : 3x钻石竿\n-> 三叉戟 : 3x下界合金竿\n注:合成成功率90%(包括梭哈),合成鱼竿的附魔等级=(附魔等级合/合成鱼竿数量)\n" +
148149
"9.杂项:\n-> 无装备的情况下,每人最多可以购买3次100块钱的鱼竿\n-> 默认状态钓鱼上钩概率为60%(理论值!!!)\n-> 附魔的鱼竿会因附魔变得昂贵,每个附魔最高3级\n-> 三叉戟不算鱼竿,修复时可直接满耐久\n" +
149150
"-> 鱼竿数量大于50的不能买东西;\n 鱼竿数量大于30的不能钓鱼;\n 每购/售10次鱼竿获得1层宝藏诅咒;\n 每购买20次物品将获得3次价格减半福利;\n 每钓鱼75次获得1本净化书;\n" +
150-
" 每天最多只可出售5个鱼竿和购买15次物品;鱼类交易每天最多100条.",
151+
" 每天可交易鱼竿10个,物品200件.",
151152
PublicDataFolder: "McFish",
152153
}).ApplySingle(ctxext.DefaultSingle)
153154
getdb = fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool {
@@ -344,7 +345,7 @@ func (sql *fishdb) setEquipFor(uid int64) (err error) {
344345
if err != nil {
345346
return err
346347
}
347-
_ = sql.db.Find("fishState", &userInfo, "WHERE ID = ?", uid)
348+
err = sql.db.Find("fishState", &userInfo, "WHERE ID = ?", uid)
348349
if err != nil {
349350
return err
350351
}
@@ -608,7 +609,7 @@ func (sql *fishdb) refreshStroeInfo() (ok bool, err error) {
608609
}
609610
}
610611
thing := store{}
611-
oldThing := []store{}
612+
var oldThing []store
612613
_ = sql.db.FindFor("stroeDiscount", &thing, "WHERE type = 'pole'", func() error {
613614
if time.Since(time.Unix(thing.Duration, 0)) > 24 {
614615
oldThing = append(oldThing, thing)
@@ -628,7 +629,7 @@ func (sql *fishdb) refreshStroeInfo() (ok bool, err error) {
628629
Price: priceList[fish] * discountList[fish] / 100,
629630
}
630631
_ = sql.db.Find("store", &thingInfo, "WHERE Name = ?", fish)
631-
thingInfo.Number += (100 - discountList[fish])
632+
thingInfo.Number += 100 - discountList[fish]
632633
if thingInfo.Number < 1 {
633634
thingInfo.Number = 100
634635
}
@@ -774,15 +775,14 @@ func (sql *fishdb) useCouponAt(uid int64, times int) (int, error) {
774775
return useTimes, sql.db.Insert("buff", &userInfo)
775776
}
776777

777-
// 检测卖鱼竿上限
778-
func (sql *fishdb) checkCanSalesFor(uid int64, sales bool) (int, error) {
779-
residue := 0
778+
// 买卖上限检测
779+
func (sql *fishdb) checkCanSalesFor(uid int64, saleType string, salesNum int) (int, error) {
780780
sql.Lock()
781781
defer sql.Unlock()
782782
userInfo := buffInfo{ID: uid}
783783
err := sql.db.Create("buff", &userInfo)
784784
if err != nil {
785-
return residue, err
785+
return salesNum, err
786786
}
787787
_ = sql.db.Find("buff", &userInfo, "WHERE ID = ?", uid)
788788
if time.Now().Day() != time.Unix(userInfo.Duration, 0).Day() {
@@ -791,14 +791,22 @@ func (sql *fishdb) checkCanSalesFor(uid int64, sales bool) (int, error) {
791791
userInfo.BuyTing = 0
792792
userInfo.SalesFish = 0
793793
}
794-
if sales && userInfo.SalesPole < 5 {
795-
residue = 5 - userInfo.SalesPole
794+
if strings.Contains(saleType, "竿") {
795+
if userInfo.SalesPole >= 10 {
796+
salesNum = -1
797+
}
796798
userInfo.SalesPole++
797-
} else if userInfo.BuyTing < 15 {
798-
residue = 15 - userInfo.BuyTing
799-
userInfo.BuyTing++
799+
} else {
800+
maxSales := 200 - userInfo.SalesFish
801+
if maxSales < 0 {
802+
salesNum = 0
803+
}
804+
if salesNum > maxSales {
805+
salesNum = maxSales
806+
}
800807
}
801-
return residue, sql.db.Insert("buff", &userInfo)
808+
809+
return salesNum, sql.db.Insert("buff", &userInfo)
802810
}
803811

804812
// 检测物品是否是鱼
@@ -811,38 +819,6 @@ func checkIsFish(thing string) bool {
811819
return false
812820
}
813821

814-
// 查询能交易鱼类的数量
815-
func (sql *fishdb) selectCanSalesFishFor(uid int64, sales int) int {
816-
residue := 0
817-
sql.Lock()
818-
defer sql.Unlock()
819-
userInfo := buffInfo{ID: uid}
820-
err := sql.db.Create("buff", &userInfo)
821-
if err != nil {
822-
return residue
823-
}
824-
_ = sql.db.Find("buff", &userInfo, "WHERE ID = ?", uid)
825-
if time.Now().Day() != time.Unix(userInfo.Duration, 0).Day() {
826-
userInfo.Duration = time.Now().Unix()
827-
// 在 checkCanSalesFor 也有更新buff时间,TODO:重构 *CanSalesFishFor 俩个函数
828-
userInfo.SalesPole = 0
829-
userInfo.BuyTing = 0
830-
userInfo.SalesFish = 0
831-
err := sql.db.Insert("buff", &userInfo)
832-
if err != nil {
833-
return residue
834-
}
835-
}
836-
maxSales := 100 - userInfo.SalesFish
837-
if maxSales < 0 {
838-
maxSales = 0
839-
}
840-
if sales > maxSales {
841-
sales = maxSales
842-
}
843-
return sales
844-
}
845-
846822
// 更新买卖鱼上限,假定sales变量已经在 selectCanSalesFishFor 进行了防护
847823
func (sql *fishdb) updateCanSalesFishFor(uid int64, sales int) error {
848824
sql.Lock()

plugin/mcfish/store.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,28 @@ func init() {
7070
engine.OnRegex(`^出售(`+strings.Join(thingList, "|")+`)\s*(\d*)$`, getdb, refreshFish).SetBlock(true).Limit(limitSet).Handle(func(ctx *zero.Ctx) {
7171
uid := ctx.Event.UserID
7272
thingName := ctx.State["regex_matched"].([]string)[1]
73-
if strings.Contains(thingName, "竿") {
74-
times, err := dbdata.checkCanSalesFor(uid, true)
75-
if err != nil {
76-
ctx.SendChain(message.Text("[ERROR at store.go.75]:", err))
77-
return
78-
}
79-
if times <= 0 {
80-
ctx.SendChain(message.Text("出售次数已达到上限,明天再来售卖吧"))
81-
return
82-
}
83-
}
8473
number, _ := strconv.Atoi(ctx.State["regex_matched"].([]string)[2])
8574
if number == 0 || strings.Contains(thingName, "竿") {
8675
number = 1
8776
}
88-
if checkIsFish(thingName) {
89-
residue := dbdata.selectCanSalesFishFor(uid, number)
90-
if residue <= 0 {
91-
ctx.SendChain(message.Text("一天只能交易100条鱼,明天再来卖鱼吧"))
92-
return
77+
78+
// 检测物品交易次数
79+
number, err := dbdata.checkCanSalesFor(uid, thingName, number)
80+
if err != nil {
81+
ctx.SendChain(message.Text("[ERROR at store.go.75]:", err))
82+
return
83+
}
84+
if number <= 0 {
85+
var msg string
86+
if strings.Contains(thingName, "竿") {
87+
msg = "一天只能交易10把鱼竿,明天再来售卖吧"
88+
} else {
89+
msg = "一天只能交易200次物品,明天再来吧~"
9390
}
94-
number = residue
91+
ctx.SendChain(message.Text(msg))
92+
return
9593
}
94+
9695
articles, err := dbdata.getUserThingInfo(uid, thingName)
9796
if err != nil {
9897
ctx.SendChain(message.Text("[ERROR at store.go.5]:", err))
@@ -402,6 +401,12 @@ func init() {
402401
})
403402
engine.OnRegex(`^购买(`+strings.Join(thingList, "|")+`)\s*(\d*)$`, getdb, refreshFish).SetBlock(true).Limit(limitSet).Handle(func(ctx *zero.Ctx) {
404403
uid := ctx.Event.UserID
404+
thingName := ctx.State["regex_matched"].([]string)[1]
405+
number, _ := strconv.Atoi(ctx.State["regex_matched"].([]string)[2])
406+
if number == 0 || strings.Contains(thingName, "竿") {
407+
number = 1
408+
}
409+
405410
numberOfPole, err := dbdata.getNumberFor(uid, "竿")
406411
if err != nil {
407412
ctx.SendChain(message.Text("[ERROR at store.go.9.3]:", err))
@@ -411,28 +416,24 @@ func init() {
411416
ctx.SendChain(message.Text("你有", numberOfPole, "支鱼竿,大于50支的玩家不允许购买东西"))
412417
return
413418
}
414-
buytimes, err := dbdata.checkCanSalesFor(uid, false)
419+
420+
// 检测物品交易次数
421+
number, err = dbdata.checkCanSalesFor(uid, thingName, number)
415422
if err != nil {
416423
ctx.SendChain(message.Text("[ERROR at store.go.75]:", err))
417424
return
418425
}
419-
if buytimes <= 0 {
420-
ctx.SendChain(message.Text("购买次数已达到上限,明天再来购买吧"))
421-
return
422-
}
423-
thingName := ctx.State["regex_matched"].([]string)[1]
424-
number, _ := strconv.Atoi(ctx.State["regex_matched"].([]string)[2])
425-
if number == 0 {
426-
number = 1
427-
}
428-
if checkIsFish(thingName) {
429-
residue := dbdata.selectCanSalesFishFor(uid, number)
430-
if residue <= 0 {
431-
ctx.SendChain(message.Text("一天只能交易100条鱼,明天再来买鱼吧"))
432-
return
426+
if number <= 0 {
427+
var msg string
428+
if strings.Contains(thingName, "竿") {
429+
msg = "一天只能交易10把鱼竿,明天再来售卖吧"
430+
} else {
431+
msg = "一天只能交易200次物品,明天再来吧~"
433432
}
434-
number = residue
433+
ctx.SendChain(message.Text(msg))
434+
return
435435
}
436+
436437
thingInfos, err := dbdata.getStoreThingInfo(thingName)
437438
if err != nil {
438439
ctx.SendChain(message.Text("[ERROR at store.go.11]:", err))

0 commit comments

Comments
 (0)