diff options
author | Yingtong Li <runassudo@yingtongli.me> | 2017-03-27 16:49:07 +1100 |
---|---|---|
committer | Yingtong Li <runassudo@yingtongli.me> | 2017-03-27 16:49:07 +1100 |
commit | d50c7b9d32f1c16a752bca4eed8341570df9168a (patch) | |
tree | 22f7b4ce9139502ba184c54be0064658259a7b8f | |
parent | a1ca469f781f88759e428120e17b33059279fbad (diff) |
Add dodgy initial implementation of speed quiz
-rw-r--r-- | pblive/__init__.py | 37 | ||||
-rw-r--r-- | pblive/data.py | 37 | ||||
-rw-r--r-- | pblive/templates/question_speed.html | 30 | ||||
-rw-r--r-- | pblive/templates/question_speed_admin.html | 40 | ||||
-rw-r--r-- | pblive/templates/question_speed_review.html | 43 | ||||
-rw-r--r-- | pblive/templates/question_speed_review_admin.html | 50 |
6 files changed, 223 insertions, 14 deletions
diff --git a/pblive/__init__.py b/pblive/__init__.py index d0ab26b..63b9d8f 100644 --- a/pblive/__init__.py +++ b/pblive/__init__.py @@ -159,10 +159,18 @@ def socket_answer(question_num, answer): if isinstance(user.session.questions[user.session.question_num], pblive.data.MCQQuestion): flask_socketio.emit('update', render_question(user, user.session, user.session.question_num), room=user.sid) + # Hurry! + #if isinstance(user.session.questions[user.session.question_num], pblive.data.SpeedQuestion): + # if user.session.questions[user.session.question_num].timer_thread is None: + # user.session.questions[user.session.question_num].timer_thread = pblive.data.SpeedQuestionTimerThread(do_goto_question, user.session, user.session.question_num + 1) + # user.session.questions[user.session.question_num].timer_thread.start() + # Relay change for _, other_user in pblive.data.iterate_users(): if other_user.session == user.session: flask_socketio.emit('update_left', render_sidebar(other_user, user.session), room=other_user.sid) + if isinstance(user.session.questions[user.session.question_num], pblive.data.SpeedQuestion): + flask_socketio.emit('update', render_question(other_user, user.session, user.session.question_num), room=other_user.sid) for _, admin in pblive.data.iterate_admins(): if admin.session == user.session: flask_socketio.emit('update', render_question_admin(user.session, user.session.question_num), room=admin.sid) @@ -176,25 +184,28 @@ def socket_reveal_answers(question_num): flask_socketio.emit('update', render_question_admin(user.session, user.session.question_num), room=flask.request.sid) -@socketio.on('goto_question') -def socket_goto_question(question_num): - user = pblive.data.admins[flask.request.sid] - - user.session.question_num = question_num +def do_goto_question(session, question_num): + session.question_num = question_num # Do work for some questions - if isinstance(user.session.questions[question_num], pblive.data.RandomQuestion): - user.session.questions[question_num].answerer = random.choice([other_user for _, other_user in pblive.data.users.items() if other_user.session == user.session and other_user.colour]) + if isinstance(session.questions[question_num], pblive.data.RandomQuestion): + session.questions[question_num].answerer = random.choice([other_user for _, other_user in pblive.data.users.items() if other_user.session == session and other_user.colour]) # Relay change for _, other_user in pblive.data.iterate_users(): - if other_user.session == user.session and other_user.colour: - flask_socketio.emit('update', render_question(other_user, other_user.session, other_user.session.question_num), room=other_user.sid) - flask_socketio.emit('update_left', render_sidebar(other_user, user.session), room=other_user.sid) + if other_user.session == session and other_user.colour: + flask_socketio.emit('update', render_question(other_user, session, session.question_num), room=other_user.sid) + flask_socketio.emit('update_left', render_sidebar(other_user, session), room=other_user.sid) for _, admin in pblive.data.iterate_admins(): - if admin.session == user.session: - flask_socketio.emit('update', render_question_admin(admin.session, admin.session.question_num), room=admin.sid) - flask_socketio.emit('update_left', render_sidebar(admin, user.session), room=admin.sid) + if admin.session == session: + flask_socketio.emit('update', render_question_admin(session, session.question_num), room=admin.sid) + flask_socketio.emit('update_left', render_sidebar(admin, session), room=admin.sid) + +@socketio.on('goto_question') +def socket_goto_question(question_num): + user = pblive.data.admins[flask.request.sid] + + do_goto_question(user.session, question_num) @socketio.on('pass_question') def socket_pass_question(): diff --git a/pblive/data.py b/pblive/data.py index 62187e3..dc1e449 100644 --- a/pblive/data.py +++ b/pblive/data.py @@ -15,6 +15,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import threading +import time server_ip = None @@ -56,13 +57,17 @@ class Question: 'mcq': MCQQuestion, 'draw': DrawQuestion, 'random': RandomQuestion, - 'type': TypeQuestion + 'type': TypeQuestion, + 'speed': SpeedQuestion, + 'speed_review': SpeedReviewQuestion, } question = question_types[obj['type']]() question.load_dict(obj) return question def load_dict(self, obj): + self.type = obj['type'] + if 'prompt' in obj: self.prompt = obj['prompt'] if 'image' in obj: @@ -94,6 +99,36 @@ class TypeQuestion(Question): template = 'question_type.html' template_admin = 'question_type_admin.html' +class SpeedQuestion(MCQQuestion): + template = 'question_speed.html' + template_admin = 'question_speed_admin.html' + + def __init__(self): + self.timer_thread = None + +class SpeedQuestionTimerThread(threading.Thread): + def __init__(self, do_goto_question, session, next_question): + super().__init__() + + self.do_goto_question = do_goto_question + self.session = session + self.next_question = next_question + + self._stop = threading.Event() + + def stop(self): + self._stop.set() + + def run(self): + time.sleep(2) + if self._stop.isSet(): + return + self.do_goto_question(self.session, self.next_question) + +class SpeedReviewQuestion(Question): + template = 'question_speed_review.html' + template_admin = 'question_speed_review_admin.html' + class User: def __init__(self, sid=None, session=None, answers=None, colour=None): if answers is None: diff --git a/pblive/templates/question_speed.html b/pblive/templates/question_speed.html new file mode 100644 index 0000000..fb8a687 --- /dev/null +++ b/pblive/templates/question_speed.html @@ -0,0 +1,30 @@ +{# + PBLive + Copyright © 2017 RunasSudo (Yingtong Li) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +#} + +{% extends 'question.html' %} + +{% block base %} + <div class="ui doubling eight column grid"> + {% for answer in session.questions[question_num].answers %} + <div class="column"><button class="ui button{% if answer == user.answers[question_num] %} primary{% endif %}" onclick="socket.emit('answer', {{ question_num }}, '{{ answer }}');">{{ answer }}</button></div> + {% endfor %} + {% if data.responses_for_question(session, question_num) > 0 %} + <div class="column"><button class="ui negative button">Hurry!</button></div> + {% endif %} + </div> +{% endblock %} diff --git a/pblive/templates/question_speed_admin.html b/pblive/templates/question_speed_admin.html new file mode 100644 index 0000000..a43db2e --- /dev/null +++ b/pblive/templates/question_speed_admin.html @@ -0,0 +1,40 @@ +{# + PBLive + Copyright © 2017 RunasSudo (Yingtong Li) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +#} + +{% extends 'question_admin.html' %} + +{% block base %} + <ul class="ui list"> + {% for answer in session.questions[question_num].answers %} + <li>{{ answer }}</li> + {% endfor %} + </ul> + + {% if data.responses_for_question(session, question_num) > 0 %} + <div><button class="ui negative button">Hurry!</button></div> + {# Very dodgy! #} + <script> + window.setTimeout(function() { + socket.emit('goto_question', {{ question_num + 1 }}); + }, 1500); + </script> + {% endif %} +{% endblock base %} + +{% block footmatter %} +{% endblock %} diff --git a/pblive/templates/question_speed_review.html b/pblive/templates/question_speed_review.html new file mode 100644 index 0000000..8318fb0 --- /dev/null +++ b/pblive/templates/question_speed_review.html @@ -0,0 +1,43 @@ +{# + PBLive + Copyright © 2017 RunasSudo (Yingtong Li) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +#} + +{% extends 'question.html' %} + +{% block base %} + <h1 class="ui dividing header">Quiz finished</h1> + + <ol> + {% for question in session.questions %} + {% set speed_question_num = loop.index0 %} + {% if question.type == 'speed' %} + <li> + {{ question.prompt }} + <ul> + {% for answer in question.answers %} + {% if answer == user.answers[speed_question_num] %} + <li><b>{{ answer }}</b></li> + {% else %} + <li>{{ answer }}</li> + {% endif %} + {% endfor %} + </ul> + </li> + {% endif %} + {% endfor %} + </ol> +{% endblock %} diff --git a/pblive/templates/question_speed_review_admin.html b/pblive/templates/question_speed_review_admin.html new file mode 100644 index 0000000..b963974 --- /dev/null +++ b/pblive/templates/question_speed_review_admin.html @@ -0,0 +1,50 @@ +{# + PBLive + Copyright © 2017 RunasSudo (Yingtong Li) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +#} + +{% extends 'question_admin.html' %} + +{% block base %} + <h1 class="ui dividing header">Quiz review</h1> + + {# ui list gunks things up for some reason #} + <ol> + {% for question in session.questions %} + {% set speed_question_num = loop.index0 %} + {% if question.type == 'speed' %} + <li> + {{ question.prompt }} + <ul> + {% for answer in question.answers %} + <li style="line-height: 32px;"> + {{ answer }}: + {% for _, user in data.users.items() %} + {% if user.session == session and user.answers[speed_question_num] == answer %} + <button class="ui button" style="background-color: {{ user.colour[1] }}; width: 32px; height: 32px; padding: 0 0;">{{ user.colour[0] }}</button> + {% endif %} + {% endfor %} + </li> + {% endfor %} + </ul> + </li> + {% endif %} + {% endfor %} + </ol> +{% endblock %} + +{% block footmatter %} +{% endblock %} |