diff options
-rw-r--r-- | api/game/random.py | 2 | ||||
-rw-r--r-- | api/game/socket.py | 21 | ||||
-rw-r--r-- | api/user/info.py | 2 | ||||
-rw-r--r-- | components/gameBar.tsx | 7 | ||||
-rw-r--r-- | pages/game.tsx | 5 |
5 files changed, 29 insertions, 8 deletions
diff --git a/api/game/random.py b/api/game/random.py index 38b9ba6..283190a 100644 --- a/api/game/random.py +++ b/api/game/random.py @@ -19,7 +19,7 @@ def index(): print("a temporary user should be set up here") if not user_id and token: - user_id = token_login(token)[0] + user_id = token_login(token) public_games = cursor.execute("select game_id from games where private = FALSE and status = \"wait_for_opponent\"").fetchall() diff --git a/api/game/socket.py b/api/game/socket.py index 4d6c0de..c845e08 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -8,6 +8,7 @@ import json from socket_io import io games = {} +listeners = {} class game: def __init__(self, game_id, io, player_1_id, player_2_id): @@ -17,16 +18,21 @@ class game: 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.io.emit("fieldUpdate", { "field": self.board.board }) - self.io.emit("turnUpdate", { "player1": self.board.player_1 }) + self.send("fieldUpdate", { "field": self.board.board }) + self.send("turnUpdate", { "player1": self.board.player_1 }) if len(self.board.win_positions) > 0 or self.board.board_full: - self.io.emit("finish", { + self.send("finish", { "winPositions": self.board.win_positions, "boardFull": self.board.board_full }) @@ -56,6 +62,7 @@ class game: @io.on("newMove") def new_move(data): + print(request.sid) if not data["game_id"] or \ not data["move"] or \ not data["token"]: return @@ -63,7 +70,7 @@ def new_move(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] + user_id = token_login(data["token"]) game.move(user_id, data["move"]) @io.on("resign") @@ -75,3 +82,9 @@ def resign(data): game = games[data["game_id"]] game.move(user_id, data["move"]) +@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) + diff --git a/api/user/info.py b/api/user/info.py index afb0720..edb7743 100644 --- a/api/user/info.py +++ b/api/user/info.py @@ -19,7 +19,7 @@ def index(): not token: return "", 400 - if token: id = token_login(token)[0] + if token: id = token_login(token) if username: user = cursor.execute("select username, user_id, country, type, registered, avatar from users where username = ?", [username]).fetchone() diff --git a/components/gameBar.tsx b/components/gameBar.tsx index a2ced12..439caea 100644 --- a/components/gameBar.tsx +++ b/components/gameBar.tsx @@ -29,6 +29,7 @@ var GameBarAlignStyle: CSSProperties = { export function GameBar(props: { turn: boolean; player1: boolean; + active: boolean; }) { return <Vierkant className="gameBar" style={{ padding: 8, @@ -48,7 +49,11 @@ export function GameBar(props: { margin: 12, verticalAlign: "top", display: "inline-block" - }}>{ props.turn == props.player1 ? "Jouw beurt" : "Tegenstander" }</h2> + }}>{ + !props.active ? "Wachten op tegenstander..." : + props.turn == props.player1 ? + "Jouw beurt" : "Tegenstander" + }</h2> </div> <div style={{ ...GameBarAlignStyle, diff --git a/pages/game.tsx b/pages/game.tsx index f401bfa..660156d 100644 --- a/pages/game.tsx +++ b/pages/game.tsx @@ -64,12 +64,14 @@ class VoerGame extends Component<VoerGameProps> { winPositions: Array<Array<number>>; outcome: number; board: Array<number>; + saidHello: boolean; } = { userID: "", turn: true, winPositions: [], outcome: -1, board: [], + saidHello: false, }; board = [...Array(this.width * this.height)].map(() => 0); @@ -83,6 +85,7 @@ class VoerGame extends Component<VoerGameProps> { } render() { + this.props.active && this.io.emit("registerGameListener", { game_id: this.props.gameID }); return <div style={{ position: "relative", top: "50%", @@ -91,7 +94,7 @@ class VoerGame extends Component<VoerGameProps> { margin: "0 auto" }}> <VoerBord width={this.width} height={this.height} onMove={m => this.move(m % this.width + 1)} active={this.props.active == true && this.state.outcome == -1}/> - <GameBar turn={this.state.turn} player1={this.props.player1}/> + <GameBar turn={this.state.turn} player1={this.props.player1} active={this.props.active}/> <GameOutcomeDialog outcome={this.state.outcome} player={this.props.player1 ? 1 : 2} visible={this.state.outcome != -1}/> </div> } |