package gameDesign; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import db.SqlHelper; import entity.JConfig; import entity.UserInfo; import org.apache.http.message.BasicNameValuePair; import processing.MyFunc; import java.io.UnsupportedEncodingException; import java.util.*; public class PlotConstructor extends ElementConstructor { String POST_URL = "http://localhost:1111/conditionProcess"; 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"), startCondition; JSONObject returnMessage = null; this.plots = plots; constructAdjList(plots); if(id2j.containsKey(start)) { JSONObject data = new JSONObject(); startCondition = (String)id2j.get(start).get("conditionName"); data.put("startCondition", startCondition); data.put("botName_yhm", (String)jsonObject.get("botName")); data.put("botName", (String)jsonObject.get("botName")); data.put("botId", (Integer)jsonObject.get("botId")); data.put("type", "game_plot_init"); userinfo.extraInfo.put("activeAction", startCondition); try { returnMessage = MyFunc.postJSON(POST_URL, data); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if((Integer)returnMessage.get("status") == 1) { String taskLineName = "任务线_" + id2j.get(start).get("place") + "进度"; conditionDescription.put(taskLineName, taskLineName); varInitialMap.put(taskLineName, start); varInitialMap.put("unknownText", "你说啥?"); conditionDescription.put("unknownText", "对于无法识别的问题做出的回答"); BFSGenerate4(id2j.get(start), userinfo); varInitialMap.put(start, "1"); }else { System.out.println("剧情初始化失败"); } } else { System.out.println("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); } } /** * 根据每个剧情点的进度设置计数器 * @param root * @param userinfo */ public void BFSGenerate(JSONObject root, UserInfo userinfo) { String id = (String) root.get("id"), adjId, nextId, taskName = "", value = "", extraHP = "", extraUK = "", nextTaskName, keywordOptions, conditionName, nextUnknownText, curUnknownText, 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); root.put("nextUnknownText", varInitialMap.get("unknownText")); ; Queue queue = new LinkedList(); queue.add(root); while (!queue.isEmpty()) { node = queue.remove(); id = (String) node.get("id"); conditionDescription.put(id, "剧情" + id + "进度"); varInitialMap.put(id, ""); taskName = (String) node.get("taskName"); priority = (Integer) node.get("priority"); taskStep = (Integer) node.get("taskStep"); conditionName = (String) node.get("conditionName"); curUnknownText = (String) node.get("nextUnknownText"); //初始化 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 = ""; for (int i = 0; i < branchSize; i++) { branchNode = branch.getJSONObject(i); nextId = (String) branchNode.get("nextPlotId"); Map updateKnowledge = JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class); nextUnknownText = updateKnowledge.get("unknownText"); 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); extraHP = taskName + "::match-Include=" + taskStep; if (newBranch || (revAdjList.get(nextId) != null && revAdjList.get(nextId).size() > 1)) extraHP += "," + nextId + "::match-Include=null"; extraUK += "-<-" + nextId + ":" + 1; jConfig = new JConfig( JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), updateKnowledge, "", 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) { if (id2j.get(adjId) != null) { adjNode = id2j.get(adjId); adjNode.put("nextUnknownText", nextUnknownText); 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); } else { conditionDescription.put(adjId, "剧情" + adjId + "进度"); varInitialMap.put(adjId, ""); } } } } //对于unknown的特殊处理 if (curUnknownText.equals("")) { jConfig = new JConfig( conditionName + "::match-Include=unknown", extraHP = taskName + "::match-Include=" + taskStep, "match,return-0,post::all(" + CHITCHAT_URL + ")", " ", " ", "" ); jConfigList.add(jConfig); } /* 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); } //每个结点分为0、1、2三种状态 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-Include=" + 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); } /** * 将所有剧情根据地点分为多条剧情线,每一条剧情线都有一个计数器,记录当前地点的剧情到了第几步。 * @param root * @param userinfo */ public void BFSGenerate3(JSONObject root, UserInfo userinfo) { String id, rootId = (String) root.get("id"), adjId, nextId, parentId, taskName = "", value = "", extraHP = "", extraUK = "", nextTaskName, keywordOptions, conditionName, nextUnknownText, curUnknownText, nextCondition, location, nextLocation, taskLineName, nTaskLineName, lastTaskReply, lastTaskCondition; boolean newBranch, manyParent; JSONObject node = null, branchNode = null, adjNode = null; JSONArray branch = null; List adjNodeList = null; int priority, taskStep, nextPriority, nextTaskStep, branchSize; root.put("nextUnknownText", varInitialMap.get("unknownText")); root.put("parentId", "null"); Queue queue = new LinkedList(); System.out.println("rootId"); queue.add(root); while (!queue.isEmpty()) { node = queue.remove(); id = (String) node.get("id"); parentId = (String) node.get("parentId"); location = (String) node.get("place"); conditionName = (String) node.get("conditionName"); curUnknownText = (String) node.get("nextUnknownText"); taskLineName = "任务线_" + location + "进度"; //本任务线的最后一个condition和本任务线的最后一个回答 if (!varInitialMap.containsKey(taskLineName)) { conditionDescription.put(taskLineName, taskLineName); varInitialMap.put(taskLineName, ""); } //初始化 newBranch = false; manyParent = false; adjNodeList = adjList.get(id); visited.put(id, true); branch = node.getJSONArray("branch"); branchSize = branch.size(); //当两次出现unknown时给用户一下候选项 keywordOptions = ""; for (int i = 0; i < branchSize; i++) { branchNode = branch.getJSONObject(i); nextId = (String) branchNode.get("nextPlotId"); Map updateKnowledge = JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class); nextUnknownText = updateKnowledge.get("unknownText"); //如果存在下一剧情,就将下一步剧情的condition设为下一个激活的conditin,把地点更新为下一步剧情的地点,并更新任务线进度 if (id2j.get(nextId) != null) { nextCondition = (String) id2j.get(nextId).get("conditionName"); nextLocation = (String) id2j.get(nextId).get("place"); nTaskLineName = "任务线_" + nextLocation + "进度"; } else { //如果不存在下一剧情,就将condition设为空,任务线进度不变 nextCondition = " "; nTaskLineName = taskLineName; nextLocation = location; } lastTaskCondition = "任务线" + nextLocation + "最后一个condition"; lastTaskReply = "任务线" + nextLocation + "最后的回答"; //额外条件是任务线进度达到当前剧情,额外更新变量是将任务线移动至下一剧情 extraHP = taskLineName + "::match-Include=" + id + ",site::match-Include=" + location; //测试用 System.out.println(id); extraUK = nTaskLineName + ":" + nextId + "-<-site:" + nextLocation + "-<-" + lastTaskCondition + ":var={nextStep-3}" + "-<-" + lastTaskReply + ":var={reply-3}"; jConfig = new JConfig( JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), updateKnowledge, "", 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) { if (id2j.get(adjId) != null) { adjNode = id2j.get(adjId); adjNode.put("parentId", id); adjNode.put("nextUnknownText", nextUnknownText); queue.add(adjNode); } else { conditionDescription.put(adjId, "剧情" + adjId + "进度"); varInitialMap.put(adjId, ""); } } } } //对于unknown的特殊处理 if (curUnknownText.equals("")) { jConfig = new JConfig( conditionName + "::match-Include=unknown", extraHP = "任务线_" + location + "进度::match-Include=" + parentId, "match,return-0,post::all(" + CHITCHAT_URL + ")", " ", " ", "" ); jConfigList.add(jConfig); } } jConfig = new JConfig( " ", " ", "", " ", " ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown"); jConfig.setjStatus(1); jConfigList.add(jConfig); jConfig = new JConfig( " ", "", "", " " ," ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("common_answer"); jConfig.setjStatus(1); jConfigList.add(jConfig); } /** * 将所有剧情根据地点分为多条剧情线,每一条剧情线都有一个计数器,记录当前地点的剧情到了第几步。 * 加上剧情计数器, 每一步分为三个状态 * @param root * @param userinfo */ public void BFSGenerate4(JSONObject root, UserInfo userinfo) { String id, rootId = (String) root.get("id"), adjId, nextId, parentId, taskName = "", value = "", extraHP = "", extraUK = "", nextTaskName, keywordOptions, conditionName, nextUnknownText, curUnknownText, nextCondition, location, nextLocation, taskLineName, nTaskLineName, lastTaskReply, lastTaskCondition; boolean newBranch, manyParent; JSONObject node = null, branchNode = null, adjNode = null; JSONArray branch = null; List adjNodeList = null; int priority, taskStep, nextPriority, nextTaskStep, branchSize; root.put("nextUnknownText", varInitialMap.get("unknownText")); root.put("parentId", "null"); Queue queue = new LinkedList(); queue.add(root); while (!queue.isEmpty()) { node = queue.remove(); id = (String) node.get("id"); parentId = (String) node.get("parentId"); location = (String) node.get("place"); conditionName = (String) node.get("conditionName"); curUnknownText = (String) node.get("nextUnknownText"); taskLineName = "任务线_" + location + "进度"; conditionDescription.put(id, "剧情" + id + "状态"); varInitialMap.put(id ,""); //本任务线的最后一个condition和本任务线的最后一个回答 if (!varInitialMap.containsKey(taskLineName)) { conditionDescription.put(taskLineName, taskLineName); varInitialMap.put(taskLineName, ""); } //初始化 newBranch = false; manyParent = false; adjNodeList = adjList.get(id); visited.put(id, true); branch = node.getJSONArray("branch"); branchSize = branch.size(); //当两次出现unknown时给用户一下候选项 keywordOptions = ""; for (int i = 0; i < branchSize; i++) { branchNode = branch.getJSONObject(i); nextId = (String) branchNode.get("nextPlotId"); Map updateKnowledge = JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class); nextUnknownText = updateKnowledge.get("unknownText"); //如果存在下一剧情,就将下一步剧情的condition设为下一个激活的conditin,把地点更新为下一步剧情的地点,并更新任务线进度 if (id2j.get(nextId) != null) { nextCondition = (String) id2j.get(nextId).get("conditionName"); nextLocation = (String) id2j.get(nextId).get("place"); nTaskLineName = "任务线_" + nextLocation + "进度"; } else { //如果不存在下一剧情,就将condition设为空,任务线进度不变 nextCondition = " "; nTaskLineName = taskLineName; nextLocation = location; } lastTaskCondition = "任务线" + nextLocation + "最后一个condition"; lastTaskReply = "任务线" + nextLocation + "最后的回答"; //额外条件是任务线进度达到当前剧情,额外更新变量是将任务线移动至下一剧情 extraHP = taskLineName + "::match-Include=" + id + ",site::match-Include=" + location + "," + id + "::number-BiggerOrEqual=" + 1 + "," + nextId + "::match-Include=null"; extraUK = nTaskLineName + ":" + nextId + "-<-site:" + nextLocation + "-<-" + lastTaskCondition + ":var={nextStep-3}" + "-<-" + lastTaskReply + ":var={reply-3}" + "-<-" + id + ":" + "+1-<-" + nextId + ":" + "+1";; jConfig = new JConfig( JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), updateKnowledge, "", 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) { if (id2j.get(adjId) != null) { adjNode = id2j.get(adjId); adjNode.put("parentId", id); adjNode.put("nextUnknownText", nextUnknownText); queue.add(adjNode); } else { conditionDescription.put(adjId, "剧情" + adjId + "进度"); varInitialMap.put(adjId, ""); } } } } //对于unknown的特殊处理 if (curUnknownText.equals("")) { jConfig = new JConfig( conditionName + "::match-Include=unknown", extraHP = "任务线_" + location + "进度::match-Include=" + parentId, "match,return-0,post::all(" + CHITCHAT_URL + ")", " ", " ", "" ); jConfigList.add(jConfig); } } jConfig = new JConfig( " ", " ", "", " ", " ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown"); jConfig.setjStatus(1); jConfigList.add(jConfig); jConfig = new JConfig( " ", "", "", " " ," ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("common_answer"); jConfig.setjStatus(1); jConfigList.add(jConfig); } @Override public void postProcessing(List idList){ if (idList.size() != 0) { jConfig = new JConfig(" ", "", "activate", "", idList.get(idList.size() - 2) + "," + idList.get(idList.size() - 1), ""); jConfigList.add(jConfig); } } }