-
Notifications
You must be signed in to change notification settings - Fork 1
/
jukebox.go
78 lines (62 loc) · 1.7 KB
/
jukebox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"log"
"time"
"github.com/creativenucleus/bytejammer/embed"
"github.com/creativenucleus/bytejammer/machines"
)
const (
rotatePeriod = 15 * time.Second
)
type Jukebox struct {
playlist *Playlist
comms *chan Msg
}
func NewJukebox(playlist *Playlist, comms *chan Msg) (*Jukebox, error) {
log.Printf("-> Launching Jukebox for playlist")
j := Jukebox{
comms: comms,
playlist: playlist,
}
return &j, nil
}
func (j *Jukebox) start() {
go func() {
tsFirst := machines.MakeTicStateRunning(embed.LuaJukebox)
code := machines.CodeReplace(tsFirst.GetCode(), map[string]string{
"PLAYLIST_ITEM_COUNT": fmt.Sprintf("%d", len(j.playlist.items)),
"RELEASE_TITLE": RELEASE_TITLE,
})
tsFirst.SetCode(code)
(*j.comms) <- Msg{Type: "tic-state", TicState: tsFirst}
rotateTicker := time.NewTicker(rotatePeriod)
defer rotateTicker.Stop()
for {
select {
case <-rotateTicker.C:
playlistItem, err := j.playlist.getNext()
if err != nil {
log.Println("ERR get code:", err)
break
}
fmt.Printf("Playing (TIC):\nLocation: %s\n", playlistItem.location)
if playlistItem.author != "" {
fmt.Printf("Author: %s\n", playlistItem.author)
}
if playlistItem.description != "" {
fmt.Printf("Description: %s\n", playlistItem.description)
}
code := playlistItem.code
/*
-- Removes vbank 1, so nerfed for now!
if playlistItem.author != "" {
code = machines.CodeAddAuthorShim(code, playlistItem.author)
}
*/
ts := machines.MakeTicStateRunning(code)
(*j.comms) <- Msg{Type: "tic-state", TicState: ts}
}
}
}()
}