Commit eeacf968 by hxw

c

parent 3dba26d9
package GameEnvironment;
package com.qiwu.chatbotbackend.GameEnvironment;
import db.C3p0Utils;
import com.qiwu.chatbotbackend.db.C3p0Utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
......
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<String, SuperClass> npcClassIndex;
public Map<String, NpcInstance> npcInstanceIndex;
......
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;
}
}
package com.qiwu.chatbotbackend.GameEnvironment.GameEnvironment;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class Util {
public static String getJsonString(String fullName) {
Reader reader = null;
String jsonString = "";
try {
// System.out.println("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
char[] tempchars = new char[256];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fullName));
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
// System.out.println(tempchars);
jsonString = jsonString + String.valueOf(tempchars);
// System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
jsonString = jsonString+tempchars[i];
// System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return jsonString;
}
}
package com.qiwu.chatbotbackend.GameEnvironment.GameEnvironment.bean;
import java.io.Serializable;
public class Attr implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Attr [name=" + name + ", value=" + value + "]";
}
}
package com.qiwu.chatbotbackend.GameEnvironment.GameEnvironment.bean;
import java.io.Serializable;
import java.util.List;
public class GoodsInstance extends SuperClass implements Serializable{
private static final long serialVersionUID = 1L;
private Place place;
private NpcInstance npc;
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
public NpcInstance getNpc() {
return npc;
}
public void setNpc(NpcInstance npc) {
this.npc = npc;
}
@Override
public String toString() {
String placeName = "";
if(place != null) {
placeName = place.getName();
}
String npcName = "";
if(npc != null) {
npcName = npc.getName();
}
return super.toString()+"GoodsInstance [placeName=" + placeName + ", npcName=" + npcName + "]";
}
}
package com.qiwu.chatbotbackend.GameEnvironment.GameEnvironment.bean;
import java.io.Serializable;
import java.util.List;
public class NpcInstance extends SuperClass implements Serializable{
private static final long serialVersionUID = 1L;
private Place place;
private List<GoodsInstance> goodsInstances;
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
public List<GoodsInstance> getGoodsInstances() {
return goodsInstances;
}
public void setGoodsInstances(List<GoodsInstance> 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 + "]";
}
}
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<Place> ableGo;
private List<NpcInstance> npcInstances;
private List<GoodsInstance> 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<Place> getAbleGo() {
return ableGo;
}
public void setAbleGo(List<Place> ableGo) {
this.ableGo = ableGo;
}
public List<NpcInstance> getNpcInstances() {
return npcInstances;
}
public void setNpcInstances(List<NpcInstance> npcInstances) {
this.npcInstances = npcInstances;
}
public List<GoodsInstance> getGoodsInstances() {
return goodsInstances;
}
public void setGoodsInstances(List<GoodsInstance> 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 + "]";
}
}
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<Attr> attrs;
private List<GoodsInstance> goodsInstances;
private List<NpcInstance> npcInstances;
public List<GoodsInstance> getGoodsInstances() {
return goodsInstances;
}
public void setGoodsInstances(List<GoodsInstance> goodsInstances) {
this.goodsInstances = goodsInstances;
}
public List<NpcInstance> getNpcInstances() {
return npcInstances;
}
public void setNpcInstances(List<NpcInstance> 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<Attr> getAttrs() {
return attrs;
}
public void setAttrs(List<Attr> 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 + "]";
}
}
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;
}
}
package GameEnvironment;
package com.qiwu.chatbotbackend.GameEnvironment;
import java.io.FileInputStream;
import java.io.IOException;
......
package GameEnvironment.bean;
package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable;
......
package GameEnvironment.bean;
package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable;
import java.util.List;
......
package GameEnvironment.bean;
package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable;
import java.util.List;
......
package GameEnvironment.bean;
package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable;
import java.util.List;
......
package GameEnvironment.bean;
package com.qiwu.chatbotbackend.GameEnvironment.bean;
import java.io.Serializable;
import java.util.List;
......
......@@ -12,6 +12,17 @@ public class ChatCache {
private Map<String, String> 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<String, String> getInfoPoint() {
return infoPoint;
......
......@@ -21,6 +21,7 @@ public class Condition {
public HashMap<String, String> featureMap = new HashMap<String, String>();
public String indexName = "";
public int id = 0;
public String configName = "";
public String status = "waiting";
//public ArrayList<String> indexName = new ArrayList<String>();
......@@ -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;
......
......@@ -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 + "|" +
......
......@@ -19,6 +19,7 @@ public class UserInfo {
this.id2config = new HashMap<Integer, String>();
this.id2condition = new HashMap<Integer, Condition>();
this.varInitialMap = new HashMap<String, String>();
this.name2condition = new HashMap<String, Condition>();
}
public HashMap<String, List<Condition>> conditionTable;
......@@ -27,6 +28,7 @@ public class UserInfo {
public HashMap<String, String> extraInfo;
public HashMap<Integer, String> id2config;
public HashMap<Integer, Condition> id2condition;
public HashMap<String, Condition> name2condition;
//存储需要初始化的变量
public HashMap<String, String> varInitialMap;
......
......@@ -432,6 +432,8 @@ public class PlotConstructor extends ElementConstructor {
List<String> 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<String> 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<Integer> 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);
......
......@@ -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<String ,String> infoPoint = null;
String key = chatKey.split("~")[0];
String key = chatKey.split("-")[0];
String fileDir = System.getProperty("user.dir") + "/ChatCache";
String line, pair[];
Map<String, String> infoPoints = new HashMap<String, String>();
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<String, String[]> receiveParamters, Boolean loadCacheStatus) throws Exception {
Map<String, ChatCache> chatCacheMap = null;
ChatCache chatCache = null;
if(loadCache){
loadChatCache(chatKey, context, userinfo);
}
if (context.getAttribute("chatCache") == null) {
chatCacheMap = new HashMap<String, ChatCache>();
chatCacheMap.put(chatKey, new ChatCache(userinfo));
//chatCacheMap.put(chatKey, new ChatCache(userinfo));
context.setAttribute("chatCache", chatCacheMap);
} else {
chatCacheMap = (Map<String, ChatCache>) 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<String, String>) context.getAttribute("chat_key+"+userinfo.userName);
......@@ -213,14 +222,16 @@ public class ChatCacheController {
Iterator<String> 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();
}
}
......
......@@ -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<Condition> conditionList = new ArrayList<Condition>();
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);
......
......@@ -134,7 +134,9 @@ public class MyFunc {
} else {
jobj.put("status", 0);
jobj.put("parameter", "修改condition失败");
}
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -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);
}
}
......
......@@ -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);
}
}
......
......@@ -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]);
......
......@@ -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");
}
}
......
......@@ -32,16 +32,12 @@ public class GetAnswerServlet extends HttpServlet {
Map<String, String> 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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment