-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.go
229 lines (189 loc) · 4.62 KB
/
quiz.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"path"
"strings"
"sync"
"time"
)
type answer struct {
Count int
}
type question struct {
Answers map[string]answer
}
type quiz struct {
dirty bool
Questions []question
}
type cmdUpdateRequest struct {
quizID string
question int
answerID string
}
type cmdUpdateResponse struct {
e error
answers map[string]answer
}
// QuizDatabase - exposes an interface for loading and saving quizes
type QuizDatabase interface {
LoadQuiz(name string) (quiz, error)
SaveQuiz(q *quiz, name string) error
}
type QuizDatabaseDirectory struct {
path string
}
func (qdb QuizDatabaseDirectory) getFullPathname(name string) (string, error) {
const illegals = "./\\%$!@| \"<>?"
for _, r := range illegals {
if strings.ContainsRune(name, r) {
return "", fmt.Errorf("%q contains illegal characters", name)
}
}
return (path.Join(qdb.path, name) + ".json"), nil
}
// LoadQuiz - returns a quiz
func (qdb QuizDatabaseDirectory) LoadQuiz(name string) (quiz, error) {
fullname, e := qdb.getFullPathname(name)
if e != nil {
return quiz{}, e
}
file, e := ioutil.ReadFile(fullname)
if e != nil {
return quiz{}, e
}
var result quiz
err := json.Unmarshal(file, &result)
if err != nil {
return quiz{}, err
}
result.dirty = false
return result, nil
}
// SaveQuiz - saves quiz to a file
func (qdb QuizDatabaseDirectory) SaveQuiz(q *quiz, name string) error {
fullname, e := qdb.getFullPathname(name)
if e != nil {
return e
}
buffer, err := json.MarshalIndent(q, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(fullname, buffer, 0666)
return err
}
// QuizManager Managers the quizes and exposes the results via channels
type QuizManager struct {
lock *sync.Mutex
database QuizDatabase
quizes map[string]*quiz
sweeper *time.Ticker
}
// MakeQuizManager - create a quizmanager
func MakeQuizManager(db QuizDatabase) *QuizManager {
r := QuizManager{lock: &sync.Mutex{},
database: db,
quizes: make(map[string]*quiz),
sweeper: time.NewTicker(10 * time.Second)}
go func() {
for {
<-r.sweeper.C
r.saveDirtyQuizes()
}
}()
return &r
}
func (qm QuizManager) shutdown() {
qm.sweeper.Stop()
qm.saveDirtyQuizes()
qm.database = nil
qm.quizes = nil
}
func (qm QuizManager) getQuiz(quizID string) (*quiz, error) {
var result *quiz
result, ok := qm.quizes[quizID]
if !ok {
result, err := qm.database.LoadQuiz(quizID)
if err != nil {
return nil, err
}
qm.quizes[quizID] = &result
return qm.quizes[quizID], nil
}
return result, nil
}
func (qm QuizManager) saveDirtyQuizes() {
qm.lock.Lock()
defer qm.lock.Unlock()
for k, v := range qm.quizes {
if v.dirty == true {
err := qm.database.SaveQuiz(v, k)
if err != nil {
log.Printf("Could not save %q - %s", k, err.Error())
}
v.dirty = false
}
}
}
// SubmitAnswer - submit an answer, return all the answers to the same question
func (qm *QuizManager) SubmitAnswer(quizID string, questionNum int, answerID string) (*question, error) {
qm.lock.Lock()
defer qm.lock.Unlock()
idString := fmt.Sprintf("%s[%d]=%q", quizID, questionNum, answerID)
quiz, err := qm.getQuiz(quizID)
if err != nil {
return nil, fmt.Errorf("%s quizID %q not found", idString, quizID)
}
if (questionNum < 0) || (questionNum >= len(quiz.Questions)) {
return nil, fmt.Errorf("%s question %d out of bounds", idString, questionNum)
}
q := quiz.Questions[questionNum]
foundAnswer, ok := q.Answers[answerID]
if !ok {
return nil, fmt.Errorf("%s answerID %q not found", idString, answerID)
}
foundAnswer.Count = foundAnswer.Count + 1
quiz.dirty = true
q.Answers[answerID] = foundAnswer
log.Printf("QUIZ %s total %d", idString, foundAnswer.Count)
answersCopy := make(map[string]answer)
for k, v := range q.Answers {
answersCopy[k] = v
}
// copy the question
questionCopy := question{
Answers: answersCopy}
return &questionCopy, nil
}
// GetQuizeIDs - returns the IDs of all quizes
func (qm QuizManager) GetQuizeIDs() []string {
qm.lock.Lock()
defer qm.lock.Unlock()
var r []string
for k := range qm.quizes {
r = append(r, k)
}
return r
}
// GetQuizResults - returns a copy of the list of questions in the quiz
func (qm QuizManager) GetQuizResults(quizID string) ([]question, error) {
qm.lock.Lock()
defer qm.lock.Unlock()
quiz, err := qm.getQuiz(quizID)
if err != nil {
return nil, fmt.Errorf("quizID %q not found", quizID)
}
var result []question
for _, q := range quiz.Questions {
newQuestion := question{Answers: make(map[string]answer)}
for k, v := range q.Answers {
newQuestion.Answers[k] = v
}
result = append(result, newQuestion)
}
return result, nil
}