diff --git a/data/math.go b/data/math.go new file mode 100644 index 0000000000..0ebb92aa81 --- /dev/null +++ b/data/math.go @@ -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 +} diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index 59ff3c95d7..b38d4c7376 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -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) @@ -273,7 +254,7 @@ 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) } @@ -281,7 +262,7 @@ func draw(background, title, text string) ([]byte, error) { 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: @@ -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 +} diff --git a/plugin_lolicon/lolicon.go b/plugin_lolicon/lolicon.go index 72fd761d21..9c0389274d 100644 --- a/plugin_lolicon/lolicon.go +++ b/plugin_lolicon/lolicon.go @@ -11,6 +11,7 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" + "github.com/FloatTech/ZeroBot-Plugin/data" ) const ( @@ -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)) @@ -60,10 +61,3 @@ func init() { } }) } - -func min(a, b int) int { - if a < b { - return a - } - return b -} diff --git a/plugin_manager/manager.go b/plugin_manager/manager.go index 6da0a58de0..ab6bf78ec4 100644 --- a/plugin_manager/manager.go +++ b/plugin_manager/manager.go @@ -16,6 +16,8 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" timer "github.com/FloatTech/ZeroBot-Plugin-Timer" + + "github.com/FloatTech/ZeroBot-Plugin/data" ) const ( @@ -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 { diff --git a/plugin_setutime/setu_geter.go b/plugin_setutime/setu_geter.go index 6f126d6c36..6e18303185 100644 --- a/plugin_setutime/setu_geter.go +++ b/plugin_setutime/setu_geter.go @@ -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{} // 查询出一张图片 @@ -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) } }() @@ -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])