Skip to content

Commit

Permalink
✏️ 收集 max / min 到 data 包
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Oct 11, 2021
1 parent d89e7ae commit 4373572
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
17 changes: 17 additions & 0 deletions data/math.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 17 additions & 21 deletions plugin_fortune/fortune.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,25 +246,6 @@ func draw(background, title, text string) ([]byte, error) {
if err := canvas.LoadFontFace(base+"sakura.ttf", 23); err != nil {
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 = tw+10, th+10
r := []rune(text)
Expand All @@ -273,15 +254,15 @@ func draw(background, title, text string) ([]byte, error) {
default:
for i, o := range r {
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
canvas.DrawString(string(o), -offest(xsum, xnow, tw)+115, offest(ysum, ynow, th)+320.0)
}
case 2:
div := rowsnum(len(r), 2)
for i, o := range r {
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
switch xnow {
case 1:
Expand All @@ -303,3 +284,18 @@ func draw(background, title, text string) ([]byte, error) {
encoder.Close()
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
}
10 changes: 2 additions & 8 deletions plugin_lolicon/lolicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/wdvxdr1123/ZeroBot/message"

"github.com/FloatTech/ZeroBot-Plugin/control"
"github.com/FloatTech/ZeroBot-Plugin/data"
)

const (
Expand All @@ -30,7 +31,7 @@ func init() {
}).OnFullMatch("来份萝莉").SetBlock(true).
Handle(func(ctx *zero.Ctx) {
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)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
Expand Down Expand Up @@ -60,10 +61,3 @@ func init() {
}
})
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
10 changes: 3 additions & 7 deletions plugin_manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/wdvxdr1123/ZeroBot/message"

timer "github.com/FloatTech/ZeroBot-Plugin-Timer"

"github.com/FloatTech/ZeroBot-Plugin/data"
)

const (
Expand Down Expand Up @@ -300,13 +302,7 @@ func init() { // 插件主体
sort.SliceStable(temp, func(i, j int) bool {
return temp[i].Get("last_sent_time").Int() < temp[j].Get("last_sent_time").Int()
})
max := func(a, b int) int {
if a > b {
return a
}
return b
}
temp = temp[max(0, len(temp)-10):]
temp = temp[data.Max(0, len(temp)-10):]
rand.Seed(time.Now().UnixNano())
who := temp[rand.Intn(len(temp))]
if who.Get("user_id").Int() == ctx.Event.SelfID {
Expand Down
15 changes: 1 addition & 14 deletions plugin_setutime/setu_geter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func init() { // 插件主体
var imgtype = ctx.State["regex_matched"].([]string)[1]
// 补充池子
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++ {
illust := &pixiv.Illust{}
// 查询出一张图片
Expand All @@ -122,7 +122,6 @@ func init() { // 插件主体
ctx.SendGroupMessage(pool.Group, []message.MessageSegment{message.Image(file(illust))})
// 向缓冲池添加一张图片
pool.push(imgtype, illust)

time.Sleep(time.Second * 1)
}
}()
Expand Down Expand Up @@ -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 返回缓冲池指定类型的现有大小
func (p *imgpool) size(imgtype string) int {
return len(p.Pool[imgtype])
Expand Down

0 comments on commit 4373572

Please sign in to comment.