diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-02-21 09:30:40 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-02-21 09:30:40 +0100 |
commit | 96f56b2154195191132fb43c5b27f0a7c2ce1bb6 (patch) | |
tree | a698c06ab5beac7bc65fb22529f7134aa2533b9c /api | |
parent | e1978a9b80f3f7f5a36ca4af5f6df62f494a0d6d (diff) |
working game-id's
Diffstat (limited to 'api')
-rw-r--r-- | api/app.py | 4 | ||||
-rw-r--r-- | api/game/random.py | 11 | ||||
-rw-r--r-- | api/game/socket.py | 30 | ||||
-rw-r--r-- | api/main.py | 16 | ||||
-rw-r--r-- | api/socket_io.py | 5 |
5 files changed, 42 insertions, 24 deletions
diff --git a/api/app.py b/api/app.py new file mode 100644 index 0000000..172e820 --- /dev/null +++ b/api/app.py @@ -0,0 +1,4 @@ +from flask import Flask +from flask_socketio import SocketIO + +app = Flask(__name__) diff --git a/api/game/random.py b/api/game/random.py index bbddedc..f57e4d6 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -4,6 +4,8 @@ from randid import new_uuid import time import json import random +from game.socket import io, games, game +from auth.login_token import token_login random_game = Blueprint('random', __name__) @@ -11,10 +13,14 @@ random_game = Blueprint('random', __name__) def index(): data = request.get_json() + token = request.cookies.get("token") or "" user_id = data.get("user_id") or "" - if not user_id: + if not user_id and not token: print("a temporary user should be set up here") + if not user_id and token: + user_id = token_login(token)[0] + public_games = cursor.execute("select game_id from games where private = FALSE and status = \"wait_for_opponent\"").fetchall() print(public_games) if len(public_games) == 0: @@ -27,5 +33,8 @@ def index(): timestamp = int( time.time() * 1000 ) cursor.execute("update games set player_2_id = ?, status = \"in_progress\", timestamp = ? where game_id = ?", (user_id, timestamp, game_id)) connection.commit() + games[game_id] = game(game_id, io) + print("random.py") + print(games) return { "id": game_id }, 200 diff --git a/api/game/socket.py b/api/game/socket.py index f4ea3a5..76629f0 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -1,9 +1,13 @@ from flask import Blueprint, request, make_response from flask_socketio import SocketIO, emit, Namespace from game.voerbak_connector import bord +from auth.login_token import token_login from db import cursor import time import json +from socket_io import io + +games = {} class game: def __init__(self, game_id, io): @@ -24,21 +28,13 @@ class game: "boardFull": self.board.board_full }) -def run(app): - io = SocketIO(app, cors_allowed_origins="*") - games = [game("test_game", io)] - - namespace = "/game/socket/" - @io.on("connect", namespace) - def connect(): - print("connect") - - @io.on("newMove") - def new_move(data): - # json_data = json.loads(data) - game = games[0] - if(len(game.board.win_positions) > 0 or game.board.board_full): return - game.move(data["token"], data["move"]) - - io.run(app, host="127.0.0.1", port=5000, debug=True) +@io.on("newMove") +def new_move(data): + print("socket.py") + print(games) + print(data) + game = games[data["game_id"]] + if(len(game.board.win_positions) > 0 or game.board.board_full): return + user_id = token_login(data["token"])[0] + game.move(user_id, data["move"]) diff --git a/api/main.py b/api/main.py index 052a6eb..15ead1e 100644 --- a/api/main.py +++ b/api/main.py @@ -1,7 +1,4 @@ -from flask import Flask -from flask_socketio import SocketIO - -app = Flask(__name__) +from app import app from status import status from user.info import info @@ -10,14 +7,21 @@ from auth.login import login from auth.login_token import token from game.new import new_game from game.random import random_game -from game.socket import run app.register_blueprint(status, url_prefix='/') + app.register_blueprint(info, url_prefix='/user') + app.register_blueprint(signup, url_prefix='/auth') app.register_blueprint(login, url_prefix='/auth') app.register_blueprint(token, url_prefix='/auth') + app.register_blueprint(new_game, url_prefix='/game') app.register_blueprint(random_game, url_prefix='/game') -if __name__ == "__main__": run(app) +from socket_io import io +import game.socket + +if __name__ == "__main__": + io.run(app, host="127.0.0.1", port=5000, debug=True) + diff --git a/api/socket_io.py b/api/socket_io.py new file mode 100644 index 0000000..b80e12e --- /dev/null +++ b/api/socket_io.py @@ -0,0 +1,5 @@ +from flask_socketio import SocketIO +from app import app + +io = SocketIO(app, cors_allowed_origins="*") + |