BattleSessionUnitRelationResolver.kt
package io.github.lishangbu.avalon.game.battle.engine.core.session
import io.github.lishangbu.avalon.game.battle.engine.core.constant.BattleTargetRelationValues
import io.github.lishangbu.avalon.game.battle.engine.core.model.SideState
import io.github.lishangbu.avalon.game.battle.engine.core.runtime.flow.BattleRuntimeSnapshot
/**
* BattleSession 单位关系解析器。
*
* 设计意图:
* - 收敛 session 执行期里“单位属于哪个 side、双方关系是什么”这一类
* 低层查询逻辑;
* - 避免 move 结算、直接伤害、switch/run 等多个 helper 各自重复实现;
* - 保持这类关系推导为纯函数,便于后续继续复用或替换。
*/
internal object BattleSessionUnitRelationResolver {
/**
* 推导来源单位与目标单位之间的关系字符串。
*/
fun resolveTargetRelation(
snapshot: BattleRuntimeSnapshot,
sourceId: String?,
targetId: String,
): String? {
if (sourceId == null) {
return null
}
val sourceSideId = sideOfUnit(snapshot, sourceId)?.id ?: return null
val targetSideId = sideOfUnit(snapshot, targetId)?.id ?: return null
return when {
sourceId == targetId -> BattleTargetRelationValues.SELF
sourceSideId == targetSideId -> BattleTargetRelationValues.ALLY
else -> BattleTargetRelationValues.FOE
}
}
/**
* 读取单位当前所在的 side。
*/
fun sideOfUnit(
snapshot: BattleRuntimeSnapshot,
unitId: String?,
): SideState? =
snapshot.sides.values.firstOrNull { side ->
unitId in side.unitIds || unitId in side.activeUnitIds
}
}