package gameDesign; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import db.SqlHelper; import entity.JConfig; import entity.UserInfo; import java.util.*; public class PlotConstructor extends ElementConstructor{ JSONArray plots = null; HashMap> adjList = new HashMap>(); HashMap> revAdjList = new HashMap>(); HashMap id2j = new HashMap(); HashMap visited = new HashMap(); public PlotConstructor(SqlHelper helper, int botId){ super(); this.helper = helper; this.botId = botId; } public void construct(JSONObject jsonObject, UserInfo userinfo){ JSONArray plots = jsonObject.getJSONArray("plots"); String start = (String)jsonObject.get("start"); userinfo.varInitialMap.put(start, "1"); userinfo.varInitialMap.put("unknownText", "你说啥?"); this.plots = plots; constructAdjList(plots); BFSGenerate2(id2j.get(start)); } //根据id构造邻接表 public void constructAdjList(JSONArray plots){ int length = plots.size(), branchSize; String id = "", nextPlotId = ""; JSONObject plot = null; JSONArray branch = null; for(int i = 0;i < length;i++){ plot = plots.getJSONObject(i); id = (String)plot.get("id"); plot.put("index", i); if(adjList.get(id) == null){ adjList.put(id, new ArrayList<>()); } //添加branch中的下一个plot branch = plot.getJSONArray("branch"); branchSize = branch.size(); for(int j =0;j < branchSize;j++) { nextPlotId = (String)branch.getJSONObject(j).get("nextPlotId"); if(revAdjList.get(nextPlotId) == null){ revAdjList.put(nextPlotId, new ArrayList<>()); } adjList.get(id).add(nextPlotId); revAdjList.get(nextPlotId).add(id); } id2j.put(id, plot); } } public void BFSGenerate(JSONObject root){ String id = (String)root.get("id"), adjId, nextId, taskName = "", value = "" ,extraHP = "", extraUK="", nextTaskName, keywordOptions, conditionName, unknownReply1, unknownReply2, nextCondition; boolean newBranch, manyParent; JSONObject node = null, branchNode = null, adjNode = null; JSONArray branch = null; List adjNodeList = null; int priority, taskStep, nextPriority, nextTaskStep, branchSize; root.put("priority", 0); root.put("taskName", id); root.put("taskStep", 0); Queue queue = new LinkedList(); queue.add(root); while(!queue.isEmpty()){ node = queue.remove(); id = (String)node.get("id"); taskName = (String)node.get("taskName"); priority = (Integer)node.get("priority"); taskStep = (Integer)node.get("taskStep"); conditionName = (String)node.get("conditionName"); //初始化 newBranch = false; manyParent = false; adjNodeList = adjList.get(id); visited.put(id ,true); //查看是否存在多个子结点,如果有就是一个分支 if(adjNodeList.size() > 1) newBranch = true; //查看是否存在多个父节点 if(revAdjList.get(id) != null) manyParent = revAdjList.get(id).size() > 1; if(newBranch){ //多个分支时nextTaskName为子结点的id名,不在这里设 nextTaskStep = 1; } else{ if(manyParent){ nextTaskName = id; nextTaskStep = 1; priority++; } else{ nextTaskName = taskName; nextTaskStep = taskStep + 1; } } branch = node.getJSONArray("branch"); branchSize = branch.size(); //当两次出现unknown时给用户一下候选项 keywordOptions = ""; extraHP = taskName + "::match-Keyword=" + taskStep; for(int i =0;i < branchSize;i++) { branchNode = branch.getJSONObject(i); nextId = (String)branchNode.get("nextPlotId"); if(id2j.get(nextId) != null) nextCondition = (String)id2j.get(nextId).get("conditionName"); else nextCondition = " "; //unknown计数器更新为0, 当前任务状态+1 if(newBranch) extraUK = taskName + ":" + taskStep; //多余步骤,以后要处理掉 else extraUK = taskName + ":" + (taskStep + 1); if(newBranch || (revAdjList.get(nextId) != null && revAdjList.get(nextId).size() > 1)) extraUK += "-<-" + nextId + ":" + 1; jConfig = new JConfig( JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class), "", extraHP, extraUK, "match", (String) branchNode.get("reply"), (String) nextCondition, String.valueOf(priority) ); jConfigList.add(jConfig); try{ keywordOptions += jConfig.getCurPremise().split(",")[0].split("=")[1].split("\\+")[0] + " 或者"; }catch (Exception e){ e.printStackTrace(); } if(branchNode.get("nextPlotId") != null){ adjId = (String)branchNode.get("nextPlotId"); if(visited.get(adjId) == null && id2j.get(adjId) != null) { adjNode = id2j.get(adjId); adjNode.put("taskStep", nextTaskStep); if(newBranch){ adjNode.put("priority", priority+1); adjNode.put("taskName", adjId); }else{ adjNode.put("priority", priority); adjNode.put("taskName", taskName); } queue.add(adjNode); } } } /* //对于unknown的特殊处理 jConfig = new JConfig( conditionName + "::match-Keyword=unknown", extraHP + ",unknown_counter::number-SmallOrEqual=0,unknownReply::match-Keyword=null", "match", "{lastReply-2}", " ", "unknown_counter:+1" ); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); jConfig = new JConfig( conditionName + "::match-Keyword=unknown", extraHP + ",unknown_counter::number-SmallOrEqual=0", "match", "{unknownReply-2}", " ", "unknown_counter:+1" ); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); jConfig = new JConfig( conditionName + "::match-Keyword=unknown", extraHP + ",unknown_counter::number-BiggerOrEqual=1", "match", "你可以说" + keywordOptions, " ", "unknown_counter:+1" ); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); */ } //unknown时的处理 jConfig = new JConfig(" ", " ", "", "{unknownText-2}", " ", ""); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } public void BFSGenerate2(JSONObject root){ String id = (String)root.get("id"), adjId, nextId, taskName = "", value = "" ,extraHP = "", extraUK="", nextTaskName, keywordOptions, conditionName, unknownReply1, unknownReply2, nextCondition; boolean newBranch, manyParent; JSONObject node = null, branchNode = null, adjNode = null; JSONArray branch = null; List adjNodeList = null; int priority, taskStep, nextPriority, nextTaskStep, branchSize; root.put("priority", 0); root.put("taskName", id); root.put("taskStep", 0); Queue queue = new LinkedList(); queue.add(root); while(!queue.isEmpty()){ node = queue.remove(); id = (String)node.get("id"); //初始化 newBranch = false; manyParent = false; adjNodeList = adjList.get(id); visited.put(id ,true); /* //查看是否存在多个子结点,如果有就是一个分支 if(adjNodeList.size() > 1) newBranch = true; //查看是否存在多个父节点 if(revAdjList.get(id) != null) manyParent = revAdjList.get(id).size() > 1; if(newBranch){ //多个分支时nextTaskName为子结点的id名,不在这里设 nextTaskStep = 1; } else{ if(manyParent){ nextTaskName = id; nextTaskStep = 1; priority++; } else{ nextTaskName = taskName; nextTaskStep = taskStep + 1; } } */ branch = node.getJSONArray("branch"); branchSize = branch.size(); //当两次出现unknown时给用户一下候选项 keywordOptions = ""; extraHP = id + "::match-Keyword=" + 1; for(int i =0;i < branchSize;i++) { branchNode = branch.getJSONObject(i); nextId = (String)branchNode.get("nextPlotId"); if(id2j.get(nextId) != null) nextCondition = (String)id2j.get(nextId).get("conditionName"); else nextCondition = " "; extraUK = id + ":" + "+1-<-" + nextId + ":" + "+1"; //if(newBranch || (revAdjList.get(nextId) != null && revAdjList.get(nextId).size() > 1)) // extraUK += "-<-" + nextId + ":" + 1; jConfig = new JConfig( JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class), "", extraHP, extraUK, "match", (String) branchNode.get("reply"), (String) nextCondition, "0" ); jConfigList.add(jConfig); try{ keywordOptions += jConfig.getCurPremise().split(",")[0].split("=")[1].split("\\+")[0] + " 或者"; }catch (Exception e){ e.printStackTrace(); } if(branchNode.get("nextPlotId") != null){ adjId = (String)branchNode.get("nextPlotId"); if(visited.get(adjId) == null && id2j.get(adjId) != null) { adjNode = id2j.get(adjId); queue.add(adjNode); } } } } //unknown时的处理 jConfig = new JConfig(" ", " ", "", "{unknownText-2}", " ", ""); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } }