/**
* 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.Collections.Generic;
using System.Threading.Tasks;
using DemoGame.Scripts.Gameplay.Cards;
using DemoGame.Scripts.Gameplay.Decks;
using UnityEngine;
namespace DemoGame.Scripts.Gameplay.Hands
{
///
/// Responsible for hand management, handles card play.
/// Used only by host.
///
public class Hand : MonoBehaviour
{
#region Fields
///
/// Maximum hand size for every player.
///
[SerializeField] private int _handSize = 3;
///
/// Reference to deck storage system.
///
[SerializeField] private DeckStorage _deckStorage = null;
///
/// Deck handled by this object.
///
private Deck _deck;
///
/// Queue of cards left in deck.
///
private Queue _cards;
///
/// Queue of already played cards.
///
private Queue _playedCards;
///
/// List of all cards currenly in hand.
///
private List _cardsInHand;
///
/// The user id of hand owner.
///
private string _ownerId;
#endregion
#region Methods
///
/// Retrieves users deck from Nakama server, shuffles all cards and fills the
/// hand with a number of cards equal to .
/// Returns a list of all cards in hand.
///
public async Task> InitAsync(string userId)
{
_ownerId = userId;
_deck = await _deckStorage.LoadDataAsync(userId, "deck");
_cards = new Queue();
_playedCards = new Queue(_deck.usedCards);
_cardsInHand = new List();
for (int i = 0; i < _handSize; i++)
{
DrawCard();
}
return _cardsInHand;
}
///
/// Takes the first card from user's deck and places it in hand.
/// If there are no cards left in deck, shuffles all already played
/// cards and puts them in deck.
///
public Card DrawCard()
{
if (_cards.Count == 0)
{
Debug.Log("refilling");
RefillDeck();
}
Card card = _cards.Dequeue();
_cardsInHand.Add(card);
return card;
}
///
/// Removes played card from hand and adds it into .
///
///
public void CardPlayed(int index)
{
Card card = _cardsInHand[index];
_cardsInHand.RemoveAt(index);
_playedCards.Enqueue(card);
}
///
/// Shuffles all already played cards into user's deck.
///
private void RefillDeck()
{
List cardPool = new List(_playedCards);
_playedCards.Clear();
while (cardPool.Count > 0)
{
int index = UnityEngine.Random.Range(0, cardPool.Count);
_cards.Enqueue(cardPool[index]);
cardPool.RemoveAt(index);
}
}
///
/// Returns true if user has a copy of given card in hand.
///
///
///
public bool HasCardInHand(Card card)
{
foreach (Card cardInHand in _cardsInHand)
{
if (card.IsCopy(cardInHand))
{
return true;
}
}
return false;
}
#endregion
}
}