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.JConfig; import com.qiwu.chatbotbackend.entity.UserInfo; import org.apache.http.message.BasicNameValuePair; import com.qiwu.chatbotbackend.processing.MyFunc; import java.io.UnsupportedEncodingException; import java.util.*; public class PlotConstructor extends ElementConstructor { String GAME_STEP_FORMAT = "%s_plot_%s"; JSONArray plots = null; HashMap> adjList = new HashMap>(); HashMap> revAdjList = new HashMap>(); private HashMap id2j = new HashMap(); HashMap visited = new HashMap(); public HashMap getId2j() { return id2j; } public PlotConstructor(SqlHelper helper, int botId, int skipCount, String botAccount, HashSet exceptRelation, StringBuffer POS) { super(); this.helper = helper; this.botId = botId; this.botAccount = botAccount; this.exceptRelation = exceptRelation; this.skipCount = skipCount; this.POS = POS; } public void construct(JSONObject jsonObject, UserInfo userinfo) { try { JSONArray plots = jsonObject.getJSONArray("plots"); String start = (String) jsonObject.get("start"), startCondition; JSONObject returnMessage = null; this.plots = plots; constructAdjList(plots); if (id2j.containsKey(start)) { String taskLineName = "任务线_" + id2j.get(start).get("place") + "进度"; conditionDescription.put(taskLineName, taskLineName); varInitialMap.put("unknownText", "你说啥?"); varInitialMap.put("game_step", String.format(GAME_STEP_FORMAT, botAccount, (String) id2j.get(start).get("id"))); conditionDescription.put("unknownText", "对于无法识别的问题做出的回答"); conditionDescription.put("game_step", "QA问答游戏进度"); BFSGenerate4(id2j.get(start), userinfo, jsonObject); varInitialMap.put(start, "1"); } else { System.out.println("start剧情不存在"); operationStatus = 0; message = "开始剧情不存在,请到剧情列表设置"; return; } } catch(Exception e){ operationStatus = 0; message = "地图接口处理出现异常"; return; } } //根据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 BFSGenerate4(JSONObject root, UserInfo userinfo, JSONObject jsonObject) { String id, rootId = (String) root.get("id"), adjId, nextId, parentId, taskName = "", value = "", extraHP = "", extraUK = "", nextTaskName, keywordOptions, conditionName = "yaobuyao", nextUnknownText, curUnknownText, nextCondition, location, nextLocation, taskLineName, nTaskLineName, lastTaskReply, lastTaskCondition, reply; boolean newBranch, manyParent; JSONObject node = null, branchNode = null, adjNode = null; JSONArray branch = null; String taskLine = "任务线_%s进度"; List adjNodeList = null; int priority, taskStep, nextPriority, nextTaskStep, branchSize; root.put("nextUnknownText", varInitialMap.get("unknownText")); root.put("parentId", "null"); jsonObject.put("plotStartText", ((String)jsonObject.get("plotStartText")).replace("\n", "").replace(",", ",")); if(root.get("conditionName") != null && !root.get("conditionName").equals("")) conditionName = (String)root.get("conditionName"); String startPlace = ((String)root.get("place")).split("\\+")[0]; varInitialMap.put("site", startPlace); varInitialMap.put("任务线" + startPlace + "最后一个condition", ""); varInitialMap.put("任务线" + startPlace + "最后的回答", ""); conditionDescription.put("任务线" + startPlace + "最后一个condition","任务线" + startPlace + "最后一个condition"); conditionDescription.put("任务线" + startPlace + "最后的回答", "任务线" + startPlace + "最后的回答"); //用于开始游戏的配置 jConfig = new JConfig( "start_options::match-Include=+开始+开始游戏+继续游戏+继续+重新开始", "任务线" + startPlace + "最后一个condition::match-Include=+null", "match", (String)jsonObject.get("plotStartText"), conditionName, "任务线" + startPlace + "最后一个condition:" + conditionName + "-<-" + root.get("id") + ":+1" + "-<-任务线" + startPlace + "最后的回答:" + (String)jsonObject.get("plotStartText") + "-<-" + String.format(taskLine, startPlace) + ":" + root.get("id") ); jConfig.setAuxiliaryPriority("1"); jConfigList.add(jConfig); jConfig = new JConfig( " ", "任务线" + startPlace + "最后一个condition::match-Include=+null", "match", "请说开始游戏或者继续游戏", "start_options", "" ); jConfig.setAuxiliaryPriority("1"); jConfigList.add(jConfig); 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")).split("\\+")[0]; conditionName = (String) node.get("conditionName"); curUnknownText = (String) node.get("nextUnknownText"); taskLineName = "任务线_" + location + "进度"; conditionDescription.put(id, "剧情" + id + "状态"); varInitialMap.put(id ,""); nextUnknownText = ""; //本任务线的最后一个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"); reply = (String)branchNode.get("reply"); Map updateKnowledge = JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class); if(updateKnowledge.containsKey("unkonwnText")) nextUnknownText = updateKnowledge.get("unknownText"); else nextUnknownText = "unknown"; //如果存在下一剧情,就将下一步剧情的condition设为下一个激活的conditin,把地点更新为下一步剧情的地点,并更新任务线进度 if (id2j.get(nextId) != null) { nextCondition = (String) id2j.get(nextId).get("conditionName"); if(nextCondition.equals("")) nextCondition = "yaobuyao"; nextLocation = ((String) id2j.get(nextId).get("place")).split("\\+")[0]; nTaskLineName = "任务线_" + nextLocation + "进度"; } else { //如果不存在下一剧情,就将condition设为空,任务线进度不变 nTaskLineName = taskLineName; nextLocation = location; nextCondition = "yaobuyao"; continue; } 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 + "-<-" + lastTaskCondition + ":" + nextCondition + "-<-" + lastTaskReply + ":" + (String)branchNode.get("reply") + "-<-" + id + ":" + "+1-<-" + nextId + ":" + "+1" + "-<-game_step:" + String.format(GAME_STEP_FORMAT, botAccount, nextId); if(!branchNode.containsKey("moveWithPlot") || (Boolean)branchNode.get("moveWithPlot")) { extraUK += "-<-site:" + nextLocation; }else { if(!location.equals(nextLocation)){ //如果当前地点和下一步剧情地点不同,要提示 reply = "请到" + nextLocation + "开始下一步剧情"; } } List curPremiseList = JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class); jConfig = new JConfig( curPremiseList, JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), updateKnowledge, "", extraHP, extraUK, "match", reply, (String) nextCondition, "0" ); if (curPremiseList.size() == 0){ jConfig.setFeature(jConfig.getFeature() + ",activate,post::gameQA(http://simqa.sz.chewrobot.com:28081/game)"); jConfig.setNextStep("queryskip1,queryskip2,queryskip3"); jConfig.setUpdateKnowledge(jConfig.getUpdateKnowledge() + "-<-临时变量1:"+ nextCondition +"-<-临时变量2:var={reply-3}"); jConfig.setNextStepStatus("active"); } jConfigList.add(jConfig); 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的特殊处理 //对于unknown的特殊处理 if (curUnknownText.equals("")) { jConfig = new JConfig( conditionName + "::match-Include=unknown", "任务线_" + location + "进度::match-Include=" + parentId, "match,return-0,post::gameQA(" + CHITCHAT_URL + ")", " ", " ", "" ); jConfigList.add(jConfig); } } /** * ---------------------------------------------------------------------------------------------------- * 不设剧情不设条件时的处理 * 1. 如果qa返回大于90则回答common_answer+ 剧情回答,并进入下一步剧情,这里的下一步condition是从激活之前保存的临时变量中读取出来的 * 2. 如果qa返回小于90则只回答剧情文本 * 3. 如果qa返回not_found则只回答剧情文本 * 4. 无论哪种情况都会跳下一步剧情 */ jConfig = new JConfig( " ", "qa_status::match-Include=ok,qa_score::number-Smaller=90", "match", "{临时变量2-2}", " ", "process-nextStep:var={临时变量1-2}" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("queryskip1"); jConfig.setjStatus(1); jConfigList.add(jConfig); jConfig = new JConfig( " ", "qa_status::match-Include=ok,qa_score::number-BiggerOrEqual=90", "match", "{common_answer-2}, {临时变量2-2}", " ", "process-nextStep:var={临时变量1-2}" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("queryskip2"); jConfig.setjStatus(1); jConfigList.add(jConfig); jConfig = new JConfig( " ", "qa_status::match-Include=not_found", "match", "{临时变量2-2}", " ", "process-nextStep:var={临时变量1-2}" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("queryskip3"); jConfig.setjStatus(1); jConfigList.add(jConfig); /** * ---------------------------------------------------------------------------------------------------- * 待激活列表,用于qa问答 */ jConfig = new JConfig( " ", "qa_status::match-Include=not_found", "match,return-0", "{unknownText-2}", " ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown1"); jConfig.setjStatus(1); jConfigList.add(jConfig); jConfig = new JConfig( " ", "qa_status::match-Include=not_found,unknownText::match-Include=null", "match,return-0", "不好意思,我没听懂你刚才说的话,请换个别的问题试试吧~哎呀,刚才开了小差,没理解你的意思,请换个问题吧~请根据上文信息,认真回答吧,谢谢~我们好像不在一个频道上,换个问题试试,希望我们能够接轨上吧", " ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown2"); jConfig.setjStatus(1); jConfigList.add(jConfig); jConfig = new JConfig( " ", "qa_status::match-Include=ok,qa_score::number-Smaller=90,unknownText::match-Include=null", "match,return-0", "不好意思,我没听懂你刚才说的话,请换个别的问题试试吧~哎呀,刚才开了小差,没理解你的意思,请换个问题吧~请根据上文信息,认真回答吧,谢谢~我们好像不在一个频道上,换个问题试试,希望我们能够接轨上吧", " ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown3"); jConfig.setjStatus(1); jConfigList.add(jConfig); jConfig = new JConfig( " ", "qa_status::match-Include=ok,qa_score::number-Smaller=90", "match,return-0", "{unknownText-2}", " ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown4"); jConfig.setjStatus(1); jConfigList.add(jConfig); jConfig = new JConfig( " ", "qa_status::match-Include=ok,qa_score::number-BiggerOrEqual=90", "match,return-0", "{common_answer-2}" ," ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("common_answer"); jConfig.setjStatus(1); jConfigList.add(jConfig); //--------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------- /** * ---------------------------------------------------------------------------------------------------- * 用于处理开始游戏或者继续游戏 * * */ } @Override public void postProcessing(List idList, int categoryId){ if (idList.size() != 0) { jConfig = new JConfig(" ", " ", "activate,post::gameQA(http://simqa.sz.chewrobot.com:28081/game)", "", "unknown4,unknown3,unknown2,unknown1,common_answer", ""); jConfig.setNextStepStatus("active"); JSONObject json = jConfig.toJson(this.getConditionDescription()); helper.insertConfigByBotId(botId, json, idList, categoryId); jConfigList.add(jConfig); } } }