Skip to content

Commit 9b26eea

Browse files
authored
✨ 增加查看签到排名 (#180)
* ✨ 增加查看签到排名 * 🚨 修lint * 🚨 修lint
1 parent a6bd717 commit 9b26eea

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ print("run[CQ:image,file="+j["img"]+"]")
345345
- **签到得分** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/score"`
346346
- [x] 签到
347347
- [x] 获得签到背景[@xxx] | 获得签到背景
348+
- [x] 查看分数排名
348349
- **骂人** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/curse"`
349350
- [x] 骂我
350351
- [x] 大力骂我

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/shirou/gopsutil/v3 v3.22.2
2323
github.com/sirupsen/logrus v1.8.1
2424
github.com/tidwall/gjson v1.14.0
25+
github.com/wcharczuk/go-chart/v2 v2.1.0
2526
github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220330050111-d962c23b4f4d
2627
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a
2728
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYa
123123
github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=
124124
github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o=
125125
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
126+
github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I=
127+
github.com/wcharczuk/go-chart/v2 v2.1.0/go.mod h1:yx7MvAVNcP/kN9lKXM/NTce4au4DFN99j6i1OwDclNA=
126128
github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220330050111-d962c23b4f4d h1:NGea1vhhGuvv7E6sPeUyPyTxCtCkEVn8ShNniZY9bQA=
127129
github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220330050111-d962c23b4f4d/go.mod h1:NwXIp7PgjV+kUALMXJ4v4/3QcsRSOodtjhLekuPXFog=
128130
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -135,6 +137,7 @@ golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPh
135137
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
136138
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 h1:S25/rfnfsMVgORT4/J61MJ7rdyseOZOyvLIrZEZ7s6s=
137139
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
140+
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
138141
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a h1:LnH9RNcpPv5Kzi15lXg42lYMPUf0x8CuPv1YnvBWZAg=
139142
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
140143
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=

plugin/bilibili/info.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ func init() {
171171
canvas.DrawImage(back, 0, 0)
172172
}
173173
canvas.SetColor(color.Black)
174+
_, err = file.GetLazyData(text.BoldFontFile, false, true)
175+
if err != nil {
176+
ctx.SendChain(message.Text("ERROR:", err))
177+
}
174178
if err = canvas.LoadFontFace(text.BoldFontFile, fontSize); err != nil {
175179
ctx.SendChain(message.Text("ERROR:", err))
176180
return

plugin/score/model.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,9 @@ func (sdb *scoredb) InsertOrUpdateSignInCountByUID(uid int64, count int) (err er
117117
}
118118
return
119119
}
120+
121+
func (sdb *scoredb) GetScoreRankByTopN(n int) (st []scoretable, err error) {
122+
db := (*gorm.DB)(sdb)
123+
err = db.Debug().Model(&scoretable{}).Order("score desc").Limit(n).Find(&st).Error
124+
return
125+
}

plugin/score/sign_in.go

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"time"
99

1010
"github.com/fogleman/gg"
11+
"github.com/golang/freetype"
1112
log "github.com/sirupsen/logrus"
1213
"github.com/tidwall/gjson"
14+
"github.com/wcharczuk/go-chart/v2"
1315
zero "github.com/wdvxdr1123/ZeroBot"
1416
"github.com/wdvxdr1123/ZeroBot/message"
1517

@@ -35,7 +37,7 @@ var levelArray = [...]int{0, 1, 2, 5, 10, 20, 35, 55, 75, 100, 120}
3537
func init() {
3638
engine := control.Register("score", &control.Options{
3739
DisableOnDefault: false,
38-
Help: "签到得分\n- 签到\n- 获得签到背景[@xxx] | 获得签到背景",
40+
Help: "签到得分\n- 签到\n- 获得签到背景[@xxx] | 获得签到背景\n- 查看分数排名",
3941
PrivateDataFolder: "score",
4042
})
4143
cachePath := engine.DataFolder() + "cache/"
@@ -173,6 +175,71 @@ func init() {
173175
}
174176
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + picFile))
175177
})
178+
engine.OnFullMatch("查看分数排名", zero.OnlyGroup).SetBlock(true).
179+
Handle(func(ctx *zero.Ctx) {
180+
today := time.Now().Format("20060102")
181+
drawedFile := cachePath + today + "scoreRank.png"
182+
if file.IsExist(drawedFile) {
183+
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
184+
return
185+
}
186+
st, err := sdb.GetScoreRankByTopN(10)
187+
if err != nil {
188+
ctx.SendChain(message.Text("ERROR:", err))
189+
return
190+
}
191+
if len(st) == 0 {
192+
ctx.SendChain(message.Text("ERROR:目前还没有人签到过"))
193+
return
194+
}
195+
_, err = file.GetLazyData(text.FontFile, false, true)
196+
if err != nil {
197+
ctx.SendChain(message.Text("ERROR:", err))
198+
return
199+
}
200+
b, err := os.ReadFile(text.FontFile)
201+
if err != nil {
202+
ctx.SendChain(message.Text("ERROR:", err))
203+
return
204+
}
205+
font, err := freetype.ParseFont(b)
206+
if err != nil {
207+
ctx.SendChain(message.Text("ERROR:", err))
208+
return
209+
}
210+
bars := make([]chart.Value, len(st))
211+
for i, v := range st {
212+
bars[i] = chart.Value{
213+
Value: float64(v.Score),
214+
Label: ctx.CardOrNickName(v.UID),
215+
}
216+
}
217+
graph := chart.BarChart{
218+
Font: font,
219+
Title: "饼干排名",
220+
Background: chart.Style{
221+
Padding: chart.Box{
222+
Top: 40,
223+
},
224+
},
225+
Height: 500,
226+
BarWidth: 50,
227+
Bars: bars,
228+
}
229+
f, err := os.Create(drawedFile)
230+
if err != nil {
231+
ctx.SendChain(message.Text("ERROR:", err))
232+
return
233+
}
234+
err = graph.Render(chart.PNG, f)
235+
_ = f.Close()
236+
if err != nil {
237+
os.Remove(drawedFile)
238+
ctx.SendChain(message.Text("ERROR:", err))
239+
return
240+
}
241+
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
242+
})
176243
}
177244

178245
func getHourWord(t time.Time) string {

0 commit comments

Comments
 (0)