Only showing friend's lobbies

This commit is contained in:
Chris Bell 2024-12-19 21:26:19 -06:00
parent a996951932
commit 12d25f5368

View File

@ -43,15 +43,7 @@ func _on_show_lobbies():
func _on_lobbies_received(these_lobbies: Array): func _on_lobbies_received(these_lobbies: Array):
var friend_count = Steam.getFriendCount() var friend_lobbies = get_lobbies_with_friends()
var friend_steam_ids = []
for i in range(friend_count):
friend_steam_ids.append(Steam.getFriendByIndex(i, Steam.FRIEND_FLAG_ALL))
var friend_lobbies = []
for lobby in these_lobbies:
if Steam.getLobbyOwner(lobby) in friend_steam_ids:
friend_lobbies.append(lobby)
# Clear the lobby list box before adding new lobbies # Clear the lobby list box before adding new lobbies
for child in lobby_list_box.get_children(): for child in lobby_list_box.get_children():
@ -62,26 +54,77 @@ func _on_lobbies_received(these_lobbies: Array):
no_lobbies_label.text = "No lobbies available" no_lobbies_label.text = "No lobbies available"
lobby_list_box.add_child(no_lobbies_label) lobby_list_box.add_child(no_lobbies_label)
else: else:
for this_lobby in friend_lobbies: for lobby_id in friend_lobbies.keys():
# Pull lobby data from Steam, these are specific to our example # Pull lobby data from Steam, these are specific to our example
var lobby_name: String = Steam.getLobbyData(this_lobby, "name") var lobby_name: String = Steam.getLobbyData(lobby_id, "name")
var lobby_mode: String = Steam.getLobbyData(this_lobby, "mode") var lobby_mode: String = Steam.getLobbyData(lobby_id, "mode")
# Get the current number of members # Get the current number of members
var lobby_num_members: int = Steam.getNumLobbyMembers(this_lobby) var lobby_num_members: int = Steam.getNumLobbyMembers(lobby_id)
# Create a button for the lobby # Create a button for the lobby
var lobby_button: Button = Button.new() 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_text(lobby_name + " " + str(lobby_num_members) + "/" + str(Steam.getLobbyMemberLimit(lobby_id)))
lobby_button.set_size(Vector2(800, 50)) lobby_button.set_size(Vector2(800, 50))
lobby_button.set_name("lobby_%s" % this_lobby) lobby_button.set_name("lobby_%s" % lobby_id)
lobby_button.connect("pressed", Callable(self, "join_lobby").bind(this_lobby)) lobby_button.connect("pressed", Callable(self, "join_lobby").bind(lobby_id))
# Add the new lobby to the list # Add the new lobby to the list
lobby_list_box.add_child(lobby_button) 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): func join_lobby(lobby_id: int):
NetworkManager.join_lobby(lobby_id) NetworkManager.join_lobby(lobby_id)