diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-03-22 10:50:57 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-03-22 10:50:57 +0100 |
commit | 4f5a1710891f2ac959d5f1a41de26a8854c9085f (patch) | |
tree | 871579001c28d52350a020907a8f4a7c2e6540e6 /api | |
parent | 69a9ecfe7a7f5de1c59a066a83dee22d0788e24a (diff) |
link invites + game/random cleanup
Diffstat (limited to 'api')
-rw-r--r-- | api/game/accept.py | 41 | ||||
-rw-r--r-- | api/game/random.py | 7 | ||||
-rw-r--r-- | api/game/socket.py | 1 |
3 files changed, 42 insertions, 7 deletions
diff --git a/api/game/accept.py b/api/game/accept.py new file mode 100644 index 0000000..66f20b6 --- /dev/null +++ b/api/game/accept.py @@ -0,0 +1,41 @@ +from flask import Blueprint, request, make_response +from db import cursor, connection +from randid import new_uuid +import time +import json +import random +from game.socket import game, games +from auth.login_token import token_login +from game.info import valid_game_id +from socket_io import io +from game.new import start_game + +join_game = Blueprint('game_accept', __name__) + +@join_game.route('/accept', methods = ['POST']) +def index(): + data = request.get_json() + + token = request.cookies.get("token") or "" + game_id = data.get("id") or "" + + if not valid_game_id(game_id): return "", 403 + + if not token: + print("a temporary user should be set up here") + + user_id = token_login(token) + if not user_id: return "", 403 + + if cursor.execute("select status from games where game_id = ?", [game_id]).fetchone()[0] != "wait_for_opponent": + return "", 403 + + start_game(game_id, user_id) + + return { + "id": game_id, + "player_1": False, + "game_started": True + }, 200 + +dynamic_route = ["/game", join_game] diff --git a/api/game/random.py b/api/game/random.py index ae7dc95..9c16627 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -25,8 +25,6 @@ def index(): public_games = cursor.execute("select game_id from games where private = FALSE and status = \"wait_for_opponent\"").fetchall() - timestamp = int( time.time() * 1000 ) - game_started = False if len(public_games) == 0: @@ -37,11 +35,6 @@ def index(): start_game(game_id, user_id) - players = cursor.execute("select player_1_id, player_2_id from games where game_id = ?", [game_id]).fetchone() - games[game_id] = game(game_id, io, players[0], players[1]) - - io.emit("gameStart", room=games[game_id].room) - player_1 = False game_started = True diff --git a/api/game/socket.py b/api/game/socket.py index 60fea00..5f0d710 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -24,6 +24,7 @@ class game: if user_id != move: return self.board.drop_fisje(column) + io.emit("fieldUpdate", { "field": self.board.board }, room=self.room) now = int( time.time() * 1000 ) |