Get questions from jservice

This commit is contained in:
2023-02-11 13:30:08 -06:00
parent 872cd8bec4
commit 3723cfb595
3 changed files with 80 additions and 23 deletions

View File

@@ -1,13 +1,54 @@
class Game:
def __init__(self, code):
self.code = code
self.locked = True
self.players = {}
def add_contestant(self, sid, signature):
self.players[sid] = Contestant(signature)
class Contestant:
def __init__(self, signature):
self.signature = signature
self.points = 0
import requests
from random import randrange
MAX_CATEGORIES = 28100
class Game:
def __init__(self, code):
self.code = code
self.locked = True
self.players = {}
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):
while len(self.categories) < 12:
category_id = randrange(MAX_CATEGORIES)
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)
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
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']]
for value in [200, 400, 600, 800, 1000]:
for question in questions:
if question.value == value:
category.questions.append(question)
break
if len(category.questions) == 5:
return category
else:
return None