aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api/game/random.py6
-rw-r--r--api/game/socket.py17
2 files changed, 13 insertions, 10 deletions
diff --git a/api/game/random.py b/api/game/random.py
index f57e4d6..09408e1 100644
--- a/api/game/random.py
+++ b/api/game/random.py
@@ -33,8 +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)
+
+ 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])
return { "id": game_id }, 200
diff --git a/api/game/socket.py b/api/game/socket.py
index 76629f0..3828c35 100644
--- a/api/game/socket.py
+++ b/api/game/socket.py
@@ -10,15 +10,17 @@ from socket_io import io
games = {}
class game:
- def __init__(self, game_id, io):
+ def __init__(self, game_id, io, player_1_id, player_2_id):
self.game_id = 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 move(self, user_id, column):
- # player_1 = cursor.execute("select player_1_id from games where game_id = ?", [self.game_id]).fetchone()[0]
- # player_1_move = player_1 == user_id
- # if not self.board.player_1 == player_1_move: return
+ 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 })
@@ -30,9 +32,10 @@ class game:
@io.on("newMove")
def new_move(data):
- print("socket.py")
- print(games)
- print(data)
+ if not data["game_id"] or \
+ not data["move"] or \
+ not data["token"]: return
+ if not data["game_id"] in games: return
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]