Skip to content

Commit f133423

Browse files
authored
fix: 重写交易鱼类上限逻辑 (#1002) (#1003)
1 parent b777b34 commit f133423

2 files changed

Lines changed: 36 additions & 17 deletions

File tree

plugin/mcfish/main.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,15 +812,15 @@ func checkIsFish(thing string) bool {
812812
return false
813813
}
814814

815-
// 检测买卖鱼上限
816-
func (sql *fishdb) checkCanSalesFishFor(uid int64, sales int) (int, error) {
815+
// 查询能交易鱼类的数量
816+
func (sql *fishdb) selectCanSalesFishFor(uid int64, sales int) int {
817817
residue := 0
818818
sql.Lock()
819819
defer sql.Unlock()
820820
userInfo := buffInfo{ID: uid}
821821
err := sql.db.Create("buff", &userInfo)
822822
if err != nil {
823-
return residue, err
823+
return residue
824824
}
825825
_ = sql.db.Find("buff", &userInfo, "where ID = "+strconv.FormatInt(uid, 10))
826826
if time.Now().Day() != time.Unix(userInfo.Duration, 0).Day() {
@@ -834,6 +834,19 @@ func (sql *fishdb) checkCanSalesFishFor(uid int64, sales int) (int, error) {
834834
if sales > maxSales {
835835
sales = maxSales
836836
}
837+
return sales
838+
}
839+
840+
// 更新买卖鱼上限,假定sales变量已经在 selectCanSalesFishFor 进行了防护
841+
func (sql *fishdb) updateCanSalesFishFor(uid int64, sales int) error {
842+
sql.Lock()
843+
defer sql.Unlock()
844+
userInfo := buffInfo{ID: uid}
845+
err := sql.db.Create("buff", &userInfo)
846+
if err != nil {
847+
return err
848+
}
849+
_ = sql.db.Find("buff", &userInfo, "where ID = "+strconv.FormatInt(uid, 10))
837850
userInfo.SalesFish += sales
838-
return sales, sql.db.Insert("buff", &userInfo)
851+
return sql.db.Insert("buff", &userInfo)
839852
}

plugin/mcfish/store.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,9 @@ func init() {
8686
number = 1
8787
}
8888
if checkIsFish(thingName) {
89-
residue, err := dbdata.checkCanSalesFishFor(uid, number)
90-
if err != nil {
91-
ctx.SendChain(message.Text("[ERROR]:", err))
92-
return
93-
}
89+
residue := dbdata.selectCanSalesFishFor(uid, number)
9490
if residue <= 0 {
95-
ctx.SendChain(message.Text("今天你已经超出了鱼交易数量上限,明天再来买鱼吧"))
91+
ctx.SendChain(message.Text("一天只能交易100条鱼,明天再来卖鱼吧"))
9692
return
9793
}
9894
number = residue
@@ -198,6 +194,13 @@ func init() {
198194
}
199195
}
200196

197+
// 更新交易鱼类数量
198+
if checkIsFish(thingName) {
199+
err := dbdata.updateCanSalesFishFor(uid, number)
200+
if err != nil {
201+
ctx.SendChain(message.Text("[ERROR,记录鱼类交易数量失败,此次交易不记录]:", err))
202+
}
203+
}
201204
records, err := dbdata.getUserThingInfo(uid, "唱片")
202205
if err != nil {
203206
ctx.SendChain(message.Text("[ERROR at store.go.9.1]:", err))
@@ -318,7 +321,7 @@ func init() {
318321
logrus.Warnln(err)
319322
}
320323
}
321-
ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text("出售成功,你赚到了", pice*number, msg)))
324+
ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text("成功出售", thingName, ":", number, "个", ",你赚到了", pice*number, msg)))
322325
})
323326
engine.OnRegex(`^出售所有垃圾`, getdb, refreshFish).SetBlock(true).Limit(limitSet).Handle(func(ctx *zero.Ctx) {
324327
uid := ctx.Event.UserID
@@ -422,13 +425,9 @@ func init() {
422425
number = 1
423426
}
424427
if checkIsFish(thingName) {
425-
residue, err := dbdata.checkCanSalesFishFor(uid, number)
426-
if err != nil {
427-
ctx.SendChain(message.Text("[ERROR]:", err))
428-
return
429-
}
428+
residue := dbdata.selectCanSalesFishFor(uid, number)
430429
if residue <= 0 {
431-
ctx.SendChain(message.Text("今天你已经超出了鱼交易数量上限,明天再来买鱼吧"))
430+
ctx.SendChain(message.Text("一天只能交易100条鱼,明天再来买鱼吧"))
432431
return
433432
}
434433
number = residue
@@ -533,6 +532,13 @@ func init() {
533532
}
534533
}
535534

535+
// 更新交易鱼类数量
536+
if checkIsFish(thingName) {
537+
err := dbdata.updateCanSalesFishFor(uid, number)
538+
if err != nil {
539+
ctx.SendChain(message.Text("[ERROR,更新鱼类交易数量失败,此次交易不记录]:", err))
540+
}
541+
}
536542
thing := thingInfos[index]
537543
if thing.Number < number {
538544
ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text("商店数量不足")))

0 commit comments

Comments
 (0)