-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
106 lines (98 loc) · 1.97 KB
/
functions.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
package spin
func RenderFrameLoop(e *ScriptEnv, fn func(*ScriptEnv)) bool {
for {
fn(e)
if done := e.Sleep(16); done {
fn(e)
return true
}
}
}
func CountdownLoop(e *ScriptEnv, timer *int, tickMs int, end Event) bool {
for *timer > 0 {
if done := e.Sleep(tickMs); done {
return true
}
*timer -= 1
}
e.Post(end)
return false
}
func WatcherTimerLoop(e *ScriptEnv, timer *int, fn func(v int)) bool {
seen := *timer
for {
if done := e.Sleep(16); done {
return true
}
if *timer != seen {
seen = *timer
fn(*timer)
}
}
}
func ScoreHurryUpLoop(e *ScriptEnv, score *int, tickMs int, decScore int, endScore int) bool {
for *score > endScore {
if done := e.Sleep(tickMs); done {
return true
}
*score -= decScore
if *score < endScore {
*score = endScore
}
}
return false
}
func WaitForBallArrivalLoop(e *ScriptEnv, sw string, timeMs int) bool {
for {
if _, done := e.WaitFor(SwitchEvent{ID: sw}); done {
return true
}
evt, done := e.WaitForUntil(timeMs, SwitchEvent{ID: sw, Released: true})
if done {
return true
}
if evt == nil {
return false
}
}
}
func WaitForBallArrivalFunc(e *ScriptEnv, sw string, timeMs int) func() bool {
return func() bool {
return WaitForBallArrivalLoop(e, sw, timeMs)
}
}
func WaitForBallDepartureLoop(e *ScriptEnv, sw string, timeMs int) bool {
for {
if _, done := e.WaitFor(SwitchEvent{ID: sw, Released: true}); done {
return true
}
evt, done := e.WaitForUntil(500, SwitchEvent{ID: sw})
if done {
return true
}
if evt == nil {
return false
}
}
}
func ShotSequenceLoop(e *ScriptEnv, shot string, timeMs int, switches ...string) {
for {
if _, done := e.WaitFor(SwitchEvent{ID: switches[0]}); done {
return
}
shotMade := true
for _, sw := range switches[1:] {
evt, done := e.WaitForUntil(timeMs, SwitchEvent{ID: sw})
if done {
return
}
if evt == nil {
shotMade = false
break
}
}
if shotMade {
e.Post(ShotEvent{ID: shot})
}
}
}