mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 02:00:24 +00:00
🎨 🚚 move zbpctrl into zbputils
This commit is contained in:
@@ -14,8 +14,8 @@ const (
|
||||
dbfile = dbpath + "score.db"
|
||||
)
|
||||
|
||||
// SDB 得分数据库
|
||||
var SDB *DB
|
||||
// sdb 得分数据库
|
||||
var sdb *scoredb
|
||||
|
||||
// 加载数据库
|
||||
func init() {
|
||||
@@ -24,7 +24,7 @@ func init() {
|
||||
_ = os.MkdirAll(dbpath, 0755)
|
||||
os.RemoveAll(cachePath)
|
||||
_ = os.MkdirAll(cachePath, 0755)
|
||||
SDB = Initialize(dbfile)
|
||||
sdb = initialize(dbfile)
|
||||
log.Println("[score]加载score数据库")
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -8,34 +8,34 @@ import (
|
||||
_ "github.com/logoove/sqlite" // import sql
|
||||
)
|
||||
|
||||
// DB 分数数据库
|
||||
type DB gorm.DB
|
||||
// scoredb 分数数据库
|
||||
type scoredb gorm.DB
|
||||
|
||||
// Score 分数结构体
|
||||
type Score struct {
|
||||
// scoretable 分数结构体
|
||||
type scoretable struct {
|
||||
UID int64 `gorm:"column:uid;primary_key"`
|
||||
Score int `gorm:"column:score;default:0"`
|
||||
}
|
||||
|
||||
// TableName ...
|
||||
func (Score) TableName() string {
|
||||
func (scoretable) TableName() string {
|
||||
return "score"
|
||||
}
|
||||
|
||||
// SignIn 签到结构体
|
||||
type SignIn struct {
|
||||
// signintable 签到结构体
|
||||
type signintable struct {
|
||||
UID int64 `gorm:"column:uid;primary_key"`
|
||||
Count int `gorm:"column:count;default:0"`
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// TableName ...
|
||||
func (SignIn) TableName() string {
|
||||
func (signintable) TableName() string {
|
||||
return "sign_in"
|
||||
}
|
||||
|
||||
// Initialize 初始化ScoreDB数据库
|
||||
func Initialize(dbpath string) *DB {
|
||||
// initialize 初始化ScoreDB数据库
|
||||
func initialize(dbpath string) *scoredb {
|
||||
var err error
|
||||
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
|
||||
// 生成文件
|
||||
@@ -49,46 +49,37 @@ func Initialize(dbpath string) *DB {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
gdb.AutoMigrate(&Score{}).AutoMigrate(&SignIn{})
|
||||
return (*DB)(gdb)
|
||||
}
|
||||
|
||||
// Open ...
|
||||
func Open(dbpath string) (*DB, error) {
|
||||
db, err := gorm.Open("sqlite3", dbpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return (*DB)(db), nil
|
||||
gdb.AutoMigrate(&scoretable{}).AutoMigrate(&signintable{})
|
||||
return (*scoredb)(gdb)
|
||||
}
|
||||
|
||||
// Close ...
|
||||
func (sdb *DB) Close() error {
|
||||
func (sdb *scoredb) Close() error {
|
||||
db := (*gorm.DB)(sdb)
|
||||
return db.Close()
|
||||
}
|
||||
|
||||
// GetScoreByUID 取得分数
|
||||
func (sdb *DB) GetScoreByUID(uid int64) (s Score) {
|
||||
func (sdb *scoredb) GetScoreByUID(uid int64) (s scoretable) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
db.Debug().Model(&Score{}).FirstOrCreate(&s, "uid = ? ", uid)
|
||||
db.Debug().Model(&scoretable{}).FirstOrCreate(&s, "uid = ? ", uid)
|
||||
return s
|
||||
}
|
||||
|
||||
// InsertOrUpdateScoreByUID 插入或更新分数
|
||||
func (sdb *DB) InsertOrUpdateScoreByUID(uid int64, score int) (err error) {
|
||||
func (sdb *scoredb) InsertOrUpdateScoreByUID(uid int64, score int) (err error) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
s := Score{
|
||||
s := scoretable{
|
||||
UID: uid,
|
||||
Score: score,
|
||||
}
|
||||
if err = db.Debug().Model(&Score{}).First(&s, "uid = ? ", uid).Error; err != nil {
|
||||
if err = db.Debug().Model(&scoretable{}).First(&s, "uid = ? ", uid).Error; err != nil {
|
||||
// error handling...
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
db.Debug().Model(&Score{}).Create(&s) // newUser not user
|
||||
db.Debug().Model(&scoretable{}).Create(&s) // newUser not user
|
||||
}
|
||||
} else {
|
||||
err = db.Debug().Model(&Score{}).Where("uid = ? ", uid).Update(
|
||||
err = db.Debug().Model(&scoretable{}).Where("uid = ? ", uid).Update(
|
||||
map[string]interface{}{
|
||||
"score": score,
|
||||
}).Error
|
||||
@@ -97,26 +88,26 @@ func (sdb *DB) InsertOrUpdateScoreByUID(uid int64, score int) (err error) {
|
||||
}
|
||||
|
||||
// GetSignInByUID 取得签到次数
|
||||
func (sdb *DB) GetSignInByUID(uid int64) (si SignIn) {
|
||||
func (sdb *scoredb) GetSignInByUID(uid int64) (si signintable) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
db.Debug().Model(&SignIn{}).FirstOrCreate(&si, "uid = ? ", uid)
|
||||
db.Debug().Model(&signintable{}).FirstOrCreate(&si, "uid = ? ", uid)
|
||||
return si
|
||||
}
|
||||
|
||||
// InsertOrUpdateSignInCountByUID 插入或更新签到次数
|
||||
func (sdb *DB) InsertOrUpdateSignInCountByUID(uid int64, count int) (err error) {
|
||||
func (sdb *scoredb) InsertOrUpdateSignInCountByUID(uid int64, count int) (err error) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
si := SignIn{
|
||||
si := signintable{
|
||||
UID: uid,
|
||||
Count: count,
|
||||
}
|
||||
if err = db.Debug().Model(&SignIn{}).First(&si, "uid = ? ", uid).Error; err != nil {
|
||||
if err = db.Debug().Model(&signintable{}).First(&si, "uid = ? ", uid).Error; err != nil {
|
||||
// error handling...
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
db.Debug().Model(&SignIn{}).Create(&si) // newUser not user
|
||||
db.Debug().Model(&signintable{}).Create(&si) // newUser not user
|
||||
}
|
||||
} else {
|
||||
err = db.Debug().Model(&SignIn{}).Where("uid = ? ", uid).Update(
|
||||
err = db.Debug().Model(&signintable{}).Where("uid = ? ", uid).Update(
|
||||
map[string]interface{}{
|
||||
"count": count,
|
||||
}).Error
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/fogleman/gg"
|
||||
@@ -15,7 +14,7 @@ import (
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
"github.com/wdvxdr1123/ZeroBot/utils/helper"
|
||||
|
||||
control "github.com/FloatTech/zbpctrl"
|
||||
control "github.com/FloatTech/zbputils/control"
|
||||
"github.com/FloatTech/zbputils/ctxext"
|
||||
"github.com/FloatTech/zbputils/file"
|
||||
"github.com/FloatTech/zbputils/txt2img"
|
||||
@@ -28,8 +27,8 @@ const (
|
||||
referer = "https://iw233.cn/main.html"
|
||||
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
|
||||
signinMax = 1
|
||||
// ScoreMax 分数上限定为120
|
||||
ScoreMax = 120
|
||||
// SCOREMAX 分数上限定为120
|
||||
SCOREMAX = 120
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -38,8 +37,6 @@ var (
|
||||
Help: "签到得分\n- 签到\n- 获得签到背景[@xxx]|获得签到背景",
|
||||
})
|
||||
levelArray = [...]int{0, 1, 2, 5, 10, 20, 35, 55, 75, 100, 120}
|
||||
// 下载锁
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -48,16 +45,10 @@ func init() {
|
||||
uid := ctx.Event.UserID
|
||||
now := time.Now()
|
||||
today := now.Format("20060102")
|
||||
si := SDB.GetSignInByUID(uid)
|
||||
picFile := cachePath + strconv.FormatInt(uid, 10) + today + ".png"
|
||||
if file.IsNotExist(picFile) {
|
||||
mu.Lock()
|
||||
initPic(picFile)
|
||||
mu.Unlock()
|
||||
}
|
||||
si := sdb.GetSignInByUID(uid)
|
||||
siUpdateTimeStr := si.UpdatedAt.Format("20060102")
|
||||
if siUpdateTimeStr != today {
|
||||
if err := SDB.InsertOrUpdateSignInCountByUID(uid, 0); err != nil {
|
||||
if err := sdb.InsertOrUpdateSignInCountByUID(uid, 0); err != nil {
|
||||
log.Errorln("[score]:", err)
|
||||
}
|
||||
}
|
||||
@@ -65,7 +56,11 @@ func init() {
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("今天你已经签到过了!"))
|
||||
return
|
||||
}
|
||||
if err := SDB.InsertOrUpdateSignInCountByUID(uid, si.Count+1); err != nil {
|
||||
|
||||
picFile := cachePath + strconv.FormatInt(uid, 10) + today + ".png"
|
||||
initPic(picFile)
|
||||
|
||||
if err := sdb.InsertOrUpdateSignInCountByUID(uid, si.Count+1); err != nil {
|
||||
log.Errorln("[score]:", err)
|
||||
}
|
||||
back, err := gg.LoadImage(picFile)
|
||||
@@ -91,13 +86,13 @@ func init() {
|
||||
}
|
||||
add := 1
|
||||
canvas.DrawString(nickName+fmt.Sprintf(" 小熊饼干+%d", add), float64(back.Bounds().Size().X)*0.1, float64(back.Bounds().Size().Y)*1.3)
|
||||
score := SDB.GetScoreByUID(uid).Score
|
||||
score := sdb.GetScoreByUID(uid).Score
|
||||
score += add
|
||||
if score > ScoreMax {
|
||||
score = ScoreMax
|
||||
if score > SCOREMAX {
|
||||
score = SCOREMAX
|
||||
ctx.SendChain(message.At(uid), message.Text("你获得的小熊饼干已经达到上限"))
|
||||
}
|
||||
if err := SDB.InsertOrUpdateScoreByUID(uid, score); err != nil {
|
||||
if err := sdb.InsertOrUpdateScoreByUID(uid, score); err != nil {
|
||||
log.Println("[score]:", err)
|
||||
}
|
||||
level := getLevel(score)
|
||||
@@ -110,14 +105,14 @@ func init() {
|
||||
if level < 10 {
|
||||
nextLevelScore = levelArray[level+1]
|
||||
} else {
|
||||
nextLevelScore = ScoreMax
|
||||
nextLevelScore = SCOREMAX
|
||||
}
|
||||
canvas.SetRGB255(0, 0, 0)
|
||||
canvas.DrawRectangle(float64(back.Bounds().Size().X)*0.1, float64(back.Bounds().Size().Y)*1.55, float64(back.Bounds().Size().X)*0.6*float64(score)/float64(nextLevelScore), float64(back.Bounds().Size().Y)*0.1)
|
||||
canvas.SetRGB255(102, 102, 102)
|
||||
canvas.Fill()
|
||||
canvas.DrawString(fmt.Sprintf("%d/%d", score, nextLevelScore), float64(back.Bounds().Size().X)*0.75, float64(back.Bounds().Size().Y)*1.62)
|
||||
canvasBase64, err := txt2img.CanvasToBase64(canvas)
|
||||
canvasBase64, err := txt2img.TxtCanvas{canvas}.ToBase64()
|
||||
if err != nil {
|
||||
log.Println("[score]:", err)
|
||||
}
|
||||
@@ -134,9 +129,8 @@ func init() {
|
||||
}
|
||||
picFile := cachePath + uidStr + time.Now().Format("20060102") + ".png"
|
||||
if file.IsNotExist(picFile) {
|
||||
mu.Lock()
|
||||
initPic(picFile)
|
||||
mu.Unlock()
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请先签到!"))
|
||||
return
|
||||
}
|
||||
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + picFile))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user