Commit cb904f39 by 赵国君

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

parent 98a7786c
......@@ -199,6 +199,26 @@ public class EngineUtils {
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;
import com.alibaba.fastjson.JSONObject;
import com.qiwu.chatbotbackend.GameEnvironment.GameEnv;
import com.qiwu.chatbotbackend.processing.SimpleUtil;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.DisposableBean;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class ChatCache implements DisposableBean {
......@@ -21,7 +30,16 @@ public class ChatCache implements DisposableBean {
private GameEnv gameEnv = null;
private Boolean gameStart = false;
private Boolean reload = false;
private Boolean initial = false;
public Boolean getInitial() {
return initial;
}
public void setInitial(Boolean initial) {
this.initial = initial;
}
public String getBotAccount() {
return botAccount;
......@@ -118,11 +136,55 @@ public class ChatCache implements DisposableBean {
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;
}
}
......@@ -47,9 +47,18 @@ public class GlobalVariableConstructor extends ElementConstructor {
JSONObject script = gameElements.getJSONObject("speechcraft");
for(String key:script.keySet()){
attrValue = (String)script.get(key);
//attrValue = (String)script.get(key);
attrValue = script.getJSONObject(key).getString("text");
varInitialMap.put(key, attrValue);
}
Boolean postActive = gameElements.getBoolean("postActive");
if(postActive != null && postActive == true) {
String postURL = gameElements.getString("postURL");
if(postURL != null & !postURL.equals("")) {
varInitialMap.put("postURL", postURL);
}
}
}catch (Exception e){
operationStatus = 0;
message = "全局变量处理出现异常";
......
......@@ -20,7 +20,8 @@ public class MapConstructor extends ElementConstructor {
Map<String, ArrayList<String>> name2nickname = new HashMap<String, ArrayList<String>>();
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 {
JSONObject map = gameElements.getJSONObject("map");
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){
throw new Exception("话术不存在");
......@@ -279,7 +277,8 @@ public class MapConstructor extends ElementConstructor {
//地点任务和地点回答的初始化
if (!varInitialMap.containsKey(lastTaskCondition)) {
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(lastTaskReply, lastTaskReply);
}
......
......@@ -73,7 +73,7 @@ public class ChatCacheController {
chatCacheMap.put(userKey, chatCache);
}
}
System.out.println(chatCache.getGameEnv());
//System.out.println(chatCache.getGameEnv());
return chatCache;
}
......
......@@ -111,10 +111,6 @@ public class FormsPoster {
else
ans.setAnswer("提交表单出现异常");
}
}
/**
......
......@@ -46,17 +46,13 @@ public class UserInfoController {
String line;
UserInfo userInfo = null;
File userFile = new File(System.getProperty("user.dir") + "/" + CONFIG_DIR + botAccount + ".txt");
System.out.println("配置文件地址:" + userFile.getAbsolutePath());
if (!userFile.exists()) {
BufferedReader br = new BufferedReader(new StringReader(""));
userInfo = new UserInfo(botAccount);
userInfo = buildUserInfo(br, userInfo, true);
context.setAttribute(userInfo.botAccount, userInfo);
} else {
FileReader reader = new FileReader(userFile);
BufferedReader br = new BufferedReader(reader);
......@@ -65,12 +61,8 @@ public class UserInfoController {
userInfo = buildUserInfo(br, userinfo, true);
context.setAttribute(userinfo.botAccount, userinfo);
reader.close();
br.close();
}
return userInfo;
}
......
......@@ -115,10 +115,6 @@ public class GameDesignServlet extends HttpServlet {
//从文件中读取condition相关的关键词
readKeywords(keywordMap, KEYWORD_DIR);
// ldt = LocalDateTime.now();
// now = ldt.format(dtf);
// timeUsedDetect.append("\n,预处理之后:").append(now);
String moduleList[] = {"map", "goods", "globalVariable", "npc", "plots"};
for(String moduleName: moduleList){
......@@ -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){
String startCondition, POS2 = "", botName = gameElements.getString("botName");
System.out.println(botName);
Integer startOption = gameElements.getInteger("startOption");
JSONObject returnMessage = null;
EngineUtils engine = null;
......@@ -276,7 +271,7 @@ public class GameDesignServlet extends HttpServlet {
JSONObject actionPostData = new JSONObject();
startCondition = (String)root.get("conditionName");
actionPostData.put("startCondition", "start_options");
//actionPostData.put("startCondition", "start_options");
if(startOption != null && startOption.equals(2)) {
actionPostData.put("welcome", gameElements.getString("plotStartText"));
} else {
......@@ -295,14 +290,9 @@ public class GameDesignServlet extends HttpServlet {
});
}
if (gameElements.containsKey("POS"))
POS2 = (String)gameElements.get("POS");
actionPostData.put("POS", POS.toString() + POS2);
System.out.println(actionPostData.toJSONString());
try {
engine = new EngineUtils();
ActionUtils actionUtils = new ActionUtils(engine, helper);
......@@ -314,11 +304,8 @@ public class GameDesignServlet extends HttpServlet {
}finally {
engine.destroy();
}
userInfo.extraInfo.put("activeAction", startCondition);
return intervalStatus;
}
......
......@@ -43,32 +43,47 @@ public class GetAnswerServlet extends HttpServlet {
for(Map.Entry<String, String[]> entry : entrySet) {
if(entry.getValue().length > 0) {
tmpStr = entry.getValue()[0];
if(tmpStr == null || tmpStr.equals(""))
if(tmpStr == null || tmpStr.trim().equals(""))
continue;
tmpStr = tmpStr.replace("qiwu", "我");
receiveParamters.put(entry.getKey(), tmpStr);
}
}
String botName = receiveParamters.get("bot_name");
/*
EngineUtils engineDBConn = null;
String gameName = null, playerName = null;
try {
engineDBConn = new EngineUtils();
String nickname = engineDBConn.queryBotNickNameByName(botName);
gameName = engineDBConn.queryBotNickNameByName(botName);
engineDBConn = new EngineUtils();
playerName = engineDBConn.queryPlayerNickNameByName(botName);
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
engineDBConn.destroy();
}
System.out.println("project name: " + gameName + " player name: " + playerName);
if(gameName == null || playerName == null || playerName.equals("") || gameName.equals("")) {
} else {
Set<Entry<String, String>> entrySet2 = receiveParamters.entrySet();
String _key = null, _value = null;
Boolean _replace = false;
for(Entry<String, String> entry : entrySet2) {
String value = entry.getValue();
if(value.indexOf("")) {
_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>();
Solution bestMatch = null;
......@@ -112,14 +127,9 @@ public class GetAnswerServlet extends HttpServlet {
if (userinfo.varInitialMap.get("botType") != null)
botType = userinfo.varInitialMap.get("botType");
logWriter.record("get chat cache start", 0);
chatCache = ChatCacheController.getChatCache(userKey, randKey, context, userinfo, botType);
logWriter.record("get chat cache complete", 0);
if (chatCache == null) {
throw (new Exception("chatCache初始化失败"));
}
......@@ -143,6 +153,7 @@ public class GetAnswerServlet extends HttpServlet {
//针对不同游戏类型对信息点做预处理
debugFlag = botUtil.preprocessing(receiveParamters, context);
chatCache.sendPost(curInfoPoint);
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