Commit 0a43d064 by hxw

添加激活多个剧情

parent 3d56c4c5
......@@ -18,7 +18,7 @@ public class ChatCache implements DisposableBean {
private Map<String, String> infoPoint = null;
private GameEnv gameEnv = null;
private boolean reloading = false;
private boolean gameStart = false;
public String getBotAccount() {
......@@ -29,9 +29,6 @@ public class ChatCache implements DisposableBean {
this.botAccount = botAccount;
}
public boolean isReloading() {
return reloading;
}
public String getUserKey() {
return userKey;
......@@ -49,10 +46,6 @@ public class ChatCache implements DisposableBean {
this.randKey = randKey;
}
public void setReloading(boolean reloading) {
this.reloading = reloading;
}
public Map<String, String> getInfoPoint() {
return infoPoint;
}
......@@ -61,6 +54,14 @@ public class ChatCache implements DisposableBean {
this.infoPoint = infoPoint;
}
public boolean isGameStart() {
return gameStart;
}
public void setGameStart(boolean gameStart) {
this.gameStart = gameStart;
}
public GameEnv getGameEnv() {
return gameEnv;
}
......
......@@ -121,7 +121,9 @@ public class CharacterConstructor extends ElementConstructor {
}
}
keywordMap.put("onlineModeTalkers", onlineModeTalkers);
varInitialMap.put("onlineModeTalkers", onlineModeTalkers.toString());
//System.out.println("onlineModeTalkers" + varInitialMap.get("onlineModeTalkers"));
conditionDescription.put("onlineModeTalkers", "所有在线npc");
//根据根据NPC的名字查询地点
......
......@@ -37,17 +37,26 @@ public class MapConstructor extends ElementConstructor {
JSONObject jobj = null;
int locationId, index;
String locationName;
String locationName, taskLineName = "";
//ArrayList<ArrayList<Integer>> matrix= new ArrayList<ArrayList<Integer>>();
//ArrayList<Integer> vec1d = new ArrayList<Integer>();
locationNum = locations.size();
StringBuffer nameSet = null;
keywordMap.putIfAbsent("site_list", new StringBuffer());
for (int i = 0; i < locationNum; i++) {
jobj = locations.getJSONObject(i);
jobj.put("index", i);
locationName = (String) jobj.get("name");
keywordMap.get("site_list").append("+").append(locationName);
taskLineName = String.format(PlotConstructor.TASK_LINE, locationName);
if (!varInitialMap.containsKey(taskLineName)) {
conditionDescription.put(taskLineName, taskLineName);
varInitialMap.put(taskLineName, "");
}
nameSet = new StringBuffer();
nameSet.append(locationName);
......@@ -211,8 +220,6 @@ public class MapConstructor extends ElementConstructor {
/**
* 生成‘怎么去哪里’所需要的配置文件
*/
......@@ -220,66 +227,60 @@ public class MapConstructor extends ElementConstructor {
String conditionName = "want_know_zenmezuo";
StringBuffer placeKeywords = new StringBuffer();
String placeA, placeB, descriptionB, text, pathNode;
String nameSet;
for (int i = 0; i < locationNum; i++) {
placeA = (String) locations.getJSONObject(i).get("nameSet");
placeKeywords.append("+" + placeA);
nameSet = (String) locations.getJSONObject(i).get("nameSet");
placeKeywords.append("+").append(nameSet);
}
for (int j = 0; j < locationNum; j++) {
placeB = (String) locations.getJSONObject(j).get("nameSet");
jConfig = new JConfig(
conditionName + "::match-Include=" + placeKeywords
+ ",isNeighbour:"+ conditionName + "::match-Include=0",
"site::match-Include=" + placeB ,
"match,return-0",
"",
" ",
"map_query:queryPathAt:"
);
jConfig.setNextStepStatus("return-0");
jConfig.setAuxiliaryPriority("1");
JConfig.activateCopy(jConfig, jConfigList, "map");
jConfig = new JConfig(
conditionName + "::match-Include=" + placeKeywords
+ ",isNeighbour:"+ conditionName + "::match-Include=0",
" ",
"match,return-0",
"",
" ",
"map_query:queryPathAt:"
);
jConfig.setNextStepStatus("return-0");
jConfig.setAuxiliaryPriority("1");
JConfig.activateCopy(jConfig, jConfigList, "map");
jConfig = new JConfig(
conditionName + "::match-Include=" + placeKeywords
+ ",isNeighbour:"+ conditionName + "::match-Include=1+3+4",
" " ,
"match,return-0",
"",
" ",
"map_query:queryPathAdjoin:"
);
jConfig.setNextStepStatus("return-0");
jConfig.setAuxiliaryPriority("1");
JConfig.activateCopy(jConfig, jConfigList, "map");
jConfig = new JConfig(
conditionName + "::match-Include=" + placeKeywords
+ ",isNeighbour:"+ conditionName + "::match-Include=1+3+4",
"site::match-Include=" + placeB ,
"match,return-0",
"",
" ",
"map_query:queryPathAdjoin:"
);
jConfig.setNextStepStatus("return-0");
jConfig.setAuxiliaryPriority("1");
JConfig.activateCopy(jConfig, jConfigList, "map");
jConfig = new JConfig(
conditionName + "::match-Include=" + placeKeywords
+ ",isNeighbour:"+ conditionName + "::match-Include=2",
" " ,
"match,return-0",
"",
" ",
"map_query:queryHowToPathBySite:" + conditionName
);
jConfig.setNextStepStatus("return-0");
jConfig.setAuxiliaryPriority("1");
JConfig.activateCopy(jConfig, jConfigList, "map");
jConfig = new JConfig(
conditionName + "::match-Include=" + placeKeywords
+ ",isNeighbour:"+ conditionName + "::match-Include=2",
"site::match-Include=" + placeB ,
"match,return-0",
"",
" ",
"map_query:queryHowToPathBySite:" + conditionName
);
jConfig.setNextStepStatus("return-0");
jConfig.setAuxiliaryPriority("1");
JConfig.activateCopy(jConfig, jConfigList, "map");
}
}
......
......@@ -235,7 +235,7 @@ public class FormsPoster {
}
} else {
logWriter.record(textscore + "没找到npc名字", 1);
historyInfoPoint.put("common_answer", ResponseFilter.appendTalker((String)textscore.get(0), curInfoPoint, historyInfoPoint, gameEnv, worldController));
historyInfoPoint.put("common_answer", ResponseFilter.appendTalker((String)textscore.get(0), historyInfoPoint, gameEnv, worldController));
historyInfoPoint.put("qa_score", String.valueOf(textscore.get(1)));
break;
}
......
......@@ -55,7 +55,7 @@ public class QuestionMatcher {
/**
* 用迭代器迭代当前信息点中的非空关键字,由conditionName索引到状态表中保存包含conditionName这个条件的列表,并查询状态表中的条件是否满足及其优先级,满足条件且优先级更高就设为bestMatch
* 用迭代器迭代当前信息点中的非空关键字,由conditionName索引到状态表中保存的包含conditionName这个条件的列表,并查询状态表中的条件是否满足,优先级是否更高,满足条件且优先级更高就设为bestMatch
*
* @param conditionTable 满足什么条件,就回答什么,下一步跳什么
* @return bestMatch 状态表中满足条件最多的一条
......
......@@ -5,9 +5,7 @@ import com.qiwu.chatbotbackend.entity.Condition;
import com.qiwu.chatbotbackend.entity.UserInfo;
import com.qiwu.chatbotbackend.gameDesign.PlotConstructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -31,7 +29,7 @@ public class ResponseFilter {
reply = MyFunc.randStr(reply.split("~"));
if(reply.endsWith("开始游戏或者继续游戏")){
if(reply.endsWith("请说开始剧情或者继续剧情")){
return reply;
}
......@@ -44,7 +42,7 @@ public class ResponseFilter {
}else if (sameLocation(reply, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 1){
return reply;
}else if (sameLocation(reply, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 0 && !reply.equals("")){
return appendTalker(reply,curInfoPoint, historyInfoPoint, gameEnv, worldController);
return appendTalker(reply, historyInfoPoint, gameEnv, worldController);
}else if (reply.equals("") && (
(historyInfoPoint.get("common_answer") != null && !historyInfoPoint.get("common_answer").equals("")))
|| (historyInfoPoint.get("map_query") != null && !historyInfoPoint.get("map_query").equals(""))
......@@ -63,50 +61,51 @@ public class ResponseFilter {
String lastTalker = "";
//如果作者当前地点和剧情所在地点相同,那么回答unknown文本的人必须是上个剧情中的人
if(historyInfoPoint.get("game_step").endsWith(historyInfoPoint.get(String.format(PlotConstructor.TASK_LINE, site)))){
Matcher m = findTalker(historyInfoPoint.get(String.format(PlotConstructor.TASK_REPLY, site)));
while(m.find()) {
//回答unknown文本的npc必须处于在线模式
npc = m.group(1);
if(historyInfoPoint.get("onlineModeTalkers").contains("," + npc + ",")) {
lastTalker = npc;
}else{
System.out.println(npc + "是离线模式");
}
}
for (String ans : userInfo.varInitialMap.get("unknownTextList").split("~")) {
if (ans.equals(""))
continue;
if (ans.contains(lastTalker) &&sameLocation(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 1) {
unknownList.add(ans);
//return ans;
} else if (sameLocation(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 0) {
unknownList.add(appendTalker(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController));
}
}
if (unknownList.size() > 0) {
return MyFunc.randStr(unknownList);
}
}else {
// if(historyInfoPoint.get("game_step").endsWith(historyInfoPoint.get(String.format(PlotConstructor.TASK_LINE, site)))){
// Matcher m = findTalker(historyInfoPoint.get(String.format(PlotConstructor.TASK_REPLY, site)));
// while(m.find()) {
// //回答unknown文本的npc必须处于在线模式
// npc = m.group(1);
// if(isOnlineMode(npc, historyInfoPoint)) {
// lastTalker = npc;
// }else{
// System.out.println(npc + "是离线模式");
// }
// }
//
// for (String ans : userInfo.varInitialMap.get("unknownTextList").split("~")) {
// if (ans.equals(""))
// continue;
// if (ans.contains(lastTalker) &&sameLocation(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 1) {
//
// if(isOnlineText(ans, historyInfoPoint))
// unknownList.add(ans);
// //return ans;
// } else if (sameLocation(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 0) {
// unknownList.add(appendTalker(ans, historyInfoPoint, gameEnv, worldController));
// }
// }
// if (unknownList.size() > 0) {
// return MyFunc.randStr(unknownList);
// }
//
// }else {
for (String ans : userInfo.varInitialMap.get("unknownTextList").split("~")) {
if (ans.equals(""))
continue;
if (sameLocation(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 1) {
unknownList.add(ans);
if(isOnlineText(ans, historyInfoPoint))
unknownList.add(ans);
//return ans;
} else if (sameLocation(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 0) {
unknownList.add(appendTalker(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController));
unknownList.add(appendTalker(ans, historyInfoPoint, gameEnv, worldController));
}
}
if (unknownList.size() > 0) {
return MyFunc.randStr(unknownList);
}
}
// }
}
if(userInfo.varInitialMap.get("finalUnknownText") != null){
......@@ -118,10 +117,11 @@ public class ResponseFilter {
if(ans.equals(""))
continue;
if (sameLocation(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 1){
finalUnknownList.add(ans);
if(isOnlineText(ans, historyInfoPoint))
finalUnknownList.add(ans);
//return ans;
}else if(sameLocation(ans, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 0){
finalUnknownList.add(appendTalker(ans,curInfoPoint, historyInfoPoint, gameEnv, worldController));
finalUnknownList.add(appendTalker(ans, historyInfoPoint, gameEnv, worldController));
}
}
if(finalUnknownList.size() > 0){
......@@ -148,7 +148,7 @@ public class ResponseFilter {
* 查看回答文本的说话这是否在当前地点
* 0:没有说话者
* 1:说话者在当前地点
* 2:说话者不在当前地点
* 2:说话者不在当前地点或者不能说话
* @param reply
* @param curInfoPoint
* @param historyInfoPoint
......@@ -158,9 +158,6 @@ public class ResponseFilter {
* @param logWriter
* @return
*/
static Integer sameLocation(String reply, Map<String ,String> curInfoPoint, Map<String ,String> historyInfoPoint, GameEnv gameEnv, WorldController worldController, String site, LogWriter logWriter){
String npc, location;
......@@ -190,17 +187,50 @@ public class ResponseFilter {
}
static String appendTalker(String reply, Map<String ,String> curInfoPoint, Map<String ,String> historyInfoPoint, GameEnv gameEnv, WorldController worldController){
/**
* 在头部没有【%s@%s】的文本加上【%s@%s】
* @param reply
* @param historyInfoPoint
* @param gameEnv
* @param worldController
* @return
*/
static String appendTalker(String reply, Map<String ,String> historyInfoPoint, GameEnv gameEnv, WorldController worldController){
List<String> npcList = gameEnv.queryAroundNpcBySite(historyInfoPoint.get("site"));
String npc = "";
if(npcList.size() != 0)
npc = MyFunc.randStr(npcList);
List<String> filtedNpcList = new ArrayList<String>();
for(String npcName:npcList){
if(isOnlineMode(npcName, historyInfoPoint)) {
filtedNpcList.add(npcName);
}else{
System.out.println(npcName + "是离线模式");
}
}
if(filtedNpcList.size() != 0)
npc = MyFunc.randStr(filtedNpcList);
else
npc = historyInfoPoint.get(PlotConstructor.ASSISTANT);
return "【" + npc + "@0】" + reply;
return String.format(TALKER_PATTERN, npc, PlotConstructor.ASSISTANT_EMOTION) + reply;
}
static Boolean isOnlineMode(String npc, Map<String, String> historyInfoPoint){
return historyInfoPoint.get("onlineModeTalkers").contains("," + npc + ",");
}
static Boolean isOnlineText(String text, Map<String, String> historyInfoPoint){
Matcher m = findTalker(text);
String npc = "";
if(m.find()) {
npc = m.group(1);
return historyInfoPoint.get("onlineModeTalkers").contains("," + npc + ",");
}else{
System.out.println("竟然没找到npc");
return false;
}
}
}
package com.qiwu.chatbotbackend.servlet;
import com.alibaba.fastjson.JSONObject;
import com.qiwu.chatbotbackend.processing.ChatCacheController2;
import com.qiwu.chatbotbackend.processing.ChatCacheController;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
......@@ -24,7 +24,7 @@ public class FindCacheServlet extends HttpServlet {
Boolean exist = false;
try {
exist = ChatCacheController2.findChatCache(userKey, context, botAccount);
exist = ChatCacheController.findChatCache(userKey, context, botAccount);
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -133,15 +133,15 @@ public class GameDesignServlet extends HttpServlet {
switch (moduleName) {
case "map":
moduleName2 = "地图";
constructor = new MapConstructor2(helper, botId, botAccount, keywordMap, exceptRelation, POS);
constructor = new MapConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS);
constructor.construct(gameElements, userinfo);
break;
case "plots":
moduleName2 = "剧情";
constructor = new PlotConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS);
constructor = new PlotConstructor2(helper, botId, botAccount, keywordMap, exceptRelation, POS);
constructor.construct(gameElements, userinfo);
root = ((PlotConstructor) constructor).getId2j().get((String) gameElements.get("start"));
root = ((PlotConstructor2) constructor).getId2j().get((String) gameElements.get("start"));
break;
case "goods":
moduleName2 = "物品";
......
......@@ -91,7 +91,7 @@ public class GetAnswerServlet extends HttpServlet {
Boolean loadCacheStatus = Boolean.valueOf(false);
try {
//chatCache = ChatCacheController.getChatCache2(userKey, randKey, context, userinfo, receiveParamters, loadCacheStatus);
chatCache = ChatCacheController2.getChatCache(userKey, randKey, context, userinfo, receiveParamters, loadCacheStatus);
chatCache = ChatCacheController.getChatCache(userKey, randKey, context, userinfo, receiveParamters, loadCacheStatus);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
......@@ -158,12 +158,12 @@ public class GetAnswerServlet extends HttpServlet {
matcher.infoTransfer(bestMatch, context, debugFlag);
ChatCacheController2.updateChatCache4Insert(curInfoPoint, chatCache.getInfoPoint(), chatCache.getGameEnv(), context, userinfo);
ChatCacheController.updateChatCache4Insert(curInfoPoint, chatCache.getInfoPoint(), chatCache.getGameEnv(), context, userinfo);
//清理超时的用户数据
if (context.getAttribute("cacheUpdateTime") == null)
context.setAttribute("cacheUpdateTime", System.currentTimeMillis());
if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > ChatCacheController2.TIME_OUT) {
ChatCacheController2.updateChatCache4Delete(chatKey, context, userinfo);
if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > ChatCacheController.TIME_OUT) {
ChatCacheController.updateChatCache4Delete(chatKey, context, userinfo);
context.setAttribute("cacheUpdateTime", System.currentTimeMillis());
}
......
......@@ -2,7 +2,7 @@ package com.qiwu.chatbotbackend.servlet;
import com.qiwu.chatbotbackend.db.SqlHelper;
import com.qiwu.chatbotbackend.entity.ChatCache;
import com.qiwu.chatbotbackend.processing.ChatCacheController2;
import com.qiwu.chatbotbackend.processing.ChatCacheController;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
......@@ -38,7 +38,10 @@ public class MyListener implements ServletContextListener {
try {
helper = new SqlHelper();
System.out.println(String.format("saving %s", chatKey));
ChatCacheController2.saveChatCache(cache, helper);
//当用户id和随机对话id不同时,表示用户为登录状态,用户登录且开始游戏,则可以存档
if(cache != null && !cache.getRandKey().equals(cache.getUserKey()) && cache.isGameStart()) {
ChatCacheController.saveChatCache(cache, helper);
}
}catch (Exception e){
System.out.println(String.format("保存%s时出现异常", chatKey));
}
......
......@@ -2,7 +2,7 @@ package com.qiwu.chatbotbackend.servlet;
import com.alibaba.fastjson.JSONObject;
import com.qiwu.chatbotbackend.entity.ChatCache;
import com.qiwu.chatbotbackend.processing.ChatCacheController2;
import com.qiwu.chatbotbackend.processing.ChatCacheController;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
......@@ -27,8 +27,8 @@ public class SaveChatCacheServlet extends HttpServlet {
ChatCache chatCache = chatCacheMap.get(chatKey);
if(chatCache != null && !chatCache.getRandKey().equals(chatCache.getUserKey()) && chatCache.isReloading()) {
flag = ChatCacheController2.saveChatCache(chatCache, null);
if(chatCache != null && !chatCache.getRandKey().equals(chatCache.getUserKey()) && chatCache.isGameStart()) {
flag = ChatCacheController.saveChatCache(chatCache, null);
chatCacheMap.remove(chatKey);
}
......
......@@ -5,7 +5,6 @@ import com.qiwu.chatbotbackend.db.SqlHelper;
import com.qiwu.chatbotbackend.entity.*;
import com.qiwu.chatbotbackend.gameDesign.PlotConstructor;
import com.qiwu.chatbotbackend.processing.ChatCacheController;
import com.qiwu.chatbotbackend.processing.ChatCacheController2;
import com.qiwu.chatbotbackend.processing.LogWriter;
import com.qiwu.chatbotbackend.processing.WorldController;
......@@ -88,6 +87,8 @@ public class GameBotUtil extends BasicBotUtil{
}
}
//将自动跳转文本、qa文本、查询文本拼接
String queryList[] = { "plot_jump_text" ,"common_answer","map_query", "goods_query", "npc_query"};
String queryResult;
for(String queryName:queryList){
......@@ -215,13 +216,13 @@ public class GameBotUtil extends BasicBotUtil{
if(ansCondition.featureMap.containsKey("newGame")) {
System.out.println("newGame");
this.chatCache.setReloading(true);
this.chatCache.setGameStart(true);
}else if (ansCondition.featureMap.containsKey("gameBackup")) {
System.out.println("gameBackup");
if(! userKey.equals(randKey)) { //两者不同时表示用户已登录,才有权限存档
chatCache = new ChatCache(userKey, randKey, gameEnv, historyInfoPoint);
flag = ChatCacheController2.saveChatCache(chatCache, null);
flag = ChatCacheController.saveChatCache(chatCache, null);
if(flag)
solution.setAnswer(String.format(TALKER_PATTERN, historyInfoPoint.get(ASSISTANT), ASSISTANT_EMOTION) + "存档成功");
else
......@@ -235,6 +236,7 @@ public class GameBotUtil extends BasicBotUtil{
System.out.println("loadLastArchive");
ArchiveInfo archiveInfo = helper.queryUserLastGameArchive(userKey);
loading(archiveInfo, solution);
this.chatCache.setGameStart(true);
} else if (ansCondition.featureMap.containsKey("loadArchiveBySite")) {
System.out.println("loadArchiveBySite");
......@@ -243,6 +245,7 @@ public class GameBotUtil extends BasicBotUtil{
System.out.println("要读取的存档的地点是:" + site);
ArchiveInfo archiveInfo = helper.queryUserLastGameArchiveBySite(userKey, site);
loading(archiveInfo, solution);
this.chatCache.setGameStart(true);
}else{
System.out.println("没有识别到" + LOAD_BY_SITE_CONDITION);
}
......@@ -261,9 +264,14 @@ public class GameBotUtil extends BasicBotUtil{
String keyword = "", site;
ChatCache loadCache = null;
//如果存在读档,就要将自动跳转和qa的临时数据清空
historyInfoPoint.put("plot_jump_text", "");
historyInfoPoint.put("common_answer", "");
if (archiveInfo != null) {
try {
loadCache = ChatCacheController2.loadChatCache(archiveInfo);
loadCache = ChatCacheController.loadChatCache(archiveInfo);
} catch (Exception e) {
System.out.println("从本地加载存档失败");
solution.setAnswer("从本地加载存档失败");
......@@ -271,8 +279,6 @@ public class GameBotUtil extends BasicBotUtil{
}
this.chatCache.setInfoPoint(loadCache.getInfoPoint());
this.chatCache.setGameEnv(loadCache.getGameEnv());
// this.historyInfoPoint = chatCache.getInfoPoint();
// this.gameEnv = chatCache.getGameEnv();
System.out.println("----------读档中------------");
......
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