-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz-1.4.el
277 lines (228 loc) · 9.31 KB
/
quiz-1.4.el
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
;;; quiz.el --- Multiple choice quiz game -*- lexical-binding: t -*-
;; Copyright 2017 by Dave Pearson <[email protected]>
;; Author: Dave Pearson <[email protected]>
;; Version: 1.4
;; Keywords: games, trivia, quiz
;; URL: https://github.com/davep/quiz.el
;; Package-Requires: ((cl-lib "0.5") (emacs "25"))
;; This program is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the
;; Free Software Foundation, either version 3 of the License, or (at your
;; option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
;; Public License for more details.
;;
;; You should have received a copy of the GNU General Public License along
;; with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; quiz.el implements a simple multiple-choice trivia quiz, using
;; https://opentdb.com/ as the back end.
;;; Code:
(require 'cl-lib)
(require 'url-vars)
(require 'json)
(require 'widget)
(eval-when-compile
(require 'wid-edit))
(defgroup quiz nil
"Trivia quiz game using Open Trivia DB as the back end."
:group 'games)
(defface quiz-question-number-face
'((t :height 1.3
:background "black"
:foreground "white"))
"Face for the question number."
:group 'quiz)
(defface quiz-question-face
'((t :weight bold))
"Face for the question."
:group 'quiz)
(defconst quiz-source-url "https://opentdb.com/api.php?amount=%d&encode=base64%s%s"
"URL for loading up questions from the Open Trivia DB.")
(defconst quiz-categories-url "https://opentdb.com/api_category.php"
"URL for loading up the list of quiz categories.")
(defconst quiz-difficulty-levels '("any" "easy" "medium" "hard")
"Levels of difficulty of questions.")
(defconst quiz-user-agent "quiz.el (https://github.com/davep/quiz.el)"
"User agent to send when requesting a quiz.")
(defconst quiz-buffer-name "*Quiz*"
"Name of the quiz buffer.")
(defun quiz-get (url)
"Get quiz data from URL."
(let* ((url-request-extra-headers `(("User-Agent" . ,quiz-user-agent)))
(buffer (url-retrieve-synchronously url t)))
(when buffer
(with-current-buffer buffer
(set-buffer-multibyte t)
(setf (point) (point-min))
(when (search-forward-regexp "^$" nil t)
(buffer-substring (1+ (point)) (point-max)))))))
(defvar quiz-categories nil
"Holds the list of quiz categories once they've been loaded.
Never access this directly, always call `quiz-get-categories' instead.")
(defun quiz-lispify-categories (json-categories)
"Turn JSON-CATEGORIES into a list."
(cl-loop with categories = (make-hash-table :test #'equal)
for cat across (alist-get 'trivia_categories (json-read-from-string json-categories))
do (puthash (alist-get 'name cat) (alist-get 'id cat) categories)
finally return categories))
(defun quiz-get-categories ()
"Return the list of quiz categories."
(or quiz-categories
(setq quiz-categories (quiz-lispify-categories (quiz-get quiz-categories-url)))))
(defun quiz-get-category-names ()
"Return a list of category names."
(cl-loop for cat being the hash-key of (quiz-get-categories)
collect cat into categories
finally return (sort categories #'string<)))
(defun quiz-lispify-questions (json-questions)
"Turn JSON-QUESTIONS into a list."
(alist-get 'results (json-read-from-string json-questions)))
(defun quiz-category-url-param (category)
"Return CATEGORY as a parameter for the quiz URL."
(let ((id (gethash category (quiz-get-categories))))
(if (null id)
""
(format "&category=%d" id))))
(defun quiz-difficulty-url-param (difficulty)
"Return DIFFICULTY as a parameter for the quiz URL."
(if (or (null difficulty) (string= difficulty (car quiz-difficulty-levels)))
""
(format "&difficulty=%s" difficulty)))
(defun quiz-get-questions (&optional count category difficulty)
"Load COUNT questions from the trivia server.
Ten questions are loaded if COUNT isn't supplied.
Specify CATEGORY to only get questions in that category.
DIFFICULTY can be used top optionally set the difficulty of the questions."
(quiz-lispify-questions
(quiz-get (format quiz-source-url
(or count 10)
(quiz-category-url-param category)
(quiz-difficulty-url-param difficulty)))))
(defun quiz-decode (s)
"Decode S."
(decode-coding-string (base64-decode-string s) 'utf-8))
(defun quiz-insert-question-text (questions i)
"From QUESTIONS insert the text of question I."
(insert
(propertize
(quiz-decode (alist-get 'question (aref questions i)))
'font-lock-face 'quiz-question-face)))
(defun quiz-insert-answers (questions i)
"From QUESTIONS insert the answers for question I."
(let ((q (aref questions i)))
(insert " ")
(apply #'widget-create
'radio-button-choice
:indent 2
:notify (lambda (widget &rest _)
(setf (alist-get 'given_answer (aref questions i))
(base64-encode-string (widget-value widget))))
(mapcar (lambda (answer)
(list 'item answer))
(sort
(mapcar #'quiz-decode
(append (alist-get 'incorrect_answers q)
(list (alist-get 'correct_answer q))))
#'string<)))))
(defun quiz-insert-question (questions i)
"From QUESTIONS insert QUESTION I."
(insert
(propertize (format "Question %s:\n" (1+ i)) 'font-lock-face 'quiz-question-number-face)
"\n")
(quiz-insert-question-text questions i)
(insert "\n")
(quiz-insert-answers questions i)
(insert "\n"))
(defun quiz-insert-questions (count category difficulty)
"Get and insert COUNT questions into the current buffer.
Optionally only show questions in the given CATEGORY.
Questions will be at most as hard as DIFFICULTY."
(let ((questions (quiz-get-questions count category difficulty)))
(if questions
(cl-loop for i from 0 to (1- (length questions))
do (quiz-insert-question questions i))
(insert "Sorry. Unable to load up any questions right now."))
questions))
(defun quiz-insert-finish ()
"Insert the finish button for the QUESTIONS."
(widget-create 'push-button
:notify (lambda (&rest _)
(quiz-check-answers))
:help-echo "Check how many correct answers you have"
"Check answers"))
(defun quiz-check-answers ()
"Show the results of the quiz."
(interactive)
(message "%d out of %d questions answered correctly."
(cl-loop for q across quiz-questions
if (string=
(alist-get 'correct_answer q)
(alist-get 'given_answer q ""))
sum 1)
(length quiz-questions)))
(defun quiz-reload ()
"Load a new quiz with the current settings."
(interactive)
(quiz (length quiz-questions) quiz-category quiz-difficulty))
(defvar quiz-mode-map
(let ((map widget-keymap))
(suppress-keymap map t)
(define-key map " " #'quiz-check-answers)
(define-key map "r" #'quiz-reload)
map)
"Local keymap for `quiz'.")
(defun quiz-mode-header-line ()
"Return the header line for a `quiz-mode' buffer."
'(:eval
(format " Quiz | Questions: %d | Category: %s | Difficulty: %s"
(length quiz-questions)
(if (string-empty-p quiz-category) "Any" quiz-category)
(capitalize quiz-difficulty))))
(define-derived-mode quiz-mode special-mode "Quiz"
"Major mode for playing `quiz'.
The key bindings for `quiz-mode' are:
\\{quiz-mode-map}"
(setq truncate-lines nil
header-line-format (quiz-mode-header-line))
(buffer-disable-undo))
(defvar-local quiz-questions nil
"Holds the questions for the current quiz.")
(defvar-local quiz-category nil
"Holds the category for the current set of questions.")
(defvar-local quiz-difficulty nil
"Holds the difficulty for the current set of questions.")
;;;###autoload
(defun quiz (count category difficulty)
"Play a multiple choice trivia quiz with COUNT questions.
If non-blank, questions from only CATEGORY will be asked.
Questions will be at most as hard as DIFFICULTY."
(interactive
(list
(read-number "Questions: " 10)
(completing-read "Category (default any): " (quiz-get-category-names) nil t)
(completing-read
(format "Difficulty (default %s): " (car quiz-difficulty-levels))
quiz-difficulty-levels nil t nil nil (car quiz-difficulty-levels))))
(if (< 51 count 0)
(error "Between 1 and 50 questions would seem sensible")
(when (get-buffer quiz-buffer-name)
(kill-buffer quiz-buffer-name))
(let ((buffer (get-buffer-create quiz-buffer-name)))
(with-current-buffer buffer
(quiz-mode)
(setq quiz-category category
quiz-difficulty difficulty)
(let ((buffer-read-only nil))
(setf (buffer-string) "")
(save-excursion
(setq quiz-questions (quiz-insert-questions count category difficulty))
(quiz-insert-finish))
(widget-forward 1))
(switch-to-buffer buffer)))))
(provide 'quiz)
;;; quiz.el ends here