def join_game(client: Client, user_name, game_id, game_pwd) -> List[Dict]: """Join an existing game. :param client: SocketIO client :param user_name: Name of the user to join the game :param game_id: Game ID :param game_pwd: Game password :return: The 'players' result of SioJoinGame.emit() """ players: List[Dict] = [] # Set the handler when the server responds after the player has joined the game. def on_game_joined(data): nonlocal players players = data['players'] client.on(GAME_JOINED, on_game_joined) client.emit(JOIN_GAME, { 'user_name': user_name, 'game_id': game_id, 'game_pwd': game_pwd }) # Wait until on_new_joined has been called. while not players: time.sleep(1) return players
def assign_roles(client: Client) -> List[Dict[str, str]]: """Triggers the role assignment. :param client: Client that triggers the role assignment. :return: List of dicts with data of the players and their roles. """ roles: List[Dict[str, str]] = [] # Set the handler when the server responds after the roles have been assigned. def on_roles_assigned(data): nonlocal roles roles = data['players'] client.on(ROLES_ASSIGNED, on_roles_assigned) client.emit(ASSIGN_ROLES, {}) # Wait until on_roles_assigned has been called. while not roles: time.sleep(1) return roles
class Model: def __init__(self, render, render_change, endpoint='http://127.0.0.1:5000'): self.endpoint = endpoint self.render = render self.render_change = render_change # self.user = json.loads( # requests.get(f"{endpoint}/register").text)['id'] self.socket = Client(logger=False) self.socket.on('login', self.register_self) self.socket.connect(endpoint) self.socket.emit('login') # def get_data(self): # return json.loads(requests.get(self.endpoint + '/get_data', # data={'id': self.user}).text) def register_self(self, player): # to do this correctly you would need to use rooms self.namespace = f"/data{player}" self.socket = Client(logger=True) # this is bad self.socket.connect(self.endpoint, namespaces=[self.namespace]) self.socket.emit('logged_in', {'id': player}) self.socket.on('start', self.render, namespace=self.namespace) self.socket.on('change', self.render_change, namespace=self.namespace) def send_letter(self, letter): self.socket.emit('type', {'key': letter}, namespace=self.namespace) def remove_letter(self): self.socket.emit('remove', namespace=self.namespace) def publish_current_word(self): self.socket.emit('publish', namespace=self.namespace) def switch_typing_mode(self, event=None): self.socket.emit('toggle', namespace=self.namespace)