Categories logic
This commit is contained in:
parent
b45e3e4dad
commit
0a96d571bd
4
venture/.dir-locals.el
Normal file
4
venture/.dir-locals.el
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
;;; Directory Local Variables
|
||||||
|
;;; For more information see (info "(emacs) Directory Variables")
|
||||||
|
|
||||||
|
((python-mode . ((mode . elpy))))
|
@ -16,6 +16,7 @@ def on_newgame():
|
|||||||
session.room = make_code()
|
session.room = make_code()
|
||||||
session.game = Game(session.room)
|
session.game = Game(session.room)
|
||||||
games[session.room] = session.game
|
games[session.room] = session.game
|
||||||
|
session.game.display = request.sid
|
||||||
join_room(session.room)
|
join_room(session.room)
|
||||||
emit('set-code', {'code': session.room})
|
emit('set-code', {'code': session.room})
|
||||||
|
|
||||||
@ -23,32 +24,30 @@ def on_newgame():
|
|||||||
# Contestant
|
# Contestant
|
||||||
@socketio.on('contestant-join')
|
@socketio.on('contestant-join')
|
||||||
def on_join_contestant(data):
|
def on_join_contestant(data):
|
||||||
sid = request.sid
|
|
||||||
signature = data['signature']
|
signature = data['signature']
|
||||||
session.room = data['room']
|
session.room = data['room']
|
||||||
join_room(session.room)
|
join_room(session.room)
|
||||||
session.game = games[session.room]
|
session.game = games[session.room]
|
||||||
session.game.add_contestant(sid, signature)
|
session.game.add_contestant(request.sid, signature)
|
||||||
emit('contestant-joined', {'sid': sid,
|
emit('contestant-joined', {'sid': request.sid,
|
||||||
'signature': signature}, to=session.room)
|
'signature': signature}, to=session.room)
|
||||||
|
|
||||||
|
|
||||||
# Host
|
# Host
|
||||||
@socketio.on('host-join')
|
@socketio.on('host-join')
|
||||||
def on_join_host(data):
|
def on_join_host(data):
|
||||||
sid = request.sid
|
|
||||||
session.room = data['room']
|
session.room = data['room']
|
||||||
join_room(session.room)
|
join_room(session.room)
|
||||||
session.game = games[session.room]
|
session.game = games[session.room]
|
||||||
session.game.add_host(sid)
|
session.game.host = request.sid
|
||||||
emit('host-joined', {'sid': sid}, to=session.room)
|
emit('host-joined', {'sid': request.sid}, to=session.room)
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('host-start')
|
@socketio.on('host-start')
|
||||||
def on_host_start():
|
def on_host_start():
|
||||||
session.game.get_questions()
|
session.game.load_clues()
|
||||||
emit('categories', [
|
emit('categories', session.game.categories, to=session.game.host)
|
||||||
c.name for c in session.game.categories[:6]], to=session.room)
|
emit('categories', session.game.categories, to=session.game.display)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -1,54 +1,61 @@
|
|||||||
import requests
|
import requests
|
||||||
from random import randrange
|
from random import randrange
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
|
|
||||||
MAX_CATEGORIES = 28100
|
MAX_CATEGORIES = 28100
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self, code):
|
def __init__(self, code):
|
||||||
self.code = code
|
self.code = code
|
||||||
self.locked = True
|
self.locked = True
|
||||||
self.players = {}
|
self.players = {}
|
||||||
self.categories = []
|
self.categories = {}
|
||||||
|
|
||||||
def add_contestant(self, sid, signature):
|
def add_contestant(self, sid, signature):
|
||||||
self.players[sid] = Contestant(signature)
|
self.players[sid] = Contestant(signature)
|
||||||
def add_host(self, sid):
|
|
||||||
self.host = sid
|
def load_clues(self):
|
||||||
def get_questions(self):
|
|
||||||
while len(self.categories) < 12:
|
while len(self.categories) < 12:
|
||||||
category_id = randrange(MAX_CATEGORIES)
|
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()
|
j = r.json()
|
||||||
category = check_and_build_category(j)
|
category = check_and_build_category(j)
|
||||||
if category:
|
if category:
|
||||||
self.categories.append(category)
|
name, clues = category
|
||||||
|
self.categories[name] = clues
|
||||||
|
|
||||||
|
|
||||||
class Contestant:
|
class Contestant:
|
||||||
def __init__(self, signature):
|
def __init__(self, signature):
|
||||||
self.signature = signature
|
self.signature = signature
|
||||||
self.points = 0
|
self.points = 0
|
||||||
|
|
||||||
|
|
||||||
class Category:
|
class Category:
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.questions = []
|
self.questions = []
|
||||||
|
|
||||||
class Question:
|
|
||||||
def __init__(self, value, question, answer):
|
Clue = namedtuple('Class', ['value', 'question', 'answer'])
|
||||||
self.value = value
|
|
||||||
self.question = question
|
|
||||||
self.answer = answer
|
|
||||||
|
|
||||||
def check_and_build_category(j):
|
def check_and_build_category(j):
|
||||||
if not j or not j['title'] or not j['clues_count'] or j['clues_count'] < 5:
|
if not j or not j['title'] or not j['clues_count'] or j['clues_count'] < 5:
|
||||||
return None
|
return None
|
||||||
category = Category(j['title'])
|
name = j['title']
|
||||||
questions = [Question(c['value'], c['question'], c['answer']) for c in j['clues']]
|
all_clues = [Clue(c['value'], c['question'], c['answer'])
|
||||||
|
for c in j['clues']]
|
||||||
|
clues = []
|
||||||
for value in [200, 400, 600, 800, 1000]:
|
for value in [200, 400, 600, 800, 1000]:
|
||||||
for question in questions:
|
for clue in all_clues:
|
||||||
if question.value == value:
|
if clue.value == value:
|
||||||
category.questions.append(question)
|
clues.append(clue)
|
||||||
break
|
break
|
||||||
if len(category.questions) == 5:
|
if len(clues) == 5:
|
||||||
return category
|
return name, clues
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user