aboutsummaryrefslogtreecommitdiff
path: root/api/game
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-02-19 20:32:24 +0100
committerlonkaars <l.leblansch@gmail.com>2021-02-19 20:32:24 +0100
commit0ddd056990d33f335cefc7f0a633e837ce7e116a (patch)
treeb15df5c9ce1d2eb251a0e21cf701ee08cb5a4c2c /api/game
parent1787205f04c9008a753618839bc8da72708f5cab (diff)
working connect 4 game on website
Diffstat (limited to 'api/game')
-rw-r--r--api/game/socket.py15
-rw-r--r--api/game/voerbak_connector.py4
2 files changed, 11 insertions, 8 deletions
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)