cards/CardMagicWand.cs
using Sandbox;
using System.Net.Http.Headers;
using System.Threading.Tasks;

public class CardMagicWand : Card
{
	public override bool ShouldHandleEvent( EventType eventType )
	{
		return eventType == EventType.Mismatch && Manager.Instance.ChosenCards.Contains( this ) && !Manager.Instance.IsMismatchALockedMatch;
	}

	public override async Task HandleEventAsync( EventType eventType )
	{
		var otherCard = Manager.Instance.ChosenCards[0] == this ? Manager.Instance.ChosenCards[1] : Manager.Instance.ChosenCards[0];

		var matches = Manager.Instance.GetCardsOfType( otherCard.CardType, except: otherCard );
		if ( matches.Count == 0 )
			return;

		Manager.Instance.PushEventMessage( this, eventType );

		var match = matches[Game.Random.Int( 0, matches.Count - 1 )];

		if ( Manager.IsAdjacent( otherCard.GridPos, match.GridPos ) )
		{
			await Task.DelayRealtime( 300 );
			
			Manager.Instance.PlayCardSfx( "magic_wand_no_move", otherCard, volume: 1.5f, pitch: Game.Random.Float( 0.85f, 0.875f ) );

			await Task.DelayRealtime( 100 );

			otherCard.MoveToPos( otherCard.LocalPosition.WithZ( Game.Random.Float( 70f, 90f ) ), 0.5f, EasingType.SineOut );

			await Task.DelayRealtime( 500 );

			otherCard.MoveToPos( otherCard.LocalPosition.WithZ( Globals.CARD_DEFAULT_HEIGHT + Globals.CARD_ADD_HEIGHT_REVEALED_OR_HOVERED ), 0.5f, EasingType.SineOut );

			await Task.DelayRealtime( 500 );

			otherCard.IsMovementControlled = false;

			await Task.DelayRealtime( 200 );

			Manager.Instance.PopEventMessage();

			return;
		}

		var adjacentPositions = Manager.Instance.GetNearbyGridPositions( match.GridPos, adjacentOnly: true );
		adjacentPositions.Shuffle();
		var targetGridPos = adjacentPositions.First();

		var targetCard = Manager.Instance.GetCardAtGridPos( targetGridPos );

		Manager.Instance.PlayCardSfx( "magic_wand", otherCard, volume: 1.2f, pitch: Game.Random.Float( 0.875f, 0.9f ) );

		await Task.DelayRealtime( 200 );

		otherCard.MoveToPos( otherCard.LocalPosition.WithZ( Game.Random.Float( 70f, 90f ) ), 0.4f, EasingType.SineOut );
		targetCard?.MoveToPos( targetCard.LocalPosition.WithZ( Game.Random.Float( 70f, 90f ) ), 0.4f, EasingType.SineOut );

		await Task.DelayRealtime( 400 );

		otherCard.MoveToPos( Manager.GetCardPos( targetGridPos ).WithZ( otherCard.LocalPosition.z ), 0.5f, EasingType.SineInOut );
		targetCard?.MoveToPos( Manager.GetCardPos( otherCard.GridPos ).WithZ( targetCard.LocalPosition.z ), 0.5f, EasingType.SineInOut );

		await Task.DelayRealtime( 500 );

		otherCard.MoveToPos( otherCard.LocalPosition.WithZ( Globals.CARD_DEFAULT_HEIGHT + Globals.CARD_ADD_HEIGHT_REVEALED_OR_HOVERED ), 0.4f, EasingType.SineOut );
		targetCard?.MoveToPos( targetCard.LocalPosition.WithZ( Globals.CARD_DEFAULT_HEIGHT + (targetCard.IsRevealed ? Globals.CARD_ADD_HEIGHT_REVEALED_OR_HOVERED : 0f) ), 0.4f, EasingType.SineOut );

		await Task.DelayRealtime( 400 );

		if ( targetCard != null )
		{
			await Manager.Instance.SwapCardPositions( otherCard, targetCard );
		}
		else
		{
			Manager.Instance.RemoveCardGridPos( otherCard );
			await Manager.Instance.SetCardGridPos( otherCard, targetGridPos );
		}

		await Task.DelayRealtime( 100 );

		otherCard.IsMovementControlled = false;
		if ( targetCard != null )
			targetCard.IsMovementControlled = false;

		await Task.DelayRealtime( 200 );

		Manager.Instance.PopEventMessage();

		await Manager.Instance.EventHappened( EventType.AfterCardsMoved );
	}
}