✏️ 收集 max / min 到 data 包

This commit is contained in:
fumiama 2021-10-11 14:24:22 +08:00
parent d89e7aebec
commit 43735722bf
5 changed files with 40 additions and 50 deletions

17
data/math.go Normal file
View File

@ -0,0 +1,17 @@
package data
// min 返回两数最大值,该函数将被内联
func Max(a, b int) int {
if a > b {
return a
}
return b
}
// min 返回两数最小值,该函数将被内联
func Min(a, b int) int {
if a > b {
return b
}
return a
}

View File

@ -246,25 +246,6 @@ func draw(background, title, text string) ([]byte, error) {
if err := canvas.LoadFontFace(base+"sakura.ttf", 23); err != nil { if err := canvas.LoadFontFace(base+"sakura.ttf", 23); err != nil {
return nil, err return nil, err
} }
offest := func(total, now int, distance float64) float64 {
if total%2 == 0 {
return (float64(now-total/2) - 1) * distance
}
return (float64(now-total/2) - 1.5) * distance
}
rowsnum := func(total, div int) int {
temp := total / div
if total%div != 0 {
temp++
}
return temp
}
min := func(a, b int) int {
if a < b {
return a
}
return b
}
tw, th := canvas.MeasureString("测") tw, th := canvas.MeasureString("测")
tw, th = tw+10, th+10 tw, th = tw+10, th+10
r := []rune(text) r := []rune(text)
@ -273,7 +254,7 @@ func draw(background, title, text string) ([]byte, error) {
default: default:
for i, o := range r { for i, o := range r {
xnow := rowsnum(i+1, 9) xnow := rowsnum(i+1, 9)
ysum := min(len(r)-(xnow-1)*9, 9) ysum := data.Min(len(r)-(xnow-1)*9, 9)
ynow := i%9 + 1 ynow := i%9 + 1
canvas.DrawString(string(o), -offest(xsum, xnow, tw)+115, offest(ysum, ynow, th)+320.0) canvas.DrawString(string(o), -offest(xsum, xnow, tw)+115, offest(ysum, ynow, th)+320.0)
} }
@ -281,7 +262,7 @@ func draw(background, title, text string) ([]byte, error) {
div := rowsnum(len(r), 2) div := rowsnum(len(r), 2)
for i, o := range r { for i, o := range r {
xnow := rowsnum(i+1, div) xnow := rowsnum(i+1, div)
ysum := min(len(r)-(xnow-1)*div, div) ysum := data.Min(len(r)-(xnow-1)*div, div)
ynow := i%div + 1 ynow := i%div + 1
switch xnow { switch xnow {
case 1: case 1:
@ -303,3 +284,18 @@ func draw(background, title, text string) ([]byte, error) {
encoder.Close() encoder.Close()
return buffer.Bytes(), nil return buffer.Bytes(), nil
} }
func offest(total, now int, distance float64) float64 {
if total%2 == 0 {
return (float64(now-total/2) - 1) * distance
}
return (float64(now-total/2) - 1.5) * distance
}
func rowsnum(total, div int) int {
temp := total / div
if total%div != 0 {
temp++
}
return temp
}

View File

@ -11,6 +11,7 @@ import (
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/control" "github.com/FloatTech/ZeroBot-Plugin/control"
"github.com/FloatTech/ZeroBot-Plugin/data"
) )
const ( const (
@ -30,7 +31,7 @@ func init() {
}).OnFullMatch("来份萝莉").SetBlock(true). }).OnFullMatch("来份萝莉").SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
go func() { go func() {
for i := 0; i < min(cap(queue)-len(queue), 2); i++ { for i := 0; i < data.Min(cap(queue)-len(queue), 2); i++ {
resp, err := http.Get(api) resp, err := http.Get(api)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
@ -60,10 +61,3 @@ func init() {
} }
}) })
} }
func min(a, b int) int {
if a < b {
return a
}
return b
}

View File

@ -16,6 +16,8 @@ import (
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
timer "github.com/FloatTech/ZeroBot-Plugin-Timer" timer "github.com/FloatTech/ZeroBot-Plugin-Timer"
"github.com/FloatTech/ZeroBot-Plugin/data"
) )
const ( const (
@ -300,13 +302,7 @@ func init() { // 插件主体
sort.SliceStable(temp, func(i, j int) bool { sort.SliceStable(temp, func(i, j int) bool {
return temp[i].Get("last_sent_time").Int() < temp[j].Get("last_sent_time").Int() return temp[i].Get("last_sent_time").Int() < temp[j].Get("last_sent_time").Int()
}) })
max := func(a, b int) int { temp = temp[data.Max(0, len(temp)-10):]
if a > b {
return a
}
return b
}
temp = temp[max(0, len(temp)-10):]
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
who := temp[rand.Intn(len(temp))] who := temp[rand.Intn(len(temp))]
if who.Get("user_id").Int() == ctx.Event.SelfID { if who.Get("user_id").Int() == ctx.Event.SelfID {

View File

@ -106,7 +106,7 @@ func init() { // 插件主体
var imgtype = ctx.State["regex_matched"].([]string)[1] var imgtype = ctx.State["regex_matched"].([]string)[1]
// 补充池子 // 补充池子
go func() { go func() {
times := min(pool.Max-pool.size(imgtype), 2) times := data.Min(pool.Max-pool.size(imgtype), 2)
for i := 0; i < times; i++ { for i := 0; i < times; i++ {
illust := &pixiv.Illust{} illust := &pixiv.Illust{}
// 查询出一张图片 // 查询出一张图片
@ -122,7 +122,6 @@ func init() { // 插件主体
ctx.SendGroupMessage(pool.Group, []message.MessageSegment{message.Image(file(illust))}) ctx.SendGroupMessage(pool.Group, []message.MessageSegment{message.Image(file(illust))})
// 向缓冲池添加一张图片 // 向缓冲池添加一张图片
pool.push(imgtype, illust) pool.push(imgtype, illust)
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
} }
}() }()
@ -217,18 +216,6 @@ func firstValueInList(list []string) zero.Rule {
} }
} }
// min 返回两数最小值
func min(a, b int) int {
switch {
default:
return a
case a > b:
return b
case a < b:
return a
}
}
// size 返回缓冲池指定类型的现有大小 // size 返回缓冲池指定类型的现有大小
func (p *imgpool) size(imgtype string) int { func (p *imgpool) size(imgtype string) int {
return len(p.Pool[imgtype]) return len(p.Pool[imgtype])