package com.qiwu.chatbotbackend.task.utils; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.qiwu.chatbotbackend.entity.Solution; import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.processing.ChatCacheController; import com.qiwu.chatbotbackend.processing.LogWriter; import com.qiwu.chatbotbackend.processing.WorldController; import javax.servlet.ServletContext; import java.io.IOException; import java.util.Map; public class GameBotUtil extends BasicBotUtil{ WorldController worldController = null; GameEnv gameEnv = null; public GameBotUtil(Map curInfoPoint, Map historyInfoPoint, UserInfo userinfo, LogWriter logWriter, WorldController worldController, GameEnv gameEnv){ this.curInfoPoint = curInfoPoint; this.historyInfoPoint = historyInfoPoint; this.userinfo = userinfo; this.logWriter = logWriter; this.worldController = worldController; this.gameEnv = gameEnv; } @Override public boolean preprocessing(Map receiveParameters, ServletContext context) throws IOException { Boolean debugFlag = infoPointPreprocessing(receiveParameters, context); //将要去的地点的别名替换为真名 String conditionList[] = {"yaobuyao", "want_know_zenmezuo"}; for(String conditionName: conditionList) { if (!curInfoPoint.get(conditionName).equals("") && !curInfoPoint.get(conditionName).equals("unknown")) { String info = curInfoPoint.get(conditionName); String destination = info.split(":")[1]; String site = historyInfoPoint.get("site"); String newInfo = info.replace(destination, worldController.findLocationName(gameEnv, destination, site)); logWriter.record(conditionName + ": 将’" + info + "‘替换为‘" + newInfo + "'", 1); curInfoPoint.put(conditionName, newInfo); } } String activeList[] = historyInfoPoint.get("activeList").split("\\|"); if(activeList.length == 0){ historyInfoPoint.put("临时变量condition", "yaobuyao"); }else { historyInfoPoint.put("临时变量condition", activeList[activeList.length-1]); } return debugFlag; } public boolean postprocessing(String chatKey, ServletContext context, Solution bestMatch){ if(userinfo.varInitialMap.get("botType") != null && userinfo.varInitialMap.get("botType").equals("game") && bestMatch.getAnswer().endsWith("开始游戏或者继续游戏")){ try { ChatCacheController.deleteUnuseChatCache(chatKey, context); } catch (Exception e) { e.printStackTrace(); } } String plot_jump_text = historyInfoPoint.get("plot_jump_text"); if(plot_jump_text != null && !plot_jump_text.equals("")) { bestMatch.setAnswer(bestMatch.getAnswer() + plot_jump_text + " " ); historyInfoPoint.put("plot_jump_text", ""); } String common_answer = historyInfoPoint.get("common_answer"); if(common_answer != null && !common_answer.equals("") && !common_answer.equals("no answer")){ bestMatch.setAnswer(common_answer + " " + bestMatch.getAnswer()); historyInfoPoint.put("common_answer", ""); } return true; } public String getKeyword(Map infoPoint, Map infoPoint2, Boolean chooseLater, String actionName){ String keyword = ""; String nameSeries[] = actionName.split("_"); //变量名以npc_开头,就从游戏环境中查询相应的值 if(actionName.startsWith("npc_")){ if(nameSeries.length == 4){ keyword = gameEnv.queryNpcAttrByNpcName(nameSeries[2], nameSeries[3]); } }else if (actionName.startsWith("goods_")) { if (nameSeries.length == 4) { keyword = gameEnv.queryGoodsAttrByGoodsName(nameSeries[2], nameSeries[3]); System.out.println("return" + keyword); } }else if(actionName.startsWith("isNeighbour")){ String opPair[] = actionName.split(":"); if(!curInfoPoint.containsKey(opPair[1]) || curInfoPoint.get(opPair[1]).equals("unknown") || curInfoPoint.get(opPair[1]).equals("")){ keyword = "not_exist"; }else { logWriter.record("isNeighbour(" + historyInfoPoint.get("site") + "," + curInfoPoint.get(opPair[1]).split(":")[1] + ")", 1); keyword = String.valueOf(gameEnv.isNeighbour(historyInfoPoint.get("site"), curInfoPoint.get(opPair[1]).split(":")[1])); logWriter.record("返回:" + keyword, 1); } }else { /** * 如果chooseLater为真,则先提取infoPint2中的关键字,如果infoPoint2中不存在,再提取infoPoint中的关键字 */ if(chooseLater){ keyword = infoPoint2.get(actionName); if(keyword != null && !keyword.equals("")) return keyword; } keyword = infoPoint.get(actionName); } return keyword; }; @Override public void putKeyword(String variableName, String updateValue){ //如果变量名开头为NPC或者item,就专门交给world处理 if((variableName.startsWith("npc_") || variableName.startsWith("goods_") || variableName.startsWith("map_") || variableName.startsWith("place_"))){ try { if(!updateValue.startsWith("query") && !updateValue.startsWith("update")){ updateValue = "update:" + updateValue; } String returnStr = worldController.worldOperation(gameEnv, variableName, updateValue, curInfoPoint, historyInfoPoint); updateValue = returnStr; }catch (Exception e){ e.printStackTrace(); updateValue = "游戏模块处理出错"; } } historyInfoPoint.put(variableName, updateValue); } public String knowledgeProcess(String variableName, String updateValue){ String values[] = updateValue.split(":",2), returnValue; if(values.length > 1 && values[0].equals("update")) returnValue = values[1]; else returnValue = updateValue; return returnValue; } }