diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/events.py | 28 | ||||
-rw-r--r-- | api/game/random.py | 4 | ||||
-rw-r--r-- | api/game/socket.py | 27 |
3 files changed, 41 insertions, 18 deletions
diff --git a/api/events.py b/api/events.py new file mode 100644 index 0000000..8974666 --- /dev/null +++ b/api/events.py @@ -0,0 +1,28 @@ +from flask import Blueprint, request, make_response +from flask_socketio import SocketIO, emit, Namespace, join_room, leave_room, rooms +from socket_io import io +from auth.login_token import token_login +from http import cookies + +from game.cleanup import set_interval +import time + +def get_token(environ): + cookie = environ.get("HTTP_COOKIE") + if not cookie: return None + parsed = cookies.SimpleCookie() + parsed.load(cookie) + token = parsed.get("token") + if not token: return None + return token.value + +@io.on("connect") +def connect(): + token = get_token(request.environ) + if not token: return + + user_id = token_login(token) + if not user_id: return + + join_room("user-" + user_id) + diff --git a/api/game/random.py b/api/game/random.py index bc9518d..54ab2a8 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -4,7 +4,7 @@ from randid import new_uuid import time import json import random -from game.socket import io, game, games, listeners +from game.socket import io, game, games from auth.login_token import token_login random_game = Blueprint('random', __name__) @@ -41,7 +41,7 @@ def index(): 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]) - games[game_id].send("gameStart", ""); + 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 0318e0b..60fea00 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -1,5 +1,5 @@ from flask import Blueprint, request, make_response -from flask_socketio import SocketIO, emit, Namespace +from flask_socketio import SocketIO, emit, Namespace, join_room from game.voerbak_connector import bord from auth.login_token import token_login from db import cursor, connection @@ -8,28 +8,23 @@ import json from socket_io import io games = {} -listeners = {} class game: def __init__(self, game_id, io, player_1_id, player_2_id): self.game_id = game_id + self.room = "game-" + game_id self.board = bord(7, 6) self.io = io self.player_1_id = player_1_id self.player_2_id = player_2_id - def send(self, message, data): - if not self.game_id in listeners: return - for listener in listeners[self.game_id]: - self.io.emit(message, data, room=listener) - def move(self, user_id, column): if user_id != self.player_1_id and user_id != self.player_2_id: return move = self.player_1_id if self.board.player_1 else self.player_2_id if user_id != move: return self.board.drop_fisje(column) - self.send("fieldUpdate", { "field": self.board.board }) + io.emit("fieldUpdate", { "field": self.board.board }, room=self.room) now = int( time.time() * 1000 ) cursor.execute("update games set last_activity = ?, moves = moves || ? || ',' where game_id = ?", [now, column, self.game_id]) @@ -40,18 +35,18 @@ class game: if not self.board.board_full: winner = self.board.board[int(self.board.win_positions[0][0])] outcome = "w" if winner == "2" else "l" - self.send("finish", { + io.emit("finish", { "winPositions": self.board.win_positions, "boardFull": self.board.board_full - }) + }, room=self.room) self.close("finished", outcome) return - self.send("turnUpdate", { "player1": self.board.player_1 }) + io.emit("turnUpdate", { "player1": self.board.player_1 }, room=self.room) def resign(self): self.board.kill_voerbak() - self.send("resign", "") + io.emit("resign", room=self.room) self.close("resign", "d") def close(self, new_status, outcome): @@ -71,7 +66,6 @@ class game: connection.commit() games.pop(self.game_id) - listeners.pop(self.game_id) @io.on("newMove") def new_move(data): @@ -102,7 +96,8 @@ def resign(data): @io.on("registerGameListener") def register_game_listener(data): - if not data["game_id"]: return - if not data["game_id"] in listeners: listeners[data["game_id"]] = set() - listeners[data["game_id"]].add(request.sid) + game_id = data.get("game_id") + if not game_id: return + + join_room("game-" + game_id) |