Skip to content

Commit

Permalink
Merge branch 'FloatTech:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiang-Red authored Mar 4, 2023
2 parents 225c963 + d41ae01 commit 85b02d0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
38 changes: 28 additions & 10 deletions plugin/warframeapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"net/http"
"sort"

zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"

"github.com/FloatTech/floatbox/web"
)

Expand Down Expand Up @@ -68,27 +71,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
}
29 changes: 21 additions & 8 deletions plugin/warframeapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"
"strings"
"sync"
"time"

ctrl "github.com/FloatTech/zbpctrl"
Expand All @@ -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]{
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 85b02d0

Please sign in to comment.