26-03-13-14:36:火力规则简单实现
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.solution.rule.strategy;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.solution.common.constant.ExceptionConstants;
|
||||
import com.solution.rule.domain.RuleParam;
|
||||
import com.solution.rule.domain.vo.PlatformWeaponAggregateVO;
|
||||
import com.solution.rule.enums.SceneType;
|
||||
import com.solution.rule.handler.RuleChainHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Component
|
||||
public class AirborneStrategy implements SceneStrategy{
|
||||
|
||||
|
||||
@Autowired
|
||||
private RuleChainHandler ruleChainHandler;
|
||||
/**
|
||||
* 空降场景处理
|
||||
* @param ruleParam
|
||||
* @return
|
||||
*/
|
||||
|
||||
|
||||
@Override
|
||||
public List<PlatformWeaponAggregateVO> execute(RuleParam ruleParam) {
|
||||
if(ObjectUtil.isEmpty(ruleParam) || CollUtil.isEmpty(ruleParam.getWeaponModelDTOList())){
|
||||
throw new RuntimeException(ExceptionConstants.PARAMETER_EXCEPTION);
|
||||
}
|
||||
|
||||
ruleParam = ruleChainHandler.findRuleParam(ruleParam);
|
||||
|
||||
return ruleParam.getResultWeapons();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SceneType getSceneType() {
|
||||
return SceneType.AIRBORNE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.solution.rule.strategy;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.solution.common.constant.ExceptionConstants;
|
||||
import com.solution.rule.domain.RuleParam;
|
||||
import com.solution.rule.domain.vo.PlatformWeaponAggregateVO;
|
||||
import com.solution.rule.enums.SceneType;
|
||||
import com.solution.rule.handler.RuleChainHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class DefenseStrategy implements SceneStrategy{
|
||||
|
||||
|
||||
@Autowired
|
||||
private RuleChainHandler ruleChainHandler;
|
||||
/**
|
||||
* 防御场景处理
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
|
||||
|
||||
@Override
|
||||
public List<PlatformWeaponAggregateVO> execute(RuleParam ruleParam) {
|
||||
if(ObjectUtil.isEmpty(ruleParam) || CollUtil.isEmpty(ruleParam.getWeaponModelDTOList())){
|
||||
throw new RuntimeException(ExceptionConstants.PARAMETER_EXCEPTION);
|
||||
}
|
||||
|
||||
ruleParam = ruleChainHandler.findRuleParam(ruleParam);
|
||||
|
||||
|
||||
return ruleParam.getResultWeapons();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SceneType getSceneType() {
|
||||
return SceneType.DEFENSE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.solution.rule.strategy;
|
||||
|
||||
import com.solution.rule.domain.RuleParam;
|
||||
import com.solution.rule.domain.vo.PlatformWeaponAggregateVO;
|
||||
import com.solution.rule.enums.SceneType;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public interface SceneStrategy {
|
||||
|
||||
List<PlatformWeaponAggregateVO> execute(RuleParam ruleParam);
|
||||
|
||||
SceneType getSceneType();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.solution.rule.strategy;
|
||||
|
||||
import com.solution.rule.enums.SceneType;
|
||||
import com.solution.rule.strategy.SceneStrategy;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class SceneStrategyFactory {
|
||||
|
||||
@Autowired
|
||||
private List<SceneStrategy> strategyList;
|
||||
|
||||
private final Map<SceneType, SceneStrategy> strategyMap = new EnumMap<>(SceneType.class);
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
for (SceneStrategy strategy : strategyList) {
|
||||
SceneType type = strategy.getSceneType();
|
||||
if (strategyMap.containsKey(type)) {
|
||||
throw new IllegalStateException("重复的场景类型: " + type);
|
||||
}
|
||||
strategyMap.put(type, strategy);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据前端传递的数字编码获取对应的策略
|
||||
* @param code 前端传递的数字
|
||||
* @return 策略实现
|
||||
* @throws IllegalArgumentException 如果编码无效或策略未注册
|
||||
*/
|
||||
public SceneStrategy getStrategy(int code) {
|
||||
SceneType type = SceneType.fromCode(code);
|
||||
SceneStrategy strategy = strategyMap.get(type);
|
||||
if (strategy == null) {
|
||||
throw new IllegalArgumentException("未找到编码 " + code + " 对应的策略实现");
|
||||
}
|
||||
return strategy;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user