1. 程式人生 > 實用技巧 >Java遊戲伺服器Condition元件(簡單實現)

Java遊戲伺服器Condition元件(簡單實現)

public class CIConfig {
    //唯一id
    public int id;
    //條件型別
    public ConditionType conditionType;
    //條件引數
    public String param;
    public CIConfig(int id, ConditionType conditionType, String param) {
        this.id = id;
        this.conditionType = conditionType;
        this.param = param;
    }



import java.util.HashMap; import java.util.Map; /** * 條件註冊:模擬配置 */ public class ConditionConfig { /** * key=條件型別,Value=配置 */ public static Map<Integer, CIConfig> conditionConfigs = new HashMap<Integer, CIConfig>(); static { //玩家等級 put(new CIConfig(1, ConditionType.PLAYER_LEVEL, "20"), "玩家等級大於等於20級"); put(
new CIConfig(2, ConditionType.PLAYER_LEVEL, "50"), "玩家等級大於等於50級"); put(new CIConfig(3, ConditionType.PLAYER_LEVEL, "80"), "玩家等級大於等於80級"); put(new CIConfig(4, ConditionType.PLAYER_LEVEL, "120"), "玩家等級大於等於120級"); //玩家vip等級 put(new CIConfig(5, ConditionType.PLAYER_VIP_LEVEL, "1"), "玩家vip等級大於等於1級"); put(
new CIConfig(6, ConditionType.PLAYER_VIP_LEVEL, "5"), "玩家vip等級大於等於5級"); put(new CIConfig(7, ConditionType.PLAYER_VIP_LEVEL, "8"), "玩家vip等級大於等於8級"); put(new CIConfig(8, ConditionType.PLAYER_VIP_LEVEL, "10"), "玩家vip等級大於等於10級"); //玩家戰力 put(new CIConfig(9, ConditionType.PLAYER_FOWER, "12000"), "玩家戰力於等於12000"); put(new CIConfig(10, ConditionType.PLAYER_FOWER, "30000"), "玩家戰力大於等於30000"); put(new CIConfig(11, ConditionType.PLAYER_FOWER, "80000"), "玩家戰力大於等於80000"); put(new CIConfig(12, ConditionType.PLAYER_FOWER, "180000"), "玩家戰力大於等於180000"); } public static void put(CIConfig iConfig, String des) { conditionConfigs.put(iConfig.id, iConfig); } public static CIConfig getiConfig(int id) { return conditionConfigs.get(id); } } /** * linxu */ public interface Condition { boolean check(ICondition iCondition); } /** * @author linxu * 判斷玩家等級條件處理器 */ public class PlayerLevelCondition implements Condition { @Override public boolean check(ICondition iCondition) { CIConfig ciConfig = ConditionConfig.getiConfig(iCondition.getId()); if (ciConfig != null) { int level = Integer.parseInt(ciConfig.param); return level >= 100; } return false; } } /** * @author linxu * 判斷玩家戰力條件處理器 */ public class PlayerPowerCondtiton implements Condition { @Override public boolean check(ICondition iCondition) { CIConfig ciConfig = ConditionConfig.getiConfig(iCondition.getId()); if (ciConfig != null) { int power = Integer.parseInt(ciConfig.param); return power >= 50000; } return false; } } /** * @author linxu * 判斷玩家vip等級條件處理器 */ public class PlayerVipLevelCondtiton implements Condition { @Override public boolean check(ICondition iCondition) { CIConfig ciConfig = ConditionConfig.getiConfig(iCondition.getId()); if (ciConfig != null) { int vipLevel = Integer.parseInt(ciConfig.param); return vipLevel >= 8; } return false; } } /** * linxu */ public enum ConditionType { PLAYER_LEVEL(1,"玩家等級條件判斷:大於等於指定等級"), PLAYER_VIP_LEVEL(2,"玩家vip等級條件判斷:大於等級指定vip等級"), PLAYER_FOWER(3,"玩家戰鬥力條件判斷:大於等於指定戰鬥力") ; private int type; private String des; ConditionType(int type, String des){ this.type=type; this.des=des; } /** * 條件型別處理去初始化工廠 */ public class ConditionFactory { /** * 儲存玩家條件處理器容器 */ public static Map<ConditionType, Condition> conditionMap = new HashMap<>(); static { registered(ConditionType.PLAYER_LEVEL, new PlayerLevelCondition(), "玩家等級條件處理器"); registered(ConditionType.PLAYER_VIP_LEVEL, new PlayerVipLevelCondtiton(), "玩家vip條件處理器"); registered(ConditionType.PLAYER_FOWER, new PlayerPowerCondtiton(), "玩家戰力條件處理器"); /** * 後續要增加,往後註冊 */ } public static void registered(ConditionType conditionType, Condition condition, String des) { conditionMap.put(conditionType, condition); } public static Condition getCondition(ConditionType conditionType) { return conditionMap.get(conditionType); } } /** * 條件組 */ public class ConnditionGroup { /** * 當前組所屬條件 */ public List<ICondition>iConditions=new ArrayList<>(); public List<ICondition> getiConditions() { return iConditions; } public void setiConditions(List<ICondition> iConditions) { this.iConditions = iConditions; } /** * 必須是所有條件都滿足&【沒有未達成的條件==條件符合】 * @return */ public boolean check(){ System.out.println(); return iConditions.stream().filter(s->!s.chcek()).collect(Collectors.toList()).size()<=0; } } public class ICondition { /** * 條件id */ private int id; /** * 所屬條件型別 */ private ConditionType conditionType; public int getId() { return id; } public void setId(int id) { this.id = id; } public ConditionType getConditionType() { return conditionType; } public void setConditionType(ConditionType conditionType) { this.conditionType = conditionType; } /** * 檢查條件 * @return */ public boolean chcek() { Condition condition = ConditionFactory.getCondition(this.getConditionType()); return condition == null ? false : condition.check(this); } } /** * linxu */ public class ConditionMgr { private static ConditionMgr conditionMgr = new ConditionMgr(); public static ConditionMgr getInstance() { return conditionMgr; } public boolean check(String value) { List<int[]> list = paserList(value); //條件組 List<ConnditionGroup> connditionGroups = new ArrayList<>(); for (int[] c : list) { ConnditionGroup connditionGroup = new ConnditionGroup(); for (int i : c) { //條件組裡的的單個條件 CIConfig ciConfig = ConditionConfig.getiConfig(i); if (ciConfig == null) { continue; } ICondition iCondition = new ICondition(); iCondition.setId(i); iCondition.setConditionType(ciConfig.conditionType); connditionGroup.getiConditions().add(iCondition); } connditionGroups.add(connditionGroup); } //只要有一個符合,條件就滿足 return connditionGroups.isEmpty() ? false : connditionGroups.stream().filter(s -> s.check()).collect(Collectors.toList()).size() > 0; } public List<int[]> paserList(String value) { String strVlaue = (String) value; List<int[]> list = new ArrayList<>(); if (!StringUtils.isEmpty(strVlaue)) { String[] allArray = strVlaue.split(Symbol.SHUXIAN); for (String s : allArray) { String[] typeArray = s.split(Symbol.AND); list.add(StringUtil.strArrToIntArr(typeArray)); } } return list; } } public class ConditionTest { public static void main(String[] args) { /** * 注意&是且,|值得是或 */ //必須都滿足 String con1="4&7";//玩家等級大於等於120級,且玩家vip等級大於等於8級 System.out.println(ConditionMgr.getInstance().check(con1)); //必須都滿足 String con2="4&7&9";////玩家等級大於等於120級,且玩家vip等級大於等於8級,且玩家戰力於等於12000 System.out.println(ConditionMgr.getInstance().check(con2)); //只要滿足一個即可 String con3="1&7|11";////玩家等級大於等於20級,且玩家vip等級大於等於8級,或則玩家戰力大於等於80000 System.out.println(ConditionMgr.getInstance().check(con3)); } } 測試結果: true false true Process finished with exit code 0