private async Task <IApiLeaderboardRecordList> CreateAndFetchRecords(int numRecords, int limit, int ownerIndex) { var authTasks = new List <Task <ISession> >(); for (int i = 0; i < numRecords; i++) { authTasks.Add(_client.AuthenticateCustomAsync($"{Guid.NewGuid()}")); } ISession[] sessions = await Task.WhenAll(authTasks.ToArray()); var listTasks = new List <Task <IApiLeaderboardRecord> >(); for (int i = 0; i < numRecords; i++) { int score = 100 + numRecords - i - 1; listTasks.Add(_client.WriteLeaderboardRecordAsync(sessions[i], _leaderboardId, score)); } Task.WaitAll(listTasks.ToArray()); IApiLeaderboardRecordList records = await _client.ListLeaderboardRecordsAroundOwnerAsync(sessions[ownerIndex], _leaderboardId, sessions[ownerIndex].UserId, null, limit); return(records); }
public async Task OneRecordOneLimit() { IApiLeaderboardRecordList records = await CreateAndFetchRecords(numRecords : 1, limit : 1, ownerIndex : 0); var recordArray = records.Records.ToArray(); Assert.Equal(1, recordArray.Length); Assert.Equal("100", recordArray[0].Score); }
public async Task ThreeRecordsTwoLimit() { IApiLeaderboardRecordList records = await CreateAndFetchRecords(numRecords : 3, limit : 2, ownerIndex : 1); var recordArray = records.Records.ToArray(); Assert.Equal(2, recordArray.Length); Assert.Equal("101", recordArray[0].Score); Assert.Equal("100", recordArray[1].Score); }
/// <summary> /// Fills <see cref="_userList"/> with user records sorted by the score. /// </summary> public async void ShowGlobalLeaderboards(string cursor = null) { IApiLeaderboardRecordList records = await _connection.Client.ListLeaderboardRecordsAsync(_connection.Session, "global", ownerIds : null, expiry : null, _recordsPerPage, cursor); SetLeaderboardsCursor(records, ShowGlobalLeaderboards); FillLeaderboard(records.Records); _showFriends.interactable = true; _showClan.interactable = true; _showGlobal.interactable = false; }
public async Task NotEnoughRecordsForLimit() { IApiLeaderboardRecordList records = await CreateAndFetchRecords(numRecords : 4, limit : 10, ownerIndex : 2); var recordArray = records.Records.ToArray(); Assert.Equal(4, recordArray.Length); Assert.Equal("103", recordArray[0].Score); Assert.Equal("102", recordArray[1].Score); Assert.Equal("101", recordArray[2].Score); Assert.Equal("100", recordArray[3].Score); }
public async Task OwnerNearBack() { IApiLeaderboardRecordList records = await CreateAndFetchRecords(numRecords : 10, limit : 4, ownerIndex : 8); var recordArray = records.Records.ToArray(); Assert.Equal(4, recordArray.Length); Assert.Equal("103", recordArray[0].Score); Assert.Equal("102", recordArray[1].Score); Assert.Equal("101", recordArray[2].Score); Assert.Equal("100", recordArray[3].Score); }
public async Task OddLimit() { IApiLeaderboardRecordList records = await CreateAndFetchRecords(numRecords : 5, limit : 3, ownerIndex : 3); var recordArray = records.Records.ToArray(); Assert.Equal(3, recordArray.Length); // owner score is 101 Assert.Equal("102", recordArray[0].Score); Assert.Equal("101", recordArray[1].Score); Assert.Equal("100", recordArray[2].Score); }
public async Task OwnerInFront() { IApiLeaderboardRecordList records = await CreateAndFetchRecords(numRecords : 10, limit : 4, ownerIndex : 0); var recordArray = records.Records.ToArray(); Assert.Equal(4, recordArray.Length); Assert.Equal("109", recordArray[0].Score); Assert.Equal("108", recordArray[1].Score); Assert.Equal("107", recordArray[2].Score); Assert.Equal("106", recordArray[3].Score); }
public async Task OwnerInMiddle() { IApiLeaderboardRecordList records = await CreateAndFetchRecords(numRecords : 10, limit : 4, ownerIndex : 5); var recordArray = records.Records.ToArray(); Assert.Equal(recordArray.Length, 4); // owner score is 104 Assert.Equal("105", recordArray[0].Score); Assert.Equal("104", recordArray[1].Score); Assert.Equal("103", recordArray[2].Score); Assert.Equal("102", recordArray[3].Score); }
/// <summary> /// Fills <see cref="_userList"/> with user records sorted by the score. /// </summary> public async void ShowGlobalLeaderboards(string cursor = null) { IApiLeaderboardRecordList records = await LeaderboardManager.GetGlobalLeaderboarsAsync(Client, Session, _recordsPerPage, cursor); if (records != null) { SetLeaderboardsCursor(records, ShowGlobalLeaderboards); FillLeaderboard(records.Records); _showFriends.interactable = true; _showClan.interactable = true; _showGlobal.interactable = false; } }
/// <summary> /// Retrieves top best records of all time from the server. /// </summary> public static async Task <IApiLeaderboardRecordList> GetGlobalLeaderboarsAsync(Client client, ISession session, int limit = 1, string cursor = null) { try { IApiLeaderboardRecordList list = await client.ListLeaderboardRecordsAsync(session, "global", null, limit, cursor); return(list); } catch (Exception e) { Debug.LogWarning("An error has occured while showing global leaderboards: " + e); return(null); } }
/// <summary> /// Retrieves all user ids from <paramref name="friends"/> and filters all records from global leaderboard to show only filtered users. /// </summary> public static async Task <IApiLeaderboardRecordList> GetFriendsLeaderboarsAsync(Client client, ISession session, IEnumerable <IApiFriend> friends, int limit = 1, string cursor = null) { try { List <string> ids = friends.Select(x => x.User.Id).ToList(); ids.Add(NakamaSessionManager.Instance.Session.UserId); IApiLeaderboardRecordList list = await client.ListLeaderboardRecordsAsync(session, "global", ids, limit, cursor); return(list); } catch (Exception e) { Debug.LogWarning("An error has occured while showing friends leaderboards: " + e); return(null); } }
/// <summary> /// Retrieves all user ids from <paramref name="clan"/> and filters all records from global leaderboard to show only filtered users. /// </summary> public static async Task <IApiLeaderboardRecordList> GetClanLeaderboarsAsync(Client client, ISession session, IApiGroup clan, int limit = 1, string cursor = null) { try { IApiGroupUserList users = await client.ListGroupUsersAsync(session, clan.Id); IEnumerable <string> ids = users.GroupUsers.Select(x => x.User.Id); IApiLeaderboardRecordList list = await client.ListLeaderboardRecordsAsync(session, "global", ids, limit, cursor); return(list); } catch (Exception e) { Debug.LogWarning("An error has occured while showing clan leaderboards: " + e); return(null); } }
/// <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) { var 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; } }
/// <summary> /// Fills <see cref="_userList"/> with records of all members of the clan local user belongs to. /// </summary> public async void ShowClanLeaderboards(string cursor) { IUserGroupListUserGroup clan = await ClanManager.GetUserClanAsync(Client, Session); if (clan == null) { return; } IApiLeaderboardRecordList records = await LeaderboardManager.GetClanLeaderboarsAsync(Client, Session, clan.Group, _recordsPerPage, cursor); if (records != null) { SetLeaderboardsCursor(records, ShowClanLeaderboards); FillLeaderboard(records.OwnerRecords); _showFriends.interactable = true; _showClan.interactable = false; _showGlobal.interactable = true; } }
/// <summary> /// Fills <see cref="_userList"/> with records of all members of the clan local user belongs to. /// </summary> public async void ShowClanLeaderboards(string cursor) { if (_userClan == null) { return; } var users = await _connection.Client.ListGroupUsersAsync(_connection.Session, _userClan.Group.Id, null, 1, null); IEnumerable <string> ids = users.GroupUsers.Select(x => x.User.Id); IApiLeaderboardRecordList list = await _connection.Client.ListLeaderboardRecordsAsync(_connection.Session, "global", ids, null, 1, cursor); if (list.Records != null) { SetLeaderboardsCursor(list, ShowClanLeaderboards); FillLeaderboard(list.OwnerRecords); _showFriends.interactable = true; _showClan.interactable = false; _showGlobal.interactable = true; } }
/// <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) { try { var friends = await _connection.Client.ListFriendsAsync(_connection.Session); List <string> ids = friends.Friends.Select(x => x.User.Id).ToList(); ids.Add(_connection.Session.UserId); IApiLeaderboardRecordList records = await _connection.Client.ListLeaderboardRecordsAsync(_connection.Session, "global", ids, null, 1, cursor); SetLeaderboardsCursor(records, ShowFriendsLeaderboards); FillLeaderboard(records.OwnerRecords); _showFriends.interactable = false; _showClan.interactable = true; _showGlobal.interactable = true; } catch (ApiResponseException e) { Debug.LogWarning("Error showing friends leaderboards: " + e.Message); } }
/// <summary> /// Sets <see cref="_nextPageButton"/> and <see cref="_prevPageButton"/> onClick events. /// If there is no next or previous page, disables interactions with these buttons. /// </summary> /// <param name="records"> /// Contains <see cref="IApiLeaderboardRecordList.NextCursor"/> and /// <see cref="IApiLeaderboardRecordList.PrevCursor"/> responsible for iterating through pages. /// </param> /// <param name="caller"> /// Method used to receive <paramref name="records"/>. This method will be called to iterate /// through pages using cursors. /// </param> private void SetLeaderboardsCursor(IApiLeaderboardRecordList records, Action <string> caller) { if (records.PrevCursor != null) { _prevPageButton.interactable = true; _prevPageButton.onClick.RemoveAllListeners(); _prevPageButton.onClick.AddListener(() => caller(records.PrevCursor)); } else { _prevPageButton.interactable = false; } if (records.NextCursor != null) { _nextPageButton.interactable = true; _nextPageButton.onClick.RemoveAllListeners(); _nextPageButton.onClick.AddListener(() => caller(records.NextCursor)); } else { _nextPageButton.interactable = false; } }