94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
package routes
|
|
|
|
import (
|
|
"game-driver/internal/common"
|
|
"game-driver/internal/middleware"
|
|
"game-driver/internal/schema"
|
|
"game-driver/leaf"
|
|
"game-driver/pkg/audio"
|
|
"github.com/gopxl/beep/v2"
|
|
"github.com/gopxl/beep/v2/mp3"
|
|
"github.com/gopxl/beep/v2/speaker"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
func timerAction(timestamp int64) <-chan struct{} {
|
|
a := make(chan struct{})
|
|
|
|
go func() {
|
|
if timestamp == 0 {
|
|
close(a)
|
|
} else {
|
|
<-time.After(time.Until(time.Unix(timestamp, 0)))
|
|
close(a)
|
|
}
|
|
}()
|
|
|
|
return a
|
|
}
|
|
|
|
func BackgroundAction(c *leaf.Context) {
|
|
payload := leaf.Value[*schema.BackgroundModel](c, middleware.PayloadJSONKey)
|
|
|
|
if payload.Start != 0 && payload.End != 0 && time.Unix(payload.Start, 0).After(time.Unix(payload.End, 0)) {
|
|
log.Println("开始时间大于结束时间")
|
|
return
|
|
}
|
|
|
|
if payload.End != 0 {
|
|
cancel := leaf.WithDeadline(c, time.Unix(payload.End, 0))
|
|
defer cancel()
|
|
}
|
|
|
|
select {
|
|
case <-c.Done():
|
|
case <-timerAction(payload.Start):
|
|
go audioAction(c, payload.Items[0], payload.TimeModel)
|
|
}
|
|
}
|
|
|
|
func audioAction(c *leaf.Context, item schema.BackgroundItemModel, root schema.TimeModel) {
|
|
if item.Start != 0 && time.Unix(item.Start, 0).Before(time.Unix(root.Start, 0)) {
|
|
log.Println("开始时间小于根任务开始时间")
|
|
return
|
|
}
|
|
|
|
select {
|
|
case <-c.Done():
|
|
case <-timerAction(item.Start):
|
|
{
|
|
log.Println("开始执行后台任务")
|
|
data := common.LinkAudio(item.Data)
|
|
|
|
streamer, format, err := mp3.Decode(data)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer streamer.Close()
|
|
|
|
s := beep.Resample(4, format.SampleRate, audio.DefaultSampleRate, streamer)
|
|
|
|
ctrl := &beep.Ctrl{Streamer: s}
|
|
done := make(chan struct{})
|
|
speaker.Play(beep.Seq(ctrl, beep.Callback(func() {
|
|
close(done)
|
|
})))
|
|
|
|
for {
|
|
select {
|
|
case <-done:
|
|
return
|
|
case <-c.Done():
|
|
{
|
|
speaker.Lock()
|
|
ctrl.Streamer = nil
|
|
speaker.Unlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|