/// <summary> /// Updates information about given clan. /// Fails if user has no premissions to update clan information. /// Returns the reference to newly updated clan's <see cref="IApiGroup"/>. /// If <paramref name="isPublic"/> is false, this clan will not be shown during clan search. /// </summary> public static async Task <IApiGroup> UpdateClanInfoAsync(Client client, ISession session, IApiGroup clan, string name, string description, string avatarUrl, bool isPublic) { try { await client.UpdateGroupAsync(session, clan.Id, name, isPublic, description, avatarUrl, null); // Getting list of all clans local user has joined. // In this demo a user can join only one clan at a time, so the first clan should always be the clan we updated. IApiGroupList clanList = await client.ListGroupsAsync(session, name, 1, null); IApiGroup updatedClan = clanList.Groups.First(); if (updatedClan != null && updatedClan.Id == clan.Id) { return(updatedClan); } else { Debug.LogWarning("An error has occured when retrieving updated clan data"); return(null); } } catch (Exception e) { Debug.LogWarning("An exception has occured when updating clan info: " + e); return(null); } }
/// <summary> /// Creates clan on Nakama server with given <paramref name="name"/>. /// Fails when the name is already taken. /// Returns <see cref="IApiGroup"/> on success. /// </summary> public static async Task <IApiGroup> CreateClanAsync(Client client, ISession session, string name, string avatarUrl) { try { IApiGroup group = await client.CreateGroupAsync(session, name, "", avatarUrl); return(group); } catch (ApiResponseException e) { if (e.StatusCode == (long)System.Net.HttpStatusCode.InternalServerError) { Debug.LogWarning("Clan name \"" + name + "\" already in use"); return(null); } else { Debug.LogWarning("An exception has occured when creating clan with code " + e.StatusCode + ": " + e); return(null); } } catch (Exception e) { Debug.LogWarning("An internal exception has occured when creating clan: " + e); return(null); } }
/// <summary> /// Invoked by <see cref="ClanSearchResult._joinClanButton"/>. /// Joins selected clan. /// If user is already member of this clan, changes tab to <see cref="_detailsTab"/>. /// If user is already member of another clan, does nothing. /// </summary> private async void JoinClan() { IUserGroupListUserGroup clan = await ClanManager.GetUserClanAsync(Client, Session); if (clan != null) { if (clan.Group.Id == DisplayedClan.Id) { Debug.Log("This user has already joined clan with name \"" + DisplayedClan.Name + "\""); ShowClanDetails(DisplayedClan); } else { Debug.LogWarning("Cannot join more then one clan. Leave current clan first."); } } else { IApiGroup newClan = await ClanManager.JoinClanAsync(Client, Session, DisplayedClan); if (newClan != null) { OnClanChanged(newClan); } } }
/// <summary> /// Initializes UI for this entry. /// </summary> public void SetClan(IApiGroup clan, Action <IApiGroup> onJoin) { _clanName.text = clan.Name; _joinClanButton.onClick.AddListener(() => onJoin(clan)); Sprite avatar = _avatarSprites.GetSpriteByName(clan.AvatarUrl); _clanAvatar.sprite = avatar; }
/// <summary> /// Initializes UI for this entry. /// </summary> public void SetClan(IApiGroup clan, Action <IApiGroup> onJoin) { _clanName.text = clan.Name; _joinClanButton.onClick.AddListener(() => onJoin(clan)); Sprite avatar = AvatarManager.Instance.LoadEmblem(clan.AvatarUrl); _clanAvatar.sprite = avatar; }
/// <summary> /// Invoked when user changes their clan. /// Sets the <see cref="MyClan"/> of local user. /// If <paramref name="clan"/> is null, shows <see cref="_detailsTab"/>, else /// shows <see cref="_searchTab"/>. /// </summary> /// <param name="clan"></param> private void OnClanChanged(IApiGroup clan) { Debug.Log("Clan changed"); SetMyClan(clan); if (clan == null) { ShowClanSearch(); } else { ShowClanDetails(clan); } }
/// <summary> /// Shows <see cref="_detailsTab"/> panel and hides <see cref="_searchTab"/> panel. /// Updates user list. /// </summary> private async void ShowClanDetails(IApiGroup clan) { DisplayedClan = clan; bool good = await UpdateUserListAsync(); if (good == true) { await SetClanManagementButtonsAsync(); HideTab(_searchTab); ShowTab(_detailsTab); _clanDisplayName.text = DisplayedClan.Name; } _searchTabButton.interactable = true; _detailsTabButton.interactable = false; }
/// <summary> /// Sets the value of <see cref="MyClan"/>. /// If <paramref name="clan"/> is null, disable option to see <see cref="_detailsTab"/> of my clan. /// </summary> private void SetMyClan(IApiGroup clan) { if (clan != null) { _clanId = clan.Id; _clanName = clan.Name; _clanDisplayName.text = clan.Name; _detailsTabButton.interactable = true; _createClanButton.interactable = false; } else { _clanDisplayName.text = "Clan Panel"; _detailsTabButton.interactable = false; _createClanButton.interactable = true; } }
/// <summary> /// Sends clan creation request to Nakama server. /// Does nothing if user already belongs to a clan. /// </summary> private async void CreateClan() { string name = _clanName.text; try { IApiGroup group = await _connection.Client.CreateGroupAsync(_connection.Session, name, "A super great clan.", _avatarImage.name); if (OnClanCreated != null) { OnClanCreated(group); } } catch (ApiResponseException e) { Debug.LogError("Error creating clan: " + e.Message); } }
/// <summary> /// Sends clan creation request to Nakama server. /// Does nothing if user already belongs to a clan. /// </summary> private async void CreateClan(Action <IApiGroup> onCreated) { Client client = NakamaSessionManager.Instance.Client; ISession session = NakamaSessionManager.Instance.Session; string name = _clanName.text; IUserGroupListUserGroup clan = await ClanManager.GetUserClanAsync(client, session); if (clan != null) { Debug.LogWarning("User is already a member of a clan. Leave current clan first."); } else { IApiGroup newClan = await ClanManager.CreateClanAsync(client, session, name, _currentAvatar); if (newClan != null) { MenuManager.Instance.HideTopMenu(); onCreated(newClan); } } }
/// <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> /// Deletes supplied clan. /// Fails when user's status is not <see cref="ClanUserState.Superadmin"/>. /// </summary> public static async Task <bool> DeleteClanAsync(Client client, ISession session, IApiGroup clan) { try { await client.DeleteGroupAsync(session, clan.Id); return(true); } catch (ApiResponseException e) { if (e.StatusCode == (long)System.Net.HttpStatusCode.BadRequest) { Debug.LogWarning("Unauthorized clan removal with code " + e.StatusCode + ": " + e); return(false); } else { Debug.LogWarning("An exception has occured when deleting clan with code " + e.StatusCode + ": " + e); return(false); } } catch (Exception e) { Debug.LogWarning("An internal exception has occured when deleting clan: " + e); return(false); } }
/// <summary> /// Gathers the list of all users in given clan and their <see cref="ClanUserState"/> (<see cref="IGroupUserListGroupUser.State"/>). /// </summary> public static async Task <List <IGroupUserListGroupUser> > GetClanUsersAsync(Client client, ISession session, IApiGroup clan) { try { var userEnumeration = await client.ListGroupUsersAsync(session, clan.Id, null, 1, null); List <IGroupUserListGroupUser> userList = userEnumeration.GroupUsers.ToList(); return(userList); } catch (Exception e) { Debug.LogWarning("An error has occured when getting clan user list: " + e); return(null); } }
/// <summary> /// Rises the <see cref="ClanUserState"/> of supplied member in given clan. /// Fails if promoting user has no permissions to promote given user. /// </summary> public static async Task <bool> PromoteUserAsync(Client client, ISession session, IApiUser promotedUser, IApiGroup clan) { try { await client.PromoteGroupUsersAsync(session, clan.Id, new string[] { promotedUser.Id }); return(true); } catch (ApiResponseException e) { if (e.StatusCode == (long)System.Net.HttpStatusCode.BadRequest) { Debug.LogWarning("Insufficient permissions to promote user or clan not found"); return(false); } else { Debug.LogWarning("An internal exception has occured when promoting user " + promotedUser.Username + " in clan " + clan.Name + ": " + e); return(false); } } catch (Exception e) { Debug.LogWarning("An exception has occured when promoting user " + promotedUser.Username + " in clan " + clan.Name + ": " + e); return(false); } }
/// <summary> /// Kicks supplied user out of given clan. /// Fails if kicking user has no permissions to kick the user. /// </summary> public static async Task <bool> KickUserAsync(Client client, ISession session, IApiUser kickedUser, IApiGroup clan) { try { await client.KickGroupUsersAsync(session, clan.Id, new string[] { kickedUser.Id }); return(true); } catch (ApiResponseException e) { if (e.StatusCode == (long)System.Net.HttpStatusCode.NotFound) { Debug.LogWarning("Insufficient permissions to kick " + kickedUser.Username + " from clan " + clan.Name + " or clan not found: " + e); return(false); } else { Debug.LogWarning("An internal exception has occured when kicking user " + kickedUser.Username + " from clan " + clan.Name + " with code " + e.StatusCode + ": " + e); return(false); } } catch (Exception e) { Debug.LogWarning("An exception has occured when kicking user " + kickedUser.Username + " from clan " + clan.Name + ": " + e); return(false); } }
/// <summary> /// Sends request to leave supplied clan. /// </summary> public static async Task <bool> LeaveClanAsync(Client client, ISession session, IApiGroup clan) { try { await client.LeaveGroupAsync(session, clan.Id); return(true); } catch (ApiResponseException e) { Debug.LogWarning("An exception has occured when leaving clan with code " + e.StatusCode + ": " + e); return(false); } catch (Exception e) { Debug.LogWarning("An internal server exception has occured when leaving clan: " + e); return(false); } }
/// <summary> /// Sends request to join supplied clan. /// </summary> public static async Task <IApiGroup> JoinClanAsync(Client client, ISession session, IApiGroup clan) { try { await client.JoinGroupAsync(session, clan.Id); return(clan); } catch (ApiResponseException e) { Debug.LogWarning("An exception has occured when joining clan with code " + e.StatusCode + ": " + e); return(null); } catch (Exception e) { Debug.LogWarning("An internal exception has occured when joining clan with name \"" + clan.Name + "\": " + e); return(null); } }