Archived
ugly ws controller
This commit is contained in:
@@ -0,0 +1 @@
|
||||
from controller.client import Client
|
||||
@@ -0,0 +1,15 @@
|
||||
from websockets.server import WebSocketServerProtocol
|
||||
|
||||
|
||||
class Client:
|
||||
websocket: WebSocketServerProtocol
|
||||
|
||||
def __init__(self, websocket: WebSocketServerProtocol):
|
||||
self.websocket = websocket
|
||||
|
||||
async def listen(self):
|
||||
async for message in self.websocket:
|
||||
await self.on_message(message)
|
||||
|
||||
async def on_message(self, message):
|
||||
print(message)
|
||||
+2
-2
@@ -35,8 +35,8 @@ class Card:
|
||||
pass
|
||||
|
||||
|
||||
class CardGuard(Card):
|
||||
"""Represents any guard card"""
|
||||
class CardChampion(Card):
|
||||
"""Represents any champion card"""
|
||||
|
||||
defense: int
|
||||
guard: bool
|
||||
|
||||
@@ -9,7 +9,12 @@ class Player:
|
||||
discard_pile: List[Card]
|
||||
team: Team
|
||||
|
||||
hand: List[Card]
|
||||
board: List[Card]
|
||||
|
||||
def __init__(self, team: Team):
|
||||
self.stack_pile = []
|
||||
self.discard_pile = []
|
||||
self.hand = []
|
||||
self.board = []
|
||||
self.team = team
|
||||
|
||||
+18
-1
@@ -2,11 +2,28 @@
|
||||
import asyncio
|
||||
from game import Game
|
||||
|
||||
from netcode.ws_server import WebSocketServerProtocol
|
||||
from netcode import WSServer
|
||||
from controller import Client
|
||||
|
||||
gameServer = WSServer()
|
||||
hero_game = Game()
|
||||
hero_game.broadcast = gameServer.broadcast
|
||||
|
||||
|
||||
asyncio.run(gameServer.serve())
|
||||
playerServer = WSServer()
|
||||
|
||||
|
||||
async def handler(websocket: WebSocketServerProtocol):
|
||||
client = Client(websocket)
|
||||
await client.listen()
|
||||
|
||||
|
||||
playerServer.message_callbacks.append(handler)
|
||||
|
||||
|
||||
async def main():
|
||||
await asyncio.gather(gameServer.serve(), playerServer.serve(port=8766))
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
+11
-11
@@ -1,5 +1,5 @@
|
||||
import asyncio
|
||||
from typing import Set
|
||||
from typing import Set, Callable, Any, List
|
||||
from websockets.server import serve, WebSocketServerProtocol
|
||||
from websockets import broadcast
|
||||
|
||||
@@ -9,23 +9,23 @@ class WSServer:
|
||||
|
||||
connections: Set[WebSocketServerProtocol]
|
||||
|
||||
message_callbacks: List[Callable[[WebSocketServerProtocol], Any]]
|
||||
|
||||
def __init__(self):
|
||||
self.connections = set()
|
||||
self.message_callbacks = []
|
||||
|
||||
async def _handler(self, websocket: WebSocketServerProtocol):
|
||||
async def handler(self, websocket: WebSocketServerProtocol):
|
||||
"""Is called everytime a client connects to the websocket"""
|
||||
|
||||
await asyncio.wait(
|
||||
[self._track_connection(websocket), self._message_handler(websocket)]
|
||||
await asyncio.gather(
|
||||
self.track_connection(websocket), self.run_callbacks(websocket)
|
||||
)
|
||||
|
||||
async def _message_handler(self, websocket: WebSocketServerProtocol):
|
||||
async def run_callbacks(self, websocket: WebSocketServerProtocol):
|
||||
"""Handles recieving messages from the client"""
|
||||
await asyncio.gather(*[cb(websocket) for cb in self.message_callbacks])
|
||||
|
||||
async for message in websocket:
|
||||
await websocket.send(message)
|
||||
|
||||
async def _track_connection(self, websocket: WebSocketServerProtocol):
|
||||
async def track_connection(self, websocket: WebSocketServerProtocol):
|
||||
"""Keeps track of connected clients in the connections property"""
|
||||
|
||||
try:
|
||||
@@ -36,7 +36,7 @@ class WSServer:
|
||||
async def serve(self, address="0.0.0.0", port=8765):
|
||||
"""Call to start the websocket"""
|
||||
|
||||
async with serve(self._handler, address, port):
|
||||
async with serve(self.handler, address, port):
|
||||
print("Started server on ws://" + address + ":" + str(port))
|
||||
await asyncio.Future() # run forever
|
||||
|
||||
|
||||
Reference in New Issue
Block a user