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_close = self.remove_client
|
||||
|
||||
# Event handlers
|
||||
|
||||
async def add_client(self, client_id: int) -> None:
|
||||
print(f'<{client_id}|OPEN>')
|
||||
|
||||
@@ -53,16 +55,6 @@ class Model:
|
||||
'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:
|
||||
print(f'<{client_id}|DATA> {data}')
|
||||
|
||||
@@ -82,25 +74,29 @@ class Model:
|
||||
})
|
||||
return
|
||||
|
||||
if payload['type'] == 'set-name':
|
||||
self.clients[client_id].name = payload['name']
|
||||
await self.broadcast(client_id, {
|
||||
'type': 'set-name',
|
||||
'name': payload['name']
|
||||
})
|
||||
elif payload['type'] == 'chat':
|
||||
await self.broadcast(client_id, {
|
||||
'type': 'chat',
|
||||
'message': payload['message']
|
||||
})
|
||||
elif payload['type'] == 'roll':
|
||||
self.board = self.board_generator.generate_board()
|
||||
await self.broadcast(client_id, {
|
||||
'type': 'roll',
|
||||
'board': self.board
|
||||
})
|
||||
else:
|
||||
print("Unhandled!")
|
||||
match payload:
|
||||
case {'type': 'set-name', 'name': name}:
|
||||
self.clients[client_id].name = name
|
||||
await self.broadcast(client_id, {
|
||||
'type': 'set-name',
|
||||
'name': name
|
||||
})
|
||||
|
||||
case {'type': 'chat', 'message': message}:
|
||||
await self.broadcast(client_id, {
|
||||
'type': 'chat',
|
||||
'message': message
|
||||
})
|
||||
|
||||
case {'type': 'roll'}:
|
||||
self.board = self.board_generator.generate_board()
|
||||
await self.broadcast(client_id, {
|
||||
'type': 'roll',
|
||||
'board': self.board
|
||||
})
|
||||
|
||||
case _:
|
||||
print(' Unhandled!')
|
||||
|
||||
def get_state_dto(self, client_id: int) -> Any:
|
||||
return {
|
||||
@@ -111,4 +107,15 @@ class Model:
|
||||
],
|
||||
'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