From 4be068dae11ccd73dde2b27616f1508ce3cc7443 Mon Sep 17 00:00:00 2001 From: Giovani Date: Fri, 2 Jul 2021 20:51:10 -0400 Subject: [PATCH] feat: build server connection backbone --- tetris/backend.py | 8 -------- tetris/game.py | 8 +++++++- tetris/online.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) delete mode 100644 tetris/backend.py create mode 100644 tetris/online.py diff --git a/tetris/backend.py b/tetris/backend.py deleted file mode 100644 index 24c6414..0000000 --- a/tetris/backend.py +++ /dev/null @@ -1,8 +0,0 @@ -from signalrcore.hub_connection_builder import HubConnectionBuilder - -server_url = "https://tetriscloneapp.azurewebsites.net/player" -hub_connection = HubConnectionBuilder()\ - .with_url(server_url)\ - .build() - -hub_connection.send("EnterGame", ["abc123"], lambda m: print(m)) \ No newline at end of file diff --git a/tetris/game.py b/tetris/game.py index 238ade6..97a524f 100644 --- a/tetris/game.py +++ b/tetris/game.py @@ -6,6 +6,7 @@ from tetris.util import TextGenerator from tetris.entity import PieceGenerator from tetris.entity import Well from tetris.entity import Stack +from tetris.online import OnlineService # TODO should be a singleton and refactor the whole file? class Game: @@ -23,6 +24,7 @@ class Game: def initialize(self) -> None: pygame.init() + OnlineService.init() TextGenerator.load(ConfigurationManager.get("image", "font"), (20, 20)) win_width = ConfigurationManager.get("window", "width") @@ -45,11 +47,13 @@ class Game: main_music_file = ConfigurationManager.get("sound", "main-music") self.main_music.set_volume(0.7) # TODO add volume to the config - self.main_music.play(mixer.Sound(main_music_file), -1) + #self.main_music.play(mixer.Sound(main_music_file), -1) # gets called from the games main loop def update(self) -> None: # TODO write not initialized exception + OnlineService.update() + elapsed_time = self.clock.tick(self.fps) if not self.next_piece: @@ -65,6 +69,8 @@ class Game: pygame.quit() sys.exit() + OnlineService.ping_server() + if self.stack: self.stack.update(elapsed_time, self) diff --git a/tetris/online.py b/tetris/online.py new file mode 100644 index 0000000..fb5046b --- /dev/null +++ b/tetris/online.py @@ -0,0 +1,39 @@ +import asyncio +import websockets +from threading import Thread + +class OnlineService(): + _URI = "ws://localhost:5001" # TODO remove hardcoded URI and add to config file + _TIMEOUT = 5 / 1000 # Timeout in seconds + _websocket = None + _thread = None + + @classmethod + def init(cls): + thread = Thread(target=asyncio.get_event_loop().run_until_complete, args=(cls._connect_to_server(),)) + cls._thread_pool.append(thread) + thread.start() + + @classmethod + def update(cls): + cls._thread_pool = list(filter(lambda t: t.is_alive(), cls._thread_pool)) + + @classmethod + async def _connect_to_server(cls): + print("Connecting to server...") + cls._websocket = await websockets.connect(cls._URI) + print("Connected to server...") + + @classmethod + def ping_server(cls): + thread = Thread(target=asyncio.get_event_loop().run_until_complete, args=(cls._ping_server(),)) + cls._thread_pool.append(thread) + thread.start() + + @classmethod + async def _ping_server(cls): + if cls._websocket is not None: + await cls._websocket.send("ping") + +# IDEAS +# Create running ONE thread that handles requests and reponses to the server in one running loop