mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-20 14:30:10 +08:00
🐛 fix: wordle
This commit is contained in:
parent
edb0880999
commit
7f972e3d68
@ -73,7 +73,8 @@ func init() {
|
|||||||
}()
|
}()
|
||||||
en.OnRegex(`(个人|团队)猜单词`, zero.OnlyGroup).SetBlock(true).Limit(ctxext.LimitByUser).
|
en.OnRegex(`(个人|团队)猜单词`, zero.OnlyGroup).SetBlock(true).Limit(ctxext.LimitByUser).
|
||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
game := newWordleGame()
|
target := words[rand.Intn(len(words))]
|
||||||
|
game := newWordleGame(target)
|
||||||
_, img, cl, _ := game("")
|
_, img, cl, _ := game("")
|
||||||
typ := ctx.State["regex_matched"].([]string)[1]
|
typ := ctx.State["regex_matched"].([]string)[1]
|
||||||
ctx.Send(
|
ctx.Send(
|
||||||
@ -82,6 +83,7 @@ func init() {
|
|||||||
message.Text("你有6次机会猜出单词,单词长度为5,请发送单词"),
|
message.Text("你有6次机会猜出单词,单词长度为5,请发送单词"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
cl()
|
||||||
var next *zero.FutureEvent
|
var next *zero.FutureEvent
|
||||||
if typ == "个人" {
|
if typ == "个人" {
|
||||||
next = zero.NewFutureEvent("message", 999, false, zero.RegexRule(`^[A-Z]{5}$|^[a-z]{5}$`), zero.OnlyGroup, zero.CheckUser(ctx.Event.UserID))
|
next = zero.NewFutureEvent("message", 999, false, zero.RegexRule(`^[A-Z]{5}$|^[a-z]{5}$`), zero.OnlyGroup, zero.CheckUser(ctx.Event.UserID))
|
||||||
@ -90,20 +92,19 @@ func init() {
|
|||||||
}
|
}
|
||||||
recv, cancel := next.Repeat()
|
recv, cancel := next.Repeat()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
var win bool
|
||||||
|
var err error
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-time.After(time.Second * 120):
|
case <-time.After(time.Second * 120):
|
||||||
ctx.Send(
|
ctx.Send(
|
||||||
message.ReplyWithMessage(ctx.Event.MessageID,
|
message.ReplyWithMessage(ctx.Event.MessageID,
|
||||||
message.ImageBytes(img),
|
message.Text("猜单词超时,游戏结束...答案是: ", target),
|
||||||
message.Text("猜单词超时,游戏结束..."),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
cl()
|
|
||||||
return
|
return
|
||||||
case e := <-recv:
|
case e := <-recv:
|
||||||
cl()
|
win, img, cl, err = game(e.Message.String())
|
||||||
win, img, cl, err := game(e.Message.String())
|
|
||||||
switch {
|
switch {
|
||||||
case win:
|
case win:
|
||||||
ctx.Send(
|
ctx.Send(
|
||||||
@ -118,7 +119,7 @@ func init() {
|
|||||||
ctx.Send(
|
ctx.Send(
|
||||||
message.ReplyWithMessage(e.MessageID,
|
message.ReplyWithMessage(e.MessageID,
|
||||||
message.ImageBytes(img),
|
message.ImageBytes(img),
|
||||||
message.Text("游戏结束..."),
|
message.Text("游戏结束...答案是: ", target),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
cl()
|
cl()
|
||||||
@ -126,14 +127,12 @@ func init() {
|
|||||||
case err == errLengthNotEnough:
|
case err == errLengthNotEnough:
|
||||||
ctx.Send(
|
ctx.Send(
|
||||||
message.ReplyWithMessage(e.MessageID,
|
message.ReplyWithMessage(e.MessageID,
|
||||||
message.ImageBytes(img),
|
|
||||||
message.Text("单词长度错误"),
|
message.Text("单词长度错误"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
case err == errUnknownWord:
|
case err == errUnknownWord:
|
||||||
ctx.Send(
|
ctx.Send(
|
||||||
message.ReplyWithMessage(e.MessageID,
|
message.ReplyWithMessage(e.MessageID,
|
||||||
message.ImageBytes(img),
|
|
||||||
message.Text("你确定存在这样的单词吗?"),
|
message.Text("你确定存在这样的单词吗?"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@ -143,22 +142,22 @@ func init() {
|
|||||||
message.ImageBytes(img),
|
message.ImageBytes(img),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
cl()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWordleGame() func(string) (bool, []byte, func(), error) {
|
func newWordleGame(target string) func(string) (bool, []byte, func(), error) {
|
||||||
onhand := words[rand.Intn(len(words))]
|
record := make([]string, 0, len(target)+1)
|
||||||
record := make([]string, 0, len(onhand)+1)
|
|
||||||
return func(s string) (win bool, data []byte, cl func(), err error) {
|
return func(s string) (win bool, data []byte, cl func(), err error) {
|
||||||
if s != "" {
|
if s != "" {
|
||||||
s = strings.ToLower(s)
|
s = strings.ToLower(s)
|
||||||
if onhand == s {
|
if target == s {
|
||||||
win = true
|
win = true
|
||||||
} else {
|
} else {
|
||||||
if len(s) != len(onhand) {
|
if len(s) != len(target) {
|
||||||
err = errLengthNotEnough
|
err = errLengthNotEnough
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -177,14 +176,14 @@ func newWordleGame() func(string) (bool, []byte, func(), error) {
|
|||||||
ctx := gg.NewContext((side+2)*5+26, (side+2)*6+26)
|
ctx := gg.NewContext((side+2)*5+26, (side+2)*6+26)
|
||||||
ctx.SetColor(color.RGBA{255, 255, 255, 255})
|
ctx.SetColor(color.RGBA{255, 255, 255, 255})
|
||||||
ctx.Clear()
|
ctx.Clear()
|
||||||
for i := 0; i < len(onhand)+1; i++ {
|
for i := 0; i < len(target)+1; i++ {
|
||||||
for j := 0; j < len(onhand); j++ {
|
for j := 0; j < len(target); j++ {
|
||||||
if len(record) > i {
|
if len(record) > i {
|
||||||
ctx.DrawRectangle(float64(10+j*(side+4)), float64(10+i*(side+4)), float64(side), float64(side))
|
ctx.DrawRectangle(float64(10+j*(side+4)), float64(10+i*(side+4)), float64(side), float64(side))
|
||||||
switch {
|
switch {
|
||||||
case record[i][j] == onhand[j]:
|
case record[i][j] == target[j]:
|
||||||
ctx.SetColor(colors[match])
|
ctx.SetColor(colors[match])
|
||||||
case strings.IndexByte(onhand, record[i][j]) != -1:
|
case strings.IndexByte(target, record[i][j]) != -1:
|
||||||
ctx.SetColor(colors[exist])
|
ctx.SetColor(colors[exist])
|
||||||
default:
|
default:
|
||||||
ctx.SetColor(colors[notexist])
|
ctx.SetColor(colors[notexist])
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user