BattleSessionMoveOrderAttributeResolver.kt

package io.github.lishangbu.avalon.game.battle.engine.core.session

import io.github.lishangbu.avalon.game.battle.engine.core.constant.BattleAttributeKeys

/**
 * move action 的行动先后顺序 attribute 补全器。
 *
 * 设计意图:
 * - 把 `zoom-lens` 这类依赖“是否后手出招”的规则从 battle flow 主链中解耦;
 * - 统一在 session action 真正执行前补齐 `movesAfterTarget` 上下文,
 *   避免每个 handler 各自推导一次;
 * - 保持 direct flow 调用仍可通过显式 attributes 自行覆写。
 */
internal object BattleSessionMoveOrderAttributeResolver {
    /**
     * 为当前 move action 补齐行动顺序相关 attributes。
     *
     * 规则说明:
     * - 只要目标单位在当前回合的已执行 action 列表中出现过,
     *   就认为本次出招相对该目标属于“后手”;
     * - 若调用方已经显式传入 `movesAfterTarget`,则以调用方输入为准。
     */
    fun enrich(
        action: BattleSessionMoveAction,
        executedActions: List<BattleSessionAction>,
    ): BattleSessionMoveAction {
        if (BattleAttributeKeys.MOVES_AFTER_TARGET in action.attributes) {
            return action
        }
        val movesAfterTarget =
            executedActions.any { executedAction ->
                (executedAction as? BattleSessionSubmittingAction)?.submittingUnitId == action.targetId
            }
        return action.copy(
            attributes = action.attributes + (BattleAttributeKeys.MOVES_AFTER_TARGET to movesAfterTarget),
        )
    }
}