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 java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; public class MapConstructor2 extends ElementConstructor { int[][] matrix; int locationNum; HashMap id2j = new HashMap(); Map question2keyword = new HashMap(); JSONArray locations = null; Map script = null; //话术模板 public MapConstructor2(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 constructGreph(JSONArray locations) { JSONObject jobj = null; int locationId, index; //ArrayList> matrix= new ArrayList>(); //ArrayList vec1d = new ArrayList(); locationNum = locations.size(); StringBuffer keywordSet = null; for (int i = 0; i < locationNum; i++) { jobj = locations.getJSONObject(i); jobj.put("index", i); keywordSet = new StringBuffer(); keywordSet.append(jobj.get("name")); POS.append("=" + jobj.get("name") + "|地名\r\n"); if(jobj.containsKey("nickname")){ for(String nickname:JSONArray.parseArray(locations.getJSONObject(i).getJSONArray("nickname").toJSONString(), String.class)){ if(!nickname.equals("")) { keywordSet.append("+" + nickname); POS.append("=" + nickname + "|地名\r\n"); } } } jobj.put("name", keywordSet.toString()); locationId = (Integer) jobj.get("id"); id2j.put(locationId, jobj); } //邻接矩阵初始化 matrix = new int[locationNum][locationNum]; for (int i = 0; i < locationNum; i++) { for (int j = 0; j < locationNum; j++) { matrix[i][j] = Integer.MAX_VALUE; } } for (int i = 0; i < locationNum; i++) { List neighbours = JSONArray.parseArray(locations.getJSONObject(i).getJSONArray("neighbours").toJSONString(), Integer.class); for (int neighbourId : neighbours) { JSONObject neighbourObject = id2j.get(neighbourId); System.out.println(neighbourObject); index = (Integer) neighbourObject.get("index"); matrix[i][index] = 1; matrix[index][i] = 1; } } } public void construct(JSONObject gameElements, UserInfo userinfo) { try { conditionDescription.put("site", "当前地点"); JSONObject map = gameElements.getJSONObject("map"); Map question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class); script = JSONObject.toJavaObject(gameElements.getJSONObject("speechcraft"), Map.class); if(!script.containsKey("PATH_AT")) script.put("PATH_AT", "你已经在%s"); if(!script.containsKey("WHERE_I_AM")) script.put("WHERE_I_AM", "在%s"); if(!script.containsKey("PATH_ADJOINT")) script.put("PATH_ADJOINT", "就在附近哦"); if(!script.containsKey("PATH_EXIST")) script.put("PATH_EXIST", "通过%s可以到达目的地"); if(!script.containsKey("PATH_NOT_EXIST")) script.put("PATH_NOT_EXIST", "不可到达"); if(!script.containsKey("CAN_GO_SITES")) script.put("CAN_GO_SITES", "你可以去%s"); if(!script.containsKey("CAN_GO_NOWHERE")) script.put("CAN_GO_NOWHERE", "没什么地方可去的,继续玩吧"); if(!script.containsKey("SITE_NOT_ACTIVE")) script.put("SITE_NOT_ACTIVE", "你来到了%s\""); if(!script.containsKey("SITE_NOT_ADJOINT")) script.put("SITE_NOT_ADJOINT", "通过%s可以到达目的地"); if(!script.containsKey("AROUND_PEOPLE")) script.put("AROUND_PEOPLE", "周围有%s"); if(!script.containsKey("AROUND_ITEMS")) script.put("AROUND_ITEMS", "周围有%s"); if(!script.containsKey("AROUND_THINGS")) script.put("AROUND_THINGS", "周围有%s"); if(script == null){ throw new Exception("话术不存在"); } JSONArray locations = map.getJSONArray("locations"); String userName = (String) gameElements.get("userName"); this.locations = locations; this.question2keyword = question2keyword; //通过邻接表构造邻接矩阵 constructGreph(locations); //用弗洛伊德算法求最短路径 Floyd floyd = new Floyd(); floyd.findShortestPath(matrix, locationNum); floyd.printPath(matrix, locationNum); generateWantToThere(floyd.pathMatirx, floyd.preTable, gameElements); generateWhereCanIgo(floyd.pathMatirx, floyd.preTable, gameElements); generateWhereIam(gameElements, userName); generateHowToThere(floyd.pathMatirx, floyd.preTable, gameElements); generateSurroundingItem(); generateSurroundingPeople(); generateSurroundThings(); } catch(Exception e){ operationStatus = 0; e.printStackTrace(); message = "地图处理出现异常:"; message += e.getMessage(); return; } } /** * 生成‘我在哪里’所需要的配置文件 */ public void generateWhereIam(JSONObject gameElements, String botAccount) { String conditionName = "rencheng"; String nickname = "qiwu"; String conditionKeywords = nickname + ":哪里+" + nickname + ":哪+现在:哪里+现在:哪"; jConfig = new JConfig( conditionName + "::match-Include=" + conditionKeywords, " ", "match", "【{智能助手-2}】" + String.format(script.get("WHERE_I_AM"), "{site-2}") , "yaobuyao", "" ); jConfig.setNextStepStatus("return-0"); jConfig.setAuxiliaryPriority("1"); JConfig.activateCopy(jConfig, jConfigList); } /** * 生成‘怎么去哪里’所需要的配置文件 */ public void generateHowToThere(int[][] pathMatirx, int[][] preTable, JSONObject gameElements) { String conditionName = "want_know_zenmezuo"; String conditionKeywords = "去:地点"; StringBuffer placeKeywords = new StringBuffer(); String placeA, placeB, descriptionB, text, pathNode; for (int i = 0; i < locationNum; i++) { placeA = (String) locations.getJSONObject(i).get("name"); placeKeywords.append("+" + placeA); } for (int j = 0; j < locationNum; j++) { placeB = (String) locations.getJSONObject(j).get("name"); jConfig = new JConfig( conditionName + "::match-Include=" + placeKeywords + ",isNeighbour:"+ conditionName + "::match-Include=0", "site::match-Include=" + placeB , "match,return-0", "【{智能助手-2}】" + String.format(script.get("PATH_AT"), "{site-2}"), " ", "" ); jConfig.setNextStepStatus("return-0"); jConfig.setAuxiliaryPriority("1"); JConfig.activateCopy(jConfig, jConfigList); jConfig = new JConfig( conditionName + "::match-Include=" + placeKeywords + ",isNeighbour:"+ conditionName + "::match-Include=1", "site::match-Include=" + placeB , "match,return-0", "【{智能助手-2}】" + String.format(script.get("PATH_ADJOINT"), "{site-2}"), " ", "" ); jConfig.setNextStepStatus("return-0"); jConfig.setAuxiliaryPriority("1"); JConfig.activateCopy(jConfig, jConfigList); jConfig = new JConfig( conditionName + "::match-Include=" + placeKeywords + ",isNeighbour:"+ conditionName + "::match-Include=2", "site::match-Include=" + placeB , "match,return-0", "【{智能助手-2}】{map_reply-2}", " ", "map_reply:queryHowToPathBySite:" + conditionName ); jConfig.setNextStepStatus("return-0"); jConfig.setAuxiliaryPriority("1"); JConfig.activateCopy(jConfig, jConfigList); } } /** * 生成‘我能去哪里’所需要的配置文件 */ public void generateWhereCanIgo(int[][] pathMatirx, int[][] preTable, JSONObject gameElements) { String conditionKeywords = "去:哪里+能去:哪+能去:哪里+能去:哪儿"; String conditionName = "choose_gowhere"; String placeA, placeB, pathNode; for (int i = 0; i < locationNum; i++) { placeA = (String) locations.getJSONObject(i).get("name"); pathNode = ""; for (int j = 0; j < locationNum; j++) { if (pathMatirx[i][j] == 1) { placeB = (String) locations.getJSONObject(j).get("name"); pathNode += "、" + placeB; } } jConfig = new JConfig( conditionName + "::match-Include=" + conditionKeywords, "site::match-Include=" + placeA, "match,return-0", "【{智能助手-2}】{map_reply-2}", " ", "map_reply:queryCanGoBySite:" ); jConfig.setNextStepStatus("return-0"); jConfig.setAuxiliaryPriority("1"); JConfig.activateCopy(jConfig, jConfigList); } } /** * 生成‘我要去那里’所需要的配置文件 */ public void generateWantToThere(int[][] pathMatirx, int[][] preTable, JSONObject gameElements) { String placeA, placeB, descriptionB, text, pathNode, lastTaskCondition, lastTaskReply; String conditionName = "yaobuyao"; String conditionKeywords = "地点"; String start = (String)gameElements.get("start"); String placeKeywords; for (int i = 0; i < locationNum; i++) { placeA = (String) locations.getJSONObject(i).get("name"); /** * 添加初始化变量 */ String placeAMain = placeA.split("\\+")[0]; if(!varInitialMap.containsKey("任务线" + placeAMain + "最后一个condition")) { varInitialMap.put("任务线" + placeAMain + "最后一个condition", "yaobuyao"); varInitialMap.put("任务线" + placeAMain + "最后的回答", "你来到了" + placeAMain + ",现在什么都没有, 请继续之前的剧情,接着往下玩吧!"); conditionDescription.put("任务线" + placeAMain + "最后一个condition", "任务线" + placeAMain + "最后一个condition"); conditionDescription.put("任务线" + placeAMain + "最后的回答", "任务线" + placeAMain + "最后的回答"); } for (int j = 0; j < locationNum; j++) { placeB = (String) locations.getJSONObject(j).get("name"); String placeBMain = placeB.split("\\+")[0]; descriptionB = (String) locations.getJSONObject(j).get("description"); placeKeywords = conditionKeywords.replace("地点", placeB); lastTaskCondition = "任务线" + placeBMain + "最后一个condition"; lastTaskReply = "任务线" + placeBMain + "最后的回答"; jConfig = new JConfig( conditionName + "::match-Include=" + placeKeywords + ",isNeighbour:"+ conditionName + "::match-Include=0", "site::match-Include=" + placeA , "match,return-0", "【{智能助手-2}】" + String.format(script.get("PATH_AT"), placeBMain), " ", "" ); jConfig.setConfigName("map",true); jConfigList.add(jConfig); jConfig = new JConfig( conditionName + "::match-Include=" + placeKeywords + "," + ",isNeighbour:"+ conditionName + "::match-Include=1", "site::match-Include=" + placeA, "match, reutrn-0,activate", "{" + lastTaskReply + "-2}", "plot_"+placeBMain + "_*","" ); jConfig.setConfigName("map",true); jConfigList.add(jConfig); //上一个配置文件激活时的默认选项 jConfig = new JConfig( " ", " ", "", "{" + lastTaskReply + "-2}" ," ", "process-nextStep:var={" + lastTaskCondition +"-2}-<-site:" + placeBMain ); jConfig.setStatus("sleeping"); jConfig.setConfigName("plot_"+placeBMain + "_doudi",false); jConfig.setjStatus(1); jConfigList.add(jConfig); jConfig = new JConfig( conditionName + "::match-Include=" + placeKeywords + ",isNeighbour:"+ conditionName + "::match-Include=2", "site::match-Include=" + placeA , "match,return-0", "【{智能助手-2}】{map_reply-2}", " ", "map_reply:queryToDoPathBySite:" + conditionName ); jConfig.setConfigName("map",true); jConfigList.add(jConfig); } } } public void generateSurroundingPeople(){ String conditionName = "know_exist_what"; String conditionKeywords = "周围:人+附近:人"; jConfig = new JConfig( conditionName + "::match-Include=" + conditionKeywords, " ", "match,return-0", "【{智能助手-2}】{map_query-2}。", "yaobuyao", "map_query:queryAroundNpcBySite:" ); JConfig.activateCopy(jConfig, jConfigList); } public void generateSurroundingItem(){ String conditionName = "know_exist_what"; String conditionKeywords = "周围:物品+周围:东西+附近:物品+附近:东西"; jConfig = new JConfig( conditionName + "::match-Include=" + conditionKeywords, " ", "match,return-0", "【{智能助手-2}】{map_query-2}。", "yaobuyao", "map_query:queryAroundGoodsBySite:" ); JConfig.activateCopy(jConfig, jConfigList); } public void generateSurroundThings(){ String conditionName = "know_exist_what_mohu"; String conditionKeywords = "有什么:周围+有什么:附近"; jConfig = new JConfig( conditionName + "::match-Include=" + conditionKeywords, " ", "match,return-0", "【{智能助手-2}】{map_query-2}。", "yaobuyao", "map_query:queryAroundAllBySite:" ); JConfig.activateCopy(jConfig, jConfigList); } }