diff --git a/venture/.dir-locals.el b/venture/.dir-locals.el new file mode 100644 index 0000000..121583c --- /dev/null +++ b/venture/.dir-locals.el @@ -0,0 +1,4 @@ +;;; Directory Local Variables +;;; For more information see (info "(emacs) Directory Variables") + +((python-mode . ((mode . elpy)))) diff --git a/venture/app.py b/venture/app.py index 23f62b8..450b558 100644 --- a/venture/app.py +++ b/venture/app.py @@ -16,6 +16,7 @@ def on_newgame(): session.room = make_code() session.game = Game(session.room) games[session.room] = session.game + session.game.display = request.sid join_room(session.room) emit('set-code', {'code': session.room}) @@ -23,32 +24,30 @@ def on_newgame(): # Contestant @socketio.on('contestant-join') def on_join_contestant(data): - sid = request.sid signature = data['signature'] session.room = data['room'] join_room(session.room) session.game = games[session.room] - session.game.add_contestant(sid, signature) - emit('contestant-joined', {'sid': sid, + session.game.add_contestant(request.sid, signature) + emit('contestant-joined', {'sid': request.sid, 'signature': signature}, to=session.room) # Host @socketio.on('host-join') def on_join_host(data): - sid = request.sid session.room = data['room'] join_room(session.room) session.game = games[session.room] - session.game.add_host(sid) - emit('host-joined', {'sid': sid}, to=session.room) + session.game.host = request.sid + emit('host-joined', {'sid': request.sid}, to=session.room) @socketio.on('host-start') def on_host_start(): - session.game.get_questions() - emit('categories', [ - c.name for c in session.game.categories[:6]], to=session.room) + session.game.load_clues() + emit('categories', session.game.categories, to=session.game.host) + emit('categories', session.game.categories, to=session.game.display) def main(): diff --git a/venture/game.py b/venture/game.py index 4c41991..57ebd67 100644 --- a/venture/game.py +++ b/venture/game.py @@ -1,54 +1,61 @@ import requests from random import randrange +from collections import namedtuple + + MAX_CATEGORIES = 28100 + class Game: def __init__(self, code): self.code = code self.locked = True self.players = {} - self.categories = [] + self.categories = {} + def add_contestant(self, sid, signature): self.players[sid] = Contestant(signature) - def add_host(self, sid): - self.host = sid - def get_questions(self): + + def load_clues(self): while len(self.categories) < 12: category_id = randrange(MAX_CATEGORIES) - r = requests.get('https://jservice.io/api/category?id=%d' % category_id) + r = requests.get( + 'https://jservice.io/api/category?id=%d' % category_id) j = r.json() category = check_and_build_category(j) if category: - self.categories.append(category) - + name, clues = category + self.categories[name] = clues + + class Contestant: def __init__(self, signature): self.signature = signature self.points = 0 + class Category: def __init__(self, name): self.name = name self.questions = [] -class Question: - def __init__(self, value, question, answer): - self.value = value - self.question = question - self.answer = answer + +Clue = namedtuple('Class', ['value', 'question', 'answer']) + def check_and_build_category(j): if not j or not j['title'] or not j['clues_count'] or j['clues_count'] < 5: return None - category = Category(j['title']) - questions = [Question(c['value'], c['question'], c['answer']) for c in j['clues']] + name = j['title'] + all_clues = [Clue(c['value'], c['question'], c['answer']) + for c in j['clues']] + clues = [] for value in [200, 400, 600, 800, 1000]: - for question in questions: - if question.value == value: - category.questions.append(question) + for clue in all_clues: + if clue.value == value: + clues.append(clue) break - if len(category.questions) == 5: - return category + if len(clues) == 5: + return name, clues else: return None -