SwapBoostsActionExecutor.kt

package io.github.lishangbu.avalon.game.battle.engine.core.runtime.action

import io.github.lishangbu.avalon.game.battle.engine.core.dsl.ActionNode
import io.github.lishangbu.avalon.game.battle.engine.core.dsl.action.SwapBoostsActionNode
import io.github.lishangbu.avalon.game.battle.engine.core.event.EventContext
import io.github.lishangbu.avalon.game.battle.engine.core.mutation.SetBoostsMutation
import io.github.lishangbu.avalon.game.battle.engine.core.runtime.ActionExecutor
import io.github.lishangbu.avalon.game.battle.engine.core.runtime.ActionResult
import io.github.lishangbu.avalon.game.battle.engine.core.runtime.support.BattleStoredBoostSupport
import io.github.lishangbu.avalon.game.battle.engine.core.runtime.support.EventContextTargetResolver
import io.github.lishangbu.avalon.game.battle.engine.core.type.ActionTypeId
import io.github.lishangbu.avalon.game.battle.engine.core.type.StandardActionTypeIds

/**
 * `swap_boosts` 动作执行器。
 */
class SwapBoostsActionExecutor : ActionExecutor {
    override val type: ActionTypeId = StandardActionTypeIds.SWAP_BOOSTS

    override fun execute(
        action: ActionNode,
        context: EventContext,
    ): ActionResult {
        require(action is SwapBoostsActionNode) { "Action must be SwapBoostsActionNode." }
        val leftUnit = EventContextTargetResolver.resolveSingleUnit(action.left, context) ?: return ActionResult()
        val rightUnit = EventContextTargetResolver.resolveSingleUnit(action.right, context) ?: return ActionResult()
        val (leftBoosts, rightBoosts) =
            BattleStoredBoostSupport.swap(
                left = leftUnit.boosts,
                right = rightUnit.boosts,
                stats = action.stats,
            )
        return ActionResult(
            mutations =
                listOf(
                    SetBoostsMutation(action.left, leftBoosts),
                    SetBoostsMutation(action.right, rightBoosts),
                ),
        )
    }
}