/
Quest.go
92 lines (76 loc) · 1.69 KB
/
Quest.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
package quest
import (
"fmt"
"math/rand"
"strings"
)
type Stage struct {
question string
answers map[string]bool
pic []byte
}
func NewStage(question string, answers []string) Stage {
s := Stage{
question: question,
answers: make(map[string]bool, len(answers))}
for _, a := range answers {
s.answers[strings.ToLower(a)] = true
}
return s
}
func (s *Stage) AddPicture(pic []byte) {
s.pic = pic
}
type Quest struct {
stages map[string]Stage
}
func NewQuest() Quest {
return Quest{stages: make(map[string]Stage, 0)}
}
func (q *Quest) AddStage(stageID string, stage Stage) {
if _, found := q.stages[stageID]; found {
panic(fmt.Sprintf("Stage '%s' is already known", stageID))
}
q.stages[stageID] = stage
}
type State struct {
stageIx int
stageOrder []string
}
func (s State) IsFinished() bool {
return s.stageIx >= len(s.stageOrder)
}
func (s State) GetStageID() string {
return s.stageOrder[s.stageIx]
}
func (s State) Next() *State {
s2 := s
s2.stageIx++
return &s2
}
func (q Quest) CheckAnswer(answer string, state State) (newState *State) {
stage := q.stages[state.GetStageID()]
answer = strings.ToLower(answer)
if _, found := stage.answers[answer]; found {
newState = state.Next()
}
return
}
func (q Quest) CreateInitialState() State {
order := make([]string, 0, len(q.stages))
for k, _ := range q.stages {
order = append(order, k)
}
rand.Shuffle(len(order), func(i, j int) {
order[i], order[j] = order[j], order[i]
})
return State{
stageIx: 0,
stageOrder: order}
}
func (q Quest) GetQuestion(state State) string {
return q.stages[state.GetStageID()].question
}
func (q Quest) GetPicture(state State) []byte {
return q.stages[state.GetStageID()].pic
}