-
Notifications
You must be signed in to change notification settings - Fork 42
/
quiz.go
164 lines (140 loc) · 4.37 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
package games
import (
"encoding/json"
"fmt"
"github.com/innogames/slack-bot/bot"
"github.com/innogames/slack-bot/bot/matcher"
"github.com/innogames/slack-bot/client"
"github.com/nlopes/slack"
"github.com/pkg/errors"
"html"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
)
const maxQuestions int = 50 // api limit is 50
const apiUrl string = "https://opentdb.com/api.php"
func NewQuizCommand(slackClient client.SlackClient) *quizCommand {
return &quizCommand{slackClient: slackClient, apiUrl: apiUrl}
}
type quizCommand struct {
slackClient client.SlackClient
quiz Quiz
apiUrl string
}
type question struct {
Category string `json:"category"`
CorrectAnswer string `json:"correct_answer"`
Difficulty string `json:"difficulty"`
IncorrectAnswers []string `json:"incorrect_answers"`
Question string `json:"question"`
Type string `json:"type"`
Answers []string
}
type Quiz struct {
ResponseCode int `json:"response_code"`
Questions []question `json:"results"`
currentQuestion int
tries int
}
func (c quizCommand) GetMatcher() matcher.Matcher {
return matcher.NewGroupMatcher(
matcher.NewRegexpMatcher(`quiz`, c.StartQuiz),
matcher.NewRegexpMatcher(`quiz (?P<questions>\d+)`, c.StartQuiz),
matcher.NewRegexpMatcher(`answer (?P<answer>[\w\s]+)`, c.Answer),
)
}
func (c *quizCommand) StartQuiz(match matcher.Result, event slack.MessageEvent) {
questions := match.GetInt("questions")
if questions == 0 {
questions = 2
}
if questions > maxQuestions {
c.slackClient.Reply(event, fmt.Sprintf("No more than %d questions allowed", maxQuestions))
return
}
resp, err := http.Get(fmt.Sprintf("%s?amount=%d", c.apiUrl, questions))
if err != nil {
c.slackClient.ReplyError(event, errors.Wrap(err, "Error while loading Quiz"))
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.slackClient.ReplyError(event, errors.Wrap(err, "Error while loading Quiz"))
return
}
if err := json.Unmarshal(bodyBytes, &c.quiz); err != nil {
c.slackClient.ReplyError(event, errors.Wrap(err, "Error while loading Quiz"))
return
}
}
c.quiz.currentQuestion = 0
c.quiz.tries = 0
c.parseAnswers()
c.printCurrentQuestion(event)
}
func (c *quizCommand) Answer(match matcher.Result, event slack.MessageEvent) {
c.quiz.tries++
answer := match.GetString("answer")
if number, err := strconv.Atoi(answer); err == nil {
answers := c.getCurrentAnswers()
if number <= len(answers) {
answer = answers[number-1]
}
}
if c.getCurrentQuestion().CorrectAnswer == answer {
c.slackClient.Reply(event, "correct")
c.quiz.currentQuestion++
if c.quiz.currentQuestion == len(c.quiz.Questions) {
c.slackClient.Reply(event, fmt.Sprintf("You finished this quiz with %d Questions. You needed %d answers.", len(c.quiz.Questions), c.quiz.tries))
} else {
c.printCurrentQuestion(event)
}
} else {
c.slackClient.Reply(event, "incorrect. try again")
}
}
func (c *quizCommand) parseAnswers() {
for questionNr, question := range c.quiz.Questions {
answers := append(question.IncorrectAnswers, question.CorrectAnswer)
rand.Shuffle(len(answers), func(i, j int) { answers[i], answers[j] = answers[j], answers[i] })
c.quiz.Questions[questionNr].Answers = answers
}
}
func (c *quizCommand) printCurrentQuestion(event slack.MessageEvent) {
question := c.getCurrentQuestion()
message := fmt.Sprintf(
"Next question (#%d) is of *\"%s\" difficulty* from the category: \"*%s*\"\n",
c.quiz.currentQuestion+1,
html.UnescapeString(question.Difficulty),
html.UnescapeString(question.Category),
)
message += html.UnescapeString(question.Question) + "\n"
for index, answer := range question.Answers {
message += fmt.Sprintf("%d.) %s\n", index+1, html.UnescapeString(answer))
}
message += ":interrobang: Hint type `answer {number}` to send your answer :interrobang:"
c.slackClient.Reply(event, message)
}
func (c *quizCommand) getCurrentQuestion() question {
return c.quiz.Questions[c.quiz.currentQuestion]
}
func (c *quizCommand) getCurrentAnswers() []string {
return c.getCurrentQuestion().Answers
}
func (c *quizCommand) GetHelp() []bot.Help {
return []bot.Help{
{
Command: "quiz",
Description: "small quiz for a nice break",
Examples: []string{
"quiz",
"quiz 10",
"answer 2",
},
},
}
}