aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYingtong Li <runassudo@yingtongli.me>2017-03-17 20:48:45 +1100
committerYingtong Li <runassudo@yingtongli.me>2017-03-17 20:48:45 +1100
commit93523382c61667cac4d07cac60a567e7eb2c22d5 (patch)
tree461008803b079bc4e09da7d86ce582c325e0017a
parent69ca874845e0c5055c3b7a4f8e25557ecac8ad21 (diff)
Refactor code to use OOP
-rw-r--r--pblive/__init__.py107
-rw-r--r--pblive/data.py76
-rw-r--r--pblive/templates/question_draw_admin.html4
-rw-r--r--pblive/templates/question_mcq_admin.html2
4 files changed, 118 insertions, 71 deletions
diff --git a/pblive/__init__.py b/pblive/__init__.py
index 5269b62..71fba7f 100644
--- a/pblive/__init__.py
+++ b/pblive/__init__.py
@@ -34,13 +34,7 @@ for f in os.listdir('data'):
if f.endswith('.yaml'):
session_name = f[:-5]
with open(os.path.join('data', f)) as fh:
- session = yaml.load(fh)
- session['name'] = session_name
-
- session['colours'] = [(1, '#f44336'), (2, '#e91e63'), (3, '#9c27b0'), (4, '#673ab7'), (5, '#3f51b5'), (6, '#2196f3'), (7, '#03a9f4'), (8, '#00bcd4'), (9, '#009688'), (10, '#4caf50'), (11, '#8bc34a'), (12, '#cddc39'), (13, '#ffeb3b'), (14, '#ffc107'), (15, '#ff9800'), (16, '#ff5722'), (17, '#795548'), (18, '#9e9e9e'), (19, '#607d8b')]
- session['question_num'] = 0
-
- pblive.data.sessions[session_name] = session
+ pblive.data.sessions[session_name] = data.Session.from_dict(yaml.load(fh), session_name)
@app.route('/')
def index():
@@ -63,49 +57,37 @@ def image(location):
def admin_session(session_name):
return flask.render_template('admin_session.html', session=pblive.data.sessions[session_name])
+@app.route('/debug')
+def debug():
+ assert app.debug == False
+
@socketio.on('join')
def socket_join(session_name):
app.logger.debug('New client {} connected'.format(flask.request.sid))
- user = {'sid': flask.request.sid, 'session_name': session_name, 'answers': {}}
session = pblive.data.sessions[session_name]
+ user = data.User(sid=flask.request.sid, session=session)
pblive.data.users[flask.request.sid] = user
- flask_socketio.join_room(session_name)
-
# Send initial colour picker
flask_socketio.emit('update', flask.render_template('colour_picker.html', session=session), room=flask.request.sid)
def render_question(user, session, question_num):
- if session['questions'][question_num]['type'] == 'mcq':
- template = 'question_mcq.html'
- elif session['questions'][question_num]['type'] == 'random':
- template = 'question_random.html'
- elif session['questions'][question_num]['type'] == 'draw':
- template = 'question_draw.html'
-
- return flask.render_template(template, session=session, user=user, question_num=session['question_num'])
+ return flask.render_template(session.questions[question_num].template, session=session, user=user, question_num=session.question_num)
def render_question_admin(session, question_num):
- if session['questions'][question_num]['type'] == 'mcq':
- template = 'question_mcq_admin.html'
- elif session['questions'][question_num]['type'] == 'random':
- template = 'question_random_admin.html'
- elif session['questions'][question_num]['type'] == 'draw':
- template = 'question_draw_admin.html'
-
- return flask.render_template(template, session=session, question_num=session['question_num'])
+ return flask.render_template(session.questions[question_num].template_admin, session=session, question_num=session.question_num)
@socketio.on('join_admin')
def socket_join(session_name):
app.logger.debug('New admin {} connected'.format(flask.request.sid))
- user = {'sid': flask.request.sid, 'session_name': session_name}
session = pblive.data.sessions[session_name]
+ user = pblive.data.Admin(sid=flask.request.sid, session=session)
pblive.data.admins[flask.request.sid] = user
# Send initial screen
- flask_socketio.emit('update', render_question_admin(session, session['question_num']), room=flask.request.sid)
+ flask_socketio.emit('update', render_question_admin(session, session.question_num), room=flask.request.sid)
@socketio.on('disconnect')
def socket_disconnect():
@@ -113,91 +95,82 @@ def socket_disconnect():
if flask.request.sid in pblive.data.users:
user = pblive.data.users[flask.request.sid]
- session = pblive.data.sessions[user['session_name']]
-
- flask_socketio.leave_room(session['name'])
# Release the colour if it's being held
- if 'colour' in user:
- session['colours'].append(user['colour'])
+ if user.colour:
+ user.session.colours.append(user.colour)
# Relay change
for _, other_user in pblive.data.users.items():
- if other_user != user and 'colour' not in other_user and other_user['session_name'] == session['name']:
- flask_socketio.emit('update', flask.render_template('colour_picker.html', session=session), room=other_user['sid'])
+ if other_user != user and 'colour' not in other_user and other_user.session == user.session:
+ flask_socketio.emit('update', flask.render_template('colour_picker.html', session=user.session), room=other_user.sid)
del pblive.data.users[flask.request.sid]
@socketio.on('register')
def socket_register(colour_id, colour_name):
user = pblive.data.users[flask.request.sid]
- session = pblive.data.sessions[user['session_name']]
+ flask_socketio.emit('update', render_question(user, user.session, user.session.question_num), room=user.sid)
- flask_socketio.emit('update', render_question(user, session, session['question_num']), room=user['sid'])
-
- if 'colour' not in user and (colour_id, colour_name) in session['colours']:
- user['colour'] = (colour_id, colour_name)
- session['colours'].remove((colour_id, colour_name))
+ if not user.colour and (colour_id, colour_name) in user.session.colours:
+ user.colour = (colour_id, colour_name)
+ user.session.colours.remove(user.colour)
# Relay change
for _, other_user in pblive.data.users.items():
- if other_user != user and 'colour' not in other_user and other_user['session_name'] == session['name']:
- flask_socketio.emit('update', flask.render_template('colour_picker.html', session=session), room=other_user['sid'])
+ if other_user != user and not other_user.colour and other_user.session == user.session:
+ flask_socketio.emit('update', flask.render_template('colour_picker.html', session=user.session), room=other_user.sid)
@socketio.on('answer')
def socket_answer(question_num, answer):
user = pblive.data.users[flask.request.sid]
- session = pblive.data.sessions[user['session_name']]
- if question_num == session['question_num']:
- user['answers'][question_num] = answer
+ if question_num == user.session.question_num:
+ user.answers[question_num] = answer
- if session['questions'][session['question_num']]['type'] != 'draw':
- flask_socketio.emit('update', render_question(user, session, session['question_num']), room=user['sid'])
+ if not isinstance(user.session.questions[user.session.question_num], pblive.data.DrawQuestion):
+ flask_socketio.emit('update', render_question(user, user.session, user.session.question_num), room=user.sid)
# Relay change
for _, admin in pblive.data.admins.items():
- flask_socketio.emit('update', render_question_admin(session, session['question_num']), room=admin['sid'])
+ flask_socketio.emit('update', render_question_admin(user.session, user.session.question_num), room=admin.sid)
@socketio.on('reveal_answers')
def socket_reveal_answers(question_num):
user = pblive.data.admins[flask.request.sid]
- session = pblive.data.sessions[user['session_name']]
- session['questions'][question_num]['revealed'] = True
+ user.session.questions[question_num].revealed = True
- flask_socketio.emit('update', render_question_admin(session, session['question_num']), room=flask.request.sid)
+ 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]
- session = pblive.data.sessions[user['session_name']]
- session['question_num'] = question_num
+ user.session.question_num = question_num
# Do work for some questions
- if session['questions'][question_num]['type'] == 'random':
- session['questions'][question_num]['answerer'] = random.choice([user for _, user in pblive.data.users.items() if user['session_name'] == session['name'] and 'colour' in user])
+ 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])
# Relay change
- for _, user in pblive.data.users.items():
- if user['session_name'] == session['name'] and 'colour' in user:
- flask_socketio.emit('update', render_question(user, session, session['question_num']), room=user['sid'])
+ for _, other_user in pblive.data.users.items():
+ 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)
for _, admin in pblive.data.admins.items():
- flask_socketio.emit('update', render_question_admin(session, session['question_num']), room=admin['sid'])
+ flask_socketio.emit('update', render_question_admin(admin.session, admin.session.question_num), room=admin.sid)
@socketio.on('pass_question')
def socket_pass_question():
user = pblive.data.admins[flask.request.sid] if flask.request.sid in pblive.data.admins else pblive.data.users[flask.request.sid]
- session = pblive.data.sessions[user['session_name']]
- if session['questions'][session['question_num']]['type'] == 'random':
+ if isinstance(user.session.questions[user.session.question_num], pblive.data.RandomQuestion):
# Re-randomise answerer
- session['questions'][session['question_num']]['answerer'] = random.choice([user for _, user in pblive.data.users.items() if user['session_name'] == session['name'] and 'colour' in user])
+ user.session.questions[user.session.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])
# Relay change
- for _, user in pblive.data.users.items():
- if user['session_name'] == session['name'] and 'colour' in user:
- flask_socketio.emit('update', render_question(user, session, session['question_num']), room=user['sid'])
+ for _, other_user in pblive.data.users.items():
+ 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)
for _, admin in pblive.data.admins.items():
- flask_socketio.emit('update', render_question_admin(session, session['question_num']), room=admin['sid'])
+ flask_socketio.emit('update', render_question_admin(admin.session, admin.session.question_num), room=admin.sid)
diff --git a/pblive/data.py b/pblive/data.py
index 5512b99..3c8a1b9 100644
--- a/pblive/data.py
+++ b/pblive/data.py
@@ -18,5 +18,79 @@ sessions = {}
users = {}
admins = {}
+class Session:
+ def __init__(self, name=None, title=None, questions=None, colours=None, question_num=0):
+ if questions is None:
+ questions = []
+ if colours is None:
+ colours = [(1, '#f44336'), (2, '#e91e63'), (3, '#9c27b0'), (4, '#673ab7'), (5, '#3f51b5'), (6, '#2196f3'), (7, '#03a9f4'), (8, '#00bcd4'), (9, '#009688'), (10, '#4caf50'), (11, '#8bc34a'), (12, '#cddc39'), (13, '#ffeb3b'), (14, '#ffc107'), (15, '#ff9800'), (16, '#ff5722'), (17, '#795548'), (18, '#9e9e9e'), (19, '#607d8b')]
+
+ self.name = name
+ self.title = title
+ self.questions = questions
+ self.colours = colours
+ self.question_num = question_num
+
+ @classmethod
+ def from_dict(cls, obj, name):
+ return cls(name=name, title=obj['title'], questions=[Question.from_dict(x) for x in obj['questions']])
+
+class Question:
+ def __init__(self, prompt=None, image=None, answers=None, revealed=False):
+ if answers is None:
+ answers = []
+
+ self.prompt = prompt
+ self.image = image
+ self.answers = answers
+ self.revealed = False
+
+ @staticmethod
+ def from_dict(obj):
+ question_types = {
+ 'mcq': MCQQuestion,
+ 'draw': DrawQuestion,
+ 'random': RandomQuestion
+ }
+ question = question_types[obj['type']]()
+ question.load_dict(obj)
+ return question
+
+ def load_dict(self, obj):
+ if 'prompt' in obj:
+ self.prompt = obj['prompt']
+ if 'image' in obj:
+ self.image = obj['image']
+ if 'answers' in obj:
+ self.answers = obj['answers']
+
+class MCQQuestion(Question):
+ template = 'question_mcq.html'
+ template_admin = 'question_mcq_admin.html'
+
+class DrawQuestion(Question):
+ template = 'question_draw.html'
+ template_admin = 'question_draw_admin.html'
+
+class RandomQuestion(Question):
+ template = 'question_random.html'
+ template_admin = 'question_random_admin.html'
+
+ def __init__(self):
+ self.answerer = None
+
+class User:
+ def __init__(self, sid=None, session=None, answers=None, colour=None):
+ if answers is None:
+ answers = {}
+
+ self.sid = sid
+ self.session = session
+ self.answers = answers
+ self.colour = colour
+
+class Admin(User):
+ pass
+
def responses_for_question(session, question_num):
- return len([user for _, user in users.items() if user['session_name'] == session['name'] and question_num in user['answers']])
+ return len([user for _, user in users.items() if user.session == session and question_num in user.answers])
diff --git a/pblive/templates/question_draw_admin.html b/pblive/templates/question_draw_admin.html
index 35f5464..455ff60 100644
--- a/pblive/templates/question_draw_admin.html
+++ b/pblive/templates/question_draw_admin.html
@@ -78,9 +78,9 @@
var tmp;
{% for _, user in data.users.items() %}
- {% if user['session_name'] == session['name'] and question_num in user['answers'] %}
+ {% if user.session == session and question_num in user.answers %}
tmp = new Image();
- tmp.src = '{{ user['answers'][question_num] }}';
+ tmp.src = '{{ user.answers[question_num] }}';
tmp.onload = onImageLoad;
tmp.onerror = onImageLoad;
images.push(tmp);
diff --git a/pblive/templates/question_mcq_admin.html b/pblive/templates/question_mcq_admin.html
index ffd59a2..1f885af 100644
--- a/pblive/templates/question_mcq_admin.html
+++ b/pblive/templates/question_mcq_admin.html
@@ -25,7 +25,7 @@
<li style="line-height: 32px;">
{{ answer }}:
{% for _, user in data.users.items() %}
- {% if user.session_name == session.name and user.answers[question_num] == answer %}
+ {% if user.session == session and user.answers[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 %}
Contact (issues, pull requests, etc.) at git@yingtongli.me. Generated by cgit.