feat: build server connection backbone

This commit is contained in:
2021-07-02 20:51:10 -04:00
parent 593f092c84
commit 4be068dae1
3 changed files with 46 additions and 9 deletions

View File

@@ -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))

View File

@@ -6,6 +6,7 @@ from tetris.util import TextGenerator
from tetris.entity import PieceGenerator from tetris.entity import PieceGenerator
from tetris.entity import Well from tetris.entity import Well
from tetris.entity import Stack from tetris.entity import Stack
from tetris.online import OnlineService
# TODO should be a singleton and refactor the whole file? # TODO should be a singleton and refactor the whole file?
class Game: class Game:
@@ -23,6 +24,7 @@ class Game:
def initialize(self) -> None: def initialize(self) -> None:
pygame.init() pygame.init()
OnlineService.init()
TextGenerator.load(ConfigurationManager.get("image", "font"), (20, 20)) TextGenerator.load(ConfigurationManager.get("image", "font"), (20, 20))
win_width = ConfigurationManager.get("window", "width") win_width = ConfigurationManager.get("window", "width")
@@ -45,11 +47,13 @@ class Game:
main_music_file = ConfigurationManager.get("sound", "main-music") main_music_file = ConfigurationManager.get("sound", "main-music")
self.main_music.set_volume(0.7) # TODO add volume to the config 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 # gets called from the games main loop
def update(self) -> None: def update(self) -> None:
# TODO write not initialized exception # TODO write not initialized exception
OnlineService.update()
elapsed_time = self.clock.tick(self.fps) elapsed_time = self.clock.tick(self.fps)
if not self.next_piece: if not self.next_piece:
@@ -65,6 +69,8 @@ class Game:
pygame.quit() pygame.quit()
sys.exit() sys.exit()
OnlineService.ping_server()
if self.stack: if self.stack:
self.stack.update(elapsed_time, self) self.stack.update(elapsed_time, self)

39
tetris/online.py Normal file
View File

@@ -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