/// <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> /// Adds cards to local user's hand. /// </summary> private void OnStartingHandReceived(MatchMessageStartingHand message) { if (message.PlayerId == _connection.Session.UserId) { for (int i = 0; i < message.Cards.Count; i++) { _localHandPanel.DrawCard(message.Cards[i], i); } _localGold.Restart(); _localGoldPanel.Init(_localGold); } }
/// <summary> /// Selects a number of cards equal to <see cref="Hand._cardIdsInHand"/> from players deck /// and sends them to that player. /// </summary> private void SendStartingHand(string userId) { Hand hand = userId == _connection.BattleConnection.HostId ? _localHand : _opponentHand; List <Card> cards = hand.DrawInitialCards(); MatchMessageStartingHand message = new MatchMessageStartingHand(userId, cards); if (userId == _connection.BattleConnection.HostId) { _stateManager.SendMatchStateMessageSelf(MatchMessageType.StartingHand, message); } else { _stateManager.SendMatchStateMessage(MatchMessageType.StartingHand, message); _opponentGold.Restart(); } }