From 0ddd056990d33f335cefc7f0a633e837ce7e116a Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 19 Feb 2021 20:32:24 +0100 Subject: working connect 4 game on website --- api/game/socket.py | 15 +++++++++------ api/game/voerbak_connector.py | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'api/game') diff --git a/api/game/socket.py b/api/game/socket.py index 55bff0f..33fbf1b 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -6,28 +6,31 @@ import time import json class game: - def __init__(self, game_id): + def __init__(self, game_id, io): self.game_id = game_id self.board = bord(7, 6) + self.io = io 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 self.board.drop_fisje(column) + self.io.emit("fieldUpdate", { "field": self.board.board }) def run(app): io = SocketIO(app, cors_allowed_origins="*") + games = [game("test_game", io)] namespace = "/game/socket/" @io.on("connect", namespace) def connect(): - print("new connection", namespace) + print("connect") - @io.on("new_move") - def connect(data): - print("new_move") - print(data) + @io.on("newMove") + def new_move(data): + # json_data = json.loads(data) + games[0].move(data["token"], data["move"]) io.run(app, host="127.0.0.1", port=5000, debug=True) diff --git a/api/game/voerbak_connector.py b/api/game/voerbak_connector.py index 6274ada..9627f29 100644 --- a/api/game/voerbak_connector.py +++ b/api/game/voerbak_connector.py @@ -11,7 +11,7 @@ DISC_A = Fore.RED + DISC_SHAPE + Fore.RESET DISC_B = Fore.BLUE + DISC_SHAPE + Fore.RESET EMPTY = Fore.LIGHTBLACK_EX + "_" + Fore.RESET -VOERBAK_LOCATION = "./voerbak" +VOERBAK_LOCATION = os.path.dirname(__file__) + "/voerbak" if os.name == "nt": VOERBAK_LOCATION += ".exe" class bord: @@ -21,7 +21,7 @@ class bord: self.player_1 = True self.board = "0" * (w * h) self.win_positions = [] - self.process = subprocess.Popen(["./voerbak"], + self.process = subprocess.Popen([VOERBAK_LOCATION], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None) -- cgit v1.2.3