Commit 0329b36b by hxw

添加自动跳转,无条件剧情允许人物移动等功能

parent ff2aa1bb
package com.qiwu.chatbotbackend.entity;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import com.qiwu.chatbotbackend.GameEnvironment.GameEnv;
import com.qiwu.chatbotbackend.processing.ChatCacheController;
import org.springframework.beans.factory.DisposableBean;
public class ChatCache {
import javax.annotation.PreDestroy;
public class ChatCache implements DisposableBean {
private String chatKey = "不知道";
private Map<String, String> infoPoint = null;
private GameEnv gameEnv = null;
......@@ -22,7 +30,9 @@ public class ChatCache {
this.reloading = reloading;
}
public String getChatKey() { return chatKey; }
public void setChatKey(String chatKey) { this.chatKey = chatKey; }
public Map<String, String> getInfoPoint() {
return infoPoint;
......@@ -40,11 +50,20 @@ public class ChatCache {
this.gameEnv = gameEnv;
}
public ChatCache(GameEnv gameEnv, Map<String, String> infoPoint){
public ChatCache(String chatKey, GameEnv gameEnv, Map<String, String> infoPoint){
this.chatKey = chatKey;
this.gameEnv = gameEnv;
this.infoPoint = infoPoint;
}
@Override
public void destroy() throws IOException {
//System.out.println("销毁:" + chatKey);
//ChatCacheController.saveChatCache(chatKey, this);
}
public ChatCache(UserInfo userInfo) throws Exception {
try {
gameEnv = new GameEnv(userInfo.botAccount);
......
......@@ -10,6 +10,8 @@ import java.util.Map;
public class JConfig implements Cloneable{
static int iterNum = 0;
private int id = 0;
private String jCurPremise = "";
private String jHisPremise = "";
......@@ -51,8 +53,14 @@ public class JConfig implements Cloneable{
private String nextStep = "";
public void setConfigName(String configName) {
this.configName = configName;
public void setConfigName(String configName, Boolean append) {
if(append) {
iterNum++;
this.configName = configName + iterNum;
}else{
this.configName = configName;
}
}
public String getConfigName() {
......@@ -65,7 +73,7 @@ public class JConfig implements Cloneable{
return curPremise;
}
private String auxiliaryPriority = "0";
private String auxiliaryPriority = "1";
private String status = "waiting";
......@@ -178,6 +186,7 @@ public class JConfig implements Cloneable{
put("return-1", "back");
put("return-0", "stay");
put("activate", "activate");
put("activate:after", "activate:after");
}
};
static int querySkipCount = 10;
......@@ -266,6 +275,7 @@ public class JConfig implements Cloneable{
String pair1[] = premise.split("=", 2);
String key = pair1[0];
String keywords = pair1[1];
String pair2[] = key.split("::");
String conditionName = pair2[0];
......@@ -367,6 +377,8 @@ public class JConfig implements Cloneable{
nextStepStatus = "return-0";
}else if(feat.equals("activate")){
nextStepStatus = "activate";
}else if(feat.equals("activate:after")){
nextStepStatus = "activate:after";
}
}
......@@ -431,18 +443,28 @@ public class JConfig implements Cloneable{
}
public static JConfig activateCopy(JConfig jConfig){
public static void activateCopy(JConfig jConfig, List<JConfig> jConfigList){
jConfig.setReply(jConfig.getReply() + "{临时变量reply-2}");
JConfig jConfig2 = (JConfig) jConfig.clone();
jConfig2.setReply(jConfig2.getReply() + ",{临时变量2-2}");
jConfig2.setUpdateKnowledge(jConfig2.getUpdateKnowledge() + "-<-process-nextStep:var={临时变量1-2}");
jConfig2.setStatus("sleeping");
jConfig2.setConfigName("queryskip" + String.valueOf(querySkipCount));
querySkipCount++;
jConfig2.setjStatus(1);
jConfig2.setAuxiliaryPriority("2");
if(jConfig.getHisPremise().equals(" "))
jConfig.setHisPremise("临时变量condition::match-Include=+null");
else
jConfig.setHisPremise(jConfig.getjHisPremise() + ",临时变量condition::match-Include=+null");
jConfig.setConfigName("map", true);
if(jConfig2.getUpdateKnowledge().equals(""))
jConfig2.setUpdateKnowledge("process-nextStep:var={临时变量condition-2}");
else
jConfig2.setUpdateKnowledge(jConfig2.getUpdateKnowledge() + "-<-process-nextStep:var={临时变量condition-2}");
jConfig2.setConfigName("map", true);
jConfigList.add(jConfig);
jConfigList.add(jConfig2);
return jConfig2;
}
......
......@@ -41,6 +41,8 @@ public class GlobalVariableConstructor extends ElementConstructor {
}
}
varInitialMap.put("botType", "game");
JSONObject script = gameElements.getJSONObject("speechcraft");
for(String key:script.keySet()){
......
......@@ -48,6 +48,7 @@ public class MapConstructor extends ElementConstructor {
keywordSet.append(jobj.get("name"));
POS.append("=" + jobj.get("name") + "|地名\r\n");
if(jobj.containsKey("nickname")){
for(String nickname:JSONArray.parseArray(locations.getJSONObject(i).getJSONArray("nickname").toJSONString(), String.class)){
if(!nickname.equals("")) {
keywordSet.append("+" + nickname);
......@@ -390,6 +391,7 @@ public class MapConstructor extends ElementConstructor {
}
public void generateSurroundThings(){
String conditionName = "know_exist_what_mohu";
String conditionKeywords = "有什么:周围+有什么:附近";
......
......@@ -129,27 +129,26 @@ public class ChatCacheController {
ChatCache chatCache = null;
GameEnv gameEnv = null;
Map<String ,String> infoPoint = null;
String key = chatKey.split("-")[0];
String fileDir = System.getProperty("user.dir") + "/ChatCache";
String line, pair[];
Map<String, String> infoPoints = new HashMap<String, String>();
try {
File file = new File(String.format(ENV_PATH, key));
File file = new File(String.format(ENV_PATH, chatKey));
if (file.exists()){
gameEnv = (GameEnv) ObjectFileUtil.readObjectFromFile(String.format(ENV_PATH, key));
gameEnv = (GameEnv) ObjectFileUtil.readObjectFromFile(String.format(ENV_PATH, chatKey));
}else{
throw new IOException("环境存档不存在");
}
file = new File(String.format(INFO_PATH, key));
file = new File(String.format(INFO_PATH, chatKey));
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 = new ChatCache(chatKey, gameEnv, infoPoint);
chatCache.setReloading(true);
br.close();
......@@ -281,26 +280,34 @@ public class ChatCacheController {
context.setAttribute("chatCache", keyCacheMap);
}
// public static synchronized String saveChatCache(String chatKey, ChatCache chatCache){
//
//
// if(chatCache == null){
// return "chat_key不存在或者已失效";
// }else {
// String jChatCache = JSON.toJSONString(chatCache);
// System.out.println(jChatCache);
//
//
//
// chatCache = JSON.parseObject(jChatCache,ChatCache.class);
// System.out.println(chatCache.getInfoPoint().size());
//
// return "成功";
// }
//
//
//
// }
public static synchronized boolean findChatCache(String chatKey, ServletContext context) throws Exception {
ChatCache chatCache = null;
GameEnv gameEnv = null;
Map<String ,String> infoPoint = null;
String fileDir = System.getProperty("user.dir") + "/ChatCache";
String line, pair[];
Map<String, String> infoPoints = new HashMap<String, String>();
try {
if (context.getAttribute("chatCache") != null) {
if (((HashMap<String, ChatCache>)context.getAttribute("chatCache")).get(chatKey) != null){
return true;
}
}
File file = new File(String.format(ENV_PATH, chatKey));
if (file.exists()){
return true;
}
} catch(Exception e){
e.printStackTrace();
return false;
}
return false;
}
}
......@@ -59,7 +59,7 @@ public class FormsPoster {
key = k_iter.next();
if (historyInfoPoint.get(key) != "")
//infoMap.put(key + "_his", historyInfoPoint.get(key));
if (key.equals("game_step"))
if (key.equals("game_step") || key.equals("qa_threshold"))
pairList.add(new BasicNameValuePair(key, historyInfoPoint.get(key)));
else if(skipSet.contains(key)){}
else
......@@ -81,14 +81,17 @@ public class FormsPoster {
historyInfoPoint.put("process", (String) jobj.get(keyName));
} else if (keyName.equals("solution")) {
ans.setAnswer((String) jobj.get(keyName));
} else if (keyName.equals("status")){
} else if (keyName.equals("status") || keyName.equals("qa_candidates")){
continue;
} else{
historyInfoPoint.put(keyName, jobj.getString(keyName));
}
}
}else {
ans.setAnswer("提交表单出现异常");
if(URL.equals("http://simqa.sz.chewrobot.com:28081/game"))
ans.setAnswer("qa_threshold, game_step, query_text 看看少了哪个");
else
ans.setAnswer("提交表单出现异常");
}
......@@ -169,7 +172,7 @@ public class FormsPoster {
String key, keyName;
Iterator<String> k_iter = null;
List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
String threshold = "0.9";
String threshold = "50";
if (historyInfoPoint.get("qa_threshold") != null)
threshold = historyInfoPoint.get("qa_threshold");
......@@ -199,7 +202,7 @@ public class FormsPoster {
Matcher m = findTalker.matcher(textscore.get(0));
if(m.find()) {
npc = m.group(1);
if (worldController.worldOperation(gameEnv, "NPC_query", "queryLocationByNPCName:", curInfoPoint, historyInfoPoint).equals(site)){
if (worldController.worldOperation(gameEnv, "NPC_query", "queryLocationByNPCName:" + npc, curInfoPoint, historyInfoPoint).equals(site)){
historyInfoPoint.put("common_answer", textscore.get(0));
historyInfoPoint.put("qa_score", textscore.get(1));
break;
......
package com.qiwu.chatbotbackend.processing;
import com.qiwu.chatbotbackend.GameEnvironment.GameEnv;
import com.qiwu.chatbotbackend.entity.Condition;
import com.qiwu.chatbotbackend.entity.Solution;
import com.qiwu.chatbotbackend.entity.UserInfo;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.qiwu.chatbotbackend.processing.FormsPoster.TALKER;
public class ResponseFilter {
static String filt(Map<String ,String> curInfoPoint, Map<String ,String> historyInfoPoint, GameEnv gameEnv, WorldController worldController,
String reply, UserInfo userInfo, Condition ansCondition, LogWriter logWriter){
String botType = userInfo.varInitialMap.get("botType"), npc, finalUnknownText;
if(botType == null){
int randNum = (int) (Math.random() * ansCondition.answer.length);
return ansCondition.answer[randNum];
}else if (botType.equals("game")){
String site = historyInfoPoint.get("site");
String answer[] = reply.split("~");
int randNum = (int) (Math.random() * answer.length);
reply = answer[randNum];
if(reply.startsWith("【" + historyInfoPoint.get("智能助手"))){
return reply;
}else if (sameLocation(reply, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter)){
return reply;
}
if(userInfo.varInitialMap.get("unknownTextList") != null){
for(String ans:userInfo.varInitialMap.get("unknownTextList").split("~")){
if (sameLocation(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter)){
return ans;
}
}
}
if(userInfo.varInitialMap.get("finalUnknownText") != null){
finalUnknownText = userInfo.varInitialMap.get("finalUnknownText");
if (sameLocation(finalUnknownText, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter)){
return finalUnknownText;
}
}
}
return "";
}
static Boolean sameLocation(String reply, Map<String ,String> curInfoPoint, Map<String ,String> historyInfoPoint, GameEnv gameEnv, WorldController worldController, String site, LogWriter logWriter){
String npc;
Pattern findTalker = Pattern.compile(TALKER);
Matcher m = findTalker.matcher(reply);
if(m.find()) {
npc = m.group(1);
if (worldController.worldOperation(gameEnv, "NPC_query", "queryLocationByNPCName:" + npc, curInfoPoint, historyInfoPoint).equals(site)){
return true;
}{
logWriter.record(npc +"不在" + site + "跳过",1 );
}
}else{
logWriter.record(reply + "没有npc名,直接选中",1 );
return true;
}
return false;
}
}
package com.qiwu.chatbotbackend.servlet;
import com.alibaba.fastjson.JSONObject;
import com.qiwu.chatbotbackend.processing.ChatCacheController;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "FindCacheServlet", urlPatterns = {"/findCache"})
public class FindCacheServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
request.setCharacterEncoding("UTF-8");
String chatKey = request.getParameter("client_id");
Boolean exist = false;
try {
exist = ChatCacheController.findChatCache(chatKey, context);
} catch (Exception e) {
e.printStackTrace();
}
JSONObject responseJSONObject = new JSONObject();
responseJSONObject.put("exist", exist);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
response.setHeader("Access-Control-Allow-Origin", "*");
System.out.println("xx:" + responseJSONObject.toJSONString());
PrintWriter out = response.getWriter();
out.append(responseJSONObject.toString());
out.flush();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
......@@ -39,15 +39,6 @@ public class GameDesignServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> enum1 = request.getHeaderNames();
while(enum1.hasMoreElements()) {
System.out.println(enum1.nextElement());
}
//所有需要修改的词性
StringBuffer POS = new StringBuffer();
......@@ -120,7 +111,7 @@ public class GameDesignServlet extends HttpServlet {
switch (moduleName) {
case "map":
moduleName2 = "地图";
constructor = new MapConstructor(helper, botId, skipCount, botAccount, exceptRelation, POS);
constructor = new MapConstructor2(helper, botId, skipCount, botAccount, exceptRelation, POS);
constructor.construct(gameElements, userinfo);
break;
case "plots":
......@@ -270,7 +261,7 @@ public class GameDesignServlet extends HttpServlet {
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if((Integer)returnMessage.get("status") == 1) {
if((Integer)returnMessage.get("post_status") == 1 && (Integer)returnMessage.get("status") == 1 ) {
return "success";
}else {
System.out.println(returnMessage.get("parameter"));
......
package com.qiwu.chatbotbackend.servlet;
import com.qiwu.chatbotbackend.entity.*;
import com.qiwu.chatbotbackend.gameDesign.PlotConstructor;
import com.qiwu.chatbotbackend.processing.*;
import javax.servlet.ServletContext;
......@@ -35,6 +36,8 @@ public class GetAnswerServlet extends HttpServlet {
String ansJ = "", return_str = "", state = "", chatKey = "";
String botName = receiveParamters.get("bot_name")[0];
//如果有client_id,会用@分隔
if(!receiveParamters.get("chat_key")[0].startsWith("@"))
chatKey = receiveParamters.get("chat_key")[0].split("@")[0];
else
......@@ -97,7 +100,7 @@ public class GetAnswerServlet extends HttpServlet {
try {
bestMatch = matcher.getSolution(ansCondition, context, userinfo);
if (ansCondition.featureMap.containsKey("activate") && ansCondition.featureMap.containsKey("post")) {
if ((ansCondition.featureMap.containsKey("activate") && ansCondition.featureMap.containsKey("post")) || ansCondition.featureMap.containsKey("activate:after")) {
//如果当前condition包含激活和提交表单两个操作,那么在所有操作操作完成之后会再激活一次待激活列表,简称后激活阶段
bestMatch = matcher.postActivate(ansCondition, context, userinfo);
}
......@@ -113,6 +116,8 @@ public class GetAnswerServlet extends HttpServlet {
matcher.appendDebugInfo(bestMatch, context);
}
//if(userinfo.varInitialMap.get("infoTransferTime") != null && userinfo.varInitialMap.get("infoTransferTime").equals("after"))
matcher.infoTransfer(bestMatch, context, debugFlag);
......@@ -129,8 +134,10 @@ public class GetAnswerServlet extends HttpServlet {
} else {
System.out.println("----------读档中------------");
String nextStep = chatCache.getInfoPoint().get("currentAction");
String reply = chatCache.getInfoPoint().get("lastReply");
String site = chatCache.getInfoPoint().get("site");
String nextStep = chatCache.getInfoPoint().get(String.format(PlotConstructor.TASK_CONDITION, site));
String reply = chatCache.getInfoPoint().get(String.format(PlotConstructor.TASK_REPLY, site));
bestMatch = new Solution(1, "--读取存档--" + reply, "nextStep=" + nextStep);
}
ansJ = MyFunc.toJson(bestMatch);
......
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