diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c710ac7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +env/ +*.pyc \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..9b1ecb2 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b891cce --- /dev/null +++ b/requirements.txt @@ -0,0 +1,11 @@ +bidict==0.22.1 +click==8.1.3 +colorama==0.4.6 +Flask==2.2.2 +Flask-SocketIO==5.3.2 +itsdangerous==2.1.2 +Jinja2==3.1.2 +MarkupSafe==2.1.2 +python-engineio==4.3.4 +python-socketio==5.7.2 +Werkzeug==2.2.2 diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..831b63c --- /dev/null +++ b/src/__init__.py @@ -0,0 +1,41 @@ +from flask import Flask, render_template, request +from flask_socketio import SocketIO, emit, join_room, rooms + +from src.code import make_code +from src.game import Game + +games = {} + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'secret!' +socketio = SocketIO(app, cors_allowed_origins="*") + +if __name__ == '__main__': + socketio.run(app) + +def with_game(listener): + global games + room = rooms()[1] + game = games[room] + def inner(event, data={}): + data['game'] = game + listener(game, data) + return inner + +@socketio.on('new-game') +def on_newgame(): + data = {} + code = make_code() + + games[code] = Game(code) + join_room(code) + emit('set-code', {'code': code}) + +@socketio.on('contestant-join') +def on_join_contestant(data): + sid = request.sid + signature = data['signature'] + room = data['room'] + join_room(room) + games[room].add_contestant(sid, signature) + emit('contestant-joined', {'sid': sid, 'signature': signature }, to=room) diff --git a/src/code.py b/src/code.py new file mode 100644 index 0000000..508b0a4 --- /dev/null +++ b/src/code.py @@ -0,0 +1,6 @@ +import uuid + +def make_code(): + code = uuid.uuid1().hex[:6] + return code + diff --git a/src/game.py b/src/game.py new file mode 100644 index 0000000..128a3f2 --- /dev/null +++ b/src/game.py @@ -0,0 +1,13 @@ +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 +