-
Notifications
You must be signed in to change notification settings - Fork 0
/
mission.go
72 lines (59 loc) · 1.09 KB
/
mission.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
package quester
import "github.com/looplab/fsm"
type Mission struct {
Name string
Next string
Start func()
End func()
Tasks Tasks
currTask int
finished bool
}
func (m *Mission) ResolveCurrentTask(answer string) bool {
curr := m.CurrentTask()
res := true
if curr.Resolve != nil {
res = curr.Resolve(answer)
}
if res {
m.nextTask()
}
return res
}
func (m *Mission) CurrentTask() *Task {
l := len(m.Tasks)
if m == nil || l == 0 || m.IsFinished() {
return nil
}
return m.Tasks[m.currTask]
}
func (m *Mission) Clue() string {
curr := m.CurrentTask()
if curr == nil {
return ""
}
return curr.Clue
}
func (m *Mission) IsFinished() bool {
return m.finished
}
func (m *Mission) nextTask() {
m.currTask++
if m.currTask >= len(m.Tasks) {
m.finished = true
}
}
func (m *Mission) createLeaveCallback(callbacks fsm.Callbacks) {
if m.End != nil {
callbacks["leave_" + m.Name] = func(e *fsm.Event) {
m.End()
}
}
}
func (m *Mission) createEnterCallback(callbacks fsm.Callbacks) {
if m.Start != nil {
callbacks["enter_" + m.Name] = func(e *fsm.Event) {
m.Start()
}
}
}