优化每日随机

This commit is contained in:
源文雨 2022-05-05 13:50:54 +08:00
parent a8fba6e7b5
commit b8a6e07095
5 changed files with 25 additions and 59 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.18
require (
github.com/FloatTech/AnimeAPI v1.3.4-0.20220505052643-ddd1de623794
github.com/FloatTech/sqlite v0.2.1
github.com/FloatTech/zbputils v1.3.4-0.20220505052538-84c0e9ec63e9
github.com/FloatTech/zbputils v1.3.4-0.20220505053852-f180d4b6dc51
github.com/antchfx/htmlquery v1.2.4
github.com/corona10/goimagehash v1.0.3
github.com/fogleman/gg v1.3.0

4
go.sum
View File

@ -2,8 +2,8 @@ github.com/FloatTech/AnimeAPI v1.3.4-0.20220505052643-ddd1de623794 h1:oX0/zACMcF
github.com/FloatTech/AnimeAPI v1.3.4-0.20220505052643-ddd1de623794/go.mod h1:q+7q1hStGYsk69rykzt4h0c9a1EP7cuacazi5jGbPOc=
github.com/FloatTech/sqlite v0.2.1 h1:9t6Me48XJJCIoPy4nLRvcdhcVKfT0c2lilp7SEKROG8=
github.com/FloatTech/sqlite v0.2.1/go.mod h1:6NfHRzqOo9RWeMJEoAQVuo51Omd5LFNxCNQhMF02/9U=
github.com/FloatTech/zbputils v1.3.4-0.20220505052538-84c0e9ec63e9 h1:fcUZGApzmSLSF1K8lauouRuYxQjocRYGpLwL3gNG10o=
github.com/FloatTech/zbputils v1.3.4-0.20220505052538-84c0e9ec63e9/go.mod h1:yYXxsdc8fouB9yd2XMr17wso2TV0fH1ukARwhV3U43M=
github.com/FloatTech/zbputils v1.3.4-0.20220505053852-f180d4b6dc51 h1:kA7WjNOf6KrmgJ1ZAryREwtkWsBB9BBRzn5LLLZsmlc=
github.com/FloatTech/zbputils v1.3.4-0.20220505053852-f180d4b6dc51/go.mod h1:yYXxsdc8fouB9yd2XMr17wso2TV0fH1ukARwhV3U43M=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c h1:cNPOdTNiVwxLpROLjXCgbIPvdkE+BwvxDvgmdYmWx6Q=
github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c/go.mod h1:KqZzu7slNKROh3TSYEH/IUMG6f4M+1qubZ5e52QypsE=

View File

@ -8,10 +8,8 @@ import (
"encoding/json"
"image"
"io"
"math/rand"
"os"
"strconv"
"time"
"github.com/fogleman/gg" // 注册了 jpg png gif
"github.com/sirupsen/logrus"
@ -132,19 +130,16 @@ func init() {
return
}
// 生成种子
t, _ := strconv.ParseInt(time.Now().Format("20060102"), 10, 64)
seed := ctx.Event.UserID + t
// 随机获取背景
background, index, err := randimage(zipfile, seed)
background, index, err := randimage(zipfile, ctx)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
// 随机获取签文
title, text := randtext(seed)
randtextindex := ctxext.RandSenderPerDayN(ctx, len(omikujis))
title, text := omikujis[randtextindex]["title"], omikujis[randtextindex]["content"]
digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text))
cachefile := cache + hex.EncodeToString(digest[:])
@ -166,18 +161,16 @@ func init() {
// @function randimage 随机选取zip内的文件
// @param path zip路径
// @param seed 随机数种子
// @param ctx *zero.Ctx
// @return 文件路径 & 错误信息
func randimage(path string, seed int64) (im image.Image, index int, err error) {
func randimage(path string, ctx *zero.Ctx) (im image.Image, index int, err error) {
reader, err := zip.OpenReader(path)
if err != nil {
return
}
defer reader.Close()
r := rand.New(rand.NewSource(seed))
index = r.Intn(len(reader.File))
file := reader.File[index]
file := reader.File[ctxext.RandSenderPerDayN(ctx, len(reader.File))]
f, err := file.Open()
if err != nil {
return
@ -188,16 +181,6 @@ func randimage(path string, seed int64) (im image.Image, index int, err error) {
return
}
// @function randtext 随机选取签文
// @param file 文件路径
// @param seed 随机数种子
// @return 签名 & 签文 & 错误信息
func randtext(seed int64) (string, string) {
r := rand.New(rand.NewSource(seed))
i := r.Intn(len(omikujis))
return omikujis[i]["title"], omikujis[i]["content"]
}
// @function draw 绘制运势图
// @param background 背景图片路径
// @param seed 随机数种子

View File

@ -3,11 +3,8 @@ package omikuji
import (
"fmt"
"log"
"math/rand"
"strconv"
"time"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
@ -29,15 +26,11 @@ func init() { // 插件主体
engine.OnFullMatchGroup([]string{"求签", "占卜"}).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
miku, err := bangoToday(ctx.Event.UserID)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
i := ctxext.RandSenderPerDayN(ctx, 100) + 1
ctx.SendChain(
message.At(ctx.Event.UserID),
message.Image(fmt.Sprintf(bed, miku, 0)),
message.Image(fmt.Sprintf(bed, miku, 1)),
message.Image(fmt.Sprintf(bed, i, 0)),
message.Image(fmt.Sprintf(bed, i, 1)),
)
})
engine.OnFullMatch("解签", ctxext.DoOnceOnSuccess(
@ -58,17 +51,17 @@ func init() { // 插件主体
ctx.SendChain(message.Text("ERROR:", err))
return false
}
log.Printf("[kuji]读取%d条签文", n)
logrus.Infof("[kuji]读取%d条签文", n)
return true
},
)).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
bg, err := bangoToday(ctx.Event.UserID)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
kujiBytes, err := text.RenderToBase64(getKujiByBango(bg), text.FontFile, 400, 20)
kujiBytes, err := text.RenderToBase64(
getKujiByBango(
uint8(ctxext.RandSenderPerDayN(ctx, 100)+1),
),
text.FontFile, 400, 20,
)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
@ -78,13 +71,3 @@ func init() { // 插件主体
}
})
}
func bangoToday(uid int64) (uint8, error) {
today, err := strconv.ParseInt(time.Now().Format("20060102"), 10, 64)
if err != nil {
return 0, err
}
seed := uid + today
r := rand.New(rand.NewSource(seed))
return uint8(r.Intn(100) + 1), nil
}

View File

@ -3,12 +3,12 @@ package tarot
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"strconv"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
@ -49,12 +49,12 @@ func init() {
if err != nil {
panic(err)
}
log.Printf("[tarot]读取%d张塔罗牌", len(cardMap))
logrus.Infof("[tarot]读取%d张塔罗牌", len(cardMap))
return true
},
)).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
i := rand.Intn(22)
p := rand.Intn(2)
i := ctxext.RandSenderPerDayN(ctx, 22)
p := ctxext.RandSenderPerDayN(ctx, 2)
card := cardMap[(strconv.Itoa(i))]
name := card.Name
var info string
@ -67,7 +67,7 @@ func init() {
message.At(ctx.Event.UserID),
message.Text(reasons[rand.Intn(len(reasons))], position[p], " 的 ", name, "\n"),
message.Image(fmt.Sprintf(bed+"MajorArcana/%d.png", i)),
message.Text("\n其意义为", info),
message.Text("\n其意义为: ", info),
); id.ID() == 0 {
ctx.SendChain(message.Text("ERROR:可能被风控了"))
}