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.UserInfo; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; public class CharacterConstructor extends ElementConstructor { public CharacterConstructor(SqlHelper helper, int botId, String botAccount, Map keywordMap, HashSet exceptRelation, StringBuffer POS) { super(); this.helper = helper; this.botId = botId; this.botAccount = botAccount; this.exceptRelation = exceptRelation; this.keywordMap = keywordMap; this.POS = POS; } public void construct(JSONObject gameElements, UserInfo userinfo) { String category, name, className, description, attrValue,botName = (String)gameElements.get("botName"); try { //FIND_NPC_LOCATION String pattern; StringBuffer onlineModeTalkers = new StringBuffer(","); JSONArray allNPC = gameElements.getJSONArray("npc"); Map attribute = null; HashSet classSet = new HashSet(); List replaceList = new ArrayList(); copyPattern("HOW_FIND_NPC"); copyPattern("NPC_LOCATION"); copyPattern("AROUND_NPC_CLASS"); copyPattern("NPC_ATTR"); copyPattern("WHO_YOU_ARE"); for (JSONObject character : allNPC.toJavaList(JSONObject.class)) { replaceList.clear(); name = character.getString("name"); replaceList.add(new String[]{"name", name}); //找到所有处于离线模式的npc if(character.getBoolean("offline")!= null && !character.getBoolean("offline")){ onlineModeTalkers.append(name).append(","); } List nameList = new ArrayList(); nameList.add(name); //添加词性 className = (String) character.get("class"); appendPOS(POS, name, "人名"); appendPOS(POS, className, "人名"); appendReplacedKeyword("NPC_LOCATION", replaceList); appendReplacedKeyword("HOW_FIND_NPC", replaceList); //添加别名 if(character.containsKey("nickname")){ for(String nickname:JSONArray.parseArray(character.getJSONArray("nickname").toJSONString(), String.class)){ if(!nickname.equals("")) { appendPOS(POS, nickname, "人名"); nameList.add(nickname); replaceList.get(0)[1] = nickname; appendReplacedKeyword("NPC_LOCATION", replaceList); appendReplacedKeyword("HOW_FIND_NPC", replaceList); } } } //添加类名称 if (!classSet.contains(className)) { classSet.add(className); //替换后的名字改为类名 replaceList.get(0)[1] = className; appendReplacedKeyword("AROUND_NPC_CLASS", replaceList); } attribute = gameElements.toJavaObject(character.getJSONObject("attr"), Map.class); //替换后的名字改为npc名 replaceList.get(0)[1] = name; replaceList.add(new String[]{"attrName", ""}); for(String attrName:attribute.keySet()){ replaceList.get(1)[1] = attrName; appendPOS(POS, attrName, "名词"); for(String NPCName:nameList) { appendReplacedKeyword("NPC_ATTR", replaceList); } } } replaceList.add(new String[]{"botName", botName}); appendReplacedKeyword("WHO_YOU_ARE", replaceList); keywordMap.put("onlineModeTalkers", onlineModeTalkers); varInitialMap.put("onlineModeTalkers", onlineModeTalkers.toString()); conditionDescription.put("onlineModeTalkers", "所有在线npc"); // //同时识别zainali和wenzainali // configTermText = new ConfigTermText( // "zainali::match-Include=" + keywordMap.get("WHERE_I_AM_zainali") + "," + // "wenzainali::match-Keyword=" + keywordMap.get("NPC_LOCATION_wenzainali").toString(), " ", // "match,return-0", "", "yaobuyao", "npc_query:queryLocationByConditionName:wenzainali" // ); // ConfigTermText.activateCopy(configTermText, configTermTextList, "npc"); //根据根据NPC的名字查询地点 queryTypeConfigTermGenerate(keywordMap, "NPC_LOCATION", "npc", "queryLocationByConditionName"); //根据根据NPC名字和属性查询属性值 queryTypeConfigTermGenerate(keywordMap, "NPC_ATTR", "npc", "queryAttrByNPCName"); //根据根据NPC类别和地点属性查询所有NPC实例 queryTypeConfigTermGenerate(keywordMap, "AROUND_NPC_CLASS", "npc", "queryAroundNpcByClassName"); //根据类名查询周围所有实例 queryTypeConfigTermGenerate(keywordMap, "HOW_FIND_NPC", "npc", "queryFindNPCWay"); //查询说话的人 //WHO_YOU_ARE queryTypeConfigTermGenerate(keywordMap, "WHO_YOU_ARE", "npc", "queryLastTalker"); //查询xx是谁,输出介绍语 queryTypeConfigTermGenerate(keywordMap, "NPC_INTRO", "npc", "queryNpcIntro"); }catch (Exception e){ operationStatus = 0; message = "NPC接口处理出现异常"; e.printStackTrace(); return; } } }