diff options
-rw-r--r-- | api/game/new.py | 19 | ||||
-rw-r--r-- | api/game/random.py | 11 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | pages/game.tsx | 32 | ||||
-rw-r--r-- | yarn.lock | 12 |
5 files changed, 63 insertions, 12 deletions
diff --git a/api/game/new.py b/api/game/new.py index 80d0cae..8268ea2 100644 --- a/api/game/new.py +++ b/api/game/new.py @@ -1,8 +1,10 @@ import time +from flask import Blueprint, request from db import cursor, connection from socket_io import io from randid import new_uuid from game.socket import games, game +from auth.login_token import token_login def create_game(user_1_id, private = False, user_2_id = None): timestamp = int( time.time() * 1000 ) @@ -29,4 +31,21 @@ def start_game(game_id, user_2_id): io.emit("gameStart", room=games[game_id].room) +new_game = Blueprint('new_game', __name__) +@new_game.route('/new', methods = ["GET", "POST"]) +def index(): + data = request.get_json() + + token = request.cookies.get("token") or "" + if not token: + print("a temporary user should be set up here") + + user_id = token_login(token) + if not user_id: return "", 403 + + game_id = create_game(user_id, True) + + return { "id": game_id }, 200 + +dynamic_route = ["/game", new_game] diff --git a/api/game/random.py b/api/game/random.py index 9c16627..ffba520 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -1,4 +1,4 @@ -from flask import Blueprint, request, make_response +from flask import Blueprint, request from db import cursor, connection from randid import new_uuid import time @@ -11,17 +11,16 @@ from socket_io import io random_game = Blueprint('random', __name__) -@random_game.route('/random', methods = ['POST']) +@random_game.route('/random') def index(): data = request.get_json() token = request.cookies.get("token") or "" - user_id = data.get("user_id") or "" - if not user_id and not token: + if not token: print("a temporary user should be set up here") - if not user_id and token: - user_id = token_login(token) + user_id = token_login(token) + if not user_id: return "", 403 public_games = cursor.execute("select game_id from games where private = FALSE and status = \"wait_for_opponent\"").fetchall() diff --git a/package.json b/package.json index 5edb6eb..b0e68b5 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@types/react-cookies": "^0.1.0", "@types/uuid": "^8.3.0", "axios": "^0.21.1", + "copy-to-clipboard": "^3.3.1", "email-validator": "^2.0.4", "friendly-time": "^1.1.1", "image-blob-reduce": "^2.2.2", diff --git a/pages/game.tsx b/pages/game.tsx index 29eb4f0..4fa58a9 100644 --- a/pages/game.tsx +++ b/pages/game.tsx @@ -3,6 +3,8 @@ import axios from 'axios'; import * as cookies from 'react-cookies'; import { SocketContext } from '../components/socketContext'; import { Socket } from 'socket.io-client'; +import Icon from '@mdi/react'; +import copy from 'copy-to-clipboard'; import { NavBar } from '../components/navbar'; import { CenteredPage } from '../components/page'; @@ -17,6 +19,7 @@ import WifiTetheringRoundedIcon from '@material-ui/icons/WifiTetheringRounded'; import LinkRoundedIcon from '@material-ui/icons/LinkRounded'; import RefreshIcon from '@material-ui/icons/Refresh'; import FlagOutlinedIcon from '@material-ui/icons/FlagOutlined'; +import { mdiContentCopy } from '@mdi/js'; function VoerGame(props: { gameID: string; @@ -58,8 +61,8 @@ function VoerGame(props: { props.io.on("resign", () => { props.toast({ message: "Het potje is opgegeven", - type: "normal", - icon: <FlagOutlinedIcon/>}); + type: "normal", + icon: <FlagOutlinedIcon/>}); }); }, []); @@ -227,10 +230,7 @@ export default function GamePage() { }}> <Button style={InviteButtonStyle} onclick={() => { axios.request<{ id: string, player_1: boolean, game_started: boolean }>({ - method: "post", url: "/api/game/random", - headers: {"content-type": "application/json"}, - data: {} }) .then(response => { setGameID(response.data.id); @@ -247,7 +247,27 @@ export default function GamePage() { }}/> <h2 style={InviteButtonLabelStyle}>Willekeurige speler</h2> </Button> - <Button style={InviteButtonStyle}> + <Button style={InviteButtonStyle} onclick={() => { + axios.request<{ id: string }>({ + method: "post", + url: "/api/game/new", + headers: {"content-type": "application/json"}, + data: {} + }) + .then(response => { + setGameID(response.data.id); + window.history.replaceState(null, null, "?id=" + response.data.id); + setPlayer1(true); + io.emit("registerGameListener", { game_id: response.data.id }); + setActive(false); + + copy(window.location.href); + toast({ message: "Link gekopiƫerd naar klembord", + type: "confirmation", + icon: <Icon size={1} path={mdiContentCopy}/> }); + }) + .catch(() => {}); + }}> <LinkRoundedIcon style={{ color: "var(--disk-a)", ...InviteButtonIconStyle @@ -3952,6 +3952,13 @@ copy-descriptor@^0.1.0: resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d" integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40= +copy-to-clipboard@^3.3.1: + version "3.3.1" + resolved "https://registry.yarnpkg.com/copy-to-clipboard/-/copy-to-clipboard-3.3.1.tgz#115aa1a9998ffab6196f93076ad6da3b913662ae" + integrity sha512-i13qo6kIHTTpCm8/Wup+0b1mVWETvu2kIMzKoK8FpkLkFxlt0znUAHcMzox+T8sPlqtZXq3CulEjQHsYiGFJUw== + dependencies: + toggle-selection "^1.0.6" + core-js-compat@^3.6.2, core-js-compat@^3.7.0: version "3.7.0" resolved "https://registry.yarnpkg.com/core-js-compat/-/core-js-compat-3.7.0.tgz#8479c5d3d672d83f1f5ab94cf353e57113e065ed" @@ -11944,6 +11951,11 @@ to-regex@^3.0.1, to-regex@^3.0.2: regex-not "^1.0.2" safe-regex "^1.1.0" +toggle-selection@^1.0.6: + version "1.0.6" + resolved "https://registry.yarnpkg.com/toggle-selection/-/toggle-selection-1.0.6.tgz#6e45b1263f2017fa0acc7d89d78b15b8bf77da32" + integrity sha1-bkWxJj8gF/oKzH2J14sVuL932jI= + toidentifier@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553" |