SetWeatherActionExecutor.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.SetWeatherActionNode
import io.github.lishangbu.avalon.game.battle.engine.core.event.EventContext
import io.github.lishangbu.avalon.game.battle.engine.core.mutation.SetWeatherMutation
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.type.ActionTypeId
import io.github.lishangbu.avalon.game.battle.engine.core.type.StandardActionTypeIds

/**
 * `set_weather` 动作执行器。
 */
class SetWeatherActionExecutor : ActionExecutor {
    override val type: ActionTypeId = StandardActionTypeIds.SET_WEATHER

    override fun execute(
        action: ActionNode,
        context: EventContext,
    ): ActionResult {
        require(action is SetWeatherActionNode) { "Action must be SetWeatherActionNode." }
        val resolvedDuration =
            AttachedEffectDurationResolver.resolve(
                explicitDuration = action.duration,
                effectId = action.value,
                context = context,
            )
        return ActionResult(
            mutations =
                listOf(
                    SetWeatherMutation(
                        weatherEffectId = action.value,
                        duration = resolvedDuration,
                        sourceId = context.source?.id ?: context.self?.id,
                    ),
                ),
        )
    }
}