/// <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> /// This method is used by host to invoke locally event connected with match message which is sent to other players. /// Should be always runned on host client after sending any message, otherwise some of the game logic would not be runned on host game instance. /// Don't use this method when client is not a host! /// </summary> /// <typeparam name="T"></typeparam> /// <param name="opCode"></param> /// <param name="message"></param> public void SendMatchStateMessageSelf <T>(MatchMessageType opCode, T message) where T : MatchMessage <T> { //Choosing which event should be invoked basing on opCode and firing event switch (opCode) { //GAME case MatchMessageType.MatchEnded: OnGameEnded?.Invoke(message as MatchMessageGameEnded); break; //UNITS case MatchMessageType.UnitSpawned: OnUnitSpawned?.Invoke(message as MatchMessageUnitSpawned); break; case MatchMessageType.UnitMoved: OnUnitMoved?.Invoke(message as MatchMessageUnitMoved); break; case MatchMessageType.UnitAttacked: OnUnitAttacked?.Invoke(message as MatchMessageUnitAttacked); break; //SPELLS case MatchMessageType.SpellActivated: OnSpellActivated?.Invoke(message as MatchMessageSpellActivated); break; //CARDS case MatchMessageType.CardPlayRequest: OnCardRequested?.Invoke(message as MatchMessageCardPlayRequest); break; case MatchMessageType.CardPlayed: OnCardPlayed?.Invoke(message as MatchMessageCardPlayed); break; case MatchMessageType.CardCanceled: OnCardCancelled?.Invoke(message as MatchMessageCardCanceled); break; case MatchMessageType.StartingHand: OnStartingHandReceived?.Invoke(message as MatchMessageStartingHand); break; default: break; } }
/// <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) { if (GameStarted == false) { _incommingMessages.Enqueue(new IncommingMessageState(opCode, messageJson)); return; } //Choosing which event should be invoked basing on opCode, then parsing json to MatchMessage class and firing event switch ((MatchMessageType)opCode) { //GAME case MatchMessageType.MatchEnded: MatchMessageGameEnded matchMessageGameEnded = MatchMessageGameEnded.Parse(messageJson); OnGameEnded?.Invoke(matchMessageGameEnded); break; //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 (IsHost == true) { 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; } }