/**
* Copyright 2019 The Knights Of Unity, created by Pawel Stolarczyk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DemoGame.Scripts.Session;
using Nakama;
using UnityEngine;
namespace DemoGame.Scripts.Leaderboards
{
///
/// Static class responsible for managing leaderboards requests.
///
public static class LeaderboardManager
{
#region Methods
///
/// Retrieves top best records of all time from the server.
///
public static async Task 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;
}
}
///
/// Retrieves all user ids from and filters all records from global leaderboard to show only filtered users.
///
public static async Task GetClanLeaderboarsAsync(Client client, ISession session, IApiGroup clan, int limit = 1, string cursor = null)
{
try
{
IApiGroupUserList users = await client.ListGroupUsersAsync(session, clan.Id);
IEnumerable 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;
}
}
///
/// Retrieves all user ids from and filters all records from global leaderboard to show only filtered users.
///
public static async Task GetFriendsLeaderboarsAsync(Client client, ISession session, IEnumerable friends, int limit = 1, string cursor = null)
{
try
{
List 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;
}
}
#endregion
}
}