diff options
-rw-r--r-- | api/game/accept.py | 41 | ||||
-rw-r--r-- | api/game/random.py | 7 | ||||
-rw-r--r-- | api/game/socket.py | 1 | ||||
-rw-r--r-- | pages/game.tsx | 37 |
4 files changed, 60 insertions, 26 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 ) diff --git a/pages/game.tsx b/pages/game.tsx index e44a7d9..29eb4f0 100644 --- a/pages/game.tsx +++ b/pages/game.tsx @@ -1,7 +1,6 @@ import { CSSProperties, useState, useEffect, useContext } from 'react'; import axios from 'axios'; import * as cookies from 'react-cookies'; -import { useRouter } from 'next/router'; import { SocketContext } from '../components/socketContext'; import { Socket } from 'socket.io-client'; @@ -29,15 +28,12 @@ function VoerGame(props: { var width = 7; var height = 6; - var [ioListeners, setIoListeners] = useState(false); var [turn, setTurn] = useState(true); // var [winPositions, setWinPositions] = useState<Array<Array<number>>>([]); var [outcome, setOutcome] = useState(-1); var board: Array<number> = [...Array(width * height)].map(() => 0); useEffect(() => { - if (ioListeners) return; - props.io.on("connect", () => console.log("connect")); props.io.on("disconnect", () => console.log("disconnect")); @@ -65,8 +61,6 @@ function VoerGame(props: { type: "normal", icon: <FlagOutlinedIcon/>}); }); - - setIoListeners(true); }, []); return <div style={{ @@ -172,38 +166,43 @@ var InviteButtonLabelStyle: CSSProperties = { } export default function GamePage() { - var [ioListeners, setIoListeners] = useState(false); var [gameID, setGameID] = useState(""); var [player1, setPlayer1] = useState(true); var [active, setActive] = useState(false); - var gameIDUrl = useRouter().query["id"] as string; + var [gameIDUrl, setGameIDUrl] = useState(""); var { io } = useContext(SocketContext); var { toast } = useContext(ToastContext); - if (gameIDUrl && gameIDUrl != gameID) { - // join game - axios.request<{ id: string, player_1: boolean }>({ + useEffect(() => { + var gameIDUrl = new URLSearchParams(window.location.search).get("id") || ""; + setGameIDUrl(gameIDUrl); + + if (!gameIDUrl || gameIDUrl == gameID) return; + + axios.request<{ id: string, player_1: boolean, game_started: boolean }>({ method: "post", url: "/api/game/accept", headers: {"content-type": "application/json"}, data: { id: gameIDUrl } }) - .then(() => { + .then(response => { + setGameID(response.data.id); + setPlayer1(response.data.player_1); + io.emit("registerGameListener", { game_id: response.data.id }); setActive(true); - io.emit("registerGameListener", { game_id: gameIDUrl }); }) - .catch(() => {}); + .catch(err => { + toast({ message: "error", + type: "confirmation", + description: err.toString() }); + }); setGameID(gameIDUrl); - } + }, []); useEffect(() => { - if (ioListeners) return; - io.on("gameStart", () => setActive(true)); - - setIoListeners(true); }, []); return <div> |