package com.qiwu.chatbotbackend.processing; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.gameDesign.PlotConstructor2; import com.qiwu.chatbotbackend.task.utils.GameBotUtil; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.regex.Matcher; 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="", owner; 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")){ //查询专用 String ItemName = ""; String currentLocation; if(varName.startsWith("goods_")) { currentLocation = historyInfoPoint.get("site"); 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": // ItemName = currentInfoPoint.get(valuePair[1]).split(":")[1]; ItemName = gameEnv.getGoodsNameByNickname(currentLocation, ItemName); returnStr = gameEnv.queryGoodsAttrByGoodsName(ItemName, "可被询问"); if(returnStr == null || returnStr.equals("否")){ returnStr = "隐藏了"; } else if(returnStr.equals("是")){ location = gameEnv.queryGoodsAttrByGoodsName(ItemName, "location"); if(location == null){ returnStr = "没找到"; }else{ owner = gameEnv.queryGoodsAttrByGoodsName(ItemName, "所属角色"); logWriter.record(ItemName + "-owner:" + owner, 0); if(owner != null && !owner.equals("")){ //如果物品的所属角色是玩家,那么回答物品在你身上 if(owner.equals("玩家")){ returnStr = userInfo.varInitialMap.get("ITEM_IS_YOURS"); }else { //不然回答物品在某个人身上 returnStr = userInfo.varInitialMap.get("FIND_TEM_OWNER").replace("[物品名]", ItemName).replace("[人名]", owner); } }else { //没有所属角色就回答物品在某个地点 returnStr = userInfo.varInitialMap.get("FIND_ITEM_LOCATION").replace("[物品名]", ItemName).replace("[地点]", location); } } } break; case "queryPlayerItems": String player = gameEnv.queryPlayerName(); textList = gameEnv.queryGoodsByNpcName(player); if(textList == null || textList.size() == 0) returnStr = "什么都没有"; else { for (String site : textList) returnStr += "、" + site; returnStr = userInfo.varInitialMap.get("WE_HAVE").replace("[物品列表]", returnStr.substring(1)); } break; case "queryFindItemWay": // ItemName = currentInfoPoint.get(valuePair[1]).split(":")[1]; returnStr = gameEnv.queryGoodsAttrByGoodsName(ItemName, "可被询问"); if(returnStr == null || returnStr.equals("否")){ returnStr = "没找到"; } else if(returnStr.equals("是")){ location = gameEnv.queryGoodsAttrByGoodsName(ItemName, "location"); if(location == null){ returnStr = "没找到"; }else{ returnStr = userInfo.varInitialMap.get("FIND_ITEM_WAY").replace("[物品名]", ItemName).replace("[地点]", location); } } break; default: } }else if(varName.startsWith("npc_")) { String NPCName = ""; 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(value == 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": // NPCName = currentInfoPoint.get(valuePair[1]).split(":")[1]; NPCName = gameEnv.getNpcNameByNickname(historyInfoPoint.get("site"), NPCName); System.out.println(NPCName); 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; case "queryFindNPCWay": // 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_WAY").replace("[人名]", NPCName).replace("[地点]", location); } } break; case "queryLastTalker": String lastTalker = "没找到npc名字"; Matcher m = GameBotUtil.findTalker(historyInfoPoint.get("lastReply")); while(m.find()){ lastTalker = m.group(1); } returnStr = userInfo.varInitialMap.get("WHO_YOU_ARE").replace("[人名]", lastTalker); break; default: } }else if (varName.startsWith("map_")) { String format = ""; String start = historyInfoPoint.get("site"); String destination = ""; 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; } destination = currentInfoPoint.get(valuePair[1]).split(":")[1]; destination = gameEnv.getPlaceNameByNickname(start, destination); sites = gameEnv.queryPathBySite(historyInfoPoint.get("site"), destination); if(sites == null) { returnStr = userInfo.varInitialMap.get("PATH_NOT_EXIST") + destination; } else { if(sites.size() > 0) { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace("[途径]", text.substring(1)).replace("[目的地]", destination); } } break; case "queryToDoPathBySite": if(userInfo.varInitialMap.containsKey("SITE_NOT_ADJOINT")) { format = userInfo.varInitialMap.get("SITE_NOT_ADJOINT"); }else { format = "通过[途径]可以到达[目的地]"; } destination = currentInfoPoint.get(valuePair[1]).split(":")[1]; destination = gameEnv.getPlaceNameByNickname(start, destination); if(!currentInfoPoint.containsKey(valuePair[1])) { returnStr = userInfo.varInitialMap.get("PATH_NOT_EXIST") + destination; break; } sites = gameEnv.queryPathBySite(start, destination); if(sites == null) { returnStr = userInfo.varInitialMap.get("PATH_NOT_EXIST") + destination; } else { if(sites.size() > 0) { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace("[途径]", text.substring(1)).replace("[目的地]", destination); } } break; case "queryPathNotExist": returnStr = userInfo.varInitialMap.get("PATH_NOT_EXIST") + destination; break; case "queryAroundPlace": if(userInfo.varInitialMap.containsKey("AROUND_PLACE")) { format = userInfo.varInitialMap.get("AROUND_PLACE"); }else { format = "周围有[地点列表]"; } sites = gameEnv.queryCanGoBySite(start); 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 "queryInnerNpcBySite": if(userInfo.varInitialMap.containsKey("INNER_PEOPLE")) { format = userInfo.varInitialMap.get("INNER_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 "queryTaskDescription": if(userInfo.varInitialMap.containsKey("TASK_DESCRIPTION")) { format = userInfo.varInitialMap.get("TASK_DESCRIPTION"); }else { format = "我们的任务是[任务描述]"; } returnStr = format.replace("[任务描述]", userInfo.varInitialMap.get(PlotConstructor2.TASK_DESCRIPTION)); break; case "queryWhereIam": returnStr = userInfo.varInitialMap.get("WHERE_I_AM").replace("[地点名]", historyInfoPoint.get("site")); break; case "queryPathAt": returnStr = userInfo.varInitialMap.get("PATH_AT").replace("[地点名]", historyInfoPoint.get("site")); break; case "queryPathAdjoin": returnStr = userInfo.varInitialMap.get("PATH_ADJOINT"); break; } } }else if(valuePair[0].startsWith("updateFollowerLocation")){ Boolean flag = true; switch (valuePair[0]) { case "updateFollowerLocation": flag = gameEnv.updateFollowerLocation(valuePair[1]); } if(flag){ returnStr = "更新成功"; }else returnStr = "更新失败"; }else{ //增删改统一使用 logWriter.record("更新环境--varName:" + varName + "valuePair:" + valuePair[0] + ":" + valuePair[1], 1); try { gameEnv.CUD(varName, valuePair[0], valuePair[1]); } catch (Exception e){ System.out.println("更新环境出现异常--varName:" + varName + "valuePair:" + 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); } }