/// <summary>
        /// Handles card play request.
        /// If user playing the card has insufficiend gold or used card can not be played on specified node,
        /// a cancel message is sent to that card owner and its effects don't resolve.
        /// </summary>
        private void HandleCardRequest(MatchMessageCardPlayRequest message, Hand hand, Gold gold)
        {
            int cost = message.Card.CardData.GetCardInfo().Cost;

            if (gold.CurrentGold < cost)
            {
                SendCardCanceledMessage(message);
            }
            else
            {
                bool       isHost       = _connection.BattleConnection.HostId == _connection.Session.UserId;
                Vector3    position     = new Vector3(message.X, message.Y, message.Z);
                Vector2Int nodePosition = ScreenToNodePos(position, isHost, message.Card.CardData.GetCardInfo().DropRegion);
                Node       node         = Nodes[nodePosition.x, nodePosition.y];

                if (node != null && (node.Unit == null || message.Card.CardData.GetCardInfo().CanBeDroppedOverOtherUnits))
                {
                    SendCardPlayedMessage(message, hand, nodePosition);
                }
                else
                {
                    SendCardCanceledMessage(message);
                }
            }
        }
        /// <summary>
        /// Reads match messages sent by other players, and fires locally events basing on opCode.
        /// </summary>
        /// <param name="opCode"></param>
        /// <param name="messageJson"></param>
        public void ReceiveMatchStateHandle(long opCode, string messageJson)
        {
            //Choosing which event should be invoked basing on opCode, then parsing json to MatchMessage class and firing event
            switch ((MatchMessageType)opCode)
            {
            //UNITS
            case MatchMessageType.UnitSpawned:
                MatchMessageUnitSpawned matchMessageUnitSpawned = MatchMessageUnitSpawned.Parse(messageJson);
                OnUnitSpawned?.Invoke(matchMessageUnitSpawned);
                break;

            case MatchMessageType.UnitMoved:
                MatchMessageUnitMoved matchMessageUnitMoved = MatchMessageUnitMoved.Parse(messageJson);
                OnUnitMoved?.Invoke(matchMessageUnitMoved);
                break;

            case MatchMessageType.UnitAttacked:
                MatchMessageUnitAttacked matchMessageUnitAttacked = MatchMessageUnitAttacked.Parse(messageJson);
                OnUnitAttacked?.Invoke(matchMessageUnitAttacked);
                break;

            //SPELLS
            case MatchMessageType.SpellActivated:
                MatchMessageSpellActivated matchMessageSpellActivated = MatchMessageSpellActivated.Parse(messageJson);
                OnSpellActivated?.Invoke(matchMessageSpellActivated);
                break;

            //CARDS
            case MatchMessageType.CardPlayRequest:
                if (_connection.BattleConnection.HostId == _connection.Account.User.Id)
                {
                    MatchMessageCardPlayRequest matchMessageCardPlayRequest = MatchMessageCardPlayRequest.Parse(messageJson);
                    OnCardRequested?.Invoke(matchMessageCardPlayRequest);
                }
                break;

            case MatchMessageType.CardPlayed:
                MatchMessageCardPlayed matchMessageCardPlayed = MatchMessageCardPlayed.Parse(messageJson);
                OnCardPlayed?.Invoke(matchMessageCardPlayed);
                break;


            case MatchMessageType.CardCanceled:
                MatchMessageCardCanceled matchMessageCardCancelled = MatchMessageCardCanceled.Parse(messageJson);
                OnCardCancelled?.Invoke(matchMessageCardCancelled);
                break;

            case MatchMessageType.StartingHand:
                MatchMessageStartingHand matchMessageStartingHand = MatchMessageStartingHand.Parse(messageJson);
                OnStartingHandReceived?.Invoke(matchMessageStartingHand);
                break;
            }
        }
        /// <summary>
        /// Cancels played card and returns it to its owner's hand.
        /// </summary>
        private void SendCardCanceledMessage(MatchMessageCardPlayRequest message)
        {
            MatchMessageCardCanceled cardCanceled = new MatchMessageCardCanceled(message.PlayerId, message.CardSlotIndex);

            if (message.PlayerId == _connection.Session.UserId)
            {
                _stateManager.SendMatchStateMessageSelf(MatchMessageType.CardCanceled, cardCanceled);
            }
            else
            {
                _stateManager.SendMatchStateMessage(MatchMessageType.CardCanceled, cardCanceled);
            }
        }
        /// <summary>
        /// Requests card play.
        /// </summary>
        private void SendCardPlayedMessage(MatchMessageCardPlayRequest message, Hand userHand, Vector2Int nodePosition)
        {
            Card newCard = userHand.DrawCard();

            var matchMessageCardPlayed = new MatchMessageCardPlayed(
                message.PlayerId,
                message.Card,
                message.CardSlotIndex,
                newCard,
                nodePosition.x,
                nodePosition.y);

            _stateManager.SendMatchStateMessage(MatchMessageType.CardPlayed, matchMessageCardPlayed);
            _stateManager.SendMatchStateMessageSelf(MatchMessageType.CardPlayed, matchMessageCardPlayed);
        }
예제 #5
0
        /// <summary>
        /// Invoked on card played.
        /// Sends <see cref="MatchMessageCardPlayRequest"/> to host.
        /// </summary>
        private void PlayCard(CardGrabber grabber, Vector3 dropPosition)
        {
            grabber.OnDragStarted  -= StartCardDrag;
            grabber.OnCardReturned -= ReturnCard;
            grabber.OnCardPlayed   -= PlayCard;

            string id    = _connection.Session.UserId;
            int    index = _cardsInHand.IndexOf(grabber);
            //_cardsInHand.RemoveAt(index);
            MatchMessageCardPlayRequest message = new MatchMessageCardPlayRequest(
                id, grabber.Card, index, dropPosition.x, dropPosition.y, dropPosition.z);

            OnCardPlayed?.Invoke(message);
            CurrentlyGrabbedCard = null;
            //Destroy(grabber.gameObject);
        }
 /// <summary>
 /// User requested card play.
 /// </summary>
 private void OnCardRequested(MatchMessageCardPlayRequest message)
 {
     HandleCardRequest(message, _localHand, _localGold);
 }