diff --git a/assets/core/networking/scripts/lobby.gd b/assets/core/networking/scripts/lobby.gd index 2587a40..15bb5d9 100644 --- a/assets/core/networking/scripts/lobby.gd +++ b/assets/core/networking/scripts/lobby.gd @@ -43,25 +43,87 @@ func _on_show_lobbies(): func _on_lobbies_received(these_lobbies: Array): - for this_lobby in these_lobbies: - # Pull lobby data from Steam, these are specific to our example - var lobby_name: String = Steam.getLobbyData(this_lobby, "name") - var lobby_mode: String = Steam.getLobbyData(this_lobby, "mode") + var friend_lobbies = get_lobbies_with_friends() - # Get the current number of members - var lobby_num_members: int = Steam.getNumLobbyMembers(this_lobby) + # Clear the lobby list box before adding new lobbies + for child in lobby_list_box.get_children(): + child.queue_free() - # Create a button for the lobby - var lobby_button: Button = Button.new() - lobby_button.set_text("Lobby %s: %s [%s] - %s Player(s)" % [this_lobby, lobby_name, lobby_mode, lobby_num_members]) - lobby_button.set_size(Vector2(800, 50)) - lobby_button.set_name("lobby_%s" % this_lobby) - lobby_button.connect("pressed", Callable(self, "join_lobby").bind(this_lobby)) + if friend_lobbies.size() == 0: + var no_lobbies_label = Label.new() + no_lobbies_label.text = "No lobbies available" + lobby_list_box.add_child(no_lobbies_label) + else: + for lobby_id in friend_lobbies.keys(): + # Pull lobby data from Steam, these are specific to our example + var lobby_name: String = Steam.getLobbyData(lobby_id, "name") + var lobby_mode: String = Steam.getLobbyData(lobby_id, "mode") - # Add the new lobby to the list - lobby_list_box.add_child(lobby_button) + # Get the current number of members + var lobby_num_members: int = Steam.getNumLobbyMembers(lobby_id) + + # Create a button for the lobby + var lobby_button: Button = Button.new() + lobby_button.set_text(lobby_name + " " + str(lobby_num_members) + "/" + str(Steam.getLobbyMemberLimit(lobby_id))) + lobby_button.set_size(Vector2(800, 50)) + lobby_button.set_name("lobby_%s" % lobby_id) + lobby_button.connect("pressed", Callable(self, "join_lobby").bind(lobby_id)) + + # Add the new lobby to the list + lobby_list_box.add_child(lobby_button) + + + +func get_lobbies_with_friends() -> Dictionary: + var results: Dictionary = {} + + for i in range(0, Steam.getFriendCount()): + var steam_id: int = Steam.getFriendByIndex(i, Steam.FRIEND_FLAG_IMMEDIATE) + var game_info: Dictionary = Steam.getFriendGamePlayed(steam_id) + + if game_info.is_empty(): + # This friend is not playing a game + continue + else: + # They are playing a game, check if it's the same game as ours + var app_id: int = game_info['id'] + var lobby = game_info['lobby'] + + if app_id != Steam.getAppID() or lobby is String: + # Either not in this game, or not in a lobby + continue + + if not results.has(lobby): + results[lobby] = [] + + results[lobby].append(steam_id) + + return results + + +func get_friends_in_lobbies() -> Dictionary: + var results: Dictionary = {} + + for i in range(0, Steam.getFriendCount()): + var steam_id: int = Steam.getFriendByIndex(i, Steam.FRIEND_FLAG_IMMEDIATE) + var game_info: Dictionary = Steam.getFriendGamePlayed(steam_id) + + if game_info.is_empty(): + # This friend is not playing a game + continue + else: + # They are playing a game, check if it's the same game as ours + var app_id: int = game_info['id'] + var lobby = game_info['lobby'] + + if app_id != Steam.getAppID() or lobby is String: + # Either not in this game, or not in a lobby + continue + + results[steam_id] = lobby + + return results - func join_lobby(lobby_id: int): NetworkManager.join_lobby(lobby_id)