Get questions from jservice

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

View File

@ -1,4 +1,6 @@
bidict==0.22.1 bidict==0.22.1
certifi==2022.12.7
charset-normalizer==3.0.1
click==8.1.3 click==8.1.3
colorama==0.4.6 colorama==0.4.6
dnspython==2.3.0 dnspython==2.3.0
@ -6,10 +8,13 @@ eventlet==0.33.3
Flask==2.2.2 Flask==2.2.2
Flask-SocketIO==5.3.2 Flask-SocketIO==5.3.2
greenlet==2.0.2 greenlet==2.0.2
idna==3.4
itsdangerous==2.1.2 itsdangerous==2.1.2
Jinja2==3.1.2 Jinja2==3.1.2
MarkupSafe==2.1.2 MarkupSafe==2.1.2
python-engineio==4.3.4 python-engineio==4.3.4
python-socketio==5.7.2 python-socketio==5.7.2
requests==2.28.2
six==1.16.0 six==1.16.0
urllib3==1.26.14
Werkzeug==2.2.2 Werkzeug==2.2.2

View File

@ -1,22 +1,14 @@
from flask import Flask, request from flask import Flask, request
from flask_socketio import SocketIO, emit, join_room, rooms from flask_socketio import SocketIO, emit, join_room, rooms
import requests
from gencode import make_code from gencode import make_code
from game import Game from game import Game
games = {}
app = Flask('venture') app = Flask('venture')
socketio = SocketIO(app, cors_allowed_origins="*") socketio = SocketIO(app, cors_allowed_origins="*")
def with_game(listener): games = {}
global games
room = rooms()[1]
game = games[room]
def inner(event, data={}):
data['game'] = game
listener(game, data)
return inner
@socketio.on('new-game') @socketio.on('new-game')
def on_newgame(): def on_newgame():
@ -26,6 +18,8 @@ def on_newgame():
join_room(code) join_room(code)
emit('set-code', {'code': code}) emit('set-code', {'code': code})
## Contestant
@socketio.on('contestant-join') @socketio.on('contestant-join')
def on_join_contestant(data): def on_join_contestant(data):
sid = request.sid sid = request.sid
@ -35,6 +29,23 @@ def on_join_contestant(data):
games[room].add_contestant(sid, signature) games[room].add_contestant(sid, signature)
emit('contestant-joined', {'sid': sid, 'signature': signature }, to=room) emit('contestant-joined', {'sid': sid, 'signature': signature }, to=room)
## Host
@socketio.on('host-join')
def on_join_host(data):
sid = request.sid
room = data['room']
join_room(room)
games[room].add_host(sid)
emit('host-joined', {'sid': sid}, to=room)
@socketio.on('host-start')
def on_host_start():
room = rooms()[0]
game = games[room]
game.get_questions()
emit('categories', [c.name for c in game.categories[:6]], to=room)
def main(): def main():
socketio.run(app, host='0.0.0.0', debug=True) socketio.run(app, host='0.0.0.0', debug=True)

View File

@ -1,13 +1,54 @@
class Game: import requests
def __init__(self, code): from random import randrange
self.code = code MAX_CATEGORIES = 28100
self.locked = True
self.players = {} class Game:
def add_contestant(self, sid, signature): def __init__(self, code):
self.players[sid] = Contestant(signature) self.code = code
self.locked = True
class Contestant: self.players = {}
def __init__(self, signature): self.categories = []
self.signature = signature def add_contestant(self, sid, signature):
self.points = 0 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