package com.qiwu.chatbotbackend.processing; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.task.utils.GameBotUtil; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import static com.qiwu.chatbotbackend.Config.Config.*; 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; String format = ""; 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 "queryItemIntro": format = userInfo.varInitialMap.get("ITEM_INTRO"); if(format == null){ format = SCRIPT_ITEM_INTRO; } itemName = currentInfoPoint.get(valuePair[1]).split(":", 2)[1]; returnStr = gameEnv.queryGoodsIntroByGoodsName(itemName); if(returnStr == null ) returnStr = DONT_FOUND; else{ returnStr = format.replace(ITEM_NAME_SLOT, itemName).replace(ATTR_VALUE_SLOT, returnStr); } break; case "queryAttrByGoodsName": subValuePair = currentInfoPoint.get(valuePair[1]).split(":", 2); value = gameEnv.queryGoodsAttrByGoodsName(subValuePair[0], subValuePair[1]); if(returnStr == null) { returnStr = DONT_FOUND; }else{ returnStr = userInfo.varInitialMap.get("GET_ITEM_ATTR").replace(ITEM_NAME_SLOT, subValuePair[0]).replace(ATTR_NAME_SLOT, subValuePair[1]).replace(ATTR_VALUE_SLOT, value); } break; case "queryAllAttrByGoodsName": textList = gameEnv.queryGoodsAllAttrByGoodsName(varName); if(textList == null || textList.size() == 0) returnStr = DONT_FOUND; 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 = DONT_FOUND; 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 = ATTRIBUTE_HIDDEN; } else if(returnStr.equals("是")){ location = gameEnv.queryGoodsAttrByGoodsName(itemName, "location"); if(location == null){ returnStr = DONT_FOUND; }else{ owner = gameEnv.queryGoodsAttrByGoodsName(itemName, "owner"); //logWriter.record(ItemName + "-owner:" + owner, 0); if(owner != null && !owner.equals("")){ //如果物品的所属角色是玩家,那么回答物品在你身上 if(owner.equals("玩家")){ returnStr = userInfo.varInitialMap.get("ITEM_IS_YOURS"); }else { //不然回答物品在某个人身上 if(userInfo.varInitialMap.get("FIND_ITEM_OWNER") == null) userInfo.varInitialMap.put("FIND_ITEM_OWNER", FIND_ITEM_OWNER); System.out.println("pattern:" + userInfo.varInitialMap.get("FIND_ITEM_OWNER") + ", ItemName:" + itemName + ", owner:" + owner); returnStr = userInfo.varInitialMap.get("FIND_ITEM_OWNER").replace(ITEM_NAME_SLOT, itemName).replace(NPC_NAME_SLOT, owner); } }else { //没有所属角色就回答物品在某个地点 returnStr = userInfo.varInitialMap.get("FIND_ITEM_LOCATION").replace(ITEM_NAME_SLOT, itemName).replace(SITE_SLOT, location); } } } break; case "queryPlayerItems": String player = gameEnv.queryPlayerName(); textList = gameEnv.queryGoodsByNpcName(player); if(textList == null || textList.size() == 0) returnStr = NOTHING_FOUND; else { for (String site : textList) returnStr += "、" + site; returnStr = userInfo.varInitialMap.get("WE_HAVE").replace(ITEM_LIST_SLOT, returnStr.substring(1)); } break; case "queryFindItemWay": // itemName = currentInfoPoint.get(valuePair[1]).split(":")[1]; returnStr = gameEnv.queryGoodsAttrByGoodsName(itemName, "可被询问"); if(returnStr == null || returnStr.equals("否")){ returnStr = DONT_FOUND; } else if(returnStr.equals("是")){ location = gameEnv.queryGoodsAttrByGoodsName(itemName, "location"); if(location == null){ returnStr = DONT_FOUND; }else{ returnStr = userInfo.varInitialMap.get("FIND_ITEM_WAY").replace(ITEM_NAME_SLOT, itemName).replace(SITE_SLOT, location); } } break; default: } }else if(varName.startsWith("npc_")) { String NPCName = ""; switch(valuePair[0]){ case "queryNpcIntro": format = userInfo.varInitialMap.get("NPC_INTRO"); if(format == null){ format = SCRIPT_NPC_INTRO; } NPCName = currentInfoPoint.get(valuePair[1]).split(":", 2)[1]; returnStr = gameEnv.queryNpcIntroByNpcName(NPCName); if(returnStr == null ) returnStr = DONT_FOUND; else{ returnStr = format.replace(NPC_NAME_SLOT, NPCName).replace(ATTR_VALUE_SLOT, 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 = DONT_FOUND; }else{ returnStr = userInfo.varInitialMap.get("GET_NPC_ATTR").replace(NPC_NAME_SLOT, subValuePair[0]).replace(ATTR_NAME_SLOT, subValuePair[1]).replace(ATTR_VALUE_SLOT, value); } break; case "queryLocationByNPCName": // location = gameEnv.queryNpcAttrByNpcName(valuePair[1], "location"); if(location == null){ returnStr = DONT_FOUND; }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 = DONT_FOUND; } else if(returnStr.equals("是")){ location = gameEnv.queryNpcAttrByNpcName(NPCName, "location"); if(location == null){ returnStr = DONT_FOUND; }else{ returnStr = userInfo.varInitialMap.get("FIND_NPC_LOCATION").replace(NPC_NAME_SLOT, NPCName).replace(SITE_SLOT2, location); } } break; case "queryFindNPCWay": // NPCName = currentInfoPoint.get(valuePair[1]).split(":")[1]; returnStr = gameEnv.queryNpcAttrByNpcName(NPCName, "可被询问"); if(returnStr == null || returnStr.equals("否")){ returnStr = DONT_FOUND; } else if(returnStr.equals("是")){ location = gameEnv.queryNpcAttrByNpcName(NPCName, "location"); if(location == null){ returnStr = DONT_FOUND; }else{ returnStr = userInfo.varInitialMap.get("FIND_NPC_WAY").replace(NPC_NAME_SLOT, NPCName).replace(SITE_SLOT2, 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(NPC_NAME_SLOT, lastTalker); break; default: } }else if (varName.startsWith("map_")) { String start = historyInfoPoint.get("site"); String destination = ""; List sites = new ArrayList<>(); switch (valuePair[0]) { case "queryAroundAllBySite": format = userInfo.varInitialMap.get("AROUND_THINGS"); sites = gameEnv.queryAroundAllBySite(historyInfoPoint.get("site")); if(sites == null || sites.size() == 0) returnStr = NOTHING_FOUND; else { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace(NPC_ITEM_LIST_SLOT, text.substring(1)); } break; case "queryAroundNpcBySite": format = userInfo.varInitialMap.get("AROUND_PEOPLE"); sites = gameEnv.queryAroundNpcBySite(historyInfoPoint.get("site")); if(sites == null || sites.size() == 0) returnStr = NOTHING_FOUND; else { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace(NPC_LIST_SLOT, text.substring(1)); } break; case "queryAroundGoodsBySite": format = userInfo.varInitialMap.get("AROUND_ITEMS"); sites = gameEnv.queryAroundGoodsBySite(historyInfoPoint.get("site")); if(sites == null || sites.size() == 0) returnStr = NOTHING_FOUND; else { if(sites.size() > 0) { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace(ITEM_LIST_SLOT, text.substring(1)); } } break; case "queryCanGoBySite": format = userInfo.varInitialMap.get("CAN_GO_SITES"); 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(CAN_GO_SITE_SLOT, text.substring(1)); } } break; case "queryHowToPathBySite": format = userInfo.varInitialMap.get("PATH_EXIST"); 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(PATH_SLOT, text.substring(1)).replace(DESTINATION_SLOT, destination); } } break; case "queryToDoPathBySite": format = userInfo.varInitialMap.get("SITE_NOT_ADJOINT"); 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(PATH_SLOT, text.substring(1)).replace(DESTINATION_SLOT, destination); } } break; case "queryPathNotExist": returnStr = userInfo.varInitialMap.get("PATH_NOT_EXIST") + destination; break; case "queryAroundPlace": format = userInfo.varInitialMap.get("AROUND_PLACE"); sites = gameEnv.queryCanGoBySite(start); if(sites == null || sites.size() == 0) { returnStr = NOWHERE_CAN_GO; } else { if(sites.size() > 0) { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace(SITE_LIST_SLOT, text.substring(1)); } } break; case "queryInnerNpcBySite": format = userInfo.varInitialMap.get("INNER_PEOPLE"); sites = gameEnv.queryAroundNpcBySite(historyInfoPoint.get("site")); if(sites == null || sites.size() == 0) returnStr = NOTHING_FOUND; else { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace(NPC_LIST_SLOT, text.substring(1)); } break; case "queryTaskDescription": format = userInfo.varInitialMap.get("TASK_DESCRIPTION"); returnStr = format.replace(TASK_DESCRIPTION_SLOT, userInfo.varInitialMap.get(TASK_DESCRIPTION)); break; case "queryWhereIam": returnStr = userInfo.varInitialMap.get("WHERE_I_AM").replace(SITE_SLOT, historyInfoPoint.get("site")); break; case "queryPathAt": returnStr = userInfo.varInitialMap.get("PATH_AT").replace(SITE_SLOT, historyInfoPoint.get("site")); break; case "queryPathAdjoin": returnStr = userInfo.varInitialMap.get("PATH_ADJOINT"); break; case "queryItemsInPlace": format = userInfo.varInitialMap.get("ITEM_IN_PLACE"); if(format == null){ format = SCRIPT_ITEM_IN_PLACE; } destination = currentInfoPoint.get(valuePair[1]).split(":")[0]; sites = gameEnv.queryAroundGoodsBySite(destination); if(sites == null || sites.size() == 0) returnStr = NOTHING_FOUND; else { if(sites.size() > 0) { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace(SITE_SLOT, destination).replace(ITEM_LIST_SLOT, text.substring(1)); } } break; case "queryNpcInPlace": format = userInfo.varInitialMap.get("NPC_IN_PLACE"); if(format == null){ format = SCRIPT_NPC_IN_PLACE; } destination = currentInfoPoint.get(valuePair[1]).split(":")[0]; sites = gameEnv.queryAroundNpcBySite(destination); if(sites == null || sites.size() == 0) returnStr = NOTHING_FOUND; else { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace(SITE_SLOT, destination).replace(NPC_LIST_SLOT, text.substring(1)); } break; case "queryThingsInPlace": format = userInfo.varInitialMap.get("ALL_IN_PLACE"); if(format == null){ format = SCRIPT_NPC_IN_PLACE; } destination = currentInfoPoint.get(valuePair[1]).split(":")[1]; sites = gameEnv.queryAroundAllBySite(destination); if(sites == null || sites.size() == 0) returnStr = NOTHING_FOUND; else { StringBuffer text = new StringBuffer(); for(String site:sites){ text.append("、" + site); } returnStr = format.replace(SITE_SLOT, destination).replace(NPC_ITEM_LIST_SLOT, text.substring(1)); } break; default: returnStr = "error"; 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); } }