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/game | |
parent | e1978a9b80f3f7f5a36ca4af5f6df62f494a0d6d (diff) |
working game-id's
Diffstat (limited to 'api/game')
-rw-r--r-- | api/game/random.py | 11 | ||||
-rw-r--r-- | api/game/socket.py | 30 |
2 files changed, 23 insertions, 18 deletions
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"]) |