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

修改牛牛排名为图片发送 #1022

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions plugin/niuniu/draw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package niuniu

import (
"fmt"
"github.com/FloatTech/rendercard"
"image"
"net/http"
"os"
)

var font, _ = os.ReadFile("./font/GlowSans.otf")

type drawUserRanking struct {
Name string
User *userInfo
}

func drawRanking(allUsers []drawUserRanking, title string) (img image.Image, err error) {
var ri []*rendercard.RankInfo

Check failure on line 19 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: rendercard.RankInfo

Check failure on line 19 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: rendercard.RankInfo

Check failure on line 19 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm64)

undefined: rendercard.RankInfo

Check failure on line 19 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: rendercard.RankInfo

Check failure on line 19 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: rendercard.RankInfo

Check failure on line 19 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / lint

undefined: rendercard.RankInfo

Check failure on line 19 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / lint

undefined: rendercard.RankInfo
for _, user := range allUsers {
resp, err := http.Get(fmt.Sprintf("http://q1.qlogo.cn/g?b=qq&nk=%d&s=100", user.User.UID))
if err != nil {
return nil, err
}
decode, _, err := image.Decode(resp.Body)
if err != nil {
return nil, err
}
ri = append(ri, &rendercard.RankInfo{

Check failure on line 29 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: rendercard.RankInfo

Check failure on line 29 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: rendercard.RankInfo

Check failure on line 29 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm64)

undefined: rendercard.RankInfo

Check failure on line 29 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: rendercard.RankInfo

Check failure on line 29 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: rendercard.RankInfo

Check failure on line 29 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / lint

undefined: rendercard.RankInfo

Check failure on line 29 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / lint

undefined: rendercard.RankInfo
Avatar: decode,
TopLeftText: user.Name,
BottomLeftText: fmt.Sprintf("QQ:%d", user.User.UID),
RightText: fmt.Sprintf("牛牛长度:%.2fcm", user.User.Length),
})
}
img, err = rendercard.DrawRankingCard(font, title, ri)

Check failure on line 36 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: rendercard.DrawRankingCard

Check failure on line 36 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: rendercard.DrawRankingCard

Check failure on line 36 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm64)

undefined: rendercard.DrawRankingCard

Check failure on line 36 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: rendercard.DrawRankingCard

Check failure on line 36 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: rendercard.DrawRankingCard

Check failure on line 36 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / lint

undefined: rendercard.DrawRankingCard (typecheck)

Check failure on line 36 in plugin/niuniu/draw.go

View workflow job for this annotation

GitHub Actions / lint

undefined: rendercard.DrawRankingCard) (typecheck)
return
}
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
for _, info := range m {
allUsers = append(allUsers, 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, "牛牛长度排行")
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
for _, info := range m {
allUsers = append(allUsers, drawUserRanking{
Name: ctx.CardOrNickName(info.UID),
User: info,
})
}
ranking, err := drawRanking(allUsers, "牛牛深度排行")
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
Loading