Commit cb904f39 by 赵国君

登陆后发送post请求,快速开始

parent 98a7786c
...@@ -199,6 +199,26 @@ public class EngineUtils { ...@@ -199,6 +199,26 @@ public class EngineUtils {
return nickname; return nickname;
} }
public String queryPlayerNickNameByName(String userName){
String sql = "select nickname from chats where bot = ? limit 1";
String nickname = "";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ResultSet rs = ps.executeQuery();
if (rs.next())
nickname = rs.getString("nickname");
ps.close();
rs.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return nickname;
}
} }
package com.qiwu.chatbotbackend.entity; package com.qiwu.chatbotbackend.entity;
import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.DisposableBean; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv;
import com.qiwu.chatbotbackend.processing.SimpleUtil;
import java.io.IOException;
import java.util.HashMap; import org.apache.http.message.BasicNameValuePair;
import java.util.Iterator; import org.springframework.beans.factory.DisposableBean;
import java.util.Map;
import java.io.IOException;
public class ChatCache implements DisposableBean { import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
public String ENV_ERROR = "游戏平台初始化失败"; import java.util.Iterator;
// private String chatKey = "不知道"; import java.util.List;
private String userKey = ""; import java.util.Map;
private String randKey = ""; import java.util.Map.Entry;
private String botAccount = ""; import java.util.Set;
private Map<String, String> infoPoint = null; public class ChatCache implements DisposableBean {
private GameEnv gameEnv = null;
private Boolean gameStart = false;
private Boolean reload = false; public String ENV_ERROR = "游戏平台初始化失败";
// private String chatKey = "不知道";
private String userKey = "";
public String getBotAccount() { private String randKey = "";
return botAccount; private String botAccount = "";
}
private Map<String, String> infoPoint = null;
public void setBotAccount(String botAccount) { private GameEnv gameEnv = null;
this.botAccount = botAccount; private Boolean gameStart = false;
} private Boolean reload = false;
private Boolean initial = false;
public String getUserKey() {
return userKey; public Boolean getInitial() {
} return initial;
}
public void setUserKey(String userKey) {
this.userKey = userKey; public void setInitial(Boolean initial) {
} this.initial = initial;
}
public String getRandKey() {
return randKey; public String getBotAccount() {
} return botAccount;
}
public void setRandKey(String randKey) {
this.randKey = randKey; public void setBotAccount(String botAccount) {
} this.botAccount = botAccount;
}
public Map<String, String> getInfoPoint() {
return infoPoint;
} public String getUserKey() {
return userKey;
public void setInfoPoint(Map<String, String> infoPoint) { }
this.infoPoint = infoPoint;
} public void setUserKey(String userKey) {
this.userKey = userKey;
public boolean isGameStart() { }
return gameStart;
} public String getRandKey() {
return randKey;
public void setGameStart(boolean gameStart) { }
this.gameStart = gameStart;
} public void setRandKey(String randKey) {
this.randKey = randKey;
public Boolean getReload() { }
return reload;
} public Map<String, String> getInfoPoint() {
return infoPoint;
public void setReload(Boolean reload) { }
this.reload = reload;
} public void setInfoPoint(Map<String, String> infoPoint) {
this.infoPoint = infoPoint;
public GameEnv getGameEnv() { }
return gameEnv;
} public boolean isGameStart() {
return gameStart;
public void setGameEnv(GameEnv gameEnv) { }
this.gameEnv = gameEnv;
} public void setGameStart(boolean gameStart) {
this.gameStart = gameStart;
public ChatCache(String userKey, String randKey, GameEnv gameEnv, Map<String, String> infoPoint) { }
this.userKey = userKey;
this.randKey = randKey; public Boolean getReload() {
this.gameEnv = gameEnv; return reload;
this.infoPoint = infoPoint; }
}
public void setReload(Boolean reload) {
this.reload = reload;
@Override }
public void destroy() throws IOException {
} public GameEnv getGameEnv() {
return gameEnv;
public ChatCache(UserInfo userInfo, String userKey, String randKey, String botType) throws Exception { }
String k;
this.botAccount = userInfo.botAccount; public void setGameEnv(GameEnv gameEnv) {
this.infoPoint = new HashMap<String, String>(); this.gameEnv = gameEnv;
}
if(botType.equals("game")) { public ChatCache(String userKey, String randKey, GameEnv gameEnv, Map<String, String> infoPoint) {
try { this.userKey = userKey;
this.gameEnv = new GameEnv(userInfo.botAccount); this.randKey = randKey;
System.out.println("初始化时:" + this.gameEnv); this.gameEnv = gameEnv;
} catch (Exception e) { this.infoPoint = infoPoint;
System.out.println(ENV_ERROR); }
this.infoPoint.put("error_message", ENV_ERROR);
e.printStackTrace();
} @Override
} public void destroy() throws IOException {
}
Iterator<String> iit = userInfo.varInitialMap.keySet().iterator(); public ChatCache(UserInfo userInfo, String userKey, String randKey, String botType) throws Exception {
while (iit.hasNext()) { String k;
k = iit.next(); this.botAccount = userInfo.botAccount;
this.infoPoint.put(k, userInfo.varInitialMap.get(k)); this.infoPoint = new HashMap<String, String>();
}
this.infoPoint.put("lastUpdateTime", String.valueOf(System.currentTimeMillis()));
this.randKey = randKey; if(botType.equals("game")) {
this.userKey = userKey; try {
this.gameEnv = new GameEnv(userInfo.botAccount);
} System.out.println("初始化时:" + this.gameEnv);
} catch (Exception e) {
public ChatCache(String userKey, String randKey) { System.out.println(ENV_ERROR);
this.userKey = userKey; this.infoPoint.put("error_message", ENV_ERROR);
this.randKey = randKey; e.printStackTrace();
} }
} }
Iterator<String> iit = userInfo.varInitialMap.keySet().iterator();
while (iit.hasNext()) {
k = iit.next();
this.infoPoint.put(k, userInfo.varInitialMap.get(k));
}
this.infoPoint.put("lastUpdateTime", String.valueOf(System.currentTimeMillis()));
this.randKey = randKey;
this.userKey = userKey;
this.initial = true;
}
public ChatCache(String userKey, String randKey) {
this.userKey = userKey;
this.randKey = randKey;
}
public Boolean sendPost(Map<String, String>curInfoPoint) {
if(initial == false)
return false;
initial = false;
String postURL = infoPoint.get("postURL");
if(postURL == null || postURL.trim().equals(""))
return false;
List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
Set<Entry<String, String>> entrySet = curInfoPoint.entrySet();
String _key = null, _value = null;
for(Map.Entry<String, String> entry : entrySet) {
_key = entry.getKey();
_value = entry.getValue().trim();
if(_value == null || _value.equals("")) {
continue;
}
pairList.add(new BasicNameValuePair(_key, _value));
}
entrySet = infoPoint.entrySet();
for(Map.Entry<String, String> entry : entrySet) {
_key = entry.getKey();
_value = entry.getValue().trim();
if(_value == null || _value.equals("")) {
continue;
}
pairList.add(new BasicNameValuePair(_key, _value));
}
JSONObject result = null;
try {
result = SimpleUtil.postFORM(postURL, pairList);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("initial post:" + result.toJSONString());
Set<Entry<String, Object>> entrySet2 = result.entrySet();
for(Entry<String, Object> entry : entrySet2) {
_key = entry.getKey();
if(_key == "post_status")
continue;
infoPoint.put(_key, entry.getValue().toString());
}
return true;
}
}
package com.qiwu.chatbotbackend.gameDesign; package com.qiwu.chatbotbackend.gameDesign;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.db.SqlHelper;
import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.entity.UserInfo;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import static com.qiwu.chatbotbackend.Config.Config.CC_URL; import static com.qiwu.chatbotbackend.Config.Config.CC_URL;
public class GlobalVariableConstructor extends ElementConstructor { public class GlobalVariableConstructor extends ElementConstructor {
public GlobalVariableConstructor(SqlHelper helper, int botId, String botAccount, Map<String, StringBuffer> keywordMap, HashSet<String> exceptRelation, StringBuffer POS) { public GlobalVariableConstructor(SqlHelper helper, int botId, String botAccount, Map<String, StringBuffer> keywordMap, HashSet<String> exceptRelation, StringBuffer POS) {
super(); super();
this.helper = helper; this.helper = helper;
this.botId = botId; this.botId = botId;
this.botAccount = botAccount; this.botAccount = botAccount;
this.exceptRelation = exceptRelation; this.exceptRelation = exceptRelation;
this.keywordMap = keywordMap; this.keywordMap = keywordMap;
this.POS = POS; this.POS = POS;
} }
public void construct(JSONObject gameElements, UserInfo userinfo) { public void construct(JSONObject gameElements, UserInfo userinfo) {
try { try {
String category, name, description, attrValue; String category, name, description, attrValue;
JSONObject globalVariable = gameElements.getJSONObject("globalVariable"); JSONObject globalVariable = gameElements.getJSONObject("globalVariable");
Map<String, String> attribute = null; Map<String, String> attribute = null;
Iterator<String> attrIter = null; Iterator<String> attrIter = null;
for(String attrName:globalVariable.keySet()) { for(String attrName:globalVariable.keySet()) {
attrValue = (String)globalVariable.get(attrName); attrValue = (String)globalVariable.get(attrName);
varInitialMap.put("globalVariable_" + attrName, attrValue); varInitialMap.put("globalVariable_" + attrName, attrValue);
} }
varInitialMap.put("botType", "game"); varInitialMap.put("botType", "game");
varInitialMap.put("unknownTextList", (String)gameElements.get("unknownTextList")); varInitialMap.put("unknownTextList", (String)gameElements.get("unknownTextList"));
varInitialMap.put("finalUnknownText", (String)gameElements.get("finalUnknownText")); varInitialMap.put("finalUnknownText", (String)gameElements.get("finalUnknownText"));
varInitialMap.put("CC_URL", CC_URL); varInitialMap.put("CC_URL", CC_URL);
JSONObject script = gameElements.getJSONObject("speechcraft"); JSONObject script = gameElements.getJSONObject("speechcraft");
for(String key:script.keySet()){ for(String key:script.keySet()){
attrValue = (String)script.get(key); //attrValue = (String)script.get(key);
varInitialMap.put(key, attrValue); attrValue = script.getJSONObject(key).getString("text");
} varInitialMap.put(key, attrValue);
}catch (Exception e){ }
operationStatus = 0;
message = "全局变量处理出现异常"; Boolean postActive = gameElements.getBoolean("postActive");
e.printStackTrace(); if(postActive != null && postActive == true) {
return; String postURL = gameElements.getString("postURL");
} if(postURL != null & !postURL.equals("")) {
} varInitialMap.put("postURL", postURL);
} }
}
}catch (Exception e){
operationStatus = 0;
message = "全局变量处理出现异常";
e.printStackTrace();
return;
}
}
}
...@@ -20,7 +20,8 @@ public class MapConstructor extends ElementConstructor { ...@@ -20,7 +20,8 @@ public class MapConstructor extends ElementConstructor {
Map<String, ArrayList<String>> name2nickname = new HashMap<String, ArrayList<String>>(); Map<String, ArrayList<String>> name2nickname = new HashMap<String, ArrayList<String>>();
JSONArray locations = null; JSONArray locations = null;
Map<String, String> script = null; //话术模板 //Map<String, String> script = null; //话术模板
JSONObject script = null; //话术模板
...@@ -85,11 +86,8 @@ public class MapConstructor extends ElementConstructor { ...@@ -85,11 +86,8 @@ public class MapConstructor extends ElementConstructor {
JSONObject map = gameElements.getJSONObject("map"); JSONObject map = gameElements.getJSONObject("map");
Map<String, String> question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class); Map<String, String> question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class);
script = JSONObject.toJavaObject(gameElements.getJSONObject("speechcraft"), Map.class); //script = JSONObject.toJavaObject(gameElements.getJSONObject("speechcraft"), Map.class);
script = gameElements.getJSONObject("speechcraft");
if(script == null){ if(script == null){
throw new Exception("话术不存在"); throw new Exception("话术不存在");
...@@ -279,7 +277,8 @@ public class MapConstructor extends ElementConstructor { ...@@ -279,7 +277,8 @@ public class MapConstructor extends ElementConstructor {
//地点任务和地点回答的初始化 //地点任务和地点回答的初始化
if (!varInitialMap.containsKey(lastTaskCondition)) { if (!varInitialMap.containsKey(lastTaskCondition)) {
varInitialMap.put(lastTaskCondition, conditionName); varInitialMap.put(lastTaskCondition, conditionName);
varInitialMap.put(lastTaskReply, script.get("SITE_NOT_ACTIVE").replace("[地点名]", placeA)); //varInitialMap.put(lastTaskReply, script.get("SITE_NOT_ACTIVE").replace("[地点名]", placeA));
varInitialMap.put(lastTaskReply, script.getJSONObject("SITE_NOT_ACTIVE").getString("text").replace("[地点名]", placeA));
conditionDescription.put(lastTaskCondition, lastTaskCondition); conditionDescription.put(lastTaskCondition, lastTaskCondition);
conditionDescription.put(lastTaskReply, lastTaskReply); conditionDescription.put(lastTaskReply, lastTaskReply);
} }
......
...@@ -73,7 +73,7 @@ public class ChatCacheController { ...@@ -73,7 +73,7 @@ public class ChatCacheController {
chatCacheMap.put(userKey, chatCache); chatCacheMap.put(userKey, chatCache);
} }
} }
System.out.println(chatCache.getGameEnv()); //System.out.println(chatCache.getGameEnv());
return chatCache; return chatCache;
} }
......
...@@ -111,10 +111,6 @@ public class FormsPoster { ...@@ -111,10 +111,6 @@ public class FormsPoster {
else else
ans.setAnswer("提交表单出现异常"); ans.setAnswer("提交表单出现异常");
} }
} }
/** /**
......
...@@ -115,10 +115,6 @@ public class GameDesignServlet extends HttpServlet { ...@@ -115,10 +115,6 @@ public class GameDesignServlet extends HttpServlet {
//从文件中读取condition相关的关键词 //从文件中读取condition相关的关键词
readKeywords(keywordMap, KEYWORD_DIR); readKeywords(keywordMap, KEYWORD_DIR);
// ldt = LocalDateTime.now();
// now = ldt.format(dtf);
// timeUsedDetect.append("\n,预处理之后:").append(now);
String moduleList[] = {"map", "goods", "globalVariable", "npc", "plots"}; String moduleList[] = {"map", "goods", "globalVariable", "npc", "plots"};
for(String moduleName: moduleList){ for(String moduleName: moduleList){
...@@ -267,7 +263,6 @@ public class GameDesignServlet extends HttpServlet { ...@@ -267,7 +263,6 @@ public class GameDesignServlet extends HttpServlet {
*/ */
public IntervalStatus actionRegenerate(JSONObject root, JSONObject gameElements, UserInfo userInfo, HashMap<String, HashSet<String>> additionalKeywords, HashSet<String> exceptRelation, StringBuffer POS, SqlHelper helper){ public IntervalStatus actionRegenerate(JSONObject root, JSONObject gameElements, UserInfo userInfo, HashMap<String, HashSet<String>> additionalKeywords, HashSet<String> exceptRelation, StringBuffer POS, SqlHelper helper){
String startCondition, POS2 = "", botName = gameElements.getString("botName"); String startCondition, POS2 = "", botName = gameElements.getString("botName");
System.out.println(botName);
Integer startOption = gameElements.getInteger("startOption"); Integer startOption = gameElements.getInteger("startOption");
JSONObject returnMessage = null; JSONObject returnMessage = null;
EngineUtils engine = null; EngineUtils engine = null;
...@@ -276,7 +271,7 @@ public class GameDesignServlet extends HttpServlet { ...@@ -276,7 +271,7 @@ public class GameDesignServlet extends HttpServlet {
JSONObject actionPostData = new JSONObject(); JSONObject actionPostData = new JSONObject();
startCondition = (String)root.get("conditionName"); startCondition = (String)root.get("conditionName");
actionPostData.put("startCondition", "start_options"); //actionPostData.put("startCondition", "start_options");
if(startOption != null && startOption.equals(2)) { if(startOption != null && startOption.equals(2)) {
actionPostData.put("welcome", gameElements.getString("plotStartText")); actionPostData.put("welcome", gameElements.getString("plotStartText"));
} else { } else {
...@@ -295,14 +290,9 @@ public class GameDesignServlet extends HttpServlet { ...@@ -295,14 +290,9 @@ public class GameDesignServlet extends HttpServlet {
}); });
} }
if (gameElements.containsKey("POS")) if (gameElements.containsKey("POS"))
POS2 = (String)gameElements.get("POS"); POS2 = (String)gameElements.get("POS");
actionPostData.put("POS", POS.toString() + POS2); actionPostData.put("POS", POS.toString() + POS2);
System.out.println(actionPostData.toJSONString());
try { try {
engine = new EngineUtils(); engine = new EngineUtils();
ActionUtils actionUtils = new ActionUtils(engine, helper); ActionUtils actionUtils = new ActionUtils(engine, helper);
...@@ -314,11 +304,8 @@ public class GameDesignServlet extends HttpServlet { ...@@ -314,11 +304,8 @@ public class GameDesignServlet extends HttpServlet {
}finally { }finally {
engine.destroy(); engine.destroy();
} }
userInfo.extraInfo.put("activeAction", startCondition); userInfo.extraInfo.put("activeAction", startCondition);
return intervalStatus; return intervalStatus;
} }
......
...@@ -43,32 +43,47 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -43,32 +43,47 @@ public class GetAnswerServlet extends HttpServlet {
for(Map.Entry<String, String[]> entry : entrySet) { for(Map.Entry<String, String[]> entry : entrySet) {
if(entry.getValue().length > 0) { if(entry.getValue().length > 0) {
tmpStr = entry.getValue()[0]; tmpStr = entry.getValue()[0];
if(tmpStr == null || tmpStr.equals("")) if(tmpStr == null || tmpStr.trim().equals(""))
continue; continue;
tmpStr = tmpStr.replace("qiwu", "我"); tmpStr = tmpStr.replace("qiwu", "我");
receiveParamters.put(entry.getKey(), tmpStr); receiveParamters.put(entry.getKey(), tmpStr);
} }
} }
String botName = receiveParamters.get("bot_name"); String botName = receiveParamters.get("bot_name");
/*
EngineUtils engineDBConn = null; EngineUtils engineDBConn = null;
String gameName = null, playerName = null;
try { try {
engineDBConn = new EngineUtils(); engineDBConn = new EngineUtils();
String nickname = engineDBConn.queryBotNickNameByName(botName); gameName = engineDBConn.queryBotNickNameByName(botName);
engineDBConn = new EngineUtils();
playerName = engineDBConn.queryPlayerNickNameByName(botName);
} catch (SQLException e1) { } catch (SQLException e1) {
e1.printStackTrace(); e1.printStackTrace();
} finally { } finally {
engineDBConn.destroy(); engineDBConn.destroy();
} }
Set<Entry<String, String>> entrySet2 = receiveParamters.entrySet(); System.out.println("project name: " + gameName + " player name: " + playerName);
for(Entry<String, String> entry : entrySet2) { if(gameName == null || playerName == null || playerName.equals("") || gameName.equals("")) {
String value = entry.getValue(); } else {
if(value.indexOf("")) { Set<Entry<String, String>> entrySet2 = receiveParamters.entrySet();
String _key = null, _value = null;
Boolean _replace = false;
for(Entry<String, String> entry : entrySet2) {
_replace = false;
_key = entry.getKey();
_value = entry.getValue();
if(_value.indexOf(gameName) != -1) {
_replace = true;
_value = _value.replace(gameName, "你");
}
if(_value.indexOf(playerName) != -1) {
_replace = true;
_value = _value.replace(playerName, "我");
}
if(_replace)
receiveParamters.put(_key, _value);
} }
} }
*/
Map<String ,String> curInfoPoint = new HashMap<String, String>(); Map<String ,String> curInfoPoint = new HashMap<String, String>();
Solution bestMatch = null; Solution bestMatch = null;
...@@ -112,13 +127,8 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -112,13 +127,8 @@ public class GetAnswerServlet extends HttpServlet {
if (userinfo.varInitialMap.get("botType") != null) if (userinfo.varInitialMap.get("botType") != null)
botType = userinfo.varInitialMap.get("botType"); botType = userinfo.varInitialMap.get("botType");
logWriter.record("get chat cache start", 0);
chatCache = ChatCacheController.getChatCache(userKey, randKey, context, userinfo, botType); chatCache = ChatCacheController.getChatCache(userKey, randKey, context, userinfo, botType);
logWriter.record("get chat cache complete", 0);
if (chatCache == null) { if (chatCache == null) {
throw (new Exception("chatCache初始化失败")); throw (new Exception("chatCache初始化失败"));
...@@ -143,6 +153,7 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -143,6 +153,7 @@ public class GetAnswerServlet extends HttpServlet {
//针对不同游戏类型对信息点做预处理 //针对不同游戏类型对信息点做预处理
debugFlag = botUtil.preprocessing(receiveParamters, context); debugFlag = botUtil.preprocessing(receiveParamters, context);
chatCache.sendPost(curInfoPoint);
logWriter.record("action match start", 0); logWriter.record("action match start", 0);
......
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