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 | |
parent | e1978a9b80f3f7f5a36ca4af5f6df62f494a0d6d (diff) |
working game-id's
-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 | ||||
-rw-r--r-- | pages/game.tsx | 56 | ||||
-rw-r--r-- | pages/index.tsx | 3 |
7 files changed, 72 insertions, 53 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="*") + diff --git a/pages/game.tsx b/pages/game.tsx index 5f610f8..6890a7e 100644 --- a/pages/game.tsx +++ b/pages/game.tsx @@ -17,7 +17,8 @@ import LinkRoundedIcon from '@material-ui/icons/LinkRounded'; import RefreshIcon from '@material-ui/icons/Refresh'; interface VoerGameProps { - + gameID: string; + token: string; } class VoerGame extends Component<VoerGameProps> { @@ -71,22 +72,12 @@ class VoerGame extends Component<VoerGameProps> { }; board = [...Array(this.width * this.height)].map(() => 0); - userID = ""; move(column: number) { - if(this.state.userID == "") { - axios.request<userInfo>({ - method: "get", - url: `/api/user/info`, - headers: {"content-type": "application/json"} - }) - .then(request => this.setState({ userID: request.data.id })) - .catch(() => {}); - } this.io.emit("newMove", { move: column, - token: cookies.load("token"), - gameID: "fortnite" + token: this.props.token, + game_id: this.props.gameID }); } @@ -172,30 +163,33 @@ export default class GamePage extends Component { constructor(props: {}) { super(props); - if (typeof window === "undefined") return; - if (document.cookie.includes("token") == false) return; - axios.request<userInfo>({ - method: "get", - url: `/api/user/info`, - headers: {"content-type": "application/json"} - }) - .then(request => this.setState({ userID: request.data.id })) - .catch(() => {}); + /* if (typeof window === "undefined") return; */ + /* if (document.cookie.includes("token") == false) return; */ + /* axios.request<userInfo>({ */ + /* method: "get", */ + /* url: `/api/user/info`, */ + /* headers: {"content-type": "application/json"} */ + /* }) */ + /* .then(request => this.setState({ */ + /* user: request.data, */ + /* token: cookies.load("token") */ + /* })) */ + /* .catch(() => {}); */ } state: { - userID: string; gameID: string; + token: string; } = { - userID: "", gameID: "", + token: "", } render() { return <div> <NavBar/> <CenteredPage width={900} style={{ height: "100vh" }}> - <VoerGame/> + <VoerGame gameID={this.state.gameID} token={this.state.token}/> {true && <DialogBox title="Nieuw spel"> <CurrentGameSettings/> <div style={{ @@ -209,9 +203,12 @@ export default class GamePage extends Component { method: "post", url: "/api/game/random", headers: {"content-type": "application/json"}, - data: { user_id: this.state.userID } + data: {} }) - .then(request => this.setState({ gameID: request.data.id })) + .then(request => this.setState({ + gameID: request.data.id, + token: cookies.load("token") + })) .catch(() => {}); }}> <WifiTetheringRoundedIcon style={{ @@ -229,7 +226,10 @@ export default class GamePage extends Component { </Button> </div> <SearchBar label="Zoeken in vriendenlijst"/> - <p>{this.state.gameID}</p> + <code> + {this.state.gameID} + {this.state.token} + </code> </DialogBox>} </CenteredPage> </div> diff --git a/pages/index.tsx b/pages/index.tsx index 84f08b8..f341c35 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -61,7 +61,8 @@ function LoginOrRegisterBox() { display: "inline-block", position: "absolute", fontSize: 14, - left: 0, right: 0, top: 0 + left: 0, right: 0, top: 0, + margin: "0 100px" }}>Log in of maak een account aan om je scores op te slaan en toegang te krijgen tot meer functies</span> <div style={{ display: "grid", |