@@ -5,26 +5,17 @@ import (
5
5
"fmt"
6
6
"strconv"
7
7
"strings"
8
- "sync"
9
8
"time"
10
9
11
10
ctrl "github.com/FloatTech/zbpctrl"
12
11
"github.com/FloatTech/zbputils/control"
13
12
"github.com/FloatTech/zbputils/ctxext"
13
+ "github.com/RomiChan/syncx"
14
14
"github.com/lithammer/fuzzysearch/fuzzy"
15
15
zero "github.com/wdvxdr1123/ZeroBot"
16
16
"github.com/wdvxdr1123/ZeroBot/message"
17
17
)
18
18
19
- var wmdr sync.RWMutex
20
-
21
- type wmdata struct {
22
- wmitems map [string ]items
23
- itemNames []string
24
- }
25
-
26
- var wd , _ = newwm ()
27
-
28
19
func init () {
29
20
eng := control .Register ("warframeapi" , & ctrl.Options [* zero.Ctx ]{
30
21
DisableOnDefault : false ,
@@ -242,12 +233,24 @@ func init() {
242
233
ctx .SendChain (message .Text ("已拉取服务器时间并同步到本地模拟" ))
243
234
})
244
235
// 根据名称从Warframe市场查询物品售价
245
- eng .OnPrefix (".wm " , checknwm ).SetBlock (true ).
236
+ eng .OnPrefix (".wm " , func (ctx * zero.Ctx ) bool {
237
+ if wd .Get ().wmitems == nil || wd .Get ().itemNames == nil {
238
+ if wderr != nil { // 获取失败
239
+ ctx .SendChain (message .Text ("ERROR: 获取Warframe市场物品列表失败: " , wderr ))
240
+ } else {
241
+ ctx .SendChain (message .Text ("ERROR: Warframe市场物品列表为空!" ))
242
+ }
243
+ wd = syncx.Lazy [* wmdata ]{Init : func () (d * wmdata ) {
244
+ d , wderr = newwm ()
245
+ return
246
+ }}
247
+ return false
248
+ }
249
+ return true
250
+ }).SetBlock (true ).
246
251
Handle (func (ctx * zero.Ctx ) {
247
252
// 根据输入的名称, 从游戏物品名称列表中进行模糊搜索
248
- wmdr .RLock ()
249
- sol := fuzzy .FindNormalizedFold (ctx .State ["args" ].(string ), wd .itemNames )
250
- wmdr .RUnlock ()
253
+ sol := fuzzy .FindNormalizedFold (ctx .State ["args" ].(string ), wd .Get ().itemNames )
251
254
// 物品名称
252
255
var name string
253
256
@@ -293,19 +296,17 @@ func init() {
293
296
msgs = msgs [:0 ]
294
297
}
295
298
296
- sells , iteminfo , txt , err := getitemsorder (wd .wmitems [name ].URLName , onlymaxrank )
299
+ sells , iteminfo , txt , err := getitemsorder (wd .Get (). wmitems [name ].URLName , onlymaxrank )
297
300
if ! onlymaxrank {
298
- wmdr .RLock ()
299
301
if iteminfo .ZhHans .WikiLink == "" {
300
302
msgs = append (msgs , ctxext .FakeSenderForwardNode (ctx ,
301
- message .Image ("https://warframe.market/static/assets/" + wd .wmitems [name ].Thumb ),
302
- message .Text ("\n " , wd .wmitems [name ].ItemName )))
303
+ message .Image ("https://warframe.market/static/assets/" + wd .Get (). wmitems [name ].Thumb ),
304
+ message .Text ("\n " , wd .Get (). wmitems [name ].ItemName )))
303
305
} else {
304
306
msgs = append (msgs , ctxext .FakeSenderForwardNode (ctx ,
305
- message .Image ("https://warframe.market/static/assets/" + wd .wmitems [name ].Thumb ),
306
- message .Text ("\n " , wd .wmitems [name ].ItemName , "\n wiki: " , iteminfo .ZhHans .WikiLink )))
307
+ message .Image ("https://warframe.market/static/assets/" + wd .Get (). wmitems [name ].Thumb ),
308
+ message .Text ("\n " , wd .Get (). wmitems [name ].ItemName , "\n wiki: " , iteminfo .ZhHans .WikiLink )))
307
309
}
308
- wmdr .RUnlock ()
309
310
}
310
311
311
312
if err != nil {
0 commit comments