Skip to content

Commit

Permalink
fix:修复一些问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy0411 committed Nov 24, 2024
1 parent 1aed663 commit 2b3b507
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 11 deletions.
104 changes: 94 additions & 10 deletions niu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/FloatTech/AnimeAPI/wallet"
"github.com/FloatTech/floatbox/file"
sql "github.com/FloatTech/sqlite"
"os"
"strconv"
"strings"
Expand All @@ -17,11 +18,12 @@ var (

func init() {
if file.IsNotExist("data/niuniu") {
err := os.MkdirAll("data/niuniu", 0755)
err := os.MkdirAll("data/niuniu", 775)
if err != nil {
panic(err)
}
}
db.sql = sql.New("data/niuniu/niuniu.db")
err := db.sql.Open(time.Hour * 24)
if err != nil {
panic(err)
Expand Down Expand Up @@ -52,7 +54,6 @@ func DeleteWordNiuNiu(gid, uid int64) error {

func GetRankingInfo(gid int64, t bool) (BaseInfos, error) {
var (
f BaseInfos
list users
err error
)
Expand All @@ -65,7 +66,7 @@ func GetRankingInfo(gid int64, t bool) (BaseInfos, error) {
}
return nil, err
}

f := make(BaseInfos, len(niuOfGroup))
if t {
list = niuOfGroup.positive()
niuOfGroup.sort(t)
Expand Down Expand Up @@ -151,32 +152,34 @@ func Register(gid, uid int64) (string, error) {
return fmt.Sprintf("注册成功,你的牛牛现在有%.2fcm", u.Length), nil
}

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

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

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

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

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

adduserLength = adduserniuniu.Length
return
}

Expand All @@ -193,11 +196,22 @@ func Cancel(gid, uid int64) (string, error) {
}

func Redeem(gid, uid int64, lastLength float64) error {
money := wallet.GetWalletOf(uid)
if money < 150 {
return fmt.Errorf("赎牛牛需要150ATRI币,快去赚钱吧,目前仅有:%d个%s", money, wallet.GetWalletName())
}

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

niu, err := db.getWordNiuNiu(gid, uid)
if err != nil {
return err
}

niu.Length = lastLength

return db.setWordNiuNiu(gid, niu)
}

Expand All @@ -222,3 +236,73 @@ func Store(gid, uid int64, n int) error {

return db.setWordNiuNiu(uid, info)
}

func Sell(gid, uid int64) (string, error) {
niu, err := db.getWordNiuNiu(gid, uid)
if err != nil {
return "", errors.New("你没有牛牛怎么卖😰")
}
money, t, message := profit(niu.Length)
if !t {
return message, errors.New(``)
}
err = wallet.InsertWalletOf(uid, money)
if err != nil {
return message, err
}
u := AuctionInfo{
UID: niu.UID,
Length: niu.Length,
Money: money * 2,
}
err = db.setNiuNiuAuction(gid, &u)
return message, err
}

func ShowAuction(gid int64) ([]AuctionInfo, error) {
return db.getAllNiuNiuAuction(gid)
}

func Auction(gid, uid int64, i int) (string, error) {
auction, err := db.getAllNiuNiuAuction(gid)
if err != nil {
return "", errors.New("拍卖行还没有牛牛呢")
}
err = wallet.InsertWalletOf(uid, -auction[i].Money)
if err != nil {
return "", errors.New("你的钱不够快去赚钱吧!")
}

niu, err := db.getWordNiuNiu(gid, uid)
if err != nil {
niu = &userInfo{
UID: uid,
}
}
niu.Length = auction[i-1].Length

if auction[i].Money > 500 {
niu.WeiGe = 2
niu.Artifact = 2
}

if err = db.setWordNiuNiu(gid, niu); err != nil {
return "", err
}
if auction[i].Money > 500 {
return fmt.Sprintf("恭喜你购买成功,当前长度为%.2fcm,此次购买将赠送你%d个伟哥,%d个媚药",
niu.Length, niu.WeiGe, niu.Artifact), nil
}

return fmt.Sprintf("恭喜你购买成功,当前长度为%.2fcm", niu.Length), nil
}

func Bag(gid, uid int64) (string, error) {
niu, err := db.getWordNiuNiu(gid, uid)
message := fmt.Sprintf("当前牛牛背包如下\n伟哥: %v\n媚药: %v\n击剑神器: %v\n击剑神稽: %v",
niu.WeiGe,
niu.Philter,
niu.Artifact,
niu.ShenJi)
return message, err
}
43 changes: 42 additions & 1 deletion niu/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ type userInfo struct {
Buff5 int // 暂定
}

type AuctionInfo struct {
UID int64
Length float64
Money int
}

type BaseInfo struct {
UID int64
Length float64
Expand Down Expand Up @@ -332,5 +338,40 @@ func (db *model) deleteWordNiuNiu(gid, uid int64) error {
func (db *model) getAllNiuNiuOfGroup(gid int64) (users, error) {
db.Lock()
defer db.Unlock()
return sql.FindAll[userInfo](&db.sql, strconv.FormatInt(gid, 10), "where UserCount = 0")
var user userInfo
var useras users
err := db.sql.FindFor(fmt.Sprintf("%d", gid), &user, "",
func() error {
useras = append(useras, &user)
return nil
})
return useras, err
}

func (db *model) setNiuNiuAuction(gid int64, u *AuctionInfo) error {
db.Lock()
defer db.Unlock()
err := db.sql.Insert(fmt.Sprintf("auction_%d", gid), u)
if err != nil {
err = db.sql.Create(strconv.FormatInt(gid, 10), &AuctionInfo{})
if err != nil {
return err
}
err = db.sql.Insert(strconv.FormatInt(gid, 10), u)
}
return err
}

func (db *model) getAllNiuNiuAuction(gid int64) ([]AuctionInfo, error) {
db.Lock()
defer db.Unlock()
var user AuctionInfo
var useras []AuctionInfo
err := db.sql.FindFor(fmt.Sprintf("auction_%d", gid), &user, "",
func() error {
useras = append(useras, user)
return nil
})

return useras, err
}
77 changes: 77 additions & 0 deletions niu/utils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,92 @@
package niu

import (
"errors"
"fmt"
"github.com/FloatTech/AnimeAPI/wallet"
"io"
"math"
"math/rand"
"net/http"
"regexp"
"strconv"
)

func randomChoice(options []string) string {
return options[rand.Intn(len(options))]
}

func profit(niuniu float64) (money int, t bool, message string) {
switch {
case 0 < niuniu && niuniu <= 15:
message = randomChoice([]string{
"你的牛牛太小啦",
"这么小的牛牛就要肩负起这么大的责任吗?快去打胶吧!",
})
case niuniu > 15:
money = int(niuniu * 10)
message = randomChoice([]string{
fmt.Sprintf("你的牛牛已经离你而去了,你赚取了%d个%s", money, wallet.GetWalletName()),
fmt.Sprintf("啊!你的牛☞已经没啦🤣,为了这点钱就出卖你的牛牛可真不值,你赚取了%d个%s", money, wallet.GetWalletName()),
})
t = true
case niuniu <= 0 && niuniu >= -15:
message = randomChoice([]string{
"你的牛牛太小啦",
"这么小的牛牛就要肩负起这么大的责任吗?快去找别人玩吧!",
})
case niuniu < -15:
money = int(math.Abs(niuniu * 10))
message = randomChoice([]string{
fmt.Sprintf("此世做了女孩子来世来当男孩子(bushi),你赚取了%d个%s", money, wallet.GetWalletName()),
fmt.Sprintf("呜呜呜,不哭不哭当女孩子不委屈的,你赚取了%d个%s", money, wallet.GetWalletName()),
})
t = true
}
return
}

func getGold() (any, error) {
req, err := http.NewRequest("GET", "https://www.huilvbiao.com/gold", nil)
if err != nil {
return 0, err
}
req.Header.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
all, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
compile, err := regexp.Compile(`(?s)<span id="high" class="text-info">(.+?)</span>`)
if err != nil {
return 0, err
}
goldstr := compile.FindStringSubmatch(string(all))
// 编译正则表达式,用于匹配<span>标签中的数字
re := regexp.MustCompile(`(?s)<span id="low" class="text-info">(\d+\.?\d*)</span>`)

// 使用FindStringSubmatch来查找匹配的内容
matches := re.FindStringSubmatch(goldstr[1])

// 检查是否有匹配项,并提取第一个匹配组(即括号中的数字)
if matches != nil && len(matches) > 1 {
price, err := strconv.ParseFloat(matches[1], 64)
if err != nil {
return 0, err
}
fmt.Printf("提取的最低价格是: %.2f\n", price)
} else {
return 0, errors.New(`错误`)
}
return matches, err
}

func hitGlueNiuNiu(niuniu float64) (string, float64) {
probability := rand.Intn(100 + 1)
reduce := math.Abs(hitGlue(niuniu))
Expand Down

0 comments on commit 2b3b507

Please sign in to comment.