mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 13:59:39 +08:00
fix 初始化未能获取数据的问题 (#602)
* fix 初始化未能获取数据的问题 * fix 添加读写锁 * fix lint * fix lint * change 改为全局锁
This commit is contained in:
parent
84bc79eaa6
commit
e294059de4
@ -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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user