/// <summary> /// Returns true if /// </summary> /// <param name="user"></param> /// <returns></returns> private async Task <bool> CanBeFriendAsync(IApiUser user) { Client client = NakamaSessionManager.Instance.Client; ISession session = NakamaSessionManager.Instance.Session; if (user.Id == session.UserId) { return(false); } IApiFriends friends = await FriendsManager.LoadFriendsAsync(client, session); if (friends == null) { Debug.Log("Couldn't retrieve friends list"); return(false); } foreach (IApiFriend friend in friends.Friends) { if (friend.User.Id == user.Id) { return(false); } } return(true); }
/// <summary> /// Loading friends from database and refreshing list /// </summary> private async void ActualizeFriendsList() { //loading data from server IApiFriends friends = await FriendsManager.LoadFriendsAsync(NakamaSessionManager.Instance.Client, NakamaSessionManager.Instance.Session); if (friends != null) { //refreshing viev RefreshFriendsListUI(friends); } }
/// <summary> /// This method is loading friends list from database. Return value indicates whether operation succeed or failed. /// </summary> public static async Task <IApiFriends> LoadFriendsAsync(IClient client, ISession session) { try { IApiFriends friends = await client.ListFriendsAsync(session); return(friends); } catch (Exception e) //catching exception, if program enters this code loading operation was not successfully completed { Debug.Log("Loading friends failed (" + e.Message + ")"); return(null); } }
/// <summary> /// Populating friend list with elements created basing on loaded friends list from database /// </summary> private void RefreshFriendsListUI(IApiFriends friends) { ClearLists(); foreach (IApiFriend friend in friends.Friends) { RectTransform content; //selecting to which tab should friend panel be instantiated switch (friend.State) { // Users are friends with each other. case 0: content = _friendsContent; break; // This user has sent an invitation and pending acceptance from other user. case 1: content = _sentInvitesContent; break; // This user has received an invitation but has not accepted yet. case 2: content = _receivedInvitesContent; break; // This user has banned other user. case 3: content = _bannedUsersContent; break; // If state is none of upper log error default: Debug.LogError("Wrong friend state value: \"" + friend.State + "\" in " + friend.User.Username + "!"); return; } //instantiating friend panel object GameObject panelGO = Instantiate(_friendPanelPrefab, content) as GameObject; FriendPanel panel = panelGO.GetComponent <FriendPanel>(); if (panel) { //initializing object with FriendList object and friend data panel.Init(friend); panel.OnSelected += SelectedPanelChange; //subscribing to event fired after every successful request to friends list in database panel.OnDataChanged += ActualizeFriendsList; panel.OnChatStartButtonClicked += StartChatWithUser; } else { Debug.LogError("Invalid friend panel prefab!"); Destroy(panelGO); } } }
/// <summary> /// Initializes friends view /// </summary> /// <param name="client"></param> /// <param name="session"></param> private async void Init() { Client client = NakamaSessionManager.Instance.Client; ISession session = NakamaSessionManager.Instance.Session; //loading friends data IApiFriends friends = await FriendsManager.LoadFriendsAsync(client, session); if (friends != null) { //refreshing ui view with downloaded data RefreshFriendsListUI(friends); NakamaSessionManager.Instance.OnConnectionSuccess -= Init; } //connecting events that need server connection _addFriendButton.onClick.AddListener(AddFriend); _usernameSearcher.OnSubmit += AddFriend; _refreshButton.onClick.AddListener(ActualizeFriendsList); }
/// <summary> /// Gets ids of all friends of local user and displays their records. /// This also includes local user. /// </summary> public async void ShowFriendsLeaderboards(string cursor) { IApiFriends friends = await FriendsManager.LoadFriendsAsync(Client, Session); if (friends == null) { return; } IApiLeaderboardRecordList records = await LeaderboardManager.GetFriendsLeaderboarsAsync(Client, Session, friends.Friends, _recordsPerPage, cursor); if (records != null) { SetLeaderboardsCursor(records, ShowFriendsLeaderboards); FillLeaderboard(records.OwnerRecords); _showFriends.interactable = false; _showClan.interactable = true; _showGlobal.interactable = true; } }