/
question.go
40 lines (34 loc) · 1.01 KB
/
question.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
package models
import (
"html"
"math/rand"
)
type Question struct {
Category string `json:"category"`
Type string `json:"type"`
Difficulty string `json:"difficulty"`
Question string `json:"question"`
CorrectAnswer string `json:"correct_answer"`
IncorrectAnswers []string `json:"incorrect_answers"`
}
func (question Question) ToTextMessage() (TextMessage, byte, byte, string) {
var text string
text += html.UnescapeString(question.Question)
text += "\n\n"
perm := rand.Perm(len(question.IncorrectAnswers) + 1)
var currentChar byte = 'A'
var correctAnswer byte
for i := range perm {
text += string(currentChar)
text += " ) "
if perm[i] == 0 {
correctAnswer = currentChar
text += html.UnescapeString(question.CorrectAnswer)
} else {
text += html.UnescapeString(question.IncorrectAnswers[perm[i]-1])
}
text += "\n"
currentChar++
}
return TextMessage{Text: text}, correctAnswer, currentChar - 1, html.UnescapeString(question.CorrectAnswer)
}