package com.qiwu.chatbotbackend.gameDesign; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.entity.ConfigTerm; import com.qiwu.chatbotbackend.entity.Premise; import com.qiwu.chatbotbackend.entity.UserInfo; import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PlotConstructor2 extends ElementConstructor { public static String ASSISTANT = "globalVariable_智能助手"; public static String ASSISTANT_EMOTION = "正常"; public static String TASK_DESCRIPTION = "globalVariable_任务"; public static String TALKER_PATTERN = "【%s@%s】"; public static String GAME_STEP_FORMAT = "%s_plot_%s"; JSONArray plots = null; HashMap> adjList = new HashMap>(); HashMap> revAdjList = new HashMap>(); private HashMap plotIndex = new HashMap(); HashMap visited = new HashMap(); public HashMap getPlotIndex() { return plotIndex; } public static String TASK_LINE = "任务线_%s进度"; public static String TASK_CONDITION = "任务线%s最后一个condition"; public static String TASK_REPLY = "任务线%s最后的回答"; public PlotConstructor2(SqlHelper helper, int botId, String botAccount, Map keywordMap, HashSet exceptRelation, StringBuffer POS) { super(); this.helper = helper; this.botId = botId; this.botAccount = botAccount; this.exceptRelation = exceptRelation; this.keywordMap = keywordMap; this.POS = POS; } public void construct(JSONObject jsonObject, UserInfo userinfo) { try { addGameStartPlot(jsonObject, keywordMap); JSONArray plots = jsonObject.getJSONArray("plots"); String start = (String) jsonObject.get("start"), startCondition; JSONObject returnMessage = null; this.plots = plots; constructAdjList(plots); if (plotIndex.containsKey(start)) { String taskLineName = "任务线_" + plotIndex.get(start).get("place") + "进度"; conditionDescription.put(taskLineName, taskLineName); if(jsonObject.get("firstUnknownText") == null) varInitialMap.put("unknownText", "你说啥?"); else varInitialMap.put("unknownText", (String)jsonObject.get("firstUnknownText")); varInitialMap.put("game_step", String.format(GAME_STEP_FORMAT, botAccount, (String) plotIndex.get(start).get("id"))); conditionDescription.put("unknownText", "对于无法识别的问题做出的回答"); conditionDescription.put("game_step", "QA问答游戏进度"); BFSGenerate4(plotIndex.get(start), userinfo, jsonObject); varInitialMap.put(start, "1"); ListeningConstructor.cc(botAccount, jsonObject, plotIndex, configTermList); } else { System.out.println("start剧情不存在"); operationStatus = 0; message = "开始剧情不存在,请到剧情列表设置"; return; } } catch(Exception e){ operationStatus = 0; message = "剧情接口处理出现异常"; e.printStackTrace(); return; } } /** * 根据id构造邻接表 * @param plots */ public void constructAdjList(JSONArray plots) { int length = plots.size(), branchSize; String id = "", nextPlotId = "", nextPlotText; 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<>()); } varSlotFilt(plot, keywordMap); //添加branch中的下一个plot branch = plot.getJSONArray("branch"); branchSize = branch.size(); for (int j = 0; j < branchSize; j++) { nextPlotId = (String) branch.getJSONObject(j).get("nextPlotId"); branch.getJSONObject(j).put("curPremise", premiseTextFilt(branch.getJSONObject(j).getJSONArray("curPremise"))); branch.getJSONObject(j).put("hisPremise", premiseTextFilt(branch.getJSONObject(j).getJSONArray("hisPremise"))); //将剧情和相应文本相对应 //plotTextIndex.put(nextPlotId, nextPlotText); if (revAdjList.get(nextPlotId) == null) { revAdjList.put(nextPlotId, new ArrayList<>()); } adjList.get(id).add(nextPlotId); revAdjList.get(nextPlotId).add(id); } plotIndex.put(id, plot); } } /** * 将条件文本中的一些特殊字符过滤调 * @param premiseList 条件列表 * @return */ JSONArray premiseTextFilt(JSONArray premiseList){ JSONObject premise = null; int size = premiseList.size(); for(int i = 0;i < size;i++){ premise = premiseList.getJSONObject(i); premise.put("keyword", premise.getString("keyword").replace("\r\n", "").replace("\n", "").replace("\r", "")); premiseList.set(i, premise); } return premiseList; } /** * 如果剧情文本中出现变量槽,那么需要判断这个剧情的npc是不是在线模式,如果不是在线模式,就需要把变量槽过滤掉 * @param plot */ public static void varSlotFilt(JSONObject plot, Map keywordMap){ String VARIABLE_SEARCH_PATTERN = "【(.*?)@(.*?)(\\$\\{.*?})", npcName, middleText = "", varSlot = ""; //不加 】,直接替换npcName到变量槽的文本 System.out.println(plot.getString("id")); Pattern replacePattern = Pattern.compile(VARIABLE_SEARCH_PATTERN); Matcher m = replacePattern.matcher(plot.getString("plotText")); while (m.find()) { npcName = m.group(1); //onlineModeTalkers, 在CharecterConstructor中定义,q前面加一个逗号是为了防止出现子串匹配的问题 //System.out.println("npcName" + npcName); //System.out.println("varInitialMap.get(\"onlineModeTalkers\")" + varInitialMap.get("onlineModeTalkers")); if(keywordMap.get("onlineModeTalkers").indexOf("," + npcName + ",") == -1) { varSlot = m.group(3); middleText = m.group(2); plot.put("plotText", plot.getString("plotText").replace(npcName + "@" + middleText + varSlot, npcName + "@" + middleText)); } } } /** * 将所有剧情根据地点分为多条剧情线,每一条剧情线都有一个计数器,记录当前地点的剧情到了第几步。 * 加上剧情计数器, null时未激活,大于等于1时表示已激活 * @param root * @param userinfo */ public void BFSGenerate4(JSONObject root, UserInfo userinfo, JSONObject jsonObject) { String id, rootId = (String) root.get("id"), adjId, nextId, conditionName = "yaobuyao", nextUnknownText, curUnknownText, nextCondition, location, nextLocation, taskLineName, nTaskLineName, lastTaskReply, lastTaskCondition, reply; String hint, gameStep, nextGameStep; JSONObject node = null, branchNode = null; Boolean curAutoJump, nextAutoJump = false, prevAutoJump, moveWithPlot = false; StringBuffer skipText = new StringBuffer(), extraUK = new StringBuffer(), extraHP = new StringBuffer(), extraFT = new StringBuffer(), arriveUK = new StringBuffer(); JSONArray branch = null; int branchSize; List curPremiseList, historyPremiseList; root.put("nextUnknownText", varInitialMap.get("unknownText")); root.put("parentId", "null"); root.put("autoJump", false); root.put("prevAutoJump", false); jsonObject.put("plotStartText", ((String)jsonObject.get("plotStartText")).replace("\n", "").replace(",", ",")); if(root.get("conditionName") != null && !root.get("conditionName").equals("")) conditionName = (String)root.get("conditionName"); else{ conditionName = "yaobuyao"; root.put("conditionName", conditionName); } String startPlace = ((String)root.get("place")).split("\\+")[0]; String startTaskLine = String.format(PlotConstructor2.TASK_LINE, startPlace); String startTaskCondition = String.format(PlotConstructor2.TASK_CONDITION, startPlace); String startTaskReply = String.format(PlotConstructor2.TASK_REPLY, startPlace); varInitialMap.put("site", startPlace); varInitialMap.put(startTaskLine, String.format(GAME_STEP_FORMAT, botAccount, rootId)); varInitialMap.put(startTaskCondition, ""); varInitialMap.put(startTaskReply, ""); conditionDescription.put(startTaskLine, startTaskLine); conditionDescription.put(startTaskCondition,startTaskCondition); conditionDescription.put(startTaskReply, startTaskReply); //generateStartGame(root, jsonObject, startPlace, conditionName); Queue queue = new LinkedList(); queue.add(root); System.out.println(root.toJSONString()); while (!queue.isEmpty()) { node = queue.remove(); id = (String) node.get("id"); location = ((String) node.get("place")).split("\\+")[0]; conditionName = (String) node.get("conditionName"); curAutoJump = node.getBoolean("autoJump"); prevAutoJump = node.getBoolean("prevAutoJump"); curUnknownText = (String) node.get("unknownText"); taskLineName = String.format(PlotConstructor2.TASK_LINE, location); conditionDescription.put(id, "剧情" + id + "状态"); varInitialMap.put(id ,""); //本任务线的最后一个condition和本任务线的最后一个回答 branch = node.getJSONArray("branch"); branchSize = branch.size(); for (int i = 0; i < branchSize; i++) { skipText.setLength(0); branchNode = branch.getJSONObject(i); //reply = branchNode.getString("reply"); Map updateKnowledge = JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class); nextId = branchNode.getString("nextPlotId"); moveWithPlot = branchNode.getBoolean("moveWithPlot"); if(nextId == null || nextId.equals("") || !plotIndex.containsKey(nextId)){ nextId = id + "_end"; createEndNode(node, id, nextId, curUnknownText); } if(moveWithPlot == null || curAutoJump) moveWithPlot = true; nextUnknownText = plotIndex.get(nextId).getString("unknownText"); nextAutoJump = plotIndex.get(nextId).getBoolean("autoJump"); reply = plotIndex.get(nextId).getString("plotText"); /** * 自动跳转到下一个剧情,因为有可能存在多步自动跳转,所以要用一个while来处理, * 如果下一步剧情是自动跳转,那么把下一步剧情的更新答案(属性)移到当前剧情,并将下一步剧情id改为下下步剧情id */ if(reply.equals("")) reply = curUnknownText; //如果存在下一剧情,就将下一步剧情的condition设为下一个激活的conditin,把地点更新为下一步剧情的地点,并更新任务线进度 nextCondition = plotIndex.get(nextId).getString("conditionName"); if(nextCondition.equals("")) nextCondition = "yaobuyao"; nextLocation = plotIndex.get(nextId).getString("place"); //任务线 nTaskLineName = String.format(TASK_LINE, nextLocation); //任务线最后一个condition lastTaskCondition = String.format(TASK_CONDITION, nextLocation); lastTaskReply = String.format(TASK_REPLY, nextLocation); // if(nextId.endsWith("_end")) // nextGameStep = id; // else // nextGameStep = nextId; gameStep = String.format(GAME_STEP_FORMAT, botAccount, id); nextGameStep = String.format(GAME_STEP_FORMAT, botAccount,nextId); /** * 额外条件是任务线进度达到当前剧情,额外更新变量是将任务线移动至下一剧情 * 如果某个地点的剧情已经到结尾啦,那么有两种解决方法: * 1: 将剧情停留在最后一步剧情 * 2. 设一个专门的剧情表示剧情的结尾 */ //现在暂时放开下一步剧情未发生过的限制条件 //extraHP = taskLineName + "::match-Include=" + id + ",site::match-Include=" + location + "," + id + "::number-BiggerOrEqual=" + 1 + "," + nextId + "::match-Include=null"; appendPremise(extraHP, taskLineName, "match", "Include", gameStep); appendPremise(extraHP, "site", "match", "Include", location); appendPremise(extraHP, id, "match", "Include", "1"); appendKnowledge(extraUK, lastTaskCondition, nextCondition); appendKnowledge(extraUK, lastTaskReply, reply); appendKnowledge(extraUK, id, "+1"); //当前剧情状态加一 appendKnowledge(extraUK, nextId, "1"); //下一步设为1 appendKnowledge(extraUK, nTaskLineName, nextGameStep); //如果下一步剧情不存在,那么下一步剧情id为当前id加后缀_end,但是用户提交qa的game_step应该还是当前id,没有加上_end if(nextId.endsWith("_end")) { nextGameStep = String.format(GAME_STEP_FORMAT, botAccount,id); } System.out.println(id); appendFeature(extraFT, "match"); //下一步为自动跳转且地点相同,则将回答放在plot_jump_text if(doActivatePlot(nextAutoJump, location, nextLocation, moveWithPlot) ){ appendKnowledge(extraUK, "plot_jump_text", "str+" + reply); } /** * 1. 主角是否随剧情移动 * 如果随剧情移动,则更新地点和game_step * 如果不随剧情移动,则不更新地点和game_step,添加中间配置"去XX",设高优先级,并更新状态 * * 2. 当前条件是否为空 * 如果当前条件为空,则提交qa激活并进入下一步剧情 * 如果当前条件不为空,则需要满足条件才能进入下一步剧情 * * 3. 下一部剧情是否为空 * * * 4. 有没有说去XX * * 主角不随剧情移动+当前条件为空+去沙滩 * 1、沙滩无剧情 * 2、沙滩有剧情 * * 5. 地点变化 * 1. 当前所处地点 * 2. 下一步剧情所处地点(包含自动跳转的情况) * 3. 自动跳转完成后的地点 */ /** * 自动跳转前的剧情也要记下来,这里是自动跳转前需要更新的变量(无论是否包含自动跳转都会更新) * 1. 将自动跳转剧情的关联地点最后被激活的condition更新为yaobuyao (其实这一步是非必须的,) * 2. 自动跳转剧情文本保存到beforeJumpTaskReply * 3. 当前剧情和下一步剧情更新状态 */ if(branchNode.containsKey("activateExtraPlot")) { //激活除了下一步剧情以外的剧情 extraActivatedPlot(extraUK, nextLocation, branchNode.getJSONObject("activateExtraPlot")); } //如果主角的位置随着剧情移动或者下一步剧情地点和当前剧情相同,主角地点和game_step要做相应更新 if(moveWithPlot || location.equals(nextLocation)) { appendKnowledge(extraUK, "site", nextLocation); appendKnowledge(extraUK, "game_step", nextGameStep); appendKnowledge(extraUK,"unknownText", nextUnknownText); //extraUK += "-<-site:" + nextLocation + "-<-game_step:" + nextGameStep; //如果当前剧情所在地喝下一步剧情所在地不一样,那么unknown文本 //只有到达下一步剧情所在地时才会更新 } else { //任务线最后一个condition /** * 注意,比较的是当前地点和跳转前的地点 * 地点更新为跳转后的地点 */ //可能因为不存在自动跳转或者地点不同,所以执行现在的代码,如果在剧情中存在自动跳转的情况,那么应该比较自动跳转完成后的地点喝下一步剧情的地点是否相同 if(!location.equals(nextLocation)){ //System.out.println(id + "报异常" + curUnknownText); if(curUnknownText.equals("")) { //如果没有unknown,就提示他到下一个地点 hint = "请到" + nextLocation; //如果当前地点和下一步剧情地点不同,要提示 }else { //如果有unknown,就提示unknown文本 hint = curUnknownText; } //下一部剧情在不同地点时,为了防止到达下个地点的剧情重复执行,需要加一个标志表示下一步剧情是否已经发生过 appendKnowledge(extraUK, nextLocation + "_activate_by", id); appendKnowledge(extraUK, nextLocation + "_activate_ready", "1"); arriveActivatePlot2(jsonObject, id, nextId, nextLocation); //如果下一步剧情不在同一地点,那么即使有跳转剧情,也是在下一个地点才说 reply = hint; //必须在上一个jConfig添加完之后 //extraUK = extraUK.replace("-<-" + nextId + ":" + "+1", ""); } } //System.out.println(branchNode); curPremiseList = JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), Premise.class); historyPremiseList = JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), Premise.class); //是否将当前识别到的数据加入到已知数据列表 if((Boolean)branchNode.get("includeCurInfo")){ appendFeature(extraFT, "includeCurInfo"); } if(branchNode.get("extraFeature") != null){ appendFeature(extraFT, branchNode.getString("extraFeature")); } if(doActivatePlot(nextAutoJump, location, nextLocation, moveWithPlot) ){ appendFeature(extraFT, "activate:after"); nextCondition = "plot_" + nextId; } configTerm = new ConfigTerm( curPremiseList, historyPremiseList, updateKnowledge, "", extraHP.toString(), extraUK.toString(), extraFT.toString(), reply, nextCondition, "1" ); configTerm.setConfigName("plot_" + id, false); //当剧情没有条件限制时的操作 ,额外要求上一步剧情不是自动跳转,因为自动跳转的标识放在了上一部剧情的分支里,所以是上上个分支是否存在自动跳转 if (curPremiseList.size() == 0 && historyPremiseList.size() == 0 && !prevAutoJump){ Boolean activateFlag = false; if(extraFT.indexOf("activate") != -1) activateFlag = true; unconditionalPlotProcess(configTerm, nextCondition, activateFlag); } if (branchNode.getBoolean("higherPriority") != null && branchNode.getBoolean("higherPriority")) configTerm.setAuxiliaryPriority("3"); configTermList.add(configTerm); //将子结点加入队列 adjId = nextId; plotEnQueue(queue, id, adjId, nextUnknownText, curAutoJump); if(branchNode.getJSONObject("activateExtraPlot") != null){ for(String activateId:branchNode.getJSONObject("activateExtraPlot").keySet()){ plotEnQueue(queue, id, activateId, nextUnknownText, curAutoJump); } } extraFT.setLength(0); extraHP.setLength(0); extraUK.setLength(0); skipText.setLength(0); arriveUK.setLength(0); } } /** * ---------------------------------------------------------------------------------------------------- * 不设剧情不设条件时的处理 * 1. 如果qa返回大于90则回答common_answer+ 剧情回答,并进入下一步剧情,这里的下一步condition是从激活之前保存的临时变量中读取出来的 * 2. 如果qa返回小于90则只回答剧情文本 * 3. 如果qa返回not_found则只回答剧情文本 * 4. 无论哪种情况都会跳下一步剧情 */ configTerm = new ConfigTerm( " ", "qa_status::match-Include=OK,qa_score::number-Smaller=90", "match", "{临时变量reply-2}", " ", "process-nextStep:var={临时变量condition-2}" ); configTerm.setStatus("sleeping"); configTerm.setConfigName("queryskip1",false); configTerm.setjStatus(1); configTerm.setAuxiliaryPriority("0"); configTermList.add(configTerm); configTerm = new ConfigTerm( " ", "qa_status::match-Include=OK,qa_score::number-BiggerOrEqual=90", "match", "{临时变量reply-2}", " ", "process-nextStep:var={临时变量condition-2}" ); configTerm.setStatus("sleeping"); configTerm.setConfigName("queryskip2",false); configTerm.setjStatus(1); configTerm.setAuxiliaryPriority("0"); configTermList.add(configTerm); configTerm = new ConfigTerm( " ", "qa_status::match-Include=not_found", "match", "{临时变量reply-2}", " ", "process-nextStep:var={临时变量condition-2}" ); configTerm.setStatus("sleeping"); configTerm.setConfigName("queryskip3",false); configTerm.setjStatus(1); configTerm.setAuxiliaryPriority("0"); configTermList.add(configTerm); /** * ---------------------------------------------------------------------------------------------------- * 待激活列表,用于qa问答 */ //处于激活状态,其他的属于待激活状态,jStatus这一条不设为1,而是0 configTerm = new ConfigTerm(" ", " ", "activate,post::gameQA(" + keywordMap.get("QA_URL").toString()+ ")", "", "unknown4,unknown3,unknown2,unknown1,common_answer", ""); configTerm.setNextStepStatus("active"); configTerm.setAuxiliaryPriority("0"); configTermList.add(configTerm); configTerm = new ConfigTerm( " ", "qa_status::match-Include=not_found", "match,return-0", "{unknownText-2}", " ", "" ); configTerm.setStatus("sleeping"); configTerm.setConfigName("unknown1",false); configTerm.setjStatus(1); configTerm.setAuxiliaryPriority("0"); configTermList.add(configTerm); configTerm = new ConfigTerm( " ", "qa_status::match-Include=not_found,unknownText::match-Include=null", "match,return-0", "不好意思,我没听懂你刚才说的话,请换个别的问题试试吧~哎呀,刚才开了小差,没理解你的意思,请换个问题吧~请根据上文信息,认真回答吧,谢谢~我们好像不在一个频道上,换个问题试试,希望我们能够接轨上吧", " ", "" ); configTerm.setStatus("sleeping"); configTerm.setConfigName("unknown2",false); configTerm.setjStatus(1); configTerm.setAuxiliaryPriority("0"); configTermList.add(configTerm); configTerm = new ConfigTerm( " ", "qa_status::match-Include=OK,qa_score::number-Smaller=90,unknownText::match-Include=null", "match,return-0", "不好意思,我没听懂你刚才说的话,请换个别的问题试试吧~哎呀,刚才开了小差,没理解你的意思,请换个问题吧~请根据上文信息,认真回答吧,谢谢~我们好像不在一个频道上,换个问题试试,希望我们能够接轨上吧", " ", "" ); configTerm.setStatus("sleeping"); configTerm.setConfigName("unknown3",false); configTerm.setjStatus(1); configTerm.setAuxiliaryPriority("0"); configTermList.add(configTerm); configTerm = new ConfigTerm( " ", "qa_status::match-Include=OK,qa_score::number-Smaller=90", "match,return-0", "{unknownText-2}", " ", "" ); configTerm.setStatus("sleeping"); configTerm.setConfigName("unknown4",false); configTerm.setjStatus(1); configTerm.setAuxiliaryPriority("0"); configTermList.add(configTerm); //由于剧情跳转时地点可能发生变化, qa问答的句子可能因为npc不再同一位置而被过滤掉,所以将common_answer的字符串合并放在postprocessing中 configTerm = new ConfigTerm( " ", "qa_status::match-Include=OK,qa_score::number-BiggerOrEqual=90", "match,return-0", "" ," ", "" ); configTerm.setStatus("sleeping"); configTerm.setConfigName("common_answer",false); configTerm.setjStatus(1); configTerm.setAuxiliaryPriority("0"); configTermList.add(configTerm); //--------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------- /** * ---------------------------------------------------------------------------------------------------- * 用于存档 */ configTerm = new ConfigTerm( "save_game::match-Include=+要:存档", " ", "match,return-0,gameBackup", "{archiveOperation-2}" ," ", "" ); configTerm.setAuxiliaryPriority("4"); configTermList.add(configTerm); } public void createEndNode(JSONObject node, String id, String nextId, String nextUnknownText){ String condition = (String) node.get("conditionName"); String location = ((String) node.get("place")).split("\\+")[0]; String nextCondition = condition; String nextLocation = location; String nTaskLineName = String.format(TASK_LINE, nextCondition); JSONObject endNode = new JSONObject(); endNode.put("place", location); endNode.put("conditionName", condition); endNode.put("id", nextId); endNode.put("plotText", nextUnknownText); endNode.put("autoJump", false); endNode.put("includeCurInfo", true); endNode.put("updateKnowledge", new ArrayList()); endNode.put("unknownText", ""); plotIndex.put(nextId, endNode); visited.put(nextId, true); } void addGameStartPlot(JSONObject gameElements, Map keywordMap){ File file = null; FileReader fr = null; BufferedReader br = null; try { file = new File("gameconfig.txt"); fr = new FileReader(file); br = new BufferedReader(fr); String jsonStr = br.readLine(); jsonStr = jsonStr.replace("startAutoJump", gameElements.getString("startAutoJump")); jsonStr = jsonStr.replace("firstUnknownText", gameElements.getString("firstUnknownText")); jsonStr = jsonStr.replace("plotStartText", gameElements.getString("plotStartText")); jsonStr = jsonStr.replace("startId", gameElements.getString("start")); jsonStr = jsonStr.replace("site_list", keywordMap.get("site_list").toString()); //System.out.println("JSONSTR ------------------------------\n" + jsonStr); JSONObject config = JSONObject.parseObject(jsonStr); gameElements.getJSONArray("plots").addAll(config.getJSONArray("gameStart")); gameElements.put("start", "start1"); gameElements.put("firstUnknownText", "请说开始游戏或者继续游戏"); br.close(); fr.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //如果是无条件剧情,那么需要跟着一个激活操作 public void unconditionalPlotProcess(ConfigTerm configTerm, String nextCondition, Boolean activateFlag){ configTerm.setFeature(configTerm.getFeature() + ",activate,post::gameQA(" + keywordMap.get("QA_URL").toString() + ")"); // //if() if(activateFlag){ configTerm.setNextStep(nextCondition+ ",queryskip*,map*,item*,npc*"); }else { configTerm.setNextStep("queryskip*,map*,item*,npc*"); } //将本配置条目中nextStep的值放在“临时变量condition”中,将本配置条目中的回答放在“临时变量reply”中 configTerm.setUpdateKnowledge(configTerm.getUpdateKnowledge() + "-<-临时变量condition:"+ nextCondition +"-<-临时变量reply:var={reply-3}"); configTerm.setNextStepStatus("active"); } public void appendPremise(StringBuffer premiseStr, String conditionName, String cmpType, String operator, String value){ int strLen = premiseStr.length(); String split_punt = ""; if(premiseStr.length() != 0) split_punt = ","; premiseStr.append(split_punt); premiseStr.append(conditionName); premiseStr.append("::"); premiseStr.append(cmpType); premiseStr.append("-"); premiseStr.append(operator); premiseStr.append("="); premiseStr.append(value); } public void appendKnowledge(StringBuffer knowledgeStr, String key, String value){ knowledgeStr.append("-<-"); knowledgeStr.append(key); knowledgeStr.append(":"); knowledgeStr.append(value); } public void appendFeature(StringBuffer featureStr, String key){ featureStr.append(","); featureStr.append(key); } public void extraActivatedPlot(StringBuffer updateKnowledge, String nextPlotLocation, Map activateExtraPlot){ JSONObject plot = null; String plotId = "", location = "", lastTaskCondition = "", lastTaskReply = "", plotText = "", conditionName = "", taskLine = ""; StringBuffer newUK = updateKnowledge; Iterator plotIter = activateExtraPlot.keySet().iterator(); while(plotIter.hasNext()){ plotId = plotIter.next(); plotText = (String)activateExtraPlot.get(plotId); plot = plotIndex.get(plotId); conditionName = plot.getString("conditionName"); if(conditionName == null || conditionName.equals("")){ conditionName = "yaobuyao"; } location = plot.getString("place"); lastTaskCondition = String.format(TASK_CONDITION, location); lastTaskReply = String.format(TASK_REPLY, location); taskLine = String.format(TASK_LINE, location); appendKnowledge(newUK, lastTaskCondition, conditionName); appendKnowledge(newUK, lastTaskReply, plotText); appendKnowledge(newUK, taskLine, String.format(GAME_STEP_FORMAT, botAccount, plotId)); appendKnowledge(newUK, plotId, "1"); //newUK.append("-<-").append(lastTaskCondition).append(":").append(conditionName).append("-<-").append(lastTaskReply).append(":").append(plotText); } } void plotEnQueue(Queue queue, String parentId, String adjId, String nextUnknownText, Boolean thisAutoJump){ JSONObject adjNode; if (visited.get(adjId) == null && plotIndex.get(adjId) != null) { visited.put(adjId, true); adjNode = plotIndex.get(adjId); adjNode.put("parentId", parentId); adjNode.put("prevAutoJump", thisAutoJump); queue.add(adjNode); } else { conditionDescription.put(adjId, "剧情" + adjId + "进度"); varInitialMap.put(adjId, ""); } } /** * 当前剧情和下一步剧情不在同一个地点,需要到达地点后才能激活剧情的情况,将这块处理当做listening模块的一个剧情处理 */ void arriveActivatePlot2(JSONObject gameElements, String plotId, String nextId, String nextLocation){ //String feature; //如果下一步剧情和当前剧情的地点是不一样的,那么在玩家没有到达下一个地点是剧情还是停留在当前剧情,只有到达目的地后,下一步剧情才会发生变化 //StringBuffer hisPremise = new StringBuffer(); //StringBuffer curPremise = new StringBuffer(); // appendPremise(curPremise, "yaobuyao", "match", "Include", nextLocation); // appendPremise(hisPremise, nextLocation + "_activate_by", "match", "Include", plotId); // appendPremise(hisPremise, nextLocation + "_activate_ready", "match", "Include", "1"); // appendPremise(hisPremise, "isNeighbour:yaobuyao", "match", "Include", "1+3+4"); JSONObject updateKnowledge = new JSONObject(); JSONArray curPremiseList = new JSONArray(); //curPremiseList.add(curPremise.toString()); JSONArray hisPremiseList = new JSONArray(); //hisPremiseList.add(hisPremise.toString()); curPremiseList.add(new Premise("yaobuyao", "match", "Include", nextLocation)); hisPremiseList.add(new Premise(nextLocation + "_activate_by", "match", "Include", plotId)); hisPremiseList.add(new Premise(nextLocation + "_activate_ready", "match", "Include", "1")); hisPremiseList.add(new Premise("isNeighbour:yaobuyao", "match", "Include", "1+3+4")); JSONObject listeningConfig = new JSONObject(); listeningConfig.put("curPremise", curPremiseList); listeningConfig.put("hisPremise", hisPremiseList); listeningConfig.put("updateKnowledge", updateKnowledge); listeningConfig.put("nextPlotId", nextId); listeningConfig.put("priority", "1"); if(gameElements.getJSONArray("listeningActions") == null) gameElements.put("listeningActions", new JSONArray()); gameElements.getJSONArray("listeningActions").add(listeningConfig); } Boolean doActivatePlot(Boolean nextAutoJump, String location, String nextLocation, Boolean moveWithPlot) { return nextAutoJump && (location.equals(nextLocation) || moveWithPlot); } }