From beccb5e5e9cc7229e0e7040b294299f26ab659b1 Mon Sep 17 00:00:00 2001 From: Kyle Kukshtel Date: Mon, 31 Jul 2017 09:29:53 -0700 Subject: [PATCH] Gives LobbyList the ability to be queried for if it is Finished or not. A lobby refresh will finish when the number of cached lobbies equals the number of requests. --- Facepunch.Steamworks/Client/LobbyList.cs | 67 ++++++++++++++++++++---- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/Facepunch.Steamworks/Client/LobbyList.cs b/Facepunch.Steamworks/Client/LobbyList.cs index 7dc9266..cdee4da 100644 --- a/Facepunch.Steamworks/Client/LobbyList.cs +++ b/Facepunch.Steamworks/Client/LobbyList.cs @@ -8,16 +8,36 @@ namespace Facepunch.Steamworks public partial class LobbyList : IDisposable { internal Client client; - public List Lobbies = new List(); + + //The list of retrieved lobbies + public List Lobbies { get; private set; } + + //True when all the possible lobbies have had their data updated + //if the number of lobbies is now equal to the initial request number, we've found all lobbies + public bool Finished { get; private set; } + + //The number of possible lobbies we can get data from + internal List requests; internal LobbyList(Client client) { this.client = client; + Lobbies = new List(); + requests = new List(); } + /// + /// Refresh the List of Lobbies. If no filter is passed in, a default one is created that filters based on AppId ("appid"). + /// + /// public void Refresh ( Filter filter = null) { - if(filter == null) + //init out values + Lobbies.Clear(); + requests.Clear(); + Finished = false; + + if (filter == null) { filter = new Filter(); filter.StringFilters.Add("appid", client.AppId.ToString()); @@ -25,7 +45,7 @@ public void Refresh ( Filter filter = null) client.native.matchmaking.AddRequestLobbyListDistanceFilter((SteamNative.LobbyDistanceFilter)filter.DistanceFilter); - if(filter.SlotsAvailable != null) + if (filter.SlotsAvailable != null) { client.native.matchmaking.AddRequestLobbyListFilterSlotsAvailable((int)filter.SlotsAvailable); } @@ -43,10 +63,10 @@ public void Refresh ( Filter filter = null) { client.native.matchmaking.AddRequestLobbyListNearValueFilter(fil.Key, fil.Value); } - foreach (KeyValuePair> fil in filter.NumericalFilters) - { - client.native.matchmaking.AddRequestLobbyListNumericalFilter(fil.Key, fil.Value.Value, (SteamNative.LobbyComparison)fil.Value.Key); - } + //foreach (KeyValuePair> fil in filter.NumericalFilters) + //{ + // client.native.matchmaking.AddRequestLobbyListNumericalFilter(fil.Key, fil.Value.Value, (SteamNative.LobbyComparison)fil.Value.Key); + //} // this will never return lobbies that are full (via the actual api) @@ -54,23 +74,32 @@ public void Refresh ( Filter filter = null) } + void OnLobbyList(LobbyMatchList_t callback, bool error) { if (error) return; - Lobbies.Clear(); + + //how many lobbies matched uint lobbiesMatching = callback.LobbiesMatching; + // lobbies are returned in order of closeness to the user, so add them to the list in that order for (int i = 0; i < lobbiesMatching; i++) { + //add the lobby to the list of requests ulong lobby = client.native.matchmaking.GetLobbyByIndex(i); + requests.Add(lobby); + + //cast to a LobbyList.Lobby Lobby newLobby = Lobby.FromSteam(client, lobby); if (newLobby.Name != "") { + //if the lobby is valid add it to the valid return lobbies Lobbies.Add(newLobby); + checkFinished(); } else { - //need to get the info for the missing lobby + //else we need to get the info for the missing lobby client.native.matchmaking.RequestLobbyData(lobby); SteamNative.LobbyDataUpdate_t.RegisterCallback(client, OnLobbyDataUpdated); } @@ -80,19 +109,35 @@ void OnLobbyList(LobbyMatchList_t callback, bool error) if (OnLobbiesUpdated != null) { OnLobbiesUpdated(); } } + void checkFinished() + { + if (Lobbies.Count == requests.Count) + { + Finished = true; + return; + } + Finished = false; + } + void OnLobbyDataUpdated(LobbyDataUpdate_t callback, bool error) { if (callback.Success == 1) //1 if success, 0 if failure { + //find the lobby that has been updated Lobby lobby = Lobbies.Find(x => x.LobbyID == callback.SteamIDLobby); - if (lobby == null) //need to add this lobby to the list + + //if this lobby isn't yet in the list of lobbies, we know that we should add it + if (lobby == null) { Lobbies.Add(lobby); + checkFinished(); } //otherwise lobby data in general was updated and you should listen to see what changed if (OnLobbiesUpdated != null) { OnLobbiesUpdated(); } } + + } public Action OnLobbiesUpdated; @@ -109,7 +154,7 @@ public class Filter // Filters that are of string key and int value for that key to be close to public Dictionary NearFilters = new Dictionary(); //Filters that are of string key and int value, with a comparison filter to say how we should relate to the value - public Dictionary> NumericalFilters = new Dictionary>(); + //public Dictionary> NumericalFilters = new Dictionary>(); public Distance DistanceFilter = Distance.Worldwide; public int? SlotsAvailable { get; set; } public int? MaxResults { get; set; }