Created
May 30, 2020 16:37
-
-
Save magicien/7296e6344527a830149f4482572404bf to your computer and use it in GitHub Desktop.
Epic Online Services: Get friend data sample in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import EOSSDK | |
var platformHandle: EOS_HPlatform | |
var accountID: EOS_EpicAccountId | |
// 1. Send a query to get friends info | |
func initAndLogin() { | |
guard let friendsHandle = EOS_Platform_GetFriendsInterface(platformHandle) else { | |
return | |
} | |
var friendOptions = EOS_Friends_QueryFriendsOptions() | |
friendOptions.ApiVersion = EOS_FRIENDS_QUERYFRIENDS_API_LATEST | |
friendOptions.LocalUserId = accountID | |
EOS_Friends_QueryFriends(friendsHandle, &friendOptions, nil, queryFriendsFunc) | |
} | |
// 2. Get the result of the friend query | |
func queryFriendsFunc(data: Optional<UnsafePointer<_tagEOS_Friends_QueryFriendsCallbackInfo>>) { | |
guard let info = data?.pointee else { return } | |
guard info.ResultCode == EOS_Success else { return } | |
guard let platformHandle = EOSManager.platformHandle else { return } | |
guard let friendsHandle = EOS_Platform_GetFriendsInterface(platformHandle) else { | |
return | |
} | |
var friendsCountOptions = EOS_Friends_GetFriendsCountOptions(); | |
friendsCountOptions.ApiVersion = EOS_FRIENDS_GETFRIENDSCOUNT_API_LATEST; | |
friendsCountOptions.LocalUserId = info.LocalUserId | |
let friendsCount = EOS_Friends_GetFriendsCount(friendsHandle, &friendsCountOptions); | |
Swift.print("Friends count: \(friendsCount)") | |
var indexOptions = EOS_Friends_GetFriendAtIndexOptions() | |
indexOptions.ApiVersion = EOS_FRIENDS_GETFRIENDATINDEX_API_LATEST | |
indexOptions.LocalUserId = info.LocalUserId | |
guard let presenceHandle = EOS_Platform_GetPresenceInterface(platformHandle) else { return } | |
for i in 0..<friendsCount { | |
indexOptions.Index = Int32(i) | |
let friendId = EOS_Friends_GetFriendAtIndex(friendsHandle, &indexOptions); | |
guard EOS_EpicAccountId_IsValid(friendId) == EOS_TRUE else { return } | |
var statusOptions = EOS_Friends_GetStatusOptions() | |
statusOptions.ApiVersion = EOS_FRIENDS_GETSTATUS_API_LATEST | |
statusOptions.LocalUserId = info.LocalUserId | |
statusOptions.TargetUserId = friendId | |
let status = EOS_Friends_GetStatus(friendsHandle, &statusOptions) | |
if status == EOS_FS_Friends { | |
Swift.print("We are friends!") | |
} else { | |
// EOS_FS_NotFriends, EOS_FS_InviteSent, EOS_FS_InviteReceived, or unknown | |
} | |
var presenceOptions = EOS_Presence_QueryPresenceOptions() | |
presenceOptions.ApiVersion = EOS_PRESENCE_QUERYPRESENCE_API_LATEST | |
presenceOptions.LocalUserId = info.LocalUserId | |
presenceOptions.TargetUserId = friendId | |
EOS_Presence_QueryPresence(presenceHandle, &presenceOptions, nil, queryPresenceFunc) | |
} | |
var notificationOptions = EOS_Presence_AddNotifyOnPresenceChangedOptions() | |
notificationOptions.ApiVersion = EOS_PRESENCE_ADDNOTIFYONPRESENCECHANGED_API_LATEST | |
EOS_Presence_AddNotifyOnPresenceChanged(presenceHandle, ¬ificationOptions, nil, presenceChangedFunc) | |
} | |
// 3. Get presence of the given friend | |
func queryPresenceFunc(data: Optional<UnsafePointer<_tagEOS_Presence_QueryPresenceCallbackInfo>>) { | |
guard let info = data?.pointee else { return } | |
guard info.ResultCode == EOS_Success else { return } | |
guard let platformHandle = EOSManager.platformHandle else { return } | |
guard let presenceHandle = EOS_Platform_GetPresenceInterface(platformHandle) else { return } | |
var presenceOptions = EOS_Presence_CopyPresenceOptions() | |
presenceOptions.ApiVersion = EOS_PRESENCE_COPYPRESENCE_API_LATEST | |
presenceOptions.LocalUserId = info.LocalUserId | |
presenceOptions.TargetUserId = info.TargetUserId | |
var presence: UnsafeMutablePointer<EOS_Presence_Info>? = nil | |
let result = EOS_Presence_CopyPresence(presenceHandle, &presenceOptions, &presence) | |
guard result == EOS_Success else { return } | |
guard let presenceData = presence?.pointee else { return } | |
if let richTextPtr = presenceData.RichText { | |
let richText = String(cString: richTextPtr) | |
Swift.print("RichText: \(richText)") | |
} | |
EOS_Presence_Info_Release(presence) | |
} | |
// 4. Register notifications of presence changes | |
func presenceChangedFunc(data: Optional<UnsafePointer<_tagEOS_Presence_PresenceChangedCallbackInfo>>) { | |
guard let info = data?.pointee else { return } | |
guard let platformHandle = EOSManager.platformHandle else { return } | |
guard let presenceHandle = EOS_Platform_GetPresenceInterface(platformHandle) else { return } | |
var presenceOptions = EOS_Presence_QueryPresenceOptions() | |
presenceOptions.ApiVersion = EOS_PRESENCE_QUERYPRESENCE_API_LATEST | |
presenceOptions.LocalUserId = info.LocalUserId | |
presenceOptions.TargetUserId = info.PresenceUserId | |
EOS_Presence_QueryPresence(presenceHandle, &presenceOptions, nil, queryPresenceFunc) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment