优化 score

This commit is contained in:
源文雨 2022-06-02 12:29:37 +08:00
parent fd7f5f719f
commit e1bc4fdf36

View File

@ -15,6 +15,7 @@ import (
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/zbputils/binary"
control "github.com/FloatTech/zbputils/control" control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext" "github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/file"
@ -207,14 +208,17 @@ func init() {
ctx.SendChain(message.Text("ERROR:", err)) ctx.SendChain(message.Text("ERROR:", err))
return return
} }
f, err := os.Create(drawedFile)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
bars := make([]chart.Value, len(st)) bars := make([]chart.Value, len(st))
for i, v := range st { for i, v := range st {
bars[i] = chart.Value{ bars[i].Value = float64(v.Score)
Value: float64(v.Score), bars[i].Label = ctx.CardOrNickName(v.UID)
Label: ctx.CardOrNickName(v.UID),
} }
} err = chart.BarChart{
graph := chart.BarChart{
Font: font, Font: font,
Title: "饼干排名", Title: "饼干排名",
Background: chart.Style{ Background: chart.Style{
@ -225,13 +229,7 @@ func init() {
Height: 500, Height: 500,
BarWidth: 50, BarWidth: 50,
Bars: bars, Bars: bars,
} }.Render(chart.PNG, f)
f, err := os.Create(drawedFile)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
err = graph.Render(chart.PNG, f)
_ = f.Close() _ = f.Close()
if err != nil { if err != nil {
_ = os.Remove(drawedFile) _ = os.Remove(drawedFile)
@ -243,16 +241,17 @@ func init() {
} }
func getHourWord(t time.Time) string { func getHourWord(t time.Time) string {
h := t.Hour()
switch { switch {
case 6 <= t.Hour() && t.Hour() < 12: case 6 <= h && h < 12:
return "早上好" return "早上好"
case 12 <= t.Hour() && t.Hour() < 14: case 12 <= h && h < 14:
return "中午好" return "中午好"
case 14 <= t.Hour() && t.Hour() < 19: case 14 <= h && h < 19:
return "下午好" return "下午好"
case 19 <= t.Hour() && t.Hour() < 24: case 19 <= h && h < 24:
return "晚上好" return "晚上好"
case 0 <= t.Hour() && t.Hour() < 6: case 0 <= h && h < 6:
return "凌晨好" return "凌晨好"
default: default:
return "" return ""
@ -271,20 +270,17 @@ func getLevel(count int) int {
} }
func initPic(picFile string) error { func initPic(picFile string) error {
if file.IsNotExist(picFile) { if file.IsExist(picFile) {
return nil
}
data, err := web.RequestDataWith(web.NewDefaultClient(), backgroundURL, "GET", referer, ua) data, err := web.RequestDataWith(web.NewDefaultClient(), backgroundURL, "GET", referer, ua)
if err != nil { if err != nil {
return err return err
} }
picURL := gjson.Get(string(data), "pic.0").String() picURL := gjson.Get(binary.BytesToString(data), "pic.0").Str
data, err = web.RequestDataWith(web.NewDefaultClient(), picURL, "GET", "", ua) data, err = web.RequestDataWith(web.NewDefaultClient(), picURL, "GET", "", ua)
if err != nil { if err != nil {
return err return err
} }
err = os.WriteFile(picFile, data, 0666) return os.WriteFile(picFile, data, 0644)
if err != nil {
return err
}
}
return nil
} }