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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
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 @@ -16,6 +16,8 @@ huggingface API
二次元 AI tag 作画
## nsfw
图片合规性审查
## niu
niu
## pixiv
P站解析与图片下载
## qzone
Expand Down
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.20

require (
github.com/FloatTech/floatbox v0.0.0-20241106130736-5aea0a935024
github.com/FloatTech/rendercard v0.2.0
github.com/FloatTech/sqlite v1.7.0
github.com/FloatTech/zbputils v1.7.1
fumiama marked this conversation as resolved.
Show resolved Hide resolved
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
github.com/antchfx/htmlquery v1.3.3
github.com/corona10/goimagehash v1.1.0
Expand All @@ -15,18 +17,26 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.18.0
github.com/wdvxdr1123/ZeroBot v1.8.0
fumiama marked this conversation as resolved.
Show resolved Hide resolved
golang.org/x/image v0.21.0
)

require (
github.com/FloatTech/gg v1.1.3 // indirect
fumiama marked this conversation as resolved.
Show resolved Hide resolved
github.com/FloatTech/imgfactory v0.2.2-0.20230315152233-49741fc994f9 // indirect
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 // indirect
github.com/FloatTech/zbpctrl v1.6.1 // indirect
github.com/antchfx/xpath v1.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4 // indirect
github.com/fumiama/cron v1.3.0 // indirect
github.com/fumiama/go-registry v0.2.7 // indirect
github.com/fumiama/go-simple-protobuf v0.2.0 // indirect
github.com/fumiama/gofastTEA v0.0.10 // indirect
github.com/fumiama/imgsz v0.0.2 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -37,7 +47,6 @@ require (
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/wdvxdr1123/ZeroBot v1.8.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
github.com/FloatTech/floatbox v0.0.0-20241106130736-5aea0a935024 h1:mrvWpiwfRklt9AyiQjKgDGJjf4YL6FZ3yC+ydbkuF2o=
github.com/FloatTech/floatbox v0.0.0-20241106130736-5aea0a935024/go.mod h1:+P3hs+Cvl10/Aj3SNE96TuBvKAXCe+XD1pKphTZyiwk=
github.com/FloatTech/gg v1.1.3 h1:+GlL02lTKsxJQr4WCuNwVxC1/eBZrCvypCIBtxuOFb4=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

别改无用的地方。

github.com/FloatTech/gg v1.1.3/go.mod h1:/9oLP54CMfq4r+71XL26uaFTJ1uL1boAyX67680/1HE=
github.com/FloatTech/imgfactory v0.2.2-0.20230315152233-49741fc994f9 h1:IzZLuM/fgKclyMaU/Qb1qlLdGrs2FTietkqOWhh07Gw=
github.com/FloatTech/imgfactory v0.2.2-0.20230315152233-49741fc994f9/go.mod h1:el5hGpj1C1bDRxcTXYRwEivDCr40zZeJpcrLrB1fajs=
github.com/FloatTech/rendercard v0.2.0 h1:PBTZ2gCEy/dAEGSfWecrGTrWDYpiBJD1dVzNDDaOxh4=
github.com/FloatTech/rendercard v0.2.0/go.mod h1:Sbojcy1t3NfFz7/WicZRmR/uKFxNMYkKF8qHx69dxY0=
github.com/FloatTech/sqlite v1.7.0 h1:FGSn4pCR12kESozn7IvNx3U39dwR/AcFM9oPyGACsl0=
github.com/FloatTech/sqlite v1.7.0/go.mod h1:/4tzfCGhrZnnjC1U8vcfwGQeF6eR649fhOsS3+Le0+s=
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 h1:snfw7FNFym1eNnLrQ/VCf80LiQo9C7jHgrunZDwiRcY=
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
github.com/FloatTech/zbpctrl v1.6.1 h1:SilK5R2poO8iUT6JPzpgr/BSzxYAaybBYNZkMyZ8STw=
github.com/FloatTech/zbpctrl v1.6.1/go.mod h1:I+MetM++1sJhNPg3zww1aw04BicYsNohvHC4Jh52XSo=
github.com/FloatTech/zbputils v1.7.1 h1:3/VppB4NEeT0CPoprmjXvv3G6rXkegvuB3byh4p3skQ=
github.com/FloatTech/zbputils v1.7.1/go.mod h1:nHWYtF4g2NRv3GXZiAZDvgPjdcHGUaQHxGgD0aHz30I=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
github.com/antchfx/htmlquery v1.3.3 h1:x6tVzrRhVNfECDaVxnZi1mEGrQg3mjE/rxbH2Pe6dNE=
Expand All @@ -15,8 +25,12 @@ github.com/corona10/goimagehash v1.1.0/go.mod h1:VkvE0mLn84L4aF8vCb6mafVajEb6QYM
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4 h1:BBade+JlV/f7JstZ4pitd4tHhpN+w+6I+LyOS7B4fyU=
github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4/go.mod h1:H7chHJglrhPPzetLdzBleF8d22WYOv7UM/lEKYiwlKM=
github.com/fumiama/cron v1.3.0 h1:ZWlwuexF+HQHl3cYytEE5HNwD99q+3vNZF1GrEiXCFo=
github.com/fumiama/cron v1.3.0/go.mod h1:bz5Izvgi/xEUI8tlBN8BI2jr9Moo8N4or0KV8xXuPDY=
github.com/fumiama/go-base16384 v1.7.0 h1:6fep7XPQWxRlh4Hu+KsdH+6+YdUp+w6CwRXtMWSsXCA=
Expand All @@ -27,12 +41,17 @@ github.com/fumiama/go-simple-protobuf v0.2.0 h1:ACyN1MAlu7pDR3EszWgzUeNP+IRsSHwH
github.com/fumiama/go-simple-protobuf v0.2.0/go.mod h1:5yYNapXq1tQMOZg9bOIVhQlZk9pQqpuFIO4DZLbsdy4=
github.com/fumiama/gofastTEA v0.0.10 h1:JJJ+brWD4kie+mmK2TkspDXKzqq0IjXm89aGYfoGhhQ=
github.com/fumiama/gofastTEA v0.0.10/go.mod h1:RIdbYZyB4MbH6ZBlPymRaXn3cD6SedlCu5W/HHfMPBk=
github.com/fumiama/imgsz v0.0.2 h1:fAkC0FnIscdKOXwAxlyw3EUba5NzxZdSxGaq3Uyfxak=
github.com/fumiama/imgsz v0.0.2/go.mod h1:dR71mI3I2O5u6+PCpd47M9TZptzP+39tRBcbdIkoqM4=
github.com/fumiama/libc v0.0.0-20240530081950-6f6d8586b5c5 h1:jDxsIupsT84A6WHcs6kWbst+KqrRQ8/o0VyoFMnbBOA=
github.com/fumiama/libc v0.0.0-20240530081950-6f6d8586b5c5/go.mod h1:15P6ublJ9FJR8YQCGy8DeQ2Uwur7iW9Hserr/T3OFZE=
github.com/fumiama/sqlite3 v1.29.10-simp h1:c5y3uKyU0q9t0/SyfynzYyuslQ5zP+5CD8e0yYY554A=
github.com/fumiama/sqlite3 v1.29.10-simp/go.mod h1:ItX2a1OVGgNsFh6Dv60JQvGfJfTPHPVpV6DF59akYOA=
github.com/fumiama/terasu v0.0.0-20241027183601-987ab91031ce h1:T6iDDU16rFyxV/FwfJJR6qcgkIlXJEIFlUTSmTD1h6s=
github.com/fumiama/terasu v0.0.0-20241027183601-987ab91031ce/go.mod h1:UVx8YP1jKKL1Cj+uy+OnQRM2Ih6U36Mqy9GSf7jabsI=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down Expand Up @@ -71,6 +90,7 @@ github.com/wdvxdr1123/ZeroBot v1.8.0/go.mod h1:C86nQ0gIdAri4K2vg8IIQIslt08zzrKMc
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down
219 changes: 219 additions & 0 deletions niu/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package niu

import (
"errors"
"fmt"
"github.com/FloatTech/AnimeAPI/wallet"
"github.com/FloatTech/floatbox/file"
zero "github.com/wdvxdr1123/ZeroBot"
"os"
"strconv"
"strings"
"time"
)

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)
}
}

// SetWordNiuNiu length > 0 就增加 , length < 0 就减小
func SetWordNiuNiu(gid, uid int64, length float64) error {
niu, err := db.getWordNiuNiu(gid, uid)
fumiama marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
niu.Length += length
return db.setWordNiuNiu(gid, niu)
}

func GetWordNiuNiu(gid, uid int64) (float64, error) {
niu, err := db.getWordNiuNiu(gid, uid)
fumiama marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, err
}
return niu.Length, nil
}

func DeleteWordNiuNiu(gid, uid int64) error {
return db.deleteWordNiuNiu(gid, uid)
}

func GetAllLengthOfGroup(gid int64) ([]float64, error) {
niuOfGroup, err := db.getAllNiuNiuOfGroup(gid)
if err != nil {
return nil, err
}
f := make([]float64, len(niuOfGroup))
for k, v := range niuOfGroup {
f[k] = v.Length
}
return f, nil
}

func LengthRanking(gid int64, ctx *zero.Ctx) ([]byte, error) {
niuniuList, err := db.getAllNiuNiuOfGroup(gid)
if err != nil {
return nil, err
}
m := niuniuList.negative()
if m == nil {
return nil, errors.New("暂时没有男孩子哦")
}
m.sort(true)
buf, err := m.setupDrawList(ctx, true)
return buf, err
}

func DepthRanking(gid int64, ctx *zero.Ctx) ([]byte, error) {
niuniuList, err := db.getAllNiuNiuOfGroup(gid)
if err != nil {
return nil, err
}
m := niuniuList.positive()
if m == nil {
return nil, errors.New("暂时没有女孩子哦")
}
m.sort(false)
buf, err := m.setupDrawList(ctx, false)
return buf, err
}

func View(gid, uid int64, name string) (*strings.Builder, error) {
i, err := db.getWordNiuNiu(gid, uid)
if err != nil {
return nil, errors.New("你还没有牛牛呢不能查看")
}
niuniu := i.Length
var result strings.Builder
sexLong := "长"
sex := "♂️"
if niuniu < 0 {
sexLong = "深"
sex = "♀️"
}
niuniuList, err := db.getAllNiuNiuOfGroup(gid)
if err != nil {
return nil, err
}
result.WriteString(fmt.Sprintf("\n📛%s<%s>的牛牛信息\n⭕性别:%s\n⭕%s度:%.2fcm\n⭕排行:%d\n⭕%s ",
name, strconv.FormatInt(uid, 10),
sex, sexLong, niuniu, niuniuList.ranking(niuniu, uid), generateRandomString(niuniu)))
return &result, nil
}

func ProcessHitGlue(gid, uid int64, prop string) (string, error) {
niuniu, err := db.getWordNiuNiu(gid, uid)
if err != nil {
return "", errors.New("请先注册牛牛!")
}

messages, err := niuniu.processNiuNiuAction(prop)
if err != nil {
return "", err
}
if err = db.setWordNiuNiu(gid, niuniu); err != nil {
return "", err
}
return messages, nil
}

func Register(gid, uid int64) (string, error) {
if _, err := db.getWordNiuNiu(gid, uid); err == nil {
return "", errors.New("你已经注册过了")
}
// 获取初始长度
length := db.newLength()
u := userInfo{
UID: uid,
Length: length,
}
if err := db.setWordNiuNiu(gid, &u); err != nil {
return "", err
}
return fmt.Sprintf("注册成功,你的牛牛现在有%.2fcm", u.Length), nil
}

func JJ(gid, uid, adduser int64, prop string) (message string, err error) {
myniuniu, err := db.getWordNiuNiu(gid, uid)
if err != nil {
return "", errors.New("你还没有牛牛快去注册一个吧!")
}
adduserniuniu, err := db.getWordNiuNiu(gid, adduser)
if err != nil {
return "", errors.New("对方还没有牛牛呢,不能🤺")
}

if uid == adduser {
return "", errors.New("你要和谁🤺?你自己吗?")
}

message, err = myniuniu.processJJuAction(adduserniuniu, prop)
if err != nil {
return "", err
}

if err = db.setWordNiuNiu(gid, myniuniu); err != nil {
return "", err
}

if err = db.setWordNiuNiu(gid, adduserniuniu); err != nil {
return "", err
}
return
}

func Cancel(gid, uid int64) (string, error) {
_, err := db.getWordNiuNiu(gid, uid)
if err != nil {
return "", errors.New("你还没有牛牛呢,咋的你想凭空造一个啊")
}
err = db.deleteWordNiuNiu(gid, uid)
if err != nil {
err = errors.New("遇到不可抗力因素,注销失败!")
}
return "注销成功,你已经没有牛牛了", err
}

func Redeem(gid, uid int64, lastLength float64) error {
niu, err := db.getWordNiuNiu(gid, uid)
if err != nil {
return err
}
niu.Length = lastLength
return db.setWordNiuNiu(gid, niu)
}

func Store(gid, uid int64, n int) error {
info, err := db.getWordNiuNiu(gid, uid)
if err != nil {
return err
}

money, err := info.purchaseItem(n)
if err != nil {
return err
}

if wallet.GetWalletOf(uid) < money {
return errors.New("你还没有足够的ATRI币呢,不能购买")
}

if err = wallet.InsertWalletOf(uid, -money); err != nil {
return err
}

return db.setWordNiuNiu(uid, info)
}
Loading