Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add:封装niuniu的数据库操作 #61

Merged
merged 29 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ QQ空间API
文本理解与创作
## yandex
Yandex 搜图
## niuniu
牛牛大作战数据库系统
115 changes: 115 additions & 0 deletions niuniu/niuniu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package niuniu

import (
"github.com/FloatTech/floatbox/file"
sql "github.com/FloatTech/sqlite"
"math/rand"
"os"
"strconv"
"sync"
"time"
)

type model struct {
sql sql.Sqlite
sync.RWMutex
}

type UserInfo struct {
UID int64
Length float64
UserCount int
WeiGe int // 伟哥
Philter int // 媚药
Artifact int // 击剑神器
ShenJi int // 击剑神稽
Buff1 int // 暂定
Buff2 int // 暂定
Buff3 int // 暂定
Buff4 int // 暂定
Buff5 int // 暂定
}

var (
db = &model{}
)

func init() {
if file.IsNotExist("data/niuniu") {
err := os.MkdirAll("data/niuniu", 0755)
if err != nil {
panic(err)
}
}
err := db.sql.Open(time.Hour * 24)
if err != nil {
panic(err)
}
}

// GetInitNiuNiu ...
func GetInitNiuNiu() float64 {
return db.randLength()
}

// CreateGIDTable 创建一个表格
func CreateGIDTable(gid int64) error {
return db.createGIDTable(gid)
}

// FindNiuNiuInfo 查询一个NiuNiu数据
func FindNiuNiuInfo(gid, uid int64) (*UserInfo, error) {
return db.findNiuNiu(gid, uid)
}

// InsertNiuNiuInfo 修改NiuNiu数据
func InsertNiuNiuInfo(gid int64, u *UserInfo) error {
return db.insertNiuNiu(u, gid)
}

// DeleteNiuNiu 删除一个NiuNiu
func DeleteNiuNiu(gid, uid int64) error {
return db.deleteNiuNiu(gid, uid)
}

// GetAllNiuNiuInfo 获取当前群组所有NiuNiu数据
func GetAllNiuNiuInfo(gid int64) ([]*UserInfo, error) {
return db.readAllTable(gid)
}

func (db *model) randLength() float64 {
return float64(rand.Intn(9)+1) + (float64(rand.Intn(100)) / 100)
}

func (db *model) createGIDTable(gid int64) error {
db.Lock()
defer db.Unlock()
return db.sql.Create(strconv.FormatInt(gid, 10), &UserInfo{})
}

func (db *model) findNiuNiu(gid, uid int64) (*UserInfo, error) {
db.RLock()
defer db.RUnlock()
u := UserInfo{}
err := db.sql.Find(strconv.FormatInt(gid, 10), &u, "where UID = "+strconv.FormatInt(uid, 10))
return &u, err
}

func (db *model) insertNiuNiu(u *UserInfo, gid int64) error {
db.Lock()
defer db.Unlock()
return db.sql.Insert(strconv.FormatInt(gid, 10), u)
}

func (db *model) deleteNiuNiu(gid, uid int64) error {
db.Lock()
defer db.Unlock()
return db.sql.Del(strconv.FormatInt(gid, 10), "where UID = "+strconv.FormatInt(uid, 10))
}

func (db *model) readAllTable(gid int64) ([]*UserInfo, error) {
db.Lock()
defer db.Unlock()
a, err := sql.FindAll[UserInfo](&db.sql, strconv.FormatInt(gid, 10), "where UserCount = 0")
return a, err
}