diff --git a/Facepunch.Steamworks.Test/Client/Friends.cs b/Facepunch.Steamworks.Test/Client/Friends.cs new file mode 100644 index 0000000..fb1187d --- /dev/null +++ b/Facepunch.Steamworks.Test/Client/Friends.cs @@ -0,0 +1,41 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Facepunch.Steamworks.Test +{ + [TestClass] + public class Friends + { + [TestMethod] + public void FriendList() + { + using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) + { + Assert.IsTrue( client.Valid ); + + client.Friends.Refresh(); + + Assert.IsNotNull( client.Friends.All ); + + foreach ( var friend in client.Friends.All ) + { + Console.WriteLine( "{0}: {1} (Friend:{2}) (Blocked:{3})", friend.Id, friend.Name, friend.IsFriend, friend.IsBlocked ); + } + } + } + + [TestMethod] + public void FriendListWithoutRefresh() + { + using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) + { + Assert.IsTrue( client.Valid ); + + foreach ( var friend in client.Friends.All ) + { + Console.WriteLine( "{0}: {1} (Friend:{2}) (Blocked:{3})", friend.Id, friend.Name, friend.IsFriend, friend.IsBlocked ); + } + } + } + } +} diff --git a/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj b/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj index 4ddfa31..b4f93b7 100644 --- a/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj +++ b/Facepunch.Steamworks.Test/Facepunch.Steamworks.Test.csproj @@ -81,7 +81,9 @@ - + + False + @@ -89,6 +91,7 @@ + diff --git a/Facepunch.Steamworks/Client/Friends.cs b/Facepunch.Steamworks/Client/Friends.cs index 068fa6f..4719c2c 100644 --- a/Facepunch.Steamworks/Client/Friends.cs +++ b/Facepunch.Steamworks/Client/Friends.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Valve.Steamworks; namespace Facepunch.Steamworks { @@ -28,25 +29,74 @@ public class SteamFriend /// public ulong Id { get; internal set; } + + /// + /// Return true if blocked + /// + public bool IsBlocked { get; internal set; } + /// /// Return true if is a friend. Returns false if blocked, request etc. /// public bool IsFriend { get; internal set; } - /// - /// Returns true if this friend is playing a game (and we know about it) - /// - public bool IsPlaying { get; internal set; } - - /// - /// If they're current in a game, what are they playing? - /// - public int CurrentAppId; - /// /// Their current display name /// public string Name; + + /// + /// Returns true if this friend is online + /// + public bool IsOnline { get; internal set; } + + /// + /// Returns true if this friend is online and playing this game + /// + public bool IsPlayingThisGame { get { return CurrentAppId == Client.AppId; } } + + /// + /// Returns true if this friend is online and playing this game + /// + public bool IsPlaying { get { return CurrentAppId != 0; } } + + /// + /// The AppId this guy is playing + /// + public ulong CurrentAppId { get; internal set; } + + public uint ServerIp { get; internal set; } + public int ServerGamePort { get; internal set; } + public int ServerQueryPort { get; internal set; } + public ulong ServerLobbyId { get; internal set; } + + internal Client Client { get; set; } + + public void Refresh() + { + Name = Client.native.friends.GetFriendPersonaName( Id ); + + EFriendRelationship relationship = (EFriendRelationship) Client.native.friends.GetFriendRelationship( Id ); + + IsBlocked = relationship == EFriendRelationship.k_EFriendRelationshipBlocked; + IsFriend = relationship == EFriendRelationship.k_EFriendRelationshipFriend; + + CurrentAppId = 0; + ServerIp = 0; + ServerGamePort = 0; + ServerQueryPort = 0; + ServerLobbyId = 0; + + FriendGameInfo_t gameInfo = new FriendGameInfo_t(); + if ( Client.native.friends.GetFriendGamePlayed( Id, out gameInfo ) && gameInfo.m_gameID > 0 ) + { + CurrentAppId = gameInfo.m_gameID; + ServerIp = gameInfo.m_unGameIP; + ServerGamePort = gameInfo.m_usGamePort; + ServerQueryPort = gameInfo.m_usQueryPort; + ServerLobbyId = gameInfo.m_steamIDLobby; + } + } } public class Friends @@ -67,6 +117,9 @@ public string GetName( ulong steamid ) private List _allFriends; + /// + /// Returns all friends, even blocked, ignored, friend requests etc + /// public IEnumerable All { get @@ -81,12 +134,49 @@ public IEnumerable All } } + public IEnumerable AllFriends + { + get + { + foreach ( var friend in All ) + { + if ( !friend.IsFriend ) continue; + + yield return friend; + } + } + } + + public IEnumerable AllBlocked + { + get + { + foreach ( var friend in All ) + { + if ( !friend.IsBlocked ) continue; + + yield return friend; + } + } + } + public void Refresh() { + if ( _allFriends == null ) + { + _allFriends = new List(); + } + _allFriends.Clear(); - //client.native.friends.GetFriendCount( 0 ); + var flags = (int) EFriendFlags.k_EFriendFlagAll; + var count = client.native.friends.GetFriendCount( flags ); + for ( int i=0; i