Skip to content

Commit 5226548

Browse files
committed
优化代码结构
1 parent d41ae01 commit 5226548

File tree

3 files changed

+41
-47
lines changed

3 files changed

+41
-47
lines changed

plugin/baiduaudit/audit.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -442,17 +442,12 @@ func loadConfig() {
442442

443443
// 保存配置文件
444444
func jsonSave(v keyConfig, path string) {
445-
jsf, _ := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
446-
defer func(file *os.File) {
447-
err := file.Close()
448-
if err != nil {
449-
fmt.Println(err)
450-
}
451-
}(jsf) // 结束时关闭句柄,释放资源
452-
err := json.NewEncoder(jsf).Encode(v)
445+
jsf, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
453446
if err != nil {
454-
fmt.Println(err)
447+
return
455448
}
449+
defer jsf.Close()
450+
_ = json.NewEncoder(jsf).Encode(v)
456451
}
457452

458453
// JSON反序列化

plugin/warframeapi/api.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"net/http"
88
"sort"
99

10-
zero "github.com/wdvxdr1123/ZeroBot"
11-
"github.com/wdvxdr1123/ZeroBot/message"
10+
"github.com/RomiChan/syncx"
11+
"github.com/sirupsen/logrus"
1212

1313
"github.com/FloatTech/floatbox/web"
1414
)
@@ -71,24 +71,22 @@ func getitemsorder(cnName string, onlyMaxRank bool) (od orders, it *itemsInSet,
7171
return
7272
}
7373

74-
// 检查值是否为空,为空则重新获取
75-
func checknwm(ctx *zero.Ctx) bool {
76-
var err error
77-
wmdr.Lock()
78-
defer wmdr.Unlock()
79-
if wd.wmitems == nil || wd.itemNames == nil {
80-
wd, err = newwm()
81-
if err != nil { // 获取失败
82-
ctx.SendChain(message.Text("ERROR: 获取Warframe市场物品列表失败(" + err.Error() + ")"))
83-
return false
84-
}
85-
}
86-
return true
74+
type wmdata struct {
75+
wmitems map[string]items
76+
itemNames []string
8777
}
78+
79+
var (
80+
wderr error
81+
wd = syncx.Lazy[*wmdata]{Init: func() (d *wmdata) {
82+
d, wderr = newwm()
83+
return
84+
}}
85+
)
86+
8887
func newwm() (*wmdata, error) {
8988
var itemapi wfAPIItem // WarFrame市场的数据实例
9089
var wd wmdata
91-
println("正在获取Warframe市场物品列表")
9290
data, err := web.RequestDataWithHeaders(&http.Client{}, wfitemurl, "GET", func(request *http.Request) error {
9391
request.Header.Add("Accept", "application/json")
9492
request.Header.Add("Language", "zh-hans")
@@ -107,6 +105,6 @@ func newwm() (*wmdata, error) {
107105
wd.wmitems[v.ItemName] = v
108106
wd.itemNames[i] = v.ItemName
109107
}
110-
println("获取Warframe市场物品列表完成")
108+
logrus.Infoln("[wfapi] 获取", len(wd.itemNames), "项内容")
111109
return &wd, nil
112110
}

plugin/warframeapi/main.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,17 @@ import (
55
"fmt"
66
"strconv"
77
"strings"
8-
"sync"
98
"time"
109

1110
ctrl "github.com/FloatTech/zbpctrl"
1211
"github.com/FloatTech/zbputils/control"
1312
"github.com/FloatTech/zbputils/ctxext"
13+
"github.com/RomiChan/syncx"
1414
"github.com/lithammer/fuzzysearch/fuzzy"
1515
zero "github.com/wdvxdr1123/ZeroBot"
1616
"github.com/wdvxdr1123/ZeroBot/message"
1717
)
1818

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-
2819
func init() {
2920
eng := control.Register("warframeapi", &ctrl.Options[*zero.Ctx]{
3021
DisableOnDefault: false,
@@ -242,12 +233,24 @@ func init() {
242233
ctx.SendChain(message.Text("已拉取服务器时间并同步到本地模拟"))
243234
})
244235
// 根据名称从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).
246251
Handle(func(ctx *zero.Ctx) {
247252
// 根据输入的名称, 从游戏物品名称列表中进行模糊搜索
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)
251254
// 物品名称
252255
var name string
253256

@@ -293,19 +296,17 @@ func init() {
293296
msgs = msgs[:0]
294297
}
295298

296-
sells, iteminfo, txt, err := getitemsorder(wd.wmitems[name].URLName, onlymaxrank)
299+
sells, iteminfo, txt, err := getitemsorder(wd.Get().wmitems[name].URLName, onlymaxrank)
297300
if !onlymaxrank {
298-
wmdr.RLock()
299301
if iteminfo.ZhHans.WikiLink == "" {
300302
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)))
303305
} else {
304306
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, "\nwiki: ", 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, "\nwiki: ", iteminfo.ZhHans.WikiLink)))
307309
}
308-
wmdr.RUnlock()
309310
}
310311

311312
if err != nil {

0 commit comments

Comments
 (0)