Skip to content

Commit

Permalink
fix: try to fix guess idioms bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RicheyJang committed Apr 9, 2022
1 parent f4f521a commit 4d8adde
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions plugins/idioms/guess.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ func init() {
if proxy == nil {
return
}
// 为防止猜成语过程中,Repeat影响其它功能调用,因此初始化时block设为false
proxy.OnFullMatch([]string{"猜成语"}).SetBlock(false).SetPriority(9).Handle(guessIdioms)
proxy.OnFullMatch([]string{"猜成语"}).SetBlock(true).SetPriority(9).Handle(guessIdioms)
proxy.AddConfig("localFirst", false) // 优先使用本地词库IdiomsImageDir, 文件名:某个成语.png/jpg
_, _ = utils.MakeDir(consts.IdiomsImageDir)
}

var cancelMessage = []string{"算啦", "算了", "cancel", "取消", "不知道"}

func guessIdioms(ctx *zero.Ctx) {
defer ctx.Block()
if ctx.Event.GroupID != 0 { // 同一个群,只允许有一个猜成语
if proxy.LockUser(ctx.Event.GroupID) {
ctx.Send("群里还有正在猜的成语,先把它猜出来吧")
Expand All @@ -61,48 +59,49 @@ func guessIdioms(ctx *zero.Ctx) {
ctx.SendChain(message.At(ctx.Event.UserID), message.Text(`猜不出来的话,跟我说"算了"或者"不知道"`), msg)
log.Infof("正确答案:%v", key)
// 等待用户回复
r, cancel := ctx.FutureEvent("message", func(ctx2 *zero.Ctx) bool {
if ctx2.Event.GroupID == 0 { // 私聊消息
ctx2.Block() // 直接阻塞(根据zerobot的机制,只有在所有rule为true时,才会起作用)
return ctx.Event.UserID == ctx2.Event.UserID
} else { // 群消息
if ctx.Event.GroupID == ctx2.Event.GroupID { // 同一个群的消息
guess := strings.TrimSpace(ctx2.Event.Message.ExtractPlainText())
if (ctx2.Event.UserID == ctx.Event.UserID && utils.StringSliceContain(cancelMessage, guess)) ||
guess == key { // 发起人取消 或 有人猜对了答案,阻塞
ctx2.Block()
}
r, cancel := ctx.FutureEvent("message", func(futureCtx *zero.Ctx) bool {
if futureCtx.Event.GroupID == 0 { // 私聊消息
return ctx.Event.GroupID == 0 && ctx.Event.UserID == futureCtx.Event.UserID
} else if ctx.Event.GroupID == futureCtx.Event.GroupID { // 同一个群的群消息
guess := futureCtx.Event.Message.ExtractPlainText()
if (futureCtx.Event.UserID == ctx.Event.UserID && utils.StringSliceContain(cancelMessage, guess)) ||
guess == key { // 发起人取消 或 有人猜对了答案,处理
return true
} // 其它情况不阻塞
return false
}
}
return false
}).Repeat()
loop:
defer cancel()
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()
// 事件循环
for {
select {
case e := <-r:
if e == nil {
return
}
guess := strings.TrimSpace(e.Message.ExtractPlainText())
if e.UserID == ctx.Event.UserID && utils.StringSliceContain(cancelMessage, guess) { // 由发起者表示取消,结束游戏
ctx.SendChain(message.At(e.UserID), message.Text(fmt.Sprintf("那算啦,其实正确答案是%v哦", key)))
cancel()
break loop
return
} else if guess == key { // 猜对,结束游戏
ctx.SendChain(message.At(e.UserID), message.Text("猜对啦"))
cancel()
break loop
return
} else { // 猜错,继续游戏
if ctx.Event.GroupID == 0 { // 只有私聊提示
ctx.Send(message.Text("猜错了哦"))
}
}
case <-time.After(5 * time.Minute): // 超时取消
case <-ticker.C: // 超时取消
ctx.SendChain(message.At(ctx.Event.UserID), message.Text(fmt.Sprintf("太久啦,其实正确答案是%v哦", key)))
cancel()
break loop
return
}
}
}

// 获取猜成语图片:

func getIdiomsPicture() (msg message.MessageSegment, key string, err error) {
if proxy.GetConfigBool("localFirst") {
msg, key, err = getIdiomsPictureLocal()
Expand Down

0 comments on commit 4d8adde

Please sign in to comment.