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.JConfig; import com.qiwu.chatbotbackend.entity.UserInfo; import java.util.*; 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, attrName, attrValue; try { String pattern1 = keywordMap.get("npc_location_pattern").toString(); JSONArray allNPC = gameElements.getJSONArray("npc"); Map attribute = null; Iterator attrIter = null; HashSet classSet = new HashSet(); keywordMap.putIfAbsent("npc_wenzainali", new StringBuffer()); keywordMap.putIfAbsent("npc_shishenme", new StringBuffer()); keywordMap.putIfAbsent("npc_know_exist_what", new StringBuffer()); keywordMap.putIfAbsent("npc_nums", new StringBuffer()); //与物品属性查询相关的所有condition String attrRelateConditions[] = {"nums", "shishenme"}; //与物品类查询相关的所有condition String classRelateConditions[] = {"know_exist_what" }; String locationRelateConditions[] = {"wenzainali"}; //在condition的三元组中添加与NPC相关的关键词 StringBuffer classKeywords = new StringBuffer(); for (JSONObject character : allNPC.toJavaList(JSONObject.class)) { name = (String) character.get("name"); //添加词性 if(!name.equals("")) POS.append("=" + name + "|人名\r\n"); className = (String) character.get("class"); if(!className.equals("")) POS.append("=" + className + "|人名\r\n"); //添加类名称 if (!classSet.contains(className)) { classSet.add(className); classKeywords.append("+周围:" + className + "+附近:" + className); } attribute = gameElements.toJavaObject(character.getJSONObject("attr"), Map.class); attrIter = attribute.keySet().iterator(); keywordMap.get("npc_" + locationRelateConditions[0]).append(pattern1.replace("name", name)); while (attrIter.hasNext()) { attrName = attrIter.next(); if(!attrName.equals("")) POS.append("=" + attrName + "|名词\r\n"); attrValue = attribute.get(attrName); for(String condition: attrRelateConditions){ keywordMap.get("npc_" + condition).append("+" + attrName + ":" + attrValue); } } } //根据根据NPC的名字查询地点 for (String conditionName : locationRelateConditions) { jConfig = new JConfig( conditionName + "::match-Include=" + keywordMap.get("npc_" + conditionName), " ", "match,return-0", "【{" + PlotConstructor.ASSISTANT + "-2}】" + "{npc_query-2}", "yaobuyao", "npc_query:queryLocationByConditionName:" + conditionName ); JConfig.activateCopy(jConfig, jConfigList, "npc"); jConfig = new JConfig( conditionName + "::match-Include=" + keywordMap.get("npc_" + conditionName) + "," + "zainali::match-Keyword=" + keywordMap.get("map_zainali").toString(), " ", "match,return-0", "【{" + PlotConstructor.ASSISTANT + "-2}】" + "{npc_query-2}", "yaobuyao", "npc_query:queryLocationByConditionName:" + conditionName ); JConfig.activateCopy(jConfig, jConfigList, "npc"); //jConfigList.add(jConfig); } //根据根据NPC名字和属性查询属性值 for(String conditionName: attrRelateConditions){ jConfig = new JConfig( conditionName + "::match-Include=" + keywordMap.get("npc_" + conditionName), " ", "match,return-0", "【{" + PlotConstructor.ASSISTANT + "-2}】" + "{npc_query-2}", "yaobuyao", "npc_query:queryAttrByNPCName:" + conditionName ); JConfig.activateCopy(jConfig, jConfigList, "npc"); } //根据根据NPC类别和地点属性查询所有NPC实例 for (String conditionName : classRelateConditions) { jConfig = new JConfig( conditionName + "::match-Include=" + classKeywords, " ", "match,return-0", "【{" + PlotConstructor.ASSISTANT + "-2}】" + "{npc_query-2}", "yaobuyao", "npc_query:queryAroundNpcByClassName:" + conditionName ); JConfig.activateCopy(jConfig, jConfigList, "npc"); //jConfigList.add(jConfig); } }catch (Exception e){ operationStatus = 0; message = "NPC接口处理出现异常"; e.printStackTrace(); return; } } }