package com.qiwu.chatbotbackend.processing; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.qiwu.chatbotbackend.entity.UserInfo; import java.util.ArrayList; import java.util.List; import java.util.Map; public class WorldController { public LogWriter logWriter = null; public UserInfo userInfo = null; public WorldController(LogWriter logWriter, UserInfo userInfo) { this.logWriter = logWriter; this.userInfo = userInfo; } public String worldOperation(GameEnv gameEnv, String varName, String varValue, Map currentInfoPoint, MaphistoryInfoPoint){ String moduleName = "", valuePair[] = varValue.split(":", 2), returnStr=""; String namePair[] = varName.split("_"); boolean operationFlag = true; List textList = null; String subValuePair[] = null; logWriter.record(String.format("---------------------key:%s, value:%s", varName, varValue), 0); String location = "", value = ""; if(valuePair[0].startsWith("query")){ //查询专用 if(varName.startsWith("goods_")) { switch (valuePair[0]) { case "queryGoodsIntro": subValuePair = currentInfoPoint.get(valuePair[1]).split(":", 2); returnStr = gameEnv.queryGoodsIntroByGoodsName(subValuePair[1]); if(returnStr == null ) returnStr = "无"; break; case "queryAttrByGoodsName": subValuePair = currentInfoPoint.get(valuePair[1]).split(":", 2); value = gameEnv.queryGoodsAttrByGoodsName(subValuePair[0], subValuePair[1]); if(returnStr == null) { returnStr = "没找到"; }else{ returnStr = userInfo.varInitialMap.get("GET_ITEM_ATTR").replace("[物品名]", subValuePair[0]).replace("[属性名]", subValuePair[1]).replace("[属性值]", value); } break; case "queryAllAttrByGoodsName": textList = gameEnv.queryGoodsAllAttrByGoodsName(varName); if(textList == null || textList.size() == 0) returnStr = "无"; else { for (String site : textList) returnStr += "、" + site; returnStr = returnStr.substring(1); } break; case "queryAroundGoodsByClassName": textList = gameEnv.queryGoodsByClassAndSite(namePair[1], historyInfoPoint.get("site")); if(textList == null || textList.size() == 0) returnStr = "无"; else { for (String site : textList) returnStr += "、" + site; returnStr = returnStr.substring(1); } break; case "queryLocationByConditionName": // String NPCName = currentInfoPoint.get(valuePair[1]).split(":")[1]; returnStr = gameEnv.queryGoodsAttrByGoodsName(NPCName, "可被询问"); if(returnStr == null || returnStr.equals("否")){ returnStr = "没找到"; } else if(returnStr.equals("是")){ location = gameEnv.queryGoodsAttrByGoodsName(NPCName, "location"); if(location == null){ returnStr = "没找到"; }else{ returnStr = userInfo.varInitialMap.get("FIND_ITEM_LOCATION").replace("[物品名]", NPCName).replace("[地点]", location); } } break; default: } }else if(varName.startsWith("npc_")) { switch(valuePair[0]){ case "queryNpcIntro": //operationFlag = gameEnv.queryNpcIntro(valuePair[1], namePair[2]); subValuePair = currentInfoPoint.get(valuePair[1]).split(":", 2); returnStr = gameEnv.queryNpcIntroByNpcName(subValuePair[1]); if(returnStr == null) returnStr = "无"; break; case "queryAttrByNPCName": //根据condition的名字,从当前信息点中获取npc的名字 subValuePair = currentInfoPoint.get(valuePair[1]).split(":", 2); subValuePair[0] = subValuePair[0].replace("哪里", "location"); value = gameEnv.queryNpcAttrByNpcName(subValuePair[0], subValuePair[1]); if(returnStr == null) { returnStr = "没找到"; }else{ returnStr = userInfo.varInitialMap.get("GET_NPC_ATTR").replace("[人名]", subValuePair[0]).replace("[属性名]", subValuePair[1]).replace("[属性值]", value); } break; case "queryLocationByNPCName": // location = gameEnv.queryNpcAttrByNpcName(valuePair[1], "location"); if(location == null){ returnStr = "没找到"; }else{ returnStr = location; } break; case "queryLocationByConditionName": // String NPCName = currentInfoPoint.get(valuePair[1]).split(":")[1]; returnStr = gameEnv.queryNpcAttrByNpcName(NPCName, "可被询问"); if(returnStr == null || returnStr.equals("否")){ returnStr = "没找到"; } else if(returnStr.equals("是")){ location = gameEnv.queryNpcAttrByNpcName(NPCName, "location"); if(location == null){ returnStr = "没找到"; }else{ returnStr = userInfo.varInitialMap.get("FIND_NPC_LOCATION").replace("[人名]", NPCName).replace("[地点]", location); } } break; default: } }else if (varName.startsWith("map_")) { String format = ""; List sites = new ArrayList<>(); switch (valuePair[0]) { case "queryAroundAllBySite": if(userInfo.varInitialMap.containsKey("AROUND_THINGS")) { format = userInfo.varInitialMap.get("AROUND_THINGS"); }else { format = "周围有[人物物品列表]"; } sites = gameEnv.queryAroundAllBySite(historyInfoPoint.get("site")); if(sites == null || sites.size() == 0) returnStr = "什么都没有"; else { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace("[人物物品列表]", text.substring(1)); } break; case "queryAroundNpcBySite": if(userInfo.varInitialMap.containsKey("AROUND_PEOPLE")) { format = userInfo.varInitialMap.get("AROUND_PEOPLE"); }else { format = "周围有[人物列表]"; } sites = gameEnv.queryAroundNpcBySite(historyInfoPoint.get("site")); if(sites == null || sites.size() == 0) returnStr = "什么都没有"; else { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace("[人物列表]", text.substring(1)); } break; case "queryAroundGoodsBySite": if(userInfo.varInitialMap.containsKey("AROUND_ITEMS")) { format = userInfo.varInitialMap.get("AROUND_ITEMS"); }else { format = "周围有[物品列表]"; } sites = gameEnv.queryAroundGoodsBySite(historyInfoPoint.get("site")); if(sites == null || sites.size() == 0) returnStr = "什么都没有"; else { if(sites.size() > 0) { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace("[物品列表]", text.substring(1)); } } break; case "queryCanGoBySite": if(userInfo.varInitialMap.containsKey("CAN_GO_SITES")) { format = userInfo.varInitialMap.get("CAN_GO_SITES"); }else { format = "你可以去[可去列表]"; } sites = gameEnv.queryCanGoBySite(historyInfoPoint.get("site")); if(sites == null) { returnStr = userInfo.varInitialMap.get("CAN_GO_NOWHERE"); } else if(sites.size() == 0){ returnStr = userInfo.varInitialMap.get("CAN_GO_NOWHERE"); } else { if(sites.size() > 0) { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace("[可去列表]", text.substring(1)); } } break; case "queryHowToPathBySite": if(userInfo.varInitialMap.containsKey("PATH_EXIST")) { format = userInfo.varInitialMap.get("PATH_EXIST"); }else { format = "通过[途径]可以到达[目的地]"; } if(!currentInfoPoint.containsKey(valuePair[1])) { returnStr = userInfo.varInitialMap.get("PATH_NOT_EXIST"); break; } sites = gameEnv.queryPathBySite(historyInfoPoint.get("site"), currentInfoPoint.get(valuePair[1]).split(":")[1]); if(sites == null) { returnStr = userInfo.varInitialMap.get("PATH_NOT_EXIST"); } else { if(sites.size() > 0) { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace("[途径]", text.substring(1)).replace("[目的地]", currentInfoPoint.get(valuePair[1]).split(":")[1]); } } break; case "queryToDoPathBySite": if(userInfo.varInitialMap.containsKey("SITE_NOT_ADJOINT")) { format = userInfo.varInitialMap.get("SITE_NOT_ADJOINT"); }else { format = "通过[途径]可以到达[目的地]"; } if(!currentInfoPoint.containsKey(valuePair[1])) { returnStr = userInfo.varInitialMap.get("PATH_NOT_EXIST"); break; } sites = gameEnv.queryPathBySite(historyInfoPoint.get("site"), currentInfoPoint.get(valuePair[1]).split(":")[1]); if(sites == null) { returnStr = userInfo.varInitialMap.get("PATH_NOT_EXIST"); } else { if(sites.size() > 0) { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace("[途径]", text.substring(1)).replace("[目的地]", currentInfoPoint.get(valuePair[1]).split(":")[1]); } } break; } } }else if(valuePair[0].startsWith("update")){ Boolean flag = true; switch (valuePair[0]) { case "updateFollowerLocation": flag = gameEnv.updateFollowerLocation(valuePair[1]); } if(flag){ returnStr = "更新成功"; }else returnStr = "更新失败"; }else{ //增删改统一使用 gameEnv.CUD(varName, valuePair[0], valuePair[1]); } logWriter.record("---------------------return:" + returnStr, 0); return returnStr; } public static String findLocationName(GameEnv gameEnv, String locationName, String site){ return gameEnv.getPlaceNameByNickname(site, locationName); } }