From e294059de4f34f95bcac0f3449f91ee4e9d2efe1 Mon Sep 17 00:00:00 2001 From: GenesisAN Date: Sat, 4 Mar 2023 19:54:56 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=88=9D=E5=A7=8B=E5=8C=96=E6=9C=AA?= =?UTF-8?q?=E8=83=BD=E8=8E=B7=E5=8F=96=E6=95=B0=E6=8D=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#602)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix 初始化未能获取数据的问题 * fix 添加读写锁 * fix lint * fix lint * change 改为全局锁 --- plugin/warframeapi/api.go | 37 +++++++++++++++++++++++++++---------- plugin/warframeapi/main.go | 29 +++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/plugin/warframeapi/api.go b/plugin/warframeapi/api.go index b14054dafd..167217665c 100644 --- a/plugin/warframeapi/api.go +++ b/plugin/warframeapi/api.go @@ -4,6 +4,8 @@ import ( "encoding/json" "errors" "fmt" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" "net/http" "sort" @@ -68,27 +70,42 @@ func getitemsorder(cnName string, onlyMaxRank bool) (od orders, it *itemsInSet, return } -func newwm() (wmitems map[string]items, itemNames []string) { +// 检查值是否为空,为空则重新获取 +func checknwm(ctx *zero.Ctx) bool { + var err error + wmdr.Lock() + defer wmdr.Unlock() + if wd.wmitems == nil || wd.itemNames == nil { + wd, err = newwm() + if err != nil { // 获取失败 + ctx.SendChain(message.Text("ERROR: 获取Warframe市场物品列表失败(" + err.Error() + ")")) + return false + } + } + return true +} +func newwm() (*wmdata, error) { var itemapi wfAPIItem // WarFrame市场的数据实例 - + var wd wmdata + println("正在获取Warframe市场物品列表") data, err := web.RequestDataWithHeaders(&http.Client{}, wfitemurl, "GET", func(request *http.Request) error { request.Header.Add("Accept", "application/json") request.Header.Add("Language", "zh-hans") return nil }, nil) if err != nil { - panic(err) + return &wd, err } err = json.Unmarshal(data, &itemapi) if err != nil { - panic(err) + return &wd, err } - - wmitems = make(map[string]items, len(itemapi.Payload.Items)*4) - itemNames = make([]string, len(itemapi.Payload.Items)) + wd.wmitems = make(map[string]items, len(itemapi.Payload.Items)*4) + wd.itemNames = make([]string, len(itemapi.Payload.Items)) for i, v := range itemapi.Payload.Items { - wmitems[v.ItemName] = v - itemNames[i] = v.ItemName + wd.wmitems[v.ItemName] = v + wd.itemNames[i] = v.ItemName } - return + println("获取Warframe市场物品列表完成") + return &wd, nil } diff --git a/plugin/warframeapi/main.go b/plugin/warframeapi/main.go index f097616dda..8d32a688ff 100644 --- a/plugin/warframeapi/main.go +++ b/plugin/warframeapi/main.go @@ -5,6 +5,7 @@ import ( "fmt" "strconv" "strings" + "sync" "time" ctrl "github.com/FloatTech/zbpctrl" @@ -15,7 +16,14 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" ) -var wmitems, itemNames = newwm() +var wmdr sync.RWMutex + +type wmdata struct { + wmitems map[string]items + itemNames []string +} + +var wd, _ = newwm() func init() { eng := control.Register("warframeapi", &ctrl.Options[*zero.Ctx]{ @@ -234,10 +242,12 @@ func init() { ctx.SendChain(message.Text("已拉取服务器时间并同步到本地模拟")) }) // 根据名称从Warframe市场查询物品售价 - eng.OnPrefix(".wm ").SetBlock(true). + eng.OnPrefix(".wm ", checknwm).SetBlock(true). Handle(func(ctx *zero.Ctx) { // 根据输入的名称, 从游戏物品名称列表中进行模糊搜索 - sol := fuzzy.FindNormalizedFold(ctx.State["args"].(string), itemNames) + wmdr.RLock() + sol := fuzzy.FindNormalizedFold(ctx.State["args"].(string), wd.itemNames) + wmdr.RUnlock() // 物品名称 var name string @@ -282,17 +292,20 @@ func init() { if onlymaxrank { msgs = msgs[:0] } - sells, iteminfo, txt, err := getitemsorder(wmitems[name].URLName, onlymaxrank) + + sells, iteminfo, txt, err := getitemsorder(wd.wmitems[name].URLName, onlymaxrank) if !onlymaxrank { + wmdr.RLock() if iteminfo.ZhHans.WikiLink == "" { msgs = append(msgs, ctxext.FakeSenderForwardNode(ctx, - message.Image("https://warframe.market/static/assets/"+wmitems[name].Thumb), - message.Text("\n", wmitems[name].ItemName))) + message.Image("https://warframe.market/static/assets/"+wd.wmitems[name].Thumb), + message.Text("\n", wd.wmitems[name].ItemName))) } else { msgs = append(msgs, ctxext.FakeSenderForwardNode(ctx, - message.Image("https://warframe.market/static/assets/"+wmitems[name].Thumb), - message.Text("\n", wmitems[name].ItemName, "\nwiki: ", iteminfo.ZhHans.WikiLink))) + message.Image("https://warframe.market/static/assets/"+wd.wmitems[name].Thumb), + message.Text("\n", wd.wmitems[name].ItemName, "\nwiki: ", iteminfo.ZhHans.WikiLink))) } + wmdr.RUnlock() } if err != nil {