/**
* 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 DemoGame.Scripts.DataStorage;
using DemoGame.Scripts.Menus;
using DemoGame.Scripts.Session;
using Nakama;
using UnityEngine;
using UnityEngine.UI;
namespace DemoGame.Scripts.Clans
{
///
/// Menu responsible for creating a new clan.
///
public class ClanCreationPanel : Menu
{
#region Fields
///
/// Textbox containing clan name.
///
[SerializeField] private Text _clanName = null;
///
/// Button displaing list of available avatars.
///
[SerializeField] private Button _avatarButton = null;
///
/// Currently selected avatar.
///
[SerializeField] private Image _avatarImage = null;
///
/// Button sending Clan creation request to Nakama server.
///
[SerializeField] private Button _doneButton = null;
///
/// The path of currently selected avatar.
///
private string _currentAvatar;
#endregion
#region Mono
///
/// Adds listeners to buttons.
///
private void Awake()
{
base.Hide();
base.SetBackButtonHandler(MenuManager.Instance.HideTopMenu);
_avatarButton.onClick.AddListener(ChangeAvatar);
ChangeAvatar();
}
#endregion
#region Methods
///
/// Shows this panel and adds listener to .
///
///
public void ShowCreationPanel(Action onCreated)
{
_doneButton.onClick.RemoveAllListeners();
_doneButton.onClick.AddListener(() => CreateClan(onCreated));
MenuManager.Instance.ShowMenu(this, false);
}
///
/// Changes currently displayed avatar.
///
private void ChangeAvatar()
{
_currentAvatar = AvatarManager.Instance.NextEmblem(_currentAvatar);
_avatarImage.sprite = AvatarManager.Instance.LoadEmblem(_currentAvatar);
}
///
/// Sends clan creation request to Nakama server.
/// Does nothing if user already belongs to a clan.
///
private async void CreateClan(Action 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);
}
}
}
#endregion
}
}