Compare commits
2
Commits
1c1ca14fe7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
772f7d5a9c | ||
|
|
2761172a7c |
@@ -33,6 +33,8 @@ class Model:
|
|||||||
self.connection_manager.on_message = self.handle_incoming
|
self.connection_manager.on_message = self.handle_incoming
|
||||||
self.connection_manager.on_close = self.remove_client
|
self.connection_manager.on_close = self.remove_client
|
||||||
|
|
||||||
|
# Event handlers
|
||||||
|
|
||||||
async def add_client(self, client_id: int) -> None:
|
async def add_client(self, client_id: int) -> None:
|
||||||
print(f'<{client_id}|OPEN>')
|
print(f'<{client_id}|OPEN>')
|
||||||
|
|
||||||
@@ -53,16 +55,6 @@ class Model:
|
|||||||
'type': 'leave'
|
'type': 'leave'
|
||||||
})
|
})
|
||||||
|
|
||||||
async def send_to(self, client_id_from: Optional[int], client_id_to: int, payload: Any) -> None:
|
|
||||||
await self.connection_manager.send_to(client_id_to, json.dumps({
|
|
||||||
'from': client_id_from,
|
|
||||||
'payload': payload
|
|
||||||
}))
|
|
||||||
|
|
||||||
async def broadcast(self, client_id_from: Optional[int], payload: Any) -> None:
|
|
||||||
for client_id_to in self.clients:
|
|
||||||
await self.send_to(client_id_from, client_id_to, payload)
|
|
||||||
|
|
||||||
async def handle_incoming(self, client_id: int, data: str) -> None:
|
async def handle_incoming(self, client_id: int, data: str) -> None:
|
||||||
print(f'<{client_id}|DATA> {data}')
|
print(f'<{client_id}|DATA> {data}')
|
||||||
|
|
||||||
@@ -82,25 +74,29 @@ class Model:
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
|
||||||
if payload['type'] == 'set-name':
|
match payload:
|
||||||
self.clients[client_id].name = payload['name']
|
case {'type': 'set-name', 'name': name}:
|
||||||
await self.broadcast(client_id, {
|
self.clients[client_id].name = name
|
||||||
'type': 'set-name',
|
await self.broadcast(client_id, {
|
||||||
'name': payload['name']
|
'type': 'set-name',
|
||||||
})
|
'name': name
|
||||||
elif payload['type'] == 'chat':
|
})
|
||||||
await self.broadcast(client_id, {
|
|
||||||
'type': 'chat',
|
case {'type': 'chat', 'message': message}:
|
||||||
'message': payload['message']
|
await self.broadcast(client_id, {
|
||||||
})
|
'type': 'chat',
|
||||||
elif payload['type'] == 'roll':
|
'message': message
|
||||||
self.board = self.board_generator.generate_board()
|
})
|
||||||
await self.broadcast(client_id, {
|
|
||||||
'type': 'roll',
|
case {'type': 'roll'}:
|
||||||
'board': self.board
|
self.board = self.board_generator.generate_board()
|
||||||
})
|
await self.broadcast(client_id, {
|
||||||
else:
|
'type': 'roll',
|
||||||
print("Unhandled!")
|
'board': self.board
|
||||||
|
})
|
||||||
|
|
||||||
|
case _:
|
||||||
|
print(' Unhandled!')
|
||||||
|
|
||||||
def get_state_dto(self, client_id: int) -> Any:
|
def get_state_dto(self, client_id: int) -> Any:
|
||||||
return {
|
return {
|
||||||
@@ -111,4 +107,15 @@ class Model:
|
|||||||
],
|
],
|
||||||
'board': self.board
|
'board': self.board
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Message sending
|
||||||
|
|
||||||
|
async def send_to(self, client_id_from: Optional[int], client_id_to: int, payload: Any) -> None:
|
||||||
|
await self.connection_manager.send_to(client_id_to, json.dumps({
|
||||||
|
'from': client_id_from,
|
||||||
|
'payload': payload
|
||||||
|
}))
|
||||||
|
|
||||||
|
async def broadcast(self, client_id_from: Optional[int], payload: Any) -> None:
|
||||||
|
for client_id_to in self.clients:
|
||||||
|
await self.send_to(client_id_from, client_id_to, payload)
|
||||||
|
|||||||
Reference in New Issue
Block a user