✏️ 分离 data

This commit is contained in:
fumiama 2021-10-14 23:07:13 +08:00
parent 98b21d0fdf
commit bdffbfab67
8 changed files with 24 additions and 20 deletions

View File

@ -11,11 +11,11 @@ import (
"github.com/wdvxdr1123/ZeroBot/extension"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/data"
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
)
var (
db = &data.Sqlite{DBPath: "data/control/plugins.db"}
db = &sql.Sqlite{DBPath: "data/control/plugins.db"}
// managers 每个插件对应的管理
managers = map[string]*Control{}
mu = sync.RWMutex{}

View File

@ -20,7 +20,8 @@ import (
"github.com/wdvxdr1123/ZeroBot/utils/helper"
"github.com/FloatTech/ZeroBot-Plugin/control"
"github.com/FloatTech/ZeroBot-Plugin/data"
"github.com/FloatTech/ZeroBot-Plugin/utils/dl"
"github.com/FloatTech/ZeroBot-Plugin/utils/math"
)
var (
@ -76,7 +77,7 @@ func init() {
mikuji := base + "运势签文.json"
if _, err := os.Stat(mikuji); err != nil && !os.IsExist(err) {
ctx.SendChain(message.Text("正在下载签文文件,请稍后..."))
err := data.DownloadTo(site+"运势签文.json", mikuji)
err := dl.DownloadTo(site+"运势签文.json", mikuji)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
@ -87,7 +88,7 @@ func init() {
ttf := base + "sakura.ttf"
if _, err := os.Stat(ttf); err != nil && !os.IsExist(err) {
ctx.SendChain(message.Text("正在下载字体文件,请稍后..."))
err := data.DownloadTo(site+"sakura.ttf", ttf)
err := dl.DownloadTo(site+"sakura.ttf", ttf)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
@ -109,7 +110,7 @@ func init() {
if _, err := os.Stat(folder); err != nil && !os.IsExist(err) {
ctx.SendChain(message.Text("正在下载背景图片,请稍后..."))
zipfile := kind + ".zip"
err := data.DownloadTo(site+zipfile, zipfile)
err := dl.DownloadTo(site+zipfile, zipfile)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
@ -255,7 +256,7 @@ func draw(background, title, text string) ([]byte, error) {
default:
for i, o := range r {
xnow := rowsnum(i+1, 9)
ysum := data.Min(len(r)-(xnow-1)*9, 9)
ysum := math.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)
}
@ -263,7 +264,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 := data.Min(len(r)-(xnow-1)*div, div)
ysum := math.Min(len(r)-(xnow-1)*div, div)
ynow := i%div + 1
switch xnow {
case 1:

View File

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

View File

@ -17,7 +17,7 @@ import (
timer "github.com/FloatTech/ZeroBot-Plugin-Timer"
"github.com/FloatTech/ZeroBot-Plugin/data"
"github.com/FloatTech/ZeroBot-Plugin/utils/math"
)
const (
@ -302,7 +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()
})
temp = temp[data.Max(0, len(temp)-10):]
temp = temp[math.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 {

View File

@ -18,13 +18,14 @@ import (
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/control"
"github.com/FloatTech/ZeroBot-Plugin/data"
"github.com/FloatTech/ZeroBot-Plugin/utils/math"
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
)
// Pools 图片缓冲池
type imgpool struct {
Lock sync.Mutex
DB *data.Sqlite
DB *sql.Sqlite
Path string
Group int64
List []string
@ -40,7 +41,7 @@ const (
// NewPoolsCache 返回一个缓冲池对象
func newPools() *imgpool {
cache := &imgpool{
DB: &data.Sqlite{DBPath: "data/SetuTime/SetuTime.db"},
DB: &sql.Sqlite{DBPath: "data/SetuTime/SetuTime.db"},
Path: "data/SetuTime/cache/",
Group: 0,
List: []string{"涩图", "二次元", "风景", "车万"}, // 可以自己加类别,得自己加图片进数据库
@ -106,7 +107,7 @@ func init() { // 插件主体
var imgtype = ctx.State["regex_matched"].([]string)[1]
// 补充池子
go func() {
times := data.Min(pool.Max-pool.size(imgtype), 2)
times := math.Min(pool.Max-pool.size(imgtype), 2)
for i := 0; i < times; i++ {
illust := &pixiv.Illust{}
// 查询出一张图片

View File

@ -1,4 +1,5 @@
package data
// Package dl 下载实用工具
package dl
import (
"io"

View File

@ -1,4 +1,5 @@
package data
// Package math 计算实用工具
package math
// min 返回两数最大值,该函数将被内联
func Max(a, b int) int {

View File

@ -1,5 +1,5 @@
// Package data 数据库/数据处理相关工具
package data
// Package sql 数据库/数据处理相关工具
package sql
import (
"database/sql"