-
Notifications
You must be signed in to change notification settings - Fork 31
add:封装niuniu的数据库操作 #61
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
ae6386f
add:封装niuniu的数据库操作
xyy0411 41713cc
修改了一些readme
xyy0411 611a675
Delete niuniu directory
xyy0411 661e39d
Merge branch 'FloatTech:main' into pr
xyy0411 12c9bbf
Add files via upload
xyy0411 c17c672
Merge pull request #1 from xyy0411/xyy0411-patch-1
xyy0411 2807d39
Update README.md
xyy0411 928d366
Update main.go
xyy0411 8f652a3
Update models.go
xyy0411 67ee52b
Create go.yml
xyy0411 bbf8867
fix
xyy0411 e884c0b
fix:部分函数名
xyy0411 8576a84
fix
xyy0411 1aed663
fix
xyy0411 2b3b507
fix:修复一些问题
xyy0411 3829811
fix
xyy0411 3aaf8f8
delete getGold()
xyy0411 04f5c88
fix:要求更改的地方
xyy0411 eedb4f2
Merge remote-tracking branch 'origin/pr' into pr
xyy0411 3f7a731
fix:将错误设置为全局变量
xyy0411 665c33e
加锁,还原go.mod
xyy0411 8451406
Update go.sum
xyy0411 c4f8ed8
Update go.mod
xyy0411 2c67a28
fix:函数名:createUserInfoByProps -> applyProp
xyy0411 f83540c
Merge remote-tracking branch 'origin/pr' into pr
xyy0411 739fa17
fix:变量名 lock -> globalLock
xyy0411 4ff909d
修lint
xyy0411 72ce32b
fix
xyy0411 b41584f
修lint
xyy0411 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,3 +40,5 @@ QQ空间API | |
| 文本理解与创作 | ||
| ## yandex | ||
| Yandex 搜图 | ||
| ## niuniu | ||
| 牛牛大作战数据库系统 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
fumiama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return db.randLength() | ||
fumiama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // CreateGIDTable 创建一个表格 | ||
| func CreateGIDTable(gid int64) error { | ||
fumiama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return db.createGIDTable(gid) | ||
| } | ||
|
|
||
| // FindNiuNiuInfo 查询一个NiuNiu数据 | ||
| func FindNiuNiuInfo(gid, uid int64) (*UserInfo, error) { | ||
fumiama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return db.findNiuNiu(gid, uid) | ||
| } | ||
|
|
||
| // InsertNiuNiuInfo 修改NiuNiu数据 | ||
| func InsertNiuNiuInfo(gid int64, u *UserInfo) error { | ||
fumiama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return db.insertNiuNiu(u, gid) | ||
| } | ||
|
|
||
| // DeleteNiuNiu 删除一个NiuNiu | ||
| func DeleteNiuNiu(gid, uid int64) error { | ||
fumiama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return db.deleteNiuNiu(gid, uid) | ||
| } | ||
|
|
||
| // GetAllNiuNiuInfo 获取当前群组所有NiuNiu数据 | ||
| func GetAllNiuNiuInfo(gid int64) ([]*UserInfo, error) { | ||
fumiama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 { | ||
fumiama marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.