Skip to content

Commit

Permalink
修改牛牛排名为图片发送
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy0411 committed Oct 13, 2024
1 parent 712f241 commit a9ee34b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 42 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/FloatTech/floatbox v0.0.0-20240505082030-226ec6713e14
github.com/FloatTech/gg v1.1.3
github.com/FloatTech/imgfactory v0.2.2-0.20230413152719-e101cc3606ef
github.com/FloatTech/rendercard v0.1.1
github.com/FloatTech/rendercard v0.1.2
github.com/FloatTech/sqlite v1.6.3
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/FloatTech/gg v1.1.3 h1:+GlL02lTKsxJQr4WCuNwVxC1/eBZrCvypCIBtxuOFb4=
github.com/FloatTech/gg v1.1.3/go.mod h1:/9oLP54CMfq4r+71XL26uaFTJ1uL1boAyX67680/1HE=
github.com/FloatTech/imgfactory v0.2.2-0.20230413152719-e101cc3606ef h1:CJbK/2FRwPuZpeb6M4sWK2d7oXDnBEGhpkQuQrgc91A=
github.com/FloatTech/imgfactory v0.2.2-0.20230413152719-e101cc3606ef/go.mod h1:el5hGpj1C1bDRxcTXYRwEivDCr40zZeJpcrLrB1fajs=
github.com/FloatTech/rendercard v0.1.1 h1:vXz3x92bLavmNexTywdUvhft2/ipUSuo8aPRkqVdGQ8=
github.com/FloatTech/rendercard v0.1.1/go.mod h1:Sbojcy1t3NfFz7/WicZRmR/uKFxNMYkKF8qHx69dxY0=
github.com/FloatTech/rendercard v0.1.2 h1:W4SF9fSxv6Ava+kIUI0T1ILOjId/cgZ0huuEpMpHJbU=
github.com/FloatTech/rendercard v0.1.2/go.mod h1:Sbojcy1t3NfFz7/WicZRmR/uKFxNMYkKF8qHx69dxY0=
github.com/FloatTech/sqlite v1.6.3 h1:MQkqBNlkPuCoKQQgoNLuTL/2Ci3tBTFAnVYBdD0Wy4M=
github.com/FloatTech/sqlite v1.6.3/go.mod h1:zFbHzRfB+CJ+VidfjuVbrcin3DAz283F7hF1hIeHzpY=
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 h1:snfw7FNFym1eNnLrQ/VCf80LiQo9C7jHgrunZDwiRcY=
Expand Down
112 changes: 78 additions & 34 deletions plugin/niuniu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
package niuniu

import (
"bytes"
"fmt"
"image/png"
"math/rand"
"strconv"
"strings"
Expand Down Expand Up @@ -151,7 +153,8 @@ func init() {
en.OnFullMatch("赎牛牛", zero.OnlyGroup, getdb).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
uid := ctx.Event.UserID
last, ok := jjCount.Load(fmt.Sprintf("%d_%d", gid, uid))
t := fmt.Sprintf("%d_%d", gid, uid)
last, ok := jjCount.Load(t)

if !ok {
ctx.SendChain(message.Text("你还没有被厥呢"))
Expand All @@ -160,7 +163,7 @@ func init() {

if time.Since(last.TimeLimit) > time.Minute*45 {
ctx.SendChain(message.Text("时间已经过期了,牛牛已被收回!"))
jjCount.Delete(fmt.Sprintf("%d_%d", gid, uid))
jjCount.Delete(t)
return
}

Expand All @@ -174,27 +177,47 @@ func init() {
ctx.SendChain(message.Text("赎牛牛需要150ATRI币,快去赚钱吧"))
return
}
ctx.SendChain(message.Text(message.Text("赎牛牛后长度会变为:", last.Length, "cm,是否要这样做吗?【是|否】")))
recv, cancel := zero.NewFutureEvent("message", 999, false, zero.CheckUser(uid), zero.CheckGroup(gid), zero.RegexRule(`^(是|否)$`)).Repeat()
defer cancel()
timer := time.NewTimer(2 * time.Minute)
defer timer.Stop()
for {
select {
case <-timer.C:
ctx.SendChain(message.At(uid), message.Text("回答超时已自动取消"))
return
case r := <-recv:
answer := r.Event.Message.String()

if err := wallet.InsertWalletOf(uid, -150); err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
if answer == "否" {
ctx.SendChain(message.Text("已销毁牛牛!"))
jjCount.Delete(t)
return
}

niuniu, err := db.findNiuNiu(gid, uid)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
if err := wallet.InsertWalletOf(uid, -150); err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}

niuniu, err := db.findNiuNiu(gid, uid)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}

niuniu.Length = last.Length
niuniu.Length = last.Length

if err = db.insertNiuNiu(&niuniu, gid); err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
if err = db.insertNiuNiu(&niuniu, gid); err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}

jjCount.Delete(fmt.Sprintf("%d_%d", gid, uid))
ctx.SendChain(message.At(uid), message.Text(fmt.Sprintf("恭喜你!成功赎回牛牛,当前长度为:%.2fcm", last.Length)))
jjCount.Delete(fmt.Sprintf("%d_%d", gid, uid))
ctx.SendChain(message.At(uid), message.Text(fmt.Sprintf("恭喜你!成功赎回牛牛,当前长度为:%.2fcm", last.Length)))
}
}
})
en.OnFullMatch("牛子长度排行", zero.OnlyGroup, getdb).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
Expand All @@ -208,16 +231,27 @@ func init() {
ctx.SendChain(message.Text("暂时没有男孩子哦"))
return
}
var messages strings.Builder
messages.WriteString("牛子长度排行榜\n")
for i, user := range m.sort(true) {
messages.WriteString(fmt.Sprintf("第%d名 id:%s 长度:%.2fcm\n", i+1,
ctx.CardOrNickName(user.UID), user.Length))
m.sort(true)
var allUsers []drawUserRanking

Check failure on line 235 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: drawUserRanking

Check failure on line 235 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: drawUserRanking

Check failure on line 235 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: drawUserRanking

Check failure on line 235 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: drawUserRanking

Check failure on line 235 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: drawUserRanking

Check failure on line 235 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: drawUserRanking
for _, info := range m {
allUsers = append(allUsers, drawUserRanking{

Check failure on line 237 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: drawUserRanking

Check failure on line 237 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: drawUserRanking

Check failure on line 237 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: drawUserRanking

Check failure on line 237 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: drawUserRanking

Check failure on line 237 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: drawUserRanking

Check failure on line 237 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: drawUserRanking
Name: ctx.CardOrNickName(info.UID),
User: info,
})
}
msg := ctxext.FakeSenderForwardNode(ctx, message.Text(&messages))
if id := ctx.Send(message.Message{msg}).ID(); id == 0 {
ctx.Send(message.Text("发送排行失败"))
ranking, err := drawRanking(allUsers, "牛牛长度排行")

Check failure on line 242 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: drawRanking

Check failure on line 242 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: drawRanking

Check failure on line 242 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: drawRanking

Check failure on line 242 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: drawRanking

Check failure on line 242 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: drawRanking

Check failure on line 242 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: drawRanking
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
var buf bytes.Buffer
err = png.Encode(&buf, ranking)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
ctx.SendChain(message.ImageBytes(buf.Bytes()))

})
en.OnFullMatch("牛子深度排行", zero.OnlyGroup, getdb).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
Expand All @@ -231,16 +265,26 @@ func init() {
ctx.SendChain(message.Text("暂时没有女孩子哦"))
return
}
var messages strings.Builder
messages.WriteString("牛牛深度排行榜\n")
for i, user := range m.sort(false) {
messages.WriteString(fmt.Sprintf("第%d名 id:%s 长度:%.2fcm\n", i+1,
ctx.CardOrNickName(user.UID), user.Length))
m.sort(false)
var allUsers []drawUserRanking

Check failure on line 269 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: drawUserRanking

Check failure on line 269 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: drawUserRanking

Check failure on line 269 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: drawUserRanking

Check failure on line 269 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: drawUserRanking

Check failure on line 269 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: drawUserRanking
for _, info := range m {
allUsers = append(allUsers, drawUserRanking{

Check failure on line 271 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: drawUserRanking

Check failure on line 271 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: drawUserRanking

Check failure on line 271 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: drawUserRanking

Check failure on line 271 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: drawUserRanking

Check failure on line 271 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: drawUserRanking
Name: ctx.CardOrNickName(info.UID),
User: info,
})
}
ranking, err := drawRanking(allUsers, "牛牛深度排行")

Check failure on line 276 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: drawRanking

Check failure on line 276 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: drawRanking

Check failure on line 276 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: drawRanking

Check failure on line 276 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: drawRanking

Check failure on line 276 in plugin/niuniu/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: drawRanking (typecheck)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
msg := ctxext.FakeSenderForwardNode(ctx, message.Text(&messages))
if id := ctx.Send(message.Message{msg}).ID(); id == 0 {
ctx.Send(message.Text("发送排行失败"))
var buf bytes.Buffer
err = png.Encode(&buf, ranking)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
ctx.SendChain(message.ImageBytes(buf.Bytes()))
})
en.OnFullMatch("查看我的牛牛", getdb, zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) {
uid := ctx.Event.UserID
Expand Down
8 changes: 3 additions & 5 deletions plugin/niuniu/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (m users) negative() users {
return m1
}

func (m users) sort(isDesc bool) users {
func (m users) sort(isDesc bool) {
t := func(i, j int) bool {
return m[i].Length < m[j].Length
}
Expand All @@ -305,12 +305,12 @@ func (m users) sort(isDesc bool) users {
}
}
sort.Slice(m, t)
return m
}

func (m users) ranking(niuniu float64, uid int64) int {
result := niuniu > 0
for i, user := range m.sort(result) {
m.sort(result)
for i, user := range m {
if user.UID == uid {
return i + 1
}
Expand All @@ -328,7 +328,6 @@ func (db *model) createGIDTable(gid int64) error {
return db.sql.Create(strconv.FormatInt(gid, 10), &userInfo{})
}

// findNiuNiu 返回一个用户的牛牛信息
func (db *model) findNiuNiu(gid, uid int64) (userInfo, error) {
db.RLock()
defer db.RUnlock()
Expand All @@ -337,7 +336,6 @@ func (db *model) findNiuNiu(gid, uid int64) (userInfo, error) {
return u, err
}

// insertNiuNiu 更新一个用户的牛牛信息
func (db *model) insertNiuNiu(u *userInfo, gid int64) error {
db.Lock()
defer db.Unlock()
Expand Down

0 comments on commit a9ee34b

Please sign in to comment.