🐛 增加百人一首 修复Lazy不关闭

This commit is contained in:
源文雨
2022-03-28 17:11:44 +08:00
parent 636f14c450
commit 68980a69b5
9 changed files with 204 additions and 65 deletions

View File

@@ -23,20 +23,7 @@ import (
const ttsServiceName = "tts"
var (
t = &ttsInstances{
m: map[string]tts.TTS{
"百度女声": baidutts.NewBaiduTTS(0),
"百度男声": baidutts.NewBaiduTTS(1),
"百度度逍遥": baidutts.NewBaiduTTS(3),
"百度度丫丫": baidutts.NewBaiduTTS(4),
"拟声鸟阿梓": mockingbird.NewMockingBirdTTS(0),
"拟声鸟药水哥": mockingbird.NewMockingBirdTTS(1),
},
l: []string{"拟声鸟阿梓", "拟声鸟药水哥", "百度女声", "百度男声", "百度度逍遥", "百度度丫丫"},
}
re = regexp.MustCompile(`(\-|\+)?\d+(\.\d+)?`)
)
var re = regexp.MustCompile(`(\-|\+)?\d+(\.\d+)?`)
type ttsInstances struct {
sync.RWMutex
@@ -53,6 +40,17 @@ func (t *ttsInstances) List() []string {
}
func init() {
t := &ttsInstances{
m: map[string]tts.TTS{
"百度女声": baidutts.NewBaiduTTS(0),
"百度男声": baidutts.NewBaiduTTS(1),
"百度度逍遥": baidutts.NewBaiduTTS(3),
"百度度丫丫": baidutts.NewBaiduTTS(4),
"拟声鸟阿梓": nil,
"拟声鸟药水哥": nil,
},
l: []string{"拟声鸟阿梓", "拟声鸟药水哥", "百度女声", "百度男声", "百度度逍遥", "百度度丫丫"},
}
engine := control.Register(ttsServiceName, order.AcquirePrio(), &control.Options{
DisableOnDefault: true,
Help: "语音回复(包括拟声鸟和百度)\n" +
@@ -65,19 +63,21 @@ func init() {
msg := ctx.ExtractPlainText()
r := aireply.NewAIReply(getReplyMode(ctx))
tts := t.new(t.getSoundMode(ctx))
ctx.SendChain(message.Record(tts.Speak(ctx.Event.UserID, func() string {
reply := r.TalkPlain(msg, zero.BotConfig.NickName[0])
reply = re.ReplaceAllStringFunc(reply, func(s string) string {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
log.Errorln("[tts]:", err)
return s
}
return numcn.EncodeFromFloat64(f)
})
log.Println("[tts]:", reply)
return reply
})))
if tts != nil {
ctx.SendChain(message.Record(tts.Speak(ctx.Event.UserID, func() string {
reply := r.TalkPlain(msg, zero.BotConfig.NickName[0])
reply = re.ReplaceAllStringFunc(reply, func(s string) string {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
log.Errorln("[tts]:", err)
return s
}
return numcn.EncodeFromFloat64(f)
})
log.Println("[tts]:", reply)
return reply
})))
}
})
engine.OnRegex(`^设置语音模式(.*)$`, ctxext.FirstValueInList(t)).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
@@ -98,8 +98,23 @@ func init() {
}
// new 语音简单工厂
func (t *ttsInstances) new(name string) tts.TTS {
return t.m[name]
func (t *ttsInstances) new(name string) (ts tts.TTS) {
t.RLock()
ts = t.m[name]
t.RUnlock()
if ts == nil {
switch name {
case "拟声鸟阿梓":
t.Lock()
ts, _ = mockingbird.NewMockingBirdTTS(0)
t.Unlock()
case "拟声鸟药水哥":
t.Lock()
ts, _ = mockingbird.NewMockingBirdTTS(1)
t.Unlock()
}
}
return
}
func (t *ttsInstances) setSoundMode(ctx *zero.Ctx, name string) error {

View File

@@ -58,15 +58,15 @@ func init() {
"- 设置底图[车万 | DC4 | 爱因斯坦 | 星空列车 | 樱云之恋 | 富婆妹 | 李清歌 | 公主连结 | 原神 | 明日方舟 | 碧蓝航线 | 碧蓝幻想 | 战双 | 阴阳师 | 赛马娘 | 东方归言录]",
PublicDataFolder: "Fortune",
})
_ = os.RemoveAll(cache)
err := os.MkdirAll(cache, 0755)
if err != nil {
panic(err)
}
go func() {
for i, s := range table {
index[s] = uint8(i)
}
_ = os.RemoveAll(cache)
err := os.MkdirAll(cache, 0755)
if err != nil {
panic(err)
}
data, err := file.GetLazyData(omikujson, true, false)
if err != nil {
panic(err)
@@ -75,7 +75,9 @@ func init() {
if err != nil {
panic(err)
}
_, err = file.GetLazyData(font, false, true)
}()
go func() {
_, err := file.GetLazyData(font, false, true)
if err != nil {
panic(err)
}

107
plugin/hyaku/main.go Normal file
View File

@@ -0,0 +1,107 @@
package hyaku
import (
"encoding/csv"
"fmt"
"math/rand"
"os"
"strconv"
"unsafe"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/control/order"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/file"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
const bed = "https://gitcode.net/u011570312/OguraHyakuninIsshu/-/raw/master/"
type line struct {
no, 歌人, 上の句, 下の句, 上の句ひらがな, 下の句ひらがな string
}
var lines [100]*line
func init() {
engine := control.Register("hyaku", order.AcquirePrio(), &control.Options{
DisableOnDefault: false,
Help: "百人一首\n" +
"- 百人一首(随机发一首)\n" +
"- 百人一首之n",
PrivateDataFolder: "hyaku",
})
csvfile := engine.DataFolder() + "hyaku.csv"
go func() {
if file.IsNotExist(csvfile) {
err := file.DownloadTo(bed+"小倉百人一首.csv", csvfile, true)
if err != nil {
_ = os.Remove(csvfile)
panic(err)
}
}
f, err := os.Open(csvfile)
if err != nil {
panic(err)
}
records, err := csv.NewReader(f).ReadAll()
if err != nil {
panic(err)
}
records = records[1:] // skip title
if len(records) != 100 {
panic("invalid csvfile")
}
for j, r := range records {
i, err := strconv.Atoi(r[0])
if err != nil {
panic(err)
}
i--
if j != i {
panic("invalid csvfile")
}
lines[i] = (*line)(*(*unsafe.Pointer)(unsafe.Pointer(&r)))
}
}()
engine.OnFullMatch("百人一首").SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
i := rand.Intn(100)
ctx.SendChain(
message.Image(fmt.Sprintf(bed+"img/%03d.jpg", i+1)),
message.Text("\n",
"●番 号: ", lines[i].no, "\n",
"●歌 人: ", lines[i].歌人, "\n",
"●上の句: ", lines[i].上の句, "\n",
"●下の句: ", lines[i].下の句, "\n",
"●上の句ひらがな: ", lines[i].上の句ひらがな, "\n",
"●下の句ひらがな: ", lines[i].下の句ひらがな, "\n",
),
message.Image(fmt.Sprintf(bed+"img/%03d.png", i+1)),
)
})
engine.OnRegex(`^百人一首之\s?(\d+)$`).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
i, err := strconv.Atoi(ctx.State["regex_matched"].([]string)[1])
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
if i > 100 || i < 1 {
ctx.SendChain(message.Text("ERROR:超出范围"))
return
}
i--
ctx.SendChain(
message.Image(fmt.Sprintf(bed+"img/%03d.jpg", i+1)),
message.Text("\n",
"●番 号: ", lines[i].no, "\n",
"●歌 人: ", lines[i].歌人, "\n",
"●上の句: ", lines[i].上の句, "\n",
"●下の句: ", lines[i].下の句, "\n",
"●上の句ひらがな: ", lines[i].上の句ひらがな, "\n",
"●下の句ひらがな: ", lines[i].下の句ひらがな, "\n",
),
message.Image(fmt.Sprintf(bed+"img/%03d.png", i+1)),
)
})
}

View File

@@ -47,14 +47,6 @@ func init() {
if err != nil {
panic(err)
}
_, err = file.GetLazyData(text.BoldFontFile, false, true)
if err != nil {
panic(err)
}
_, err = file.GetLazyData(text.FontFile, false, true)
if err != nil {
panic(err)
}
sdb = initialize(engine.DataFolder() + "score.db")
log.Println("[score]加载score数据库")
}()
@@ -99,6 +91,11 @@ func init() {
monthWord := now.Format("01/02")
hourWord := getHourWord(now)
_, err = file.GetLazyData(text.BoldFontFile, false, true)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
if err = canvas.LoadFontFace(text.BoldFontFile, float64(back.Bounds().Size().X)*0.1); err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
@@ -107,6 +104,11 @@ func init() {
canvas.DrawString(hourWord, float64(back.Bounds().Size().X)*0.1, float64(back.Bounds().Size().Y)*1.2)
canvas.DrawString(monthWord, float64(back.Bounds().Size().X)*0.6, float64(back.Bounds().Size().Y)*1.2)
nickName := ctx.CardOrNickName(uid)
_, err = file.GetLazyData(text.FontFile, false, true)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
if err = canvas.LoadFontFace(text.FontFile, float64(back.Bounds().Size().X)*0.04); err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return

View File

@@ -8,6 +8,7 @@ import (
"math/rand"
"sort"
"strings"
"sync"
"time"
"github.com/FloatTech/zbputils/control"
@@ -53,7 +54,10 @@ type dictionary map[int]struct {
cet4 []string
}
var words = make(dictionary)
var (
words = make(dictionary)
wordsmu sync.Mutex
)
func init() {
en := control.Register("wordle", order.AcquirePrio(), &control.Options{
@@ -72,26 +76,34 @@ func init() {
)
}),
))
go func() {
for i := 5; i <= 7; i++ {
for i := 5; i <= 7; i++ {
go func(i int) {
dc, err := file.GetLazyData(fmt.Sprintf("%scet-4_%d.txt", en.DataFolder(), i), true, true)
if err != nil {
panic(err)
}
c := strings.Split(string(dc), "\n")
sort.Strings(c)
wordsmu.Lock()
tmp := words[i]
tmp.cet4 = c
words[i] = tmp
wordsmu.Unlock()
}(i)
go func(i int) {
dd, err := file.GetLazyData(fmt.Sprintf("%sdict_%d.txt", en.DataFolder(), i), true, true)
if err != nil {
panic(err)
}
d := strings.Split(string(dd), "\n")
sort.Strings(d)
words[i] = struct {
dict []string
cet4 []string
}{d, c}
}
}()
wordsmu.Lock()
tmp := words[i]
tmp.dict = d
words[i] = tmp
wordsmu.Unlock()
}(i)
}
en.OnRegex(`(个人|团队)(五阶|六阶|七阶)?猜单词`, zero.OnlyGroup).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
class := classdict[ctx.State["regex_matched"].([]string)[2]]