diff --git a/src/GameEnvironment/DBUtil.java b/src/GameEnvironment/DBUtil.java index 20e5af7..40031c7 100644 --- a/src/GameEnvironment/DBUtil.java +++ b/src/GameEnvironment/DBUtil.java @@ -1,6 +1,6 @@ -package GameEnvironment; +package com.qiwu.chatbotbackend.GameEnvironment; -import db.C3p0Utils; +import com.qiwu.chatbotbackend.db.C3p0Utils; import java.sql.Connection; import java.sql.PreparedStatement; diff --git a/src/GameEnvironment/GameEnv.java b/src/GameEnvironment/GameEnv.java index db2862b..7647372 100644 --- a/src/GameEnvironment/GameEnv.java +++ b/src/GameEnvironment/GameEnv.java @@ -1,5 +1,6 @@ -package GameEnvironment; +package com.qiwu.chatbotbackend.GameEnvironment; +import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -7,13 +8,13 @@ import java.util.Map; import com.alibaba.fastjson.JSON; -import GameEnvironment.bean.Attr; -import GameEnvironment.bean.GoodsInstance; -import GameEnvironment.bean.SuperClass; -import GameEnvironment.bean.NpcInstance; -import GameEnvironment.bean.Place; +import com.qiwu.chatbotbackend.GameEnvironment.bean.Attr; +import com.qiwu.chatbotbackend.GameEnvironment.bean.GoodsInstance; +import com.qiwu.chatbotbackend.GameEnvironment.bean.SuperClass; +import com.qiwu.chatbotbackend.GameEnvironment.bean.NpcInstance; +import com.qiwu.chatbotbackend.GameEnvironment.bean.Place; -public class GameEnv implements Serializable{ +public class GameEnv implements Serializable { private static final long serialVersionUID = 1L; public Map npcClassIndex; public Map npcInstanceIndex; diff --git a/src/GameEnvironment/GameEnvironment/DBUtil.java b/src/GameEnvironment/GameEnvironment/DBUtil.java new file mode 100644 index 0000000..4c90e42 --- /dev/null +++ b/src/GameEnvironment/GameEnvironment/DBUtil.java @@ -0,0 +1,40 @@ +package com.qiwu.chatbotbackend.GameEnvironment.GameEnvironment; + +import com.qiwu.chatbotbackend.db.C3p0Utils; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; + +public class DBUtil { + + public static String queryEnvJsonByBotName(String botName) throws Exception{ + Connection conn = C3p0Utils.getConnection(); + String sql = "select * from generate_json_tb where bot_name = ?"; + String result = "{}"; + try { + PreparedStatement ps = conn.prepareStatement(sql); + ps.setString(1, String.valueOf(botName)); + ResultSet rs = ps.executeQuery(); + if (rs.next()) + result = rs.getString("env_json_text"); + + ps.close(); + rs.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + if (conn != null) { + try { + conn.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + return result; + } + +} diff --git a/src/GameEnvironment/GameEnvironment/GameEnv.java b/src/GameEnvironment/GameEnvironment/GameEnv.java new file mode 100644 index 0000000..4eebb22 --- /dev/null +++ b/src/GameEnvironment/GameEnvironment/GameEnv.java @@ -0,0 +1,515 @@ +package com.qiwu.chatbotbackend.GameEnvironment.GameEnvironment; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.alibaba.fastjson.JSON; + +import com.qiwu.chatbotbackend.GameEnvironment.bean.Attr; +import com.qiwu.chatbotbackend.GameEnvironment.bean.GoodsInstance; +import com.qiwu.chatbotbackend.GameEnvironment.bean.SuperClass; +import com.qiwu.chatbotbackend.GameEnvironment.bean.NpcInstance; +import com.qiwu.chatbotbackend.GameEnvironment.bean.Place; + +public class GameEnv implements Serializable { + private static final long serialVersionUID = 1L; + public Map npcClassIndex; + public Map npcInstanceIndex; + public Map goodsClassIndex; + public Map goodsInstanceIndex; + public Map placeIndex; + + public GameEnv(String botName) throws Exception { + // 通过文件来初始化游戏环境 + // String fullName = "test.json"; + + // String jsonString = Util.getJsonString(fullName); + // System.out.println(jsonString); + String jsonString = DBUtil.queryEnvJsonByBotName(botName); + Map gameProjectMap = JSON.parseObject(jsonString); + // System.out.println(gameProjectMap); + if(!gameProjectMap.isEmpty()) { + // 地图 + Map map = (Map)gameProjectMap.get("map"); + this.placeIndex = new HashMap(); + Map placeMapTmp = new HashMap(); + if(!map.isEmpty()) { + for(String key:map.keySet()) { + Map one = (Map)map.get(key); + placeMapTmp.put(key, (String)one.get("name")); + Place place = new Place(); + place.setIntro((String)one.get("intro")); + place.setName((String)one.get("name")); + placeIndex.put((String)one.get("name"), place); + } + for(String tmp:placeMapTmp.keySet()) { + List ableGos = new ArrayList(); + List ableGoLongs = (List)((Map)map.get(tmp)).get("canGo"); + for(int j=0; j npcClasses = (Map)gameProjectMap.get("npc"); + Map npcNames = new HashMap(); + this.npcClassIndex = new HashMap(); + this.npcInstanceIndex = new HashMap(); + if(!npcClasses.isEmpty()) { + for(String classKey:npcClasses.keySet()) { + Map one = (Map)npcClasses.get(classKey); + // class + Map oneClass = (Map)one.get("class"); + SuperClass npcClass = new SuperClass(); + npcClass.setName(classKey); + npcClass.setIntro((String)oneClass.get("intro")); + + List attrs = new ArrayList(); + for(String attrKey:oneClass.keySet()) { + if(!attrKey.equals("intro") && !attrKey.equals("class") && !attrKey.equals("")) { + Attr attr = new Attr(); + attr.setName(attrKey); + attr.setValue((String)oneClass.get(attrKey)); + attrs.add(attr); + } + } + npcClass.setAttrs(attrs); + // instance + Map instances = (Map)one.get("instance"); + for(String instanceKey:instances.keySet()) { + Map instance = new HashMap(); + NpcInstance npcInstance = new NpcInstance(); + Map instanceAttrMap = (Map)instances.get(instanceKey); + npcNames.put(instanceKey, (String)instanceAttrMap.get("name")); + npcInstance.setName((String)instanceAttrMap.get("name")); + npcInstance.setIntro((String)instanceAttrMap.get("intro")); + List npcInstanceAttrs = new ArrayList(); + for(String instanceAttrKey:instanceAttrMap.keySet()) { + if(!instanceAttrKey.equals("name")&&!instanceAttrKey.equals("intro")&&!instanceAttrKey.equals("placeId")&&!instanceAttrKey.equals("placeName")&&!instanceAttrKey.equals("class")) { + Attr attr = new Attr(); + attr.setName(instanceAttrKey); + attr.setValue((String)instanceAttrMap.get(instanceAttrKey)); + npcInstanceAttrs.add(attr); + } + } + npcInstance.setAttrs(npcInstanceAttrs); + Place place = null; + if(instanceAttrMap.get("placeId")!=null) { + place = placeIndex.get(((Map)map.get(String.valueOf(instanceAttrMap.get("placeId")))).get("name")); + List npcInstances = place.getNpcInstances(); + if(npcInstances == null) { + npcInstances = new ArrayList(); + } + npcInstances.add(npcInstance); + place.setNpcInstances(npcInstances); + } + List npcInstanceClass = npcClass.getNpcInstances(); + if(npcInstanceClass == null) { + npcInstanceClass = new ArrayList(); + } + npcInstanceClass.add(npcInstance); + npcClass.setNpcInstances(npcInstanceClass); + npcInstance.setPlace(place); + this.npcInstanceIndex.put((String)instanceAttrMap.get("name"), npcInstance); + } + this.npcClassIndex.put(classKey, npcClass); + } + } + // goods + Map goodsClasses = (Map)gameProjectMap.get("goods"); + this.goodsClassIndex = new HashMap(); + this.goodsInstanceIndex = new HashMap(); + if(!goodsClasses.isEmpty()) { + for(String classKey:goodsClasses.keySet()) { + Map one = (Map)goodsClasses.get(classKey); + // class + Map oneClass = (Map)one.get("class"); + SuperClass goodsClass = new SuperClass(); + goodsClass.setName(classKey); + goodsClass.setIntro((String)oneClass.get("intro")); + + List attrs = new ArrayList(); + for(String attrKey:oneClass.keySet()) { + if(!attrKey.equals("intro")&&!attrKey.equals("placeId")&&!attrKey.equals("npcName")&&!attrKey.equals("class")&&!attrKey.equals("placeName")) { + Attr attr = new Attr(); + attr.setName(attrKey); + attr.setValue((String)oneClass.get(attrKey)); + attrs.add(attr); + } + } + goodsClass.setAttrs(attrs); + + // instance + Map instances = (Map)one.get("instance"); + + for(String instanceKey:instances.keySet()) { + Map instance = new HashMap(); + GoodsInstance goodsInstance = new GoodsInstance(); + Map instanceAttrMap = (Map)instances.get(instanceKey); + goodsInstance.setName((String)instanceAttrMap.get("name")); + goodsInstance.setIntro((String)instanceAttrMap.get("intro")); + List goodsInstanceAttrs = new ArrayList(); + for(String instanceAttrKey:instanceAttrMap.keySet()) { + if(instanceAttrKey!="placeId"&&instanceAttrKey!="npcId"&&instanceAttrKey!="name"&&instanceAttrKey!="intro") { + Attr attr = new Attr(); + attr.setName(instanceAttrKey); + attr.setValue((String)instanceAttrMap.get(instanceAttrKey)); + goodsInstanceAttrs.add(attr); + } + } + goodsInstance.setAttrs(goodsInstanceAttrs); + if(instanceAttrMap.get("placeId")!=null) { + Place place = placeIndex.get(((Map)map.get(String.valueOf(instanceAttrMap.get("placeId")))).get("name")); + goodsInstance.setPlace(place); + List placeGoodsInstances= place.getGoodsInstances(); + if(placeGoodsInstances == null) { + placeGoodsInstances = new ArrayList(); + } + placeGoodsInstances.add(goodsInstance); + place.setGoodsInstances(placeGoodsInstances); + } + if(instanceAttrMap.get("npcId")!=null){ + NpcInstance npc = this.npcInstanceIndex.get(npcNames.get(String.valueOf(instanceAttrMap.get("npcId")))); + goodsInstance.setNpc(npc); + List npcGoodsInstances = npc.getGoodsInstances(); + if(npcGoodsInstances == null) { + npcGoodsInstances = new ArrayList(); + } + npcGoodsInstances.add(goodsInstance); + npc.setGoodsInstances(npcGoodsInstances); + } + + List goodsInstancesClass = goodsClass.getGoodsInstances(); + if(goodsInstancesClass == null) { + goodsInstancesClass = new ArrayList(); + } + goodsInstancesClass.add(goodsInstance); + goodsClass.setGoodsInstances(goodsInstancesClass); + this.goodsInstanceIndex.put((String)instanceAttrMap.get("name"), goodsInstance); + } + this.goodsClassIndex.put(classKey, goodsClass); + } + } + } + } + + /********************************************地点******************************************/ + // 周围有什么 + public List queryAroundAllBySite(String site){ + List queryGoods = this.queryAroundGoodsBySite(site); + List queryNpc = this.queryAroundNpcBySite(site); + queryGoods.addAll(queryNpc); + return queryGoods; + } + // 周围有什么人 + public List queryAroundNpcBySite(String site){ + Place place = this.placeIndex.get(site); + List queryNpc = new ArrayList(); + if(place !=null) { + List npcInstances = place.getNpcInstances(); + if(npcInstances!=null) { + for(int i=0; i queryAroundGoodsBySite(String site){ + Place place = this.placeIndex.get(site); + List queryGoods = new ArrayList(); + if(place !=null) { + List goodsInstances = place.getGoodsInstances(); + if(goodsInstances!=null) { + for(int i=0; i queryCanGoBySite(String site){ + List ableGoString = new ArrayList(); + Place place = this.placeIndex.get(site); + List ableGos = place.getAbleGo(); + for(int i=0; i queryPathBySite(String start, String destination){ + return null; + } + /******************************************通过npc获取物品列表**********************************************/ + // 通过npcName获取所有物品名称列表 + public List queryGoodsByNpcName(String npcName){ + List goodsNames = new ArrayList(); + if(this.npcInstanceIndex.get(npcName)!=null) { + List goodsInstances = this.npcInstanceIndex.get(npcName).getGoodsInstances(); + if(goodsInstances != null) { + for(int i=0; i queryGoodsByClassAndSite(String className, String site){ + List goodsNames = new ArrayList(); + List placeGoodsInstances = null; + List goodsInstances = null; + if(this.placeIndex.get(site)!=null && this.goodsClassIndex.get(className)!=null) { + placeGoodsInstances = this.placeIndex.get(site).getGoodsInstances(); + goodsInstances = this.goodsClassIndex.get(className).getGoodsInstances(); + for(int i=0; i attrs = goodsInstance.getAttrs(); + if(attrName.equals("location")) { + if(goodsInstance.getPlace()!=null) { + return goodsInstance.getPlace().getName(); + } + } + if(attrName.equals("owner")) { + if(goodsInstance.getNpc()!=null) { + return goodsInstance.getNpc().getName(); + } + } + for(int i=0; i queryGoodsAllAttrByGoodsName(String name){ + List attrnames = new ArrayList(); + GoodsInstance goodsInstance = queryGoodsByGoodsName(name); + if(goodsInstance != null) { + List attrs = goodsInstance.getAttrs(); + for(int i=0; i queryNpcByClassAndSite(String className, String site){ + List npcNames = new ArrayList(); + List placeNpcInstances = this.placeIndex.get(site).getNpcInstances(); + List npcInstances = this.goodsClassIndex.get(className).getNpcInstances(); + NpcInstance npcInstance = new NpcInstance(); + for(int i=0; i attrs = npcInstance.getAttrs(); + for(int i=0; i queryNpcAttrsByNpcName(String name){ + List attrNames = new ArrayList(); + NpcInstance npcInstance = queryNpcByNpcName(name); + if(npcInstance != null) { + List attrs = npcInstance.getAttrs(); + for(int i=0; i goodsClassAttrs = goodsClass.getAttrs(); + GoodsInstance goodsInstance = new GoodsInstance(); + List goodsInstanceAttr = new ArrayList(); + goodsInstance.setName(names[2]); + goodsInstance.setIntro(goodsClass.getIntro()); + for(int i=0; i npcClassAttrs = npcClass.getAttrs(); + NpcInstance npcInstance = new NpcInstance(); + List npcInstanceAttr = new ArrayList(); + npcInstance.setName(names[2]); + npcInstance.setIntro(npcClass.getIntro()); + for(int i=0; i goodsInstancesByClass = this.goodsClassIndex.get(names[1]).getGoodsInstances(); + boolean goodsInstanceBoolean = false; + GoodsInstance goodsInstance = this.goodsInstanceIndex.get(names[2]); + for(int i=0; i attrs = goodsInstance.getAttrs(); + for(int j=0; j npcInstancesByClass = this.npcClassIndex.get(names[1]).getNpcInstances(); + boolean npcInstanceBoolean = false; + NpcInstance npcInstance = this.npcInstanceIndex.get(names[2]); + for(int i=0; i attrs = npcInstance.getAttrs(); + for(int j=0; j goodsInstances; + public Place getPlace() { + return place; + } + public void setPlace(Place place) { + this.place = place; + } + public List getGoodsInstances() { + return goodsInstances; + } + public void setGoodsInstances(List goodsInstances) { + this.goodsInstances = goodsInstances; + } + @Override + public String toString() { + String placeName = ""; + if(place != null) { + placeName = place.getName(); + } + int goodsInstancesSize = 0; + if(goodsInstances!=null) { + goodsInstancesSize = goodsInstances.size(); + } + return super.toString()+"NpcInstance [placeName=" + placeName + ", goodsInstancesSize=" + goodsInstancesSize + "]"; + } + +} diff --git a/src/GameEnvironment/GameEnvironment/bean/Place.java b/src/GameEnvironment/GameEnvironment/bean/Place.java new file mode 100644 index 0000000..b8065c6 --- /dev/null +++ b/src/GameEnvironment/GameEnvironment/bean/Place.java @@ -0,0 +1,61 @@ +package com.qiwu.chatbotbackend.GameEnvironment.GameEnvironment.bean; + +import java.io.Serializable; +import java.util.List; + +public class Place implements Serializable{ + private static final long serialVersionUID = 1L; + private String name; + private String intro; + private List ableGo; + private List npcInstances; + private List goodsInstances; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getIntro() { + return intro; + } + public void setIntro(String intro) { + this.intro = intro; + } + public List getAbleGo() { + return ableGo; + } + public void setAbleGo(List ableGo) { + this.ableGo = ableGo; + } + public List getNpcInstances() { + return npcInstances; + } + public void setNpcInstances(List npcInstances) { + this.npcInstances = npcInstances; + } + public List getGoodsInstances() { + return goodsInstances; + } + public void setGoodsInstances(List goodsInstances) { + this.goodsInstances = goodsInstances; + } + @Override + public String toString() { + int ableGoSize = 0; + int npcInstancesSize = 0; + int goodsInstancesSize = 0; + if(ableGo != null) { + ableGoSize = ableGo.size(); + } + if(npcInstances != null) { + npcInstancesSize = npcInstances.size(); + } + if(goodsInstances != null) { + goodsInstancesSize = goodsInstances.size(); + } + return "Place [name=" + name + ", intro=" + intro + ", ableGo=" + ableGoSize + ", npcInstances=" + npcInstancesSize + + ", goodsInstances=" + goodsInstancesSize + "]"; + } + +} diff --git a/src/GameEnvironment/GameEnvironment/bean/SuperClass.java b/src/GameEnvironment/GameEnvironment/bean/SuperClass.java new file mode 100644 index 0000000..2050c05 --- /dev/null +++ b/src/GameEnvironment/GameEnvironment/bean/SuperClass.java @@ -0,0 +1,53 @@ +package com.qiwu.chatbotbackend.GameEnvironment.GameEnvironment.bean; + +import java.io.Serializable; +import java.util.List; + +public class SuperClass implements Serializable{ + private static final long serialVersionUID = 1L; + private String name; + private String intro; + private List attrs; + private List goodsInstances; + private List npcInstances; + + public List getGoodsInstances() { + return goodsInstances; + } + public void setGoodsInstances(List goodsInstances) { + this.goodsInstances = goodsInstances; + } + public List getNpcInstances() { + return npcInstances; + } + public void setNpcInstances(List npcInstances) { + this.npcInstances = npcInstances; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getIntro() { + return intro; + } + public void setIntro(String intro) { + this.intro = intro; + } + public List getAttrs() { + return attrs; + } + public void setAttrs(List attrs) { + this.attrs = attrs; + } + @Override + public String toString() { + int goodsInstanceSize = 0; + if(goodsInstances!=null) { + goodsInstanceSize = goodsInstances.size(); + } + return "SuperClass [name=" + name + ", intro=" + intro + "goodsInstancesSize="+goodsInstanceSize+", attrs=" + attrs + "]"; + } + +} diff --git a/src/GameEnvironment/ObjectFileUtil.java b/src/GameEnvironment/ObjectFileUtil.java new file mode 100644 index 0000000..5afb444 --- /dev/null +++ b/src/GameEnvironment/ObjectFileUtil.java @@ -0,0 +1,46 @@ +package com.qiwu.chatbotbackend.GameEnvironment; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +public class ObjectFileUtil { + // obj 任意可以序列化的对象 fileAllName 文件全名 + public static boolean writeObjectToFile(Object obj, String fileAllName) { + File file =new File(fileAllName); + FileOutputStream out; + try { + out = new FileOutputStream(file); + ObjectOutputStream objOut=new ObjectOutputStream(out); + objOut.writeObject(obj); + objOut.flush(); + objOut.close(); + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + + // fileAllName 文件全名 + public static Object readObjectFromFile(String fileAllName) + { + Object temp=null; + File file =new File(fileAllName); + FileInputStream in; + try { + in = new FileInputStream(file); + ObjectInputStream objIn=new ObjectInputStream(in); + temp=objIn.readObject(); + objIn.close(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return temp; + } +} diff --git a/src/GameEnvironment/Util.java b/src/GameEnvironment/Util.java index 4c2d736..99d3413 100644 --- a/src/GameEnvironment/Util.java +++ b/src/GameEnvironment/Util.java @@ -1,4 +1,4 @@ -package GameEnvironment; +package com.qiwu.chatbotbackend.GameEnvironment; import java.io.FileInputStream; import java.io.IOException; diff --git a/src/GameEnvironment/bean/Attr.java b/src/GameEnvironment/bean/Attr.java index 75cbf54..075474d 100644 --- a/src/GameEnvironment/bean/Attr.java +++ b/src/GameEnvironment/bean/Attr.java @@ -1,4 +1,4 @@ -package GameEnvironment.bean; +package com.qiwu.chatbotbackend.GameEnvironment.bean; import java.io.Serializable; diff --git a/src/GameEnvironment/bean/GoodsInstance.java b/src/GameEnvironment/bean/GoodsInstance.java index ab368e4..dd32ffa 100644 --- a/src/GameEnvironment/bean/GoodsInstance.java +++ b/src/GameEnvironment/bean/GoodsInstance.java @@ -1,4 +1,4 @@ -package GameEnvironment.bean; +package com.qiwu.chatbotbackend.GameEnvironment.bean; import java.io.Serializable; import java.util.List; diff --git a/src/GameEnvironment/bean/NpcInstance.java b/src/GameEnvironment/bean/NpcInstance.java index 3a76cd2..0ce4777 100644 --- a/src/GameEnvironment/bean/NpcInstance.java +++ b/src/GameEnvironment/bean/NpcInstance.java @@ -1,4 +1,4 @@ -package GameEnvironment.bean; +package com.qiwu.chatbotbackend.GameEnvironment.bean; import java.io.Serializable; import java.util.List; diff --git a/src/GameEnvironment/bean/Place.java b/src/GameEnvironment/bean/Place.java index f311517..559f5ab 100644 --- a/src/GameEnvironment/bean/Place.java +++ b/src/GameEnvironment/bean/Place.java @@ -1,4 +1,4 @@ -package GameEnvironment.bean; +package com.qiwu.chatbotbackend.GameEnvironment.bean; import java.io.Serializable; import java.util.List; diff --git a/src/GameEnvironment/bean/SuperClass.java b/src/GameEnvironment/bean/SuperClass.java index c643046..54d9e5e 100644 --- a/src/GameEnvironment/bean/SuperClass.java +++ b/src/GameEnvironment/bean/SuperClass.java @@ -1,4 +1,4 @@ -package GameEnvironment.bean; +package com.qiwu.chatbotbackend.GameEnvironment.bean; import java.io.Serializable; import java.util.List; diff --git a/src/entity/ChatCache.java b/src/entity/ChatCache.java index e38b913..dcba96a 100644 --- a/src/entity/ChatCache.java +++ b/src/entity/ChatCache.java @@ -12,6 +12,17 @@ public class ChatCache { private Map infoPoint = null; private GameEnv gameEnv = null; + private boolean reloading = false; + + public boolean isReloading() { + return reloading; + } + + public void setReloading(boolean reloading) { + this.reloading = reloading; + } + + public Map getInfoPoint() { return infoPoint; diff --git a/src/entity/Condition.java b/src/entity/Condition.java index 3ebce66..6e59e99 100644 --- a/src/entity/Condition.java +++ b/src/entity/Condition.java @@ -21,6 +21,7 @@ public class Condition { public HashMap featureMap = new HashMap(); public String indexName = ""; public int id = 0; + public String configName = ""; public String status = "waiting"; //public ArrayList indexName = new ArrayList(); @@ -40,6 +41,7 @@ public class Condition { this.updateKnowledge = originCondition.updateKnowledge; this.indexName = originCondition.indexName; this.id = originCondition.id; + this.configName = originCondition.configName; this.status = originCondition.status; this.activePriority = originCondition.activePriority + basePriority; this.auxiliaryPriority = originCondition.auxiliaryPriority + baseAuxiliaryPriority; diff --git a/src/entity/JConfig.java b/src/entity/JConfig.java index e5ecb29..9a282af 100644 --- a/src/entity/JConfig.java +++ b/src/entity/JConfig.java @@ -30,8 +30,25 @@ public class JConfig { private String hisPremise = ""; + public String getFeature() { + return feature; + } + + public void setFeature(String feature) { + this.feature = feature; + } + private String feature = ""; private String reply = ""; + + public String getNextStep() { + return nextStep; + } + + public void setNextStep(String nextStep) { + this.nextStep = nextStep; + } + private String nextStep = ""; public void setConfigName(String configName) { @@ -52,6 +69,14 @@ public class JConfig { private String status = "waiting"; + public String getUpdateKnowledge() { + return updateKnowledge; + } + + public void setUpdateKnowledge(String updateKnowledge) { + this.updateKnowledge = updateKnowledge; + } + private String updateKnowledge = ""; private String nextStepStatus = "return"; @@ -285,7 +310,8 @@ public class JConfig { String text = curPremise + "|" + hisPremise + "|" + feature + "," + updateKnowledge + "," + nextStepStatus + "|" + - "auxiliaryPriority=" + auxiliaryPriority + "|=>|" + + "auxiliaryPriority=" + auxiliaryPriority + "|" + + "configName=" + configName + "|=>|" + reply + "|" + "nextStep=" + nextStep + "|" + "status=" + status + "|" + diff --git a/src/entity/UserInfo.java b/src/entity/UserInfo.java index 0e88ab0..af785eb 100644 --- a/src/entity/UserInfo.java +++ b/src/entity/UserInfo.java @@ -19,6 +19,7 @@ public class UserInfo { this.id2config = new HashMap(); this.id2condition = new HashMap(); this.varInitialMap = new HashMap(); + this.name2condition = new HashMap(); } public HashMap> conditionTable; @@ -27,6 +28,7 @@ public class UserInfo { public HashMap extraInfo; public HashMap id2config; public HashMap id2condition; + public HashMap name2condition; //存储需要初始化的变量 public HashMap varInitialMap; diff --git a/src/gameDesign/MapConstructor.java b/src/gameDesign/MapConstructor.java index cd014c5..1ae0ecd 100644 --- a/src/gameDesign/MapConstructor.java +++ b/src/gameDesign/MapConstructor.java @@ -65,7 +65,7 @@ public class MapConstructor extends ElementConstructor { try { conditionDescription.put("site", "当前地点"); - varInitialMap.put("site", "海边"); + JSONObject map = gameElements.getJSONObject("map"); Map question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class); JSONArray locations = map.getJSONArray("locations"); @@ -87,6 +87,9 @@ public class MapConstructor extends ElementConstructor { generateSurroundingPeople(); generateSurroundThings(); + + + } catch(Exception e){ operationStatus = 0; message = "地图处理出现异常"; @@ -106,6 +109,7 @@ public class MapConstructor extends ElementConstructor { conditionName + "::match-Include=" + zainaliKeywords, " ", "match", "你在{site-2}", "yaobuyao", "" ); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } @@ -132,36 +136,76 @@ public class MapConstructor extends ElementConstructor { placeB = (String) locations.getJSONObject(j).get("name"); placeKeywords = conditionKeywords.replace("地点", placeB); - if (i == j) { - jConfig = new JConfig( - conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, - "match", "你已经在" + placeB + "了", "yaobuyao", "" - ); - jConfigList.add(jConfig); - continue; - } - ; - if (pathMatirx[i][j] == 1) { - jConfig = new JConfig( - conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, - "match", placeB + "就在附近哦", "yaobuyao", "" - ); - jConfigList.add(jConfig); - - } else if (pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE) { - int k = preTable[i][j]; - pathNode = ""; - while (k != j) { - pathNode += (String) locations.getJSONObject(k).get("name"); - k = preTable[k][j]; - } - - jConfig = new JConfig( - conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, - "match", "经过" + pathNode + "就可以到达" + placeB, "yaobuyao", "" - ); - jConfigList.add(jConfig); - } + jConfig = new JConfig( + conditionName + "::match-Include=" + placeKeywords + + ",is_neighbour:"+ conditionName + "::match-Include=0", + "site::match-Include=" + placeA , + "match,return-0", + "你已经在"+placeB + "了", + " ", + "" + ); + jConfig.setNextStepStatus("return-0"); + jConfigList.add(jConfig); + + jConfig = new JConfig( + conditionName + "::match-Include=" + placeKeywords + + ",is_neighbour:"+ conditionName + "::match-Include=1", + "site::match-Include=" + placeA , + "match,return-0", + placeB + "就在旁边哦", + " ", + "" + ); + jConfig.setNextStepStatus("return-0"); + jConfigList.add(jConfig); + + jConfig = new JConfig( + conditionName + "::match-Include=" + placeKeywords + + ",is_neighbour:"+ conditionName + "::match-Include=2", + "site::match-Include=" + placeA , + "match,return-0", + "{map_reply-2}", + " ", + "map_reply:queryPathBySite:" + conditionName + ); + jConfig.setNextStepStatus("return-0"); + jConfigList.add(jConfig); + + + + + +// if (i == j) { +// jConfig = new JConfig( +// conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, +// "match", "你已经在" + placeB + "了", "yaobuyao", "" +// ); +// jConfigList.add(jConfig); +// continue; +// } +// ; +// if (pathMatirx[i][j] == 1) { +// jConfig = new JConfig( +// conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, +// "match", placeB + "就在附近哦", "yaobuyao", "" +// ); +// jConfigList.add(jConfig); +// +// } else if (pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE) { +// int k = preTable[i][j]; +// pathNode = ""; +// while (k != j) { +// pathNode += (String) locations.getJSONObject(k).get("name"); +// k = preTable[k][j]; +// } +// +// jConfig = new JConfig( +// conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, +// "match", "经过" + pathNode + "就可以到达" + placeB, "yaobuyao", "" +// ); +// jConfigList.add(jConfig); +// } } } } @@ -188,8 +232,9 @@ public class MapConstructor extends ElementConstructor { jConfig = new JConfig( conditionName + "::match-Include=" + conditionKeywords, "site::match-Include=" + placeA, - "match", "你可以去" + pathNode, "yaobuyao", "" + "match,return-0", "你可以去{map_reply-2}" + pathNode, " ", "map_reply:queryCanGoBySite:" ); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } } @@ -227,48 +272,91 @@ public class MapConstructor extends ElementConstructor { lastTaskReply = "任务线" + placeB + "最后的回答"; - if (i == j) { - jConfig = new JConfig( - conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, - "match", "你已经在" + placeB + "了", "yaobuyao", "" - ); - jConfigList.add(jConfig); - continue; - } - ; + jConfig = new JConfig( + conditionName + "::match-Include=" + placeKeywords + + ",is_neighbour:"+ conditionName + "::match-Include=0", + "site::match-Include=" + placeA , + "match,return-0", + "你已经在"+placeB + "了", + " ", + "" + ); + jConfig.setNextStepStatus("return-0"); + jConfigList.add(jConfig); + + jConfig = new JConfig( + conditionName + "::match-Include=" + placeKeywords + "," + conditionName + "::match-Include=" + placeKeywords, - //如果两点相邻 - if (pathMatirx[i][j] == 1) { - jConfig = new JConfig( - conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA + "," + lastTaskCondition + "::match-Include=null" + "," + lastTaskReply + "::match-Include=null", "match, reutrn-0", "地点尚未激活", "yaobuyao", "" - ); - jConfigList.add(jConfig); - jConfig = new JConfig( - conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, - "match", "{" + lastTaskReply + "-2}", "yaobuyao", "site:" + placeB + "-<-process-nextStep:var={" + lastTaskCondition +"-2}" - ); - jConfigList.add(jConfig); - - - //如果两点不是相邻点 - } else if (pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE) { - - - int k = preTable[i][j]; - pathNode = ""; - while (k != j) { - pathNode += (String) locations.getJSONObject(k).get("name"); - k = preTable[k][j]; - } - jConfig = new JConfig( - conditionName + "::match-Include=" + placeKeywords, - "site::match-Include=" + placeA, - "match", "从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB, "yaobuyao", "" - ); - jConfigList.add(jConfig); - } + ); + jConfig.setNextStepStatus("return-0"); + jConfigList.add(jConfig); + + + jConfig = new JConfig( + conditionName + "::match-Include=" + placeKeywords + "," + conditionName + "::match-Include=" + placeKeywords, + + "site::match-Include=" + placeA, + "match, reutrn-0", "{" + lastTaskReply + "-2}", "yaobuyao", "site:" + placeB + "-<-process-nextStep:var={" + lastTaskCondition +"-2}" + ); + jConfigList.add(jConfig); + + + + jConfig = new JConfig( + conditionName + "::match-Include=" + placeKeywords + + ",is_neighbour:"+ conditionName + "::match-Include=2", + "site::match-Include=" + placeA , + "match,return-0", + "{map_reply-2}", + " ", + "map_reply:queryPathBySite:" + conditionName + ); + jConfig.setNextStepStatus("return-0"); + jConfigList.add(jConfig); +// if (i == j) { +// jConfig = new JConfig( +// conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, +// "match", "你已经在" + placeB + "了", "yaobuyao", "" +// ); +// jConfigList.add(jConfig); +// continue; +// } +// ; +// +// //如果两点相邻 +// if (pathMatirx[i][j] == 1) { +// jConfig = new JConfig( +// conditionName + "::match-Include=" + placeKeywords, +// "site::match-Include=" + placeA + "," + lastTaskCondition + "::match-Include=null" + "," + lastTaskReply + "::match-Include=null", +// "match, reutrn-0", "地点尚未激活", "yaobuyao", "" +// ); +// jConfigList.add(jConfig); +// jConfig = new JConfig( +// conditionName + "::match-Include=" + placeKeywords, "site::match-Include=" + placeA, +// "match", "{" + lastTaskReply + "-2}", "yaobuyao", "site:" + placeB + "-<-process-nextStep:var={" + lastTaskCondition +"-2}" +// ); +// jConfigList.add(jConfig); +// +// +// //如果两点不是相邻点 +// } else if (pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE) { +// +// +// int k = preTable[i][j]; +// pathNode = ""; +// while (k != j) { +// pathNode += (String) locations.getJSONObject(k).get("name"); +// k = preTable[k][j]; +// } +// jConfig = new JConfig( +// conditionName + "::match-Include=" + placeKeywords, +// "site::match-Include=" + placeA, +// "match", "从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB, "yaobuyao", "" +// ); +// jConfigList.add(jConfig); +// } } } } @@ -281,6 +369,7 @@ public class MapConstructor extends ElementConstructor { conditionName + "::match-Include=" + conditionKeywords, " ", "match,return-0", "周围{map_query-2}。", "yaobuyao", "map_query:queryAroundNpcBySite:" ); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } @@ -292,6 +381,7 @@ public class MapConstructor extends ElementConstructor { conditionName + "::match-Include=" + conditionKeywords, " ", "match,return-0", "周围{map_query-2}。", "yaobuyao", "map_query:queryAroundGoodsBySite:" ); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } @@ -303,6 +393,7 @@ public class MapConstructor extends ElementConstructor { conditionName + "::match-Include=" + conditionKeywords, " ", "match,return-0", "周围{map_query-2}。", "yaobuyao", "map_query:queryAroundAllBySite:" ); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } diff --git a/src/gameDesign/PlotConstructor.java b/src/gameDesign/PlotConstructor.java index 9b20363..18dbab5 100644 --- a/src/gameDesign/PlotConstructor.java +++ b/src/gameDesign/PlotConstructor.java @@ -432,6 +432,8 @@ public class PlotConstructor extends ElementConstructor { List adjNodeList = null; int priority, taskStep, nextPriority, nextTaskStep, branchSize; + + root.put("nextUnknownText", varInitialMap.get("unknownText")); root.put("parentId", "null"); @@ -602,6 +604,7 @@ public class PlotConstructor extends ElementConstructor { String startPlace = (String)root.get("place"); + varInitialMap.put("site", startPlace); varInitialMap.put("任务线" + startPlace + "最后一个condition", (String)root.get("conditionName")); varInitialMap.put("任务线" + startPlace + "最后的回答", (String)jsonObject.get("plotStartText")); conditionDescription.put("任务线" + startPlace + "最后一个condition","任务线" + startPlace + "最后一个condition"); @@ -680,13 +683,23 @@ public class PlotConstructor extends ElementConstructor { extraUK = nTaskLineName + ":" + nextId + "-<-site:" + nextLocation + "-<-" + lastTaskCondition + ":var={nextStep-3}" + "-<-" + lastTaskReply + ":var={reply-3}" + "-<-" + id + ":" + "+1-<-" + nextId + ":" + "+1" + "-<-game_step:" + botName + "_plot_" + nextId; + + List curPremiseList = JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class); + jConfig = new JConfig( - JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), + curPremiseList, JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), updateKnowledge, "", extraHP, extraUK, "match", (String) branchNode.get("reply"), (String) nextCondition, "0" ); + + if (curPremiseList.size() == 0){ + jConfig.setFeature(jConfig.getFeature() + ",activate,post::all(http://simqa.sz.chewrobot.com:28081/game)"); + jConfig.setNextStep("queryskip1,queryskip2"); + jConfig.setUpdateKnowledge(jConfig.getUpdateKnowledge() + "-<-临时变量1:{nextStep-3}-<-临时变量2:{reply-3}"); + jConfig.setNextStepStatus("active"); + } jConfigList.add(jConfig); @@ -723,7 +736,7 @@ public class PlotConstructor extends ElementConstructor { if (curUnknownText.equals("")) { jConfig = new JConfig( conditionName + "::match-Include=unknown", - extraHP = "任务线_" + location + "进度::match-Include=" + parentId, + "任务线_" + location + "进度::match-Include=" + parentId, "match,return-0,post::all(" + CHITCHAT_URL + ")", " ", " ", @@ -741,12 +754,45 @@ public class PlotConstructor extends ElementConstructor { */ jConfig = new JConfig( + " ", "qa_status::match-Include=ok,qa_score::number-Smaller=90", "match", "{临时变量2-2}", " ", "process-nextStep:{临时变量1-2}" + ); + jConfig.setStatus("sleeping"); + jConfig.setConfigName("queryskip1"); + jConfig.setjStatus(1); + jConfigList.add(jConfig); + + + jConfig = new JConfig( + " ", "qa_status::match-Include=ok,qa_score::number-BiggerOrEqual=90", "match", "{common_answer-2}, {临时变量2-2}", " ", "process-nextStep:{临时变量1-2}" + ); + jConfig.setStatus("sleeping"); + jConfig.setConfigName("queryskip2"); + jConfig.setjStatus(1); + jConfigList.add(jConfig); + + jConfig = new JConfig( + " ", "qa_status::match-Include=not_found", "match", "{临时变量2-2}", " ", "process-nextStep:{临时变量1-2}" + ); + jConfig.setStatus("sleeping"); + jConfig.setConfigName("queryskip3"); + jConfig.setjStatus(1); + jConfigList.add(jConfig); + + + + + + + jConfig = new JConfig( " ", "qa_status::match-Include=not_found", "match,return-0", "{unknownText-2}", " ", "" ); jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown1"); jConfig.setjStatus(1); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); + + jConfig = new JConfig( " ", "qa_status::match-Include=not_found,unknownText::match-Include=null", "match,return-0", "不好意思,我没听懂你刚才说的话,请换个别的问题试试吧~哎呀,刚才开了小差,没理解你的意思,请换个问题吧~请根据上文信息,认真回答吧,谢谢~我们好像不在一个频道上,换个问题试试,希望我们能够接轨上吧", @@ -755,6 +801,7 @@ public class PlotConstructor extends ElementConstructor { jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown2"); jConfig.setjStatus(1); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); @@ -766,6 +813,7 @@ public class PlotConstructor extends ElementConstructor { jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown3"); jConfig.setjStatus(1); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); @@ -775,6 +823,7 @@ public class PlotConstructor extends ElementConstructor { jConfig.setStatus("sleeping"); jConfig.setConfigName("unknown4"); jConfig.setjStatus(1); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); @@ -783,6 +832,8 @@ public class PlotConstructor extends ElementConstructor { ); jConfig.setStatus("sleeping"); jConfig.setConfigName("common_answer"); + jConfig.setjStatus(1); + jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); //--------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------- @@ -792,7 +843,7 @@ public class PlotConstructor extends ElementConstructor { @Override public void postProcessing(List idList, int categoryId){ if (idList.size() != 0) { - jConfig = new JConfig(" ", " ", "activate,post::all(http://simqa.sz.chewrobot.com:28081/game)", "", idList.get(idList.size() - 5) + "," + idList.get(idList.size() - 4) + "," + idList.get(idList.size() - 3) + "," + idList.get(idList.size() - 2) + "," + idList.get(idList.size() - 1), ""); + jConfig = new JConfig(" ", " ", "activate,post::all(http://simqa.sz.chewrobot.com:28081/game)", "", "unknown4,unknown3,unknown2,unknown1,common_answer", ""); jConfig.setNextStepStatus("active"); JSONObject json = jConfig.toJson(this.getConditionDescription()); helper.insertConfigByBotId(botId, json, idList, categoryId); diff --git a/src/processing/ChatCacheController.java b/src/processing/ChatCacheController.java index 3dd3044..5837a46 100644 --- a/src/processing/ChatCacheController.java +++ b/src/processing/ChatCacheController.java @@ -106,10 +106,10 @@ public class ChatCacheController { if (!dir.exists()) dir.mkdir(); - ObjectFileUtil.writeObjectToFile(chatCache.getGameEnv(), String.format(ENV_PATH, chatKey.split("~")[0])); + ObjectFileUtil.writeObjectToFile(chatCache.getGameEnv(), String.format(ENV_PATH, chatKey.split("-")[0])); - File file = new File(String.format(INFO_PATH, chatKey.split("~")[0])); + File file = new File(String.format(INFO_PATH, chatKey.split("-")[0])); FileWriter fw = new FileWriter(file); String jsonString = JSON.toJSONString(chatCache.getInfoPoint()); @@ -124,34 +124,43 @@ public class ChatCacheController { return saveFlag; } - public static ChatCache loadChatCache(String chatKey, ServletContext context, UserInfo userInfo) throws Exception { + public static ChatCache loadChatCache(String chatKey, ServletContext context, UserInfo userInfo, Boolean loadCacheStatus) throws Exception { ChatCache chatCache = null; GameEnv gameEnv = null; Map infoPoint = null; - String key = chatKey.split("~")[0]; + String key = chatKey.split("-")[0]; String fileDir = System.getProperty("user.dir") + "/ChatCache"; String line, pair[]; Map infoPoints = new HashMap(); try { - gameEnv = (GameEnv) ObjectFileUtil.readObjectFromFile(String.format(ENV_PATH, key)); + File file = new File(String.format(ENV_PATH, key)); + if (file.exists()){ + gameEnv = (GameEnv) ObjectFileUtil.readObjectFromFile(String.format(ENV_PATH, key)); + }else{ + throw new IOException("环境存档不存在"); + } - File file = new File(String.format(INFO_PATH, key)); + file = new File(String.format(INFO_PATH, key)); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String jsonStr = br.readLine(); infoPoint = (Map)JSON.parseObject(jsonStr); // context.setAttribute(userName, ); chatCache = new ChatCache(gameEnv, infoPoint); + chatCache.setReloading(true); + + br.close(); - fr.close(); + loadCacheStatus = true; } catch (IOException e) { - e.printStackTrace(); + System.out.println(e.toString()); chatCache = new ChatCache(userInfo); + loadCacheStatus = false; } return chatCache; @@ -162,18 +171,13 @@ public class ChatCacheController { - public static synchronized ChatCache getChatCache(String chatKey, ServletContext context, UserInfo userinfo, Boolean loadCache) throws Exception { + public static synchronized ChatCache getChatCache(String chatKey, ServletContext context, UserInfo userinfo, Map receiveParamters, Boolean loadCacheStatus) throws Exception { Map chatCacheMap = null; ChatCache chatCache = null; - if(loadCache){ - loadChatCache(chatKey, context, userinfo); - } - - if (context.getAttribute("chatCache") == null) { chatCacheMap = new HashMap(); - chatCacheMap.put(chatKey, new ChatCache(userinfo)); + //chatCacheMap.put(chatKey, new ChatCache(userinfo)); context.setAttribute("chatCache", chatCacheMap); } else { chatCacheMap = (Map) context.getAttribute("chatCache"); @@ -185,7 +189,12 @@ public class ChatCacheController { try { chatCache = chatCacheMap.get(chatKey); if(chatCache == null){ - chatCache = new ChatCache(userinfo); + if(receiveParamters.containsKey("yaobuyao") && (receiveParamters.get("yaobuyao")[0].contains("开始游戏") || + receiveParamters.get("yaobuyao")[0].contains("继续游戏")|| receiveParamters.get("yaobuyao")[0].contains("开始"))) { + chatCache = loadChatCache(chatKey, context, userinfo, loadCacheStatus); + }else { + chatCache = new ChatCache(userinfo); + } chatCacheMap.put(chatKey, chatCache); } //historyInfoPoint = (Map) context.getAttribute("chat_key+"+userinfo.userName); @@ -213,14 +222,16 @@ public class ChatCacheController { Iterator it = chatCacheMap.keySet().iterator(); while (it.hasNext()) { chatKey = it.next(); - if(curChatKey.equals(chatKey)) + if(curChatKey.equals(chatKey)) { + saveChatCache(chatKey, chatCacheMap.get(chatKey)); continue; + } history = chatCacheMap.get(chatKey).getInfoPoint(); lastChatTime = Long.valueOf(history.get("lastUpdateTime")); now = System.currentTimeMillis(); if (now - lastChatTime > ChatCacheController.TIME_OUT) { chatCache = chatCacheMap.get(chatKey); - saveChatCache("1909509284", chatCache); + saveChatCache(chatKey, chatCache); it.remove(); } } diff --git a/src/processing/ConditionTableController.java b/src/processing/ConditionTableController.java index 377260a..c20501d 100644 --- a/src/processing/ConditionTableController.java +++ b/src/processing/ConditionTableController.java @@ -28,10 +28,10 @@ public class ConditionTableController { UserInfo userInfo = (UserInfo) context.getAttribute(userName); if (userInfo.id2config.containsKey(id)) { - + Condition oldCondition = userInfo.id2condition.get(id); //------------------- try { - Condition oldCondition = userInfo.id2condition.get(id); + conditionName = oldCondition.indexName; } catch (Exception e) { e.printStackTrace(); @@ -49,6 +49,7 @@ public class ConditionTableController { } } + userInfo.name2condition.remove(oldCondition.configName); userInfo.id2config.remove(id); userInfo.id2condition.remove(id); } else { @@ -64,8 +65,9 @@ public class ConditionTableController { for (int configId : idGroup) { if (userInfo.id2config.containsKey(configId)) { + Condition oldCondition = userInfo.id2condition.get(configId); try { - Condition oldCondition = userInfo.id2condition.get(configId); + conditionName = oldCondition.indexName; } catch (Exception e) { e.printStackTrace(); @@ -83,6 +85,7 @@ public class ConditionTableController { } } + userInfo.name2condition.remove(oldCondition.configName); userInfo.id2config.remove(configId); userInfo.id2condition.remove(configId); } else { @@ -115,6 +118,10 @@ public class ConditionTableController { List conditionList = new ArrayList(); userinfo.conditionTable.put(condition.indexName, conditionList); } + + if(!condition.configName.equals("")){ + userinfo.name2condition.put(condition.configName, condition); + } userinfo.conditionTable.get(condition.indexName).add(condition); userinfo.id2config.put(id, configLine); userinfo.id2condition.put(id, condition); diff --git a/src/processing/MyFunc.java b/src/processing/MyFunc.java index 2963822..edb91d5 100644 --- a/src/processing/MyFunc.java +++ b/src/processing/MyFunc.java @@ -134,7 +134,9 @@ public class MyFunc { } else { jobj.put("status", 0); + jobj.put("parameter", "修改condition失败"); } + } catch (Exception e) { e.printStackTrace(); } diff --git a/src/processing/QuestionMatcher.java b/src/processing/QuestionMatcher.java index 66ea24b..326f59d 100644 --- a/src/processing/QuestionMatcher.java +++ b/src/processing/QuestionMatcher.java @@ -331,11 +331,13 @@ public class QuestionMatcher { String keywrod = gameEnv.queryNpcAttrByNpcName(nameSeries[3], nameSeries[4]); } - }else if (actionName.startsWith("goods_")){ - if(nameSeries.length == 4){ + }else if (actionName.startsWith("goods_")) { + if (nameSeries.length == 4) { keyword = gameEnv.queryGoodsAttrByGoodsName(nameSeries[2], nameSeries[3]); System.out.println("return" + keyword); } + }else if(actionName.startsWith("is_neighbour")){ + //keyword = gameEnv.isNeighbour(historyInfoPoint.get("site"), actionName.split(":")[1]); }else { keyword = infoPoint.get(actionName); @@ -832,7 +834,7 @@ public class QuestionMatcher { } - ChatCacheController.updateChatCache4Insert(curInfoPoint, historyInfoPoint, gameEnv, context); + //context.setAttribute("chat_key+"+userinfo.botAccount,historyInfoPoint); @@ -884,7 +886,7 @@ public class QuestionMatcher { key = it.next(); value = historyInfoPoint.get(key); - if (value != null && !value.equals(" ") && !value.equals("") && !skipSet.contains(key) && !key.endsWith("最后的回答") && !key.endsWith("最后一个condition")) + if (value != null && !value.equals(" ") && !value.equals("") && !skipSet.contains(key) && !key.endsWith("最后的回答") && !key.endsWith("最后一个condition") && !key.startsWith("临时变量")) debugInfo += (" " + key + ":\'" + value + "\' "); } @@ -1205,13 +1207,18 @@ public class QuestionMatcher { Condition newCondition, originCondition; for (String configIdStr : activateList.split(",")) { - if (!StringUtils.isNumeric(configIdStr)) - continue; - configId = Integer.parseInt(configIdStr); + try { + if (StringUtils.isNumeric(configIdStr)) { + configId = Integer.parseInt(configIdStr); + configStr = userinfo.id2config.get(configId); + originCondition = userinfo.id2condition.get(configId); + } else { + originCondition = userinfo.name2condition.get(configIdStr); + } + + + configId = originCondition.id; - configStr = userinfo.id2config.get(configId); - if (configStr != null) { - originCondition = userinfo.id2condition.get(configId); newCondition = new Condition(originCondition, basePriority, baseAuxiliaryPriority); //condition = QuestionMatcher.buildCondition(condition, configStr); @@ -1223,6 +1230,10 @@ public class QuestionMatcher { activatedMap.put(configId, newCondition); activatedQueue.add(newCondition); } + + }catch (Exception e){ + e.printStackTrace(); + logWriter.record("----------激活失败:" + configIdStr, 5); } } diff --git a/src/processing/UserInfoController.java b/src/processing/UserInfoController.java index b63261b..12016d6 100644 --- a/src/processing/UserInfoController.java +++ b/src/processing/UserInfoController.java @@ -94,7 +94,6 @@ public class UserInfoController { element = line.replace("\n", "").split("\\|=\\>\\|"); premises = element[0].split("\\|"); - //System.out.println("element[0]:" + element[0]); feature = premises[2]; cur_premises = premises[0].split(","); history_premises = premises[1].split(","); @@ -102,7 +101,12 @@ public class UserInfoController { if (premises.length < 4) condition.auxiliaryPriority = 0; else - condition.auxiliaryPriority = Integer.parseInt(premises[3].split("=")[1]); + condition.auxiliaryPriority = Integer.parseInt(premises[3].split("=", 2)[1]); + + if(premises.length < 5) + condition.configName = ""; + else + condition.configName = premises[4].split("=", 2)[1]; next = element[1].split("\\|"); @@ -187,6 +191,7 @@ public class UserInfoController { userinfo.id2config.clear(); userinfo.configStr = ""; userinfo.varInitialMap.clear(); + userinfo.name2condition.clear(); } else { conditionTable = userinfo.conditionTable; @@ -200,6 +205,7 @@ public class UserInfoController { if (line.trim().equals("")) continue; line = line.replace("\n", ""); + //在用户信息中添加额外信息,如设为active的action if (line.contains("extraInfo")) { extraElements = line.split("\\|", -1); @@ -216,6 +222,9 @@ public class UserInfoController { conditionList.add(condition); conditionTable.put(condition.indexName, conditionList); if (condition.id != 0) { + if(!condition.configName.equals("")){ + userinfo.name2condition.put(condition.configName, condition); + } userinfo.id2config.put(condition.id, line); userinfo.id2condition.put(condition.id, condition); } @@ -223,9 +232,11 @@ public class UserInfoController { } } catch (ArrayIndexOutOfBoundsException e) { System.out.println("数据格式有误"); + e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); + System.out.println(line); } } diff --git a/src/processing/WorldController.java b/src/processing/WorldController.java index 1690e42..77f357e 100644 --- a/src/processing/WorldController.java +++ b/src/processing/WorldController.java @@ -89,31 +89,76 @@ public class WorldController { switch (valuePair[0]) { case "queryAroundAllBySite": sites = gameEnv.queryAroundAllBySite(historyInfoPoint.get("site")); + if(sites == null || sites.size() == 0) + returnStr = "啥都没有"; + else { + returnStr = "有"; + if(sites.size() > 0) { + for (String site : sites) + returnStr += "、" + site; + }else{ + returnStr = "啥都没有"; + } + } break; case "queryAroundNpcBySite": sites = gameEnv.queryAroundNpcBySite(historyInfoPoint.get("site")); + if(sites == null || sites.size() == 0) + returnStr = "啥都没有"; + else { + returnStr = "有"; + if(sites.size() > 0) { + for (String site : sites) + returnStr += "、" + site; + }else{ + returnStr = "啥都没有"; + } + } break; case "queryAroundGoodsBySite": sites = gameEnv.queryAroundGoodsBySite(historyInfoPoint.get("site")); + if(sites == null || sites.size() == 0) + returnStr = "啥都没有"; + else { + returnStr = "有"; + if(sites.size() > 0) { + for (String site : sites) + returnStr += "、" + site; + }else{ + returnStr = "啥都没有"; + } + } break; case "queryCanGoBySite": sites = gameEnv.queryCanGoBySite(historyInfoPoint.get("site")); + if(sites == null || sites.size() == 0) + returnStr = "没什么地方可去的,继续玩吧"; + else { + returnStr = "可以去"; + if(sites.size() > 0) { + for (String site : sites) + returnStr += "、" + site; + }else{ + returnStr = "没什么地方可去的,继续玩吧"; + } + } break; case "queryPathBySite": sites = gameEnv.queryPathBySite(historyInfoPoint.get("site"), valuePair[1]); + if(sites == null || sites.size() == 0) + returnStr = "就在附近哦"; + else { + returnStr = "通过"; + if(sites.size() > 0) { + for (String site : sites) + returnStr += "、" + site; + }else{ + returnStr = "就在附近哦"; + } + } break; } - if(sites == null || sites.size() == 0) - returnStr = "啥都没有"; - else { - returnStr = "有"; - if(sites.size() > 0) { - for (String site : sites) - returnStr += "、" + site; - }else{ - returnStr = "啥都没有"; - } - } + } }else { //增删改统一使用 gameEnv.CUD(varName, valuePair[0], valuePair[1]); diff --git a/src/servlet/GameDesignServlet.java b/src/servlet/GameDesignServlet.java index c132c71..1dc1649 100644 --- a/src/servlet/GameDesignServlet.java +++ b/src/servlet/GameDesignServlet.java @@ -19,7 +19,6 @@ import java.io.*; import java.sql.SQLException; import java.util.*; -import static java.lang.System.out; @WebServlet(name = "GameDesignServlet", urlPatterns = {"/gameDesign"}) public class GameDesignServlet extends HttpServlet { @@ -68,14 +67,14 @@ public class GameDesignServlet extends HttpServlet { JSONObject gameElements = helper.queryGameElementsByBotId(dataId); - out.println(gameElements.toJSONString()); + System.out.println(gameElements.toJSONString()); botId = (Integer)gameElements.get("botId"); botAccount = (String) gameElements.get("botAccount"); userName = (String)gameElements.get("userName"); - out.println("botId:" + botId + ",botAccount:" + botAccount); + System.out.println("botId:" + botId + ",botAccount:" + botAccount); //假设重建用户信息 if (context.getAttribute(botAccount) == null) { @@ -91,7 +90,7 @@ public class GameDesignServlet extends HttpServlet { try { flag = botInit(botId, userinfo, helper); } catch (SQLException e) { - out.println("清空旧数据失败"); + System.out.println("清空旧数据失败"); e.printStackTrace(); } @@ -135,7 +134,7 @@ public class GameDesignServlet extends HttpServlet { } if (constructor.getOperationStatus() == 1) { - out.println(moduleName); + System.out.println(moduleName); //先清空再插入 idList.clear(); helper.batchInsertVariableByBotId(botId, constructor); @@ -167,7 +166,12 @@ public class GameDesignServlet extends HttpServlet { UserInfoController.buildUserInfo(br, userinfo, true); context.setAttribute(botAccount, userinfo); ConditionTableController.writeConfigText(botAccount, userinfo); - Boolean postResult = actionRegenerate(root, gameElements, userinfo); + String postResult = actionRegenerate(root, gameElements, userinfo); + + if(!postResult.equals("success")){ + returnStatus = 0; + returnMessage = postResult; + } } helper.destroy(); @@ -191,19 +195,10 @@ public class GameDesignServlet extends HttpServlet { + PrintWriter out = response.getWriter(); + out.append(responseJSONObject.toString()); + out.flush(); - - try { - PrintWriter out = response.getWriter(); - out.append(responseJSONObject.toString()); - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (out != null) { - out.close(); - - } - } } @@ -226,13 +221,13 @@ public class GameDesignServlet extends HttpServlet { return deleteConfigFlag && deleteVariableFlag; } - public boolean actionRegenerate(JSONObject root, JSONObject gameElements, UserInfo userInfo){ + public String actionRegenerate(JSONObject root, JSONObject gameElements, UserInfo userInfo){ String startCondition; String botName = (String)gameElements.get("botName"); JSONObject returnMessage = null; JSONObject actionPostData = new JSONObject(); - out.println("root"+ root.toJSONString()); + System.out.println("root"+ root.toJSONString()); startCondition = (String)root.get("conditionName"); actionPostData.put("startCondition", startCondition); actionPostData.put("botName_yhm", (String)gameElements.get("botName")); @@ -242,7 +237,7 @@ public class GameDesignServlet extends HttpServlet { actionPostData.put("welcome", (String)gameElements.get("plotStartText")); actionPostData.put("relationKeywords", relationKeywords); - out.println(actionPostData.toJSONString()); + System.out.println(actionPostData.toJSONString()); userInfo.extraInfo.put("activeAction", startCondition); try { @@ -251,12 +246,12 @@ public class GameDesignServlet extends HttpServlet { e.printStackTrace(); } if((Integer)returnMessage.get("status") == 1) { - return true; + return "success"; }else { - out.println(returnMessage.get("parameter")); - out.println("剧情初始化失败"); + System.out.println(returnMessage.get("parameter")); + System.out.println("语义关系修改失败"); - return false; + return (String)returnMessage.get("parameter"); } } diff --git a/src/servlet/GetAnswerServlet.java b/src/servlet/GetAnswerServlet.java index 9065e9d..ef6bf5f 100644 --- a/src/servlet/GetAnswerServlet.java +++ b/src/servlet/GetAnswerServlet.java @@ -32,16 +32,12 @@ public class GetAnswerServlet extends HttpServlet { Map historyInfoPoint = null; Solution bestMatch = null; Condition ansCondition = null; - String ansJ = "", return_str = "", state = ""; - String chatKey = receiveParamters.get("chat_key")[0]; + String ansJ = "", return_str = "", state = "", chatKey = ""; String botName = receiveParamters.get("bot_name")[0]; - - chatKey = botName + "~" + chatKey; + chatKey = receiveParamters.get("chat_key")[0].split("-")[0]; receiveParamters.get("chat_key")[0] = chatKey; - - OutputStream out = response.getOutputStream(); ServletContext context = this.getServletContext(); boolean debugFlag = false; @@ -50,8 +46,6 @@ public class GetAnswerServlet extends HttpServlet { LogWriter logWriter = new LogWriter(LOG_PATH, chatKey); - - UserInfo userinfo = null; UserInfoController uiController = new UserInfoController(); try { @@ -67,16 +61,9 @@ public class GetAnswerServlet extends HttpServlet { if(userinfo != null) { //获得用户历史信息 ChatCache chatCache = null; + Boolean loadCacheStatus = Boolean.valueOf(false); try { - Boolean loadCache = false; - if(receiveParamters.containsKey("yaobuyao")) { - String startKeyword = receiveParamters.get("yaobuyao")[0]; - if (startKeyword.equals("开始游戏") || startKeyword.equals("继续游戏") || startKeyword.equals("继续") || startKeyword.equals("开始")) { - loadCache = true; - } - } - - chatCache = ChatCacheController.getChatCache(chatKey, context, userinfo, loadCache); + chatCache = ChatCacheController.getChatCache(chatKey, context, userinfo, receiveParamters, loadCacheStatus); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { @@ -88,7 +75,7 @@ public class GetAnswerServlet extends HttpServlet { * 如果读档失败,则开始新的游戏,读档失败的判断标准是除了初始化数据外没有其他任何数据 */ - if(userinfo.varInitialMap.size() - chatCache.getInfoPoint().size() == 0) { + if(!chatCache.isReloading()) { historyInfoPoint = chatCache.getInfoPoint(); @@ -114,14 +101,6 @@ public class GetAnswerServlet extends HttpServlet { } catch (ParseException e) { e.printStackTrace(); } - //清理超时的用户数据 - if (context.getAttribute("cacheUpdateTime") == null) - context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); - if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > ChatCacheController.TIME_OUT) { - ChatCacheController.updateChatCache4Delete(chatKey, context); - context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); - } - //bestMatch.setAnswer(" \n" + return_str + "\n " + bestMatch.getAnswer()); } else { bestMatch = new Solution(0, " \n" + return_str + "\n " + state, "nextStep=" + historyInfoPoint.get("currentAction")); } @@ -132,10 +111,24 @@ public class GetAnswerServlet extends HttpServlet { } matcher.infoTransfer(bestMatch, context, debugFlag); + + + ChatCacheController.updateChatCache4Insert(curInfoPoint, historyInfoPoint, chatCache.getGameEnv(), context); + //清理超时的用户数据 + if (context.getAttribute("cacheUpdateTime") == null) + context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); + if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > ChatCacheController.TIME_OUT) { + ChatCacheController.updateChatCache4Delete(chatKey, context); + context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); + } + + } else { + + System.out.println("----------读档中------------"); String nextStep = chatCache.getInfoPoint().get("currentAction"); String reply = chatCache.getInfoPoint().get("lastReply"); - bestMatch = new Solution(1, reply, "nextStep=" + nextStep); + bestMatch = new Solution(1, "--读取存档--" + reply, "nextStep=" + nextStep); } ansJ = MyFunc.toJson(bestMatch); } else {