待机配置
This commit is contained in:
@@ -3,26 +3,15 @@ package middleware
|
||||
import (
|
||||
"game-driver/internal/schema"
|
||||
"game-driver/leaf"
|
||||
"game-driver/pkg/tts"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimerAction 定时器中间件,用于定时触发屏幕打印和语音播报。 t 是语音播报实例
|
||||
func TimerAction(t *tts.AliTTS, maxTimeout int) leaf.HandlerFunc {
|
||||
// TimeoutOver 定时器中间件,用于定时触发屏幕打印和语音播报。 t 是语音播报实例
|
||||
func TimeoutOver(maxTimeout int) leaf.HandlerFunc {
|
||||
return func(c *leaf.Context) {
|
||||
pm := leaf.Value[*schema.PlayModal](c, PayloadJSONKey)
|
||||
|
||||
// 构建打印和语音播报的时间映射
|
||||
printMap := make(map[int]schema.PrintModal, len(pm.Print))
|
||||
for _, p := range pm.Print {
|
||||
printMap[p.Time] = p
|
||||
}
|
||||
ttsMap := make(map[int]schema.TTSTimer, len(pm.TTS.Timer))
|
||||
for _, t := range pm.TTS.Timer {
|
||||
ttsMap[t.Time] = t
|
||||
}
|
||||
|
||||
// 定时器
|
||||
var timer *time.Timer
|
||||
if pm.Timeout != 0 {
|
||||
@@ -43,34 +32,16 @@ func TimerAction(t *tts.AliTTS, maxTimeout int) leaf.HandlerFunc {
|
||||
|
||||
cancel := leaf.WithCancel(c)
|
||||
go func() {
|
||||
start := time.Now()
|
||||
// 等待结束
|
||||
wait.Add(1)
|
||||
defer wait.Done()
|
||||
// 定时器
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
// 结束标志
|
||||
for over := false; !over; {
|
||||
select {
|
||||
case <-a:
|
||||
over = true
|
||||
case <-timer.C: // 定时器结束
|
||||
{
|
||||
cancel()
|
||||
over = true
|
||||
leaf.WithValue[leaf.EndType](c, leaf.EndKey, leaf.EndTimer)
|
||||
}
|
||||
case m := <-ticker.C:
|
||||
{
|
||||
s := int(m.Sub(start).Seconds())
|
||||
if _, ok := printMap[s]; ok {
|
||||
//TODO: 屏幕打印
|
||||
}
|
||||
if to, ok := ttsMap[s]; ok {
|
||||
t.Sound(to.Value)
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-a:
|
||||
case <-timer.C: // 定时器结束
|
||||
{
|
||||
cancel()
|
||||
leaf.WithValue[leaf.EndType](c, leaf.EndKey, leaf.EndTimer)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user