-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenario.go
140 lines (109 loc) · 2.37 KB
/
scenario.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
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"math/rand"
"time"
"github.com/boynux/goward/questions"
"github.com/gen2brain/raylib-go/raygui"
"github.com/gen2brain/raylib-go/raylib"
)
var index int
type order int
const (
Random order = iota
Ordered
)
const (
NextQuestionDelay = 1 * time.Second
)
type Scenario struct {
question []questions.Question
repeat int32
index int32
correct int32
maxErrors int32
activeQuestion questions.Question
order order
}
var (
nextQuestionTimer *time.Timer
lastAnswerWasCorrect bool
)
const (
CorrectAnswer = "correct"
IncorrectAnswer = "Incorrect"
)
func NewScenario(q []questions.Question, repeat, maxErrors int32) *Scenario {
return &Scenario{
q,
repeat,
0,
0,
maxErrors,
q[0],
Ordered,
}
}
func (s *Scenario) Restart() {
s.index = 0
s.correct = 0
if nextQuestionTimer != nil {
nextQuestionTimer.Stop()
nextQuestionTimer = nil
}
}
func (s *Scenario) Play() bool {
if s.index >= s.repeat {
return false
}
if nextQuestionTimer == nil {
s.activeQuestion.Draw(50, 70, 100, 100)
} else {
showResult(lastAnswerWasCorrect)
}
if a := s.activeQuestion.IsAnswerCorrect(); a != nil {
if *a == true || s.activeQuestion.Tries() >= s.maxErrors {
lastAnswerWasCorrect = false
if *a == true {
s.correct = s.correct + 1
lastAnswerWasCorrect = true
}
s.index = s.index + 1
s.activeQuestion.Reset()
nextQuestionTimer = time.AfterFunc(NextQuestionDelay, func() {
nextQuestionTimer = nil
s.activeQuestion = s.RotateQuestions()
})
}
}
return true
}
func (s *Scenario) Order(o order) {
s.order = o
}
func (s *Scenario) RotateQuestions() questions.Question {
rand.Seed(time.Now().UTC().UnixNano())
set := rand.Intn(len(s.question))
if s.order == Ordered {
set = int(s.index * int32(len(s.question)) / s.repeat)
}
return s.question[set]
}
func (s *Scenario) Repeats() (total int32, correct int32) {
total = s.index
correct = s.correct
return
}
func showResult(isCorrect bool) {
c := rl.Green
t := CorrectAnswer
if isCorrect == false {
c = rl.Red
t = IncorrectAnswer
}
r := rl.NewRectangle(40, 90, 20, 20)
o := rl.MeasureText(t, rl.GetFontDefault().BaseSize)
raygui.LabelEx(r, fmt.Sprintf("%s!", t), c, raygui.BackgroundColor(), raygui.BackgroundColor())
r.X = r.X + 10 + float32(o)
raygui.Label(r, "Ready for next question ....")
}