def format_msg(text, m_type, msg=None): """Format the given string text to JSON for sending over the network.""" full = {'text': text, 'type': m_type.name} if msg is not None: full.update(msg) return utf(json.dumps(full)) else: return utf(json.dumps(full))
def broadcast_message(self, msg): assert len(connected) > 0 for client in connected: # Don't send to yourself if client == self: continue client.sendMessage(utf(msg), False)
def send_message(self, data): """Given a Python object, convert it to JSON and send it.""" assert 'type' in data, 'cannot send a message without giving it a type (e.g., the message dict needs an entry called "type"). (data object: {})'.format( str(data)) as_json = json.dumps(data, default=dump_obj_dict) # info('sending message: {}'.format(as_json), INFO_ID) self.client_protocol.sendMessage(utf(as_json), isBinary=False)
def send_lobby_list(player_connection): """Send the player connection a list of available lobbies.""" message = {'type': MSG.lobby_info.name, 'lobbies': []} for lobby in lobbies: max_players = MAX_LOBBY_SIZE if not lobby.is_in_game() else -1 lobby_info = { 'lobby_id': lobby.get_id(), 'lobby_name': lobby.get_name(), 'num_players': lobby.size(), 'max_players': max_players, 'players': [user_ids[player] for player in lobby.get_players()] } message['lobbies'].append(lobby_info) as_json = json.dumps(message) player_connection.sendMessage(utf(as_json), False)
def send_lobby_list(player_connection): """Send the player connection a list of available lobbies.""" message = { 'type': MSG.lobby_info.name, 'lobbies': [] } for lobby in lobbies: max_players = MAX_LOBBY_SIZE if not lobby.is_in_game() else -1 lobby_info = { 'lobby_id': lobby.get_id(), 'lobby_name': lobby.get_name(), 'num_players': lobby.size(), 'max_players': max_players, 'players': [user_ids[player] for player in lobby.get_players()] } message['lobbies'].append(lobby_info) as_json = json.dumps(message) player_connection.sendMessage(utf(as_json), False)
def broadcast_to_lobby(self, msg, send_self=False): """Broadcast a message to rest of the sender's lobby""" lobby = get_players_lobby(self) for client in lobby.get_all(): if send_self or client != self: client.sendMessage(utf(msg), False)
def handleCreepRequest(self, json_msg): lobby = get_players_lobby(self) lobby.get_game_client().sendMessage(utf(json_msg), False)
def handleDeleteTower(self, json_msg): """A client has requested that a tower be deleted.""" lobby = get_players_lobby(self) assert lobby is not None game = lobby.get_game_client() game.sendMessage(utf(json_msg))
def format_msg(text, m_type): """Format the given string text to JSON for sending over the network.""" return utf(json.dumps({'text': text, 'type': m_type.name}))
def handleTowerRequest(self, json_msg): gameloop_client.sendMessage(utf(json_msg), False)