-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_script.go
131 lines (116 loc) · 2.42 KB
/
system_script.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package spin
import (
"sort"
"github.com/drop-target-pinball/coroutine"
)
const (
ScriptGroupMode = "GroupMode"
ScriptGroupBall = "GroupBall"
)
type ScriptFn func(*ScriptEnv)
type script struct {
id string
fn ScriptFn
groups []string
cancel coroutine.CancelFunc
}
type scriptSystem struct {
eng *Engine
scripts map[string]*script
//displays map[string]Display
}
func registerScriptSystem(eng *Engine) {
sys := &scriptSystem{
eng: eng,
scripts: make(map[string]*script),
//displays: make(map[string]Display),
}
eng.RegisterActionHandler(sys)
}
func (s *scriptSystem) HandleAction(action Action) {
switch act := action.(type) {
case Debug:
s.debug(act)
// case RegisterDisplay:
// s.registerDisplay(act)
case RegisterScript:
s.registerScript(act)
case PlayScript:
s.playScript(act)
case StopScript:
s.stopScript(act)
case StopScriptGroup:
s.stopScriptGroup(act)
}
}
// func (s *scriptSystem) registerDisplay(act RegisterDisplay) {
// s.displays[act.ID] = act.Display
// }
func (s *scriptSystem) registerScript(a RegisterScript) {
var groups []string
if a.Groups != nil {
groups = a.Groups
}
if a.Group != "" {
groups = []string{a.Group}
}
s.scripts[a.ID] = &script{
id: a.ID,
groups: groups,
fn: a.Script,
}
}
func (s *scriptSystem) playScript(a PlayScript) {
script, ok := s.scripts[a.ID]
if !ok {
Warn("no such script: %v", a.ID)
return
}
if script.cancel != nil {
script.cancel()
}
script.cancel = s.eng.coroutines.NewCoroutine(func(co *coroutine.C) {
s.eng.Post(ScriptStartedEvent(a))
script.fn(newScriptEnv(s.eng, co))
s.eng.Post(ScriptFinishedEvent(a))
script.cancel = nil
})
}
func (s *scriptSystem) stopScript(act StopScript) {
script, ok := s.scripts[act.ID]
if !ok {
Warn("no such script: %v", act.ID)
return
}
if script.cancel != nil {
script.cancel()
}
}
func (s *scriptSystem) stopScriptGroup(act StopScriptGroup) {
for _, script := range s.scripts {
for _, group := range script.groups {
if group == act.ID && script.cancel != nil {
script.cancel()
break
}
}
}
}
func (s *scriptSystem) debug(evt Debug) {
switch evt.ID {
case "Scripts":
s.debugScripts()
}
}
func (s *scriptSystem) debugScripts() {
running := make([]string, 0)
for _, script := range s.scripts {
if script.cancel != nil {
running = append(running, script.id)
}
}
sort.Strings(running)
for _, name := range running {
Log(name)
}
}