mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-18 20:50:12 +08:00
Some checks failed
打包最新版为 Docker Image / build docker (push) Has been cancelled
最新版 / Build binary CI (386, linux) (push) Has been cancelled
最新版 / Build binary CI (386, windows) (push) Has been cancelled
最新版 / Build binary CI (amd64, linux) (push) Has been cancelled
最新版 / Build binary CI (amd64, windows) (push) Has been cancelled
最新版 / Build binary CI (arm, linux) (push) Has been cancelled
最新版 / Build binary CI (arm64, linux) (push) Has been cancelled
PushLint / lint (push) Has been cancelled
* 🐛 修改听歌问题 * ✨ 添加龙珠聚合搜索 * 🎨 优化聚合搜索 * 🐛 一定能点出歌 * 🎨 删除调试
152 lines
3.2 KiB
Go
152 lines
3.2 KiB
Go
package score
|
|
|
|
import (
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
)
|
|
|
|
// sdb 得分数据库
|
|
var sdb *scoredb
|
|
|
|
// scoredb 分数数据库
|
|
type scoredb struct {
|
|
db *gorm.DB
|
|
scoremu sync.Mutex
|
|
}
|
|
|
|
// scoretable 分数结构体
|
|
type scoretable struct {
|
|
UID int64 `gorm:"column:uid;primary_key"`
|
|
Score int `gorm:"column:score;default:0"`
|
|
}
|
|
|
|
// TableName ...
|
|
func (scoretable) TableName() string {
|
|
return "score"
|
|
}
|
|
|
|
// signintable 签到结构体
|
|
type signintable struct {
|
|
UID int64 `gorm:"column:uid;primary_key"`
|
|
Count int `gorm:"column:count;default:0"`
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
// TableName ...
|
|
func (signintable) TableName() string {
|
|
return "sign_in"
|
|
}
|
|
|
|
// initialize 初始化ScoreDB数据库
|
|
func initialize(dbpath string) *scoredb {
|
|
var err error
|
|
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
|
|
// 生成文件
|
|
f, err := os.Create(dbpath)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
defer f.Close()
|
|
}
|
|
gdb, err := gorm.Open("sqlite3", dbpath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
gdb.AutoMigrate(&scoretable{}).AutoMigrate(&signintable{})
|
|
return &scoredb{
|
|
db: gdb,
|
|
}
|
|
}
|
|
|
|
// Close ...
|
|
func (sdb *scoredb) Close() error {
|
|
db := sdb.db
|
|
return db.Close()
|
|
}
|
|
|
|
// GetScoreByUID 取得分数
|
|
func (sdb *scoredb) GetScoreByUID(uid int64) (s scoretable) {
|
|
sdb.scoremu.Lock()
|
|
defer sdb.scoremu.Unlock()
|
|
db := sdb.db
|
|
db.Model(&scoretable{}).FirstOrCreate(&s, "uid = ? ", uid)
|
|
return s
|
|
}
|
|
|
|
// InsertOrUpdateScoreByUID 插入或更新分数
|
|
func (sdb *scoredb) InsertOrUpdateScoreByUID(uid int64, score int) (err error) {
|
|
sdb.scoremu.Lock()
|
|
defer sdb.scoremu.Unlock()
|
|
db := sdb.db
|
|
s := scoretable{
|
|
UID: uid,
|
|
Score: score,
|
|
}
|
|
if err = db.Model(&scoretable{}).First(&s, "uid = ? ", uid).Error; err != nil {
|
|
// error handling...
|
|
if gorm.IsRecordNotFoundError(err) {
|
|
err = db.Model(&scoretable{}).Create(&s).Error // newUser not user
|
|
}
|
|
} else {
|
|
err = db.Model(&scoretable{}).Where("uid = ? ", uid).Update(
|
|
map[string]any{
|
|
"score": score,
|
|
}).Error
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetSignInByUID 取得签到次数
|
|
func (sdb *scoredb) GetSignInByUID(uid int64) (si signintable) {
|
|
sdb.scoremu.Lock()
|
|
defer sdb.scoremu.Unlock()
|
|
db := sdb.db
|
|
db.Model(&signintable{}).FirstOrCreate(&si, "uid = ? ", uid)
|
|
return si
|
|
}
|
|
|
|
// InsertOrUpdateSignInCountByUID 插入或更新签到次数
|
|
func (sdb *scoredb) InsertOrUpdateSignInCountByUID(uid int64, count int) (err error) {
|
|
sdb.scoremu.Lock()
|
|
defer sdb.scoremu.Unlock()
|
|
db := sdb.db
|
|
si := signintable{
|
|
UID: uid,
|
|
Count: count,
|
|
}
|
|
if err = db.Model(&signintable{}).First(&si, "uid = ? ", uid).Error; err != nil {
|
|
// error handling...
|
|
if gorm.IsRecordNotFoundError(err) {
|
|
err = db.Model(&signintable{}).Create(&si).Error // newUser not user
|
|
}
|
|
} else {
|
|
err = db.Model(&signintable{}).Where("uid = ? ", uid).Update(
|
|
map[string]any{
|
|
"count": count,
|
|
}).Error
|
|
}
|
|
return
|
|
}
|
|
|
|
func (sdb *scoredb) GetScoreRankByTopN(n int) (st []scoretable, err error) {
|
|
sdb.scoremu.Lock()
|
|
defer sdb.scoremu.Unlock()
|
|
db := sdb.db
|
|
err = db.Model(&scoretable{}).Order("score desc").Limit(n).Find(&st).Error
|
|
return
|
|
}
|
|
|
|
type scdata struct {
|
|
drawedfile string
|
|
picfile string
|
|
uid int64
|
|
nickname string
|
|
inc int // 增加币
|
|
score int // 钱包
|
|
level int
|
|
rank int
|
|
}
|