Commit b8dd9e53 by hxw

添加变量等

parent d592cee2
package db;
import com.alibaba.fastjson.JSONObject;
import entity.JConfig;
import entity.QuestionKeyWord;
import processing.MyFunc;
import java.sql.*;
import java.util.ArrayList;
......@@ -130,6 +132,24 @@ public class SqlHelper {
return b;
}
public boolean deleteConfigByBotId(int botId){
String sql = "delete from chatbot_config where botId= ?";
boolean b;
try{
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, String.valueOf(botId));
b = ps.execute();
ps.close();
return b;
} catch (SQLException e) {
System.out.println("删除旧数据失败");
e.printStackTrace();
return false;
}
}
public boolean batchInsertConfigByBotId(int botId, List<JConfig> jConfigList, List<Integer> idList) {
int count[];
boolean insertFlag = true;
......@@ -149,11 +169,11 @@ public class SqlHelper {
}
count = ps.executeBatch();
conn.commit(); //提交事务,这个非常重要
conn.commit();
for (int i : count) {
if (i == 0) {
conn.rollback(); // 回滚,非常重要
conn.rollback();
System.out.println("======出现异常,回滚=========");
insertFlag = false;
}
......@@ -168,7 +188,7 @@ public class SqlHelper {
} catch (SQLException e) {
try {
conn.rollback(); // 回滚,非常重要
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
......@@ -179,6 +199,27 @@ public class SqlHelper {
return insertFlag ;
}
public JSONObject queryGameElementsByBotId(int id){
String sql = "select * from generate_json_tb where id = ?";
String userName = "";
String result = "empty";
try{
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, String.valueOf(id));
ResultSet rs = ps.executeQuery();
if(rs.next())
result = rs.getString("json_text");
rs.close();
ps.close();
}catch(Exception e){
e.printStackTrace();
}
return JSONObject.parseObject(result);
}
......
......@@ -16,9 +16,9 @@ public class Condition {
public HashSet<String> keywordSet = new HashSet<String>();
public HashMap<String,HashSet<String>> historyPremise = new HashMap<String,HashSet<String>>();
public HashMap<String,HashSet<String>> currentPremise = new HashMap<String,HashSet<String>>();
public HashMap<String, String> updateKnowledge = new HashMap<String, String>();
public String[] answer = {"",};
public String nextStep = "";
public String feature = "";
public HashMap<String, String> featureMap = new HashMap<String, String>();
public String indexName = "";
public int id =0;
......@@ -38,8 +38,8 @@ public class Condition {
this.currentPremise = originCondition.currentPremise;
this.answer = originCondition.answer;
this.nextStep = originCondition.nextStep;
this.feature = originCondition.feature;
this.featureMap = originCondition.featureMap;
this.updateKnowledge = originCondition.updateKnowledge;
this.indexName = originCondition.indexName;
this.id = originCondition.id;
this.status = originCondition.status;
......
......@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
......@@ -23,17 +24,21 @@ public class JConfig {
private String curPremise = "";
private String hisPremise = "";
private String feature = "";
private String reply = "";
private String nextStep = "";
public String getCurPremise() {
return curPremise;
}
private String auxiliaryPriority = "0";
private String status = "waiting";
public void setAuxiliaryPriority(String auxiliaryPriority) {
this.auxiliaryPriority = auxiliaryPriority;
}
private String updateKnowledge = "";
private String nextStepStatus = "return";
......@@ -46,78 +51,80 @@ public class JConfig {
}
};
public JConfig(String curPremise, String hisPremise, String feature, String reply, String nextStep)
public void setAuxiliaryPriority(String auxiliaryPriority) {
this.auxiliaryPriority = auxiliaryPriority;
}
public void setNextStepStatus(String nextStepStatus) {
this.nextStepStatus = nextStepStatus;
}
public JConfig(String curPremise, String hisPremise, String feature, String reply, String nextStep, String updateKnowledge)
{
this.curPremise = curPremise;
this.hisPremise = hisPremise;
this.feature = feature;
if(!updateKnowledge.equals(""))
this.updateKnowledge += "-<-" + updateKnowledge;
this.reply = reply;
this.nextStep = nextStep;
}
public JConfig(List<String> curPremise, List<String> hisPremise, String extraCP, String extraHP, String feature, String reply, String nextStep)
{
String curPremiseStr = "";
String hisPremiseStr = "";
String extraCurPremise = "";
String extraHisPremise = "";
for(String premise: curPremise)
curPremiseStr += "," + premise;
for(String premise: hisPremise)
hisPremiseStr += "," + premise;
if(!extraCP.equals(""))
curPremiseStr += "," + extraCP;
if(!extraHP.equals(""))
hisPremiseStr += "," + extraHP;
this.curPremise = curPremiseStr.substring(1, curPremiseStr.length());
this.hisPremise = hisPremiseStr.substring(1, hisPremiseStr.length());
public JConfig(List<String> curPremise, List<String> hisPremise , Map<String, String> updateKnowledge, String extraCP, String extraHP, String extraUK, String feature, String reply, String nextStep, String auxiliaryPriority){
this.curPremise = appendPremiseStr(curPremise, extraCP);
this.hisPremise = appendPremiseStr(hisPremise, extraHP);
this.updateKnowledge = appendKnowledgeStr(updateKnowledge, extraUK);
this.feature = feature;
this.reply = reply;
this.nextStep = nextStep;
this.auxiliaryPriority = auxiliaryPriority;
}
public JConfig(List<String> curPremise, List<String> hisPremise, String extraCP, String extraHP, String feature, String reply, String nextStep, List<String> updateKnowledgeList, String extraUK)
{
String curPremiseStr = "";
String hisPremiseStr = "";
String extraCurPremise = "";
String extraHisPremise = "";
for(String premise: curPremise)
curPremiseStr += "," + premise;
for(String premise: hisPremise)
hisPremiseStr += "," + premise;
if(!extraCP.equals(""))
curPremiseStr += "," + extraCP;
if(!extraHP.equals(""))
hisPremiseStr += "," + extraHP;
for(String knowledge:updateKnowledgeList)
updateKnowledge += "-<-" + knowledge;
updateKnowledge += "-<-" + extraUK;
this.curPremise = curPremiseStr.substring(1, curPremiseStr.length());
this.hisPremise = hisPremiseStr.substring(1, hisPremiseStr.length());
this.feature = feature;
this.reply = reply;
this.nextStep = nextStep;
public String appendPremiseStr(List<String> premises, String extraPM){
StringBuffer sb = new StringBuffer();
String premiseStr = "";
for (String premise:premises)
sb.append("," + premise);
if(sb.length() > 0)
sb.deleteCharAt(0); //删除第一个逗号
if(!extraPM.equals("")){
if(sb.length() == 0)
sb.append(extraPM);
else
sb.append("," + extraPM);
}else if(sb.length() == 0){
sb.append(" ");
}
premiseStr = sb.toString();
return premiseStr;
}
public JConfig(String curPremise, String hisPremise, String feature, String reply, String nextStep, String updateKnowledge)
{
this.curPremise = curPremise;
this.hisPremise = hisPremise;
this.feature = feature;
this.updateKnowledge = "-<-" + updateKnowledge;
public String appendKnowledgeStr(Map<String, String> knowledge, String extraKL){
StringBuffer sb = new StringBuffer();
String varName = "", varValue = "", knowledgeStr;
Iterator<String> iit = knowledge.keySet().iterator();
this.reply = reply;
this.nextStep = nextStep;
while(iit.hasNext()){
varName = iit.next();
varValue = knowledge.get(varName);
sb.append("-<-" + varName + ":" + varValue);
}
if(!extraKL.equals("")){
sb.append("-<-" + extraKL);
}
if(sb.length() > 0)
knowledgeStr = "updateKnowledge" + sb.toString();
else
knowledgeStr = "";
return knowledgeStr;
}
......@@ -146,10 +153,12 @@ public class JConfig {
}
public void appendJKnowledge(JSONArray knowledgeList, String knowledge){
if(knowledge.equals(""))
String pair[] = knowledge.split(":");
if(knowledge.equals("") || pair.length < 2)
return;
String pair[] = knowledge.split(":");
JSONObject knowledgeJ = new JSONObject();
knowledgeJ.put("variableName", pair[0]);
knowledgeJ.put("keyword", pair[1]);
......@@ -234,12 +243,9 @@ public class JConfig {
public String toString(){
if(!updateKnowledge.equals(""))
updateKnowledge = ",updateKnowledge" + updateKnowledge;
String text = curPremise + "|" +
hisPremise + "|" +
feature + updateKnowledge + "|" +
feature + "," + updateKnowledge + "," + nextStepStatus + "|" +
"auxiliaryPriority=" + auxiliaryPriority + "|=>|" +
reply + "|" +
"nextStep=" + nextStep + "|" +
......
......@@ -6,9 +6,9 @@ package entity;
* 暂时没用
*/
public class QuestionKeyWord {
String actionName;
String chatKey;
String keyWord;
private String actionName;
private String chatKey;
private String keyWord;
public String getActionName() {
return actionName;
......
......@@ -5,9 +5,9 @@ package entity;
* 包含后台返回数据的实体类。
*/
public class Solution {
int status = 0;
String answer;
String next;
private int status = 0;
private String answer;
private String next;
public String getAnswer() {
return answer;
......
......@@ -13,11 +13,12 @@ public class UserInfo {
this.userName = userName;
this.extraInfo = new HashMap<String,String>(){
{
put("activeAction","");
put("activeAction"," ");
}
};
this.id2config = new HashMap<Integer, String>();
this.id2condition = new HashMap<Integer, Condition>();
this.varInitialMap = new HashMap<String, String>();
}
public HashMap<String,List<Condition>> conditionTable;
......@@ -26,6 +27,8 @@ public class UserInfo {
public HashMap<String,String> extraInfo;
public HashMap<Integer, String> id2config;
public HashMap<Integer, Condition> id2condition;
//存储需要初始化的变量
public HashMap<String, String> varInitialMap;
......
package gameDesign;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import db.SqlHelper;
import entity.UserInfo;
import java.util.Iterator;
import java.util.Map;
public class CharacterConstructor extends ElementConstructor{
public CharacterConstructor(SqlHelper helper, int botId) {
super();
this.helper = helper;
this.botId = botId;
}
public void construct(JSONObject jsonObject, UserInfo userinfo){
String category, name, description, attrName, attrValue;
JSONArray allNPC = jsonObject.getJSONArray("npc");
Map<String, String> attribute = null;
Iterator<String> attrIter = null;
for(JSONObject npc:allNPC.toJavaList(JSONObject.class)) {
category = (String) npc.get("category");
JSONArray characters = npc.getJSONArray("characters");
for (JSONObject character : characters.toJavaList(JSONObject.class)) {
name = (String) character.get("name");
description = (String) character.get("intro");
attribute = JSONObject.toJavaObject(character.getJSONObject("attr"), Map.class);
attrIter = attribute.keySet().iterator();
while (attrIter.hasNext()) {
attrName = attrIter.next();
attrValue = attribute.get(attrName);
configText.append("-varInitial-|" + category + "_" + name + "_" + attrName + "|" + attrValue + "\n");
}
}
}
}
}
......@@ -4,10 +4,12 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import db.SqlHelper;
import entity.JConfig;
import entity.UserInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ElementConstructor {
......@@ -16,18 +18,17 @@ public class ElementConstructor {
List<JSONObject> configDataList = new ArrayList<JSONObject>();
List<JConfig> jConfigList = new ArrayList<JConfig>();
String userName;
String configText = "";
StringBuffer configText = new StringBuffer();
JConfig jConfig = null;
Map<String, String> conditionDescription = new HashMap<String, String>();
public void construct(JSONObject jsonObject, UserInfo userinfo){}
public List<JConfig> getjConfigList() { return jConfigList; }
public void construct(JSONObject jsonObject){}
public List<JSONObject> getConfigDataList() { return configDataList; }
public void ElementConstructor(String userName){ this.userName = userName; }
public String getConfigText() { return configText.toString(); }
public String getConfigText() { return configText; }
public void generateConfigText(List<Integer> idList){
int id, length = idList.size();
JConfig jConfig = null;
......@@ -35,7 +36,7 @@ public class ElementConstructor {
jConfig = jConfigList.get(i);
id = idList.get(i);
configText += jConfig.toString() + id + "\n";
configText.append(jConfig.toString() + id + "\n");
}
}
......
package gameDesign;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import db.SqlHelper;
import entity.UserInfo;
import java.util.Iterator;
import java.util.Map;
public class GlobalVariableConstructor extends ElementConstructor{
public GlobalVariableConstructor(SqlHelper helper, int botId) {
super();
this.helper = helper;
this.botId = botId;
}
public void construct(JSONObject jsonObject, UserInfo userinfo){
String category, name, description, attrName, attrValue;
JSONArray globalVariable = jsonObject.getJSONArray("globalVariable");
Map<String, String> attribute = null;
Iterator<String> attrIter = null;
for(JSONObject variableList:globalVariable.toJavaList(JSONObject.class)) {
category = (String) variableList.get("category");
attribute = JSONObject.toJavaObject(variableList.getJSONObject("variables"), Map.class);
attrIter = attribute.keySet().iterator();
while(attrIter.hasNext()){
attrName = attrIter.next();
attrValue = attribute.get(attrName);
configText.append("-varInitial-|" + category + "_" + attrName + "|" + attrValue + "\n");
}
}
}
}
package gameDesign;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import db.SqlHelper;
import entity.UserInfo;
import java.util.Iterator;
import java.util.Map;
public class ItemConstructor extends ElementConstructor{
public ItemConstructor(SqlHelper helper, int botId) {
super();
this.helper = helper;
this.botId = botId;
}
public void construct(JSONObject jsonObject, UserInfo userinfo){
String category, name, description, attrName, attrValue;
JSONArray goods = jsonObject.getJSONArray("goods");
Map<String, String> attribute = null;
Iterator<String> attrIter = null;
for(JSONObject good:goods.toJavaList(JSONObject.class)) {
category = (String) good.get("category");
JSONArray items = good.getJSONArray("items");
for (JSONObject item : items.toJavaList(JSONObject.class)) {
name = (String) item.get("name");
description = (String) item.get("intro");
attribute = JSONObject.toJavaObject(item.getJSONObject("attr"), Map.class);
attrIter = attribute.keySet().iterator();
while (attrIter.hasNext()) {
attrName = attrIter.next();
attrValue = attribute.get(attrName);
configText.append("-varInitial-|" + category + "_" + name + "_" + attrName + "|" + attrValue + "\n");
}
}
}
}
}
......@@ -8,27 +8,16 @@ import entity.UserInfo;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.*;
public class MapConstructor extends ElementConstructor{
int[][] matrix;
int locationNum;
HashMap<Integer, JSONObject> id2j = new HashMap<Integer, JSONObject>();
Map<String, String> question2keyword = new HashMap<String, String>();
JSONArray locations = null;
public MapConstructor(SqlHelper helper, int botId) {
super();
this.helper = helper;
......@@ -74,9 +63,13 @@ public class MapConstructor extends ElementConstructor{
}
public void construct(JSONObject jsonObject){
JSONArray locations = jsonObject.getJSONArray("locations");
public void construct(JSONObject jsonObject, UserInfo userinfo){
conditionDescription.put("site","当前地点");
JSONObject map = jsonObject.getJSONObject("map");
Map<String, String> question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class);
JSONArray locations = map.getJSONArray("locations");
this.locations = locations;
this.question2keyword = question2keyword;
//通过邻接表构造邻接矩阵
constructGreph(locations);
......@@ -89,21 +82,16 @@ public class MapConstructor extends ElementConstructor{
generateWhereIam(jsonObject);
generateHowToThere(floyd.pathMatirx, floyd.preTable, jsonObject);
}
/**
* 生成‘我在哪里’所需要的配置文件
*/
public void generateWhereIam(JSONObject jsonObject){
String zainaKeywords = (String)jsonObject.get("zaina");
String zainaKeywords = question2keyword.get("zaina");
jConfig = new JConfig(
"zaina::match-Keyword=" + zainaKeywords,
" ",
"match",
"你在{site-2}",
"yaobuyao"
"zaina::match-Keyword=" + zainaKeywords, " ",
"match", "你在{site-2}", "yaobuyao",""
);
jConfigList.add(jConfig);
......@@ -118,7 +106,7 @@ public class MapConstructor extends ElementConstructor{
public void generateHowToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){
String placeA, placeB, descriptionB, placeKeywords, text, pathNode;
String zenmequKeywords = (String)jsonObject.get("zenmequ");
String zenmequKeywords = question2keyword.get("zenmequ");
for(int i = 0;i < locationNum;i++){
......@@ -131,22 +119,16 @@ public class MapConstructor extends ElementConstructor{
if(i == j){
jConfig = new JConfig(
"zenmequ::match-Keyword=" + placeKeywords,
"site::match-Keyword=" + placeA,
"match",
"你已经在" + placeB + "了",
"yaobuyao"
"zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", "你已经在" + placeB + "了", "yaobuyao",""
);
jConfigList.add(jConfig);
continue;
};
if(pathMatirx[i][j] == 1){
jConfig = new JConfig(
"zenmequ::match-Keyword=" + placeKeywords,
"site::match-Keyword=" + placeA,
"match",
placeB + "就在附近哦",
"yaobuyao"
"zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", placeB + "就在附近哦", "yaobuyao", ""
);
jConfigList.add(jConfig);
......@@ -159,11 +141,8 @@ public class MapConstructor extends ElementConstructor{
}
jConfig = new JConfig(
"zenmequ::match-Keyword=" + placeKeywords,
"site::match-Keyword=" + placeA,
"match",
"经过" + pathNode + "就可以到达" + placeB,
"yaobuyao"
"zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", "经过" + pathNode + "就可以到达" + placeB, "yaobuyao",""
);
jConfigList.add(jConfig);
}
......@@ -176,7 +155,7 @@ public class MapConstructor extends ElementConstructor{
* 生成‘我能去哪里’所需要的配置文件
*/
public void generateWhereCanIgo(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){
String nengqunaKeywords = (String)jsonObject.get("nengquna");
String nengqunaKeywords = question2keyword.get("nengquna");
String placeA, placeB, pathNode;
for(int i = 0;i < locationNum;i++){
......@@ -191,11 +170,8 @@ public class MapConstructor extends ElementConstructor{
}
jConfig = new JConfig(
"nengquna::match-Keyword=" + nengqunaKeywords,
"site::match-Keyword=" + placeA,
"match",
"你可以去" + pathNode,
"yaobuyao"
"nengquna::match-Keyword=" + nengqunaKeywords, "site::match-Keyword=" + placeA,
"match", "你可以去" + pathNode, "yaobuyao", ""
);
jConfigList.add(jConfig);
......@@ -212,7 +188,7 @@ public class MapConstructor extends ElementConstructor{
*/
public void generateWantToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){
String placeA, placeB, descriptionB, placeKeywords, text, pathNode;
String qunaKeywords = (String)jsonObject.get("quna");
String qunaKeywords = question2keyword.get("quna");
......@@ -227,26 +203,23 @@ public class MapConstructor extends ElementConstructor{
if(i == j){
jConfig = new JConfig(
"quna::match-Keyword=" + placeKeywords,
"site::match-Keyword=" + placeA,
"match",
"你已经在" + placeB + "了",
"yaobuyao"
"quna::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", "你已经在" + placeB + "了", "yaobuyao", ""
);
jConfigList.add(jConfig);
continue;
};
//如果两点相邻
if(pathMatirx[i][j] == 1){
jConfig = new JConfig(
"quna::match-Keyword=" + placeKeywords,
"site::match-Keyword=" + placeA,
"match,updateKnowledge-<-site:" + placeB,
"" + descriptionB,
"yaobuyao"
"quna::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", "" + descriptionB, "yaobuyao", "site:" + placeB
);
jConfigList.add(jConfig);
//如果两点不是相邻点
}else if(pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE){
int k = preTable[i][j];
pathNode = "";
......@@ -257,9 +230,7 @@ public class MapConstructor extends ElementConstructor{
jConfig = new JConfig(
"quna::match-Keyword=" + placeKeywords,
"site::match-Keyword=" + placeA,
"match",
"从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB,
"yaobuyao"
"match", "从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB, "yaobuyao", ""
);
jConfigList.add(jConfig);
}
......
......@@ -18,12 +18,13 @@ public class ConditionTableController {
int id = Integer.parseInt(configId);
Condition condition = new Condition();
ProcessQuestion process = new ProcessQuestion();
UserInfoController userInfoController = new UserInfoController();
//data=configLine+"---"+conditionNameList
String conditionNameList[] = data.split(",");
String conditionName = "";
if(context.getAttribute(userName) == null){
process.userConfigInit(userName, context);
userInfoController.userConfigInit(userName, context);
}
UserInfo userInfo = (UserInfo) context.getAttribute(userName);
......@@ -114,12 +115,13 @@ public class ConditionTableController {
String dataPair[] = data.split("---");
String configLine = dataPair[0];
String conditionNameList[];
UserInfoController userInfoController = new UserInfoController();
int id = Integer.parseInt(configId);
condition = process.buildCondition(condition, configLine);
condition = userInfoController.buildCondition(condition, configLine);
if(context.getAttribute(userName) == null){
process.userConfigInit(userName, context);
userInfoController.userConfigInit(userName, context);
}
UserInfo userinfo = (UserInfo) context.getAttribute(userName);
......@@ -135,13 +137,54 @@ public class ConditionTableController {
}
public static boolean updateVariable(String userName, String configId, String data, ServletContext context) throws IOException {
Condition condition = new Condition();
ProcessQuestion process = new ProcessQuestion();
//data=configLine+"---"+conditionNameList
String dataPair[] = data.split("---", -1);
String name = dataPair[0],value = dataPair[1];
public static boolean updateExtraInfo(String userName, String data, ServletContext context) throws IOException {
UserInfoController userInfoController = new UserInfoController();
if(context.getAttribute(userName) == null){
userInfoController.userConfigInit(userName, context);
}
UserInfo userinfo = (UserInfo) context.getAttribute(userName);
userinfo.varInitialMap.put(name, value);
return true;
}
public static boolean deleteVariable(String userName, String configId, String data, ServletContext context) throws IOException {
Condition condition = new Condition();
ProcessQuestion process = new ProcessQuestion();
//data=configLine+"---"+conditionNameList
String name = data;
UserInfoController userInfoController = new UserInfoController();
if(context.getAttribute(userName) == null){
userInfoController.userConfigInit(userName, context);
}
UserInfo userinfo = (UserInfo) context.getAttribute(userName);
userinfo.varInitialMap.remove(name);
return true;
}
public static boolean updateExtraInfo(String userName, String data, ServletContext context) throws IOException {
UserInfoController userInfoController = new UserInfoController();
if(context.getAttribute(userName) == null){
process.userConfigInit(userName, context);
userInfoController.userConfigInit(userName, context);
}
UserInfo userinfo = (UserInfo) context.getAttribute(userName);
......@@ -177,6 +220,13 @@ public class ConditionTableController {
if(!userinfo.extraInfo.get(info).equals(""))
bw.write("-extraInfo-|" + info + "|" + userinfo.extraInfo.get(info) + "\n");
}
keyIter = userinfo.varInitialMap.keySet().iterator();
while (keyIter.hasNext()) {
String info = keyIter.next();
bw.write("-varInitial-|" + info + "|" + userinfo.varInitialMap.get(info) + "\n");
}
Iterator<Integer> idIter = userinfo.id2config.keySet().iterator();
while (idIter.hasNext()) {
int id = idIter.next();
......
......@@ -5,12 +5,11 @@ import com.alibaba.fastjson.JSONObject;
import db.SqlHelper;
import entity.Condition;
import entity.UserInfo;
import gameDesign.ElementConstructor;
import gameDesign.MapConstructor;
import gameDesign.PlotConstructor;
import gameDesign.*;
import processing.MyFunc;
import processing.ProcessQuestion;
import processing.ConditionTableController;
import processing.UserInfoController;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
......@@ -24,13 +23,14 @@ import java.io.PrintWriter;
import java.io.StringReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@WebServlet(name = "GameDesignServlet",urlPatterns = {"/gameDesign"})
public class GameDesignServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer botId;
String userName = null, mergedConfigText = "";
Integer botId, projectId, dataId;
String userName = null, moduleName;
ServletContext context = this.getServletContext();
boolean flag;
SqlHelper helper = null;
......@@ -47,56 +47,73 @@ public class GameDesignServlet extends HttpServlet {
String jsonStr = MyFunc.readJSONString(request);
if(jsonStr != null && !jsonStr.equals("")){
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
botId = (Integer)jsonObject.get("botId");
userName = (String)jsonObject.get("userName");
//moduleType = (String)jsonObject.get("moduleType");
List<Integer> idGroup = JSONArray.parseArray(jsonObject.getJSONArray("configIdGroup").toJSONString(), Integer.class);
dataId = (Integer)jsonObject.get("id");
JSONObject gameElements = helper.queryGameElementsByBotId(dataId);
botId = (Integer)gameElements.get("botId");
userName = (String)gameElements.get("userName");
/*
假设重新重新建用户信息
//假设重新重新建用户信息
if(context.getAttribute(userName) == null){
ProcessQuestion.userConfigInit(userName, context);
UserInfoController.userConfigInit(userName, context);
}
UserInfo userinfo = (UserInfo) context.getAttribute(userName);
*/
UserInfo userinfo = new UserInfo(userName);
//UserInfo userinfo = new UserInfo(userName);
//清空之前生成的配置条目
if(idGroup.size() > 0) {
try {
flag = clearOldConfig(botId, idGroup, userinfo, helper);
} catch (SQLException e) {
e.printStackTrace();
}
try {
flag = clearOldConfig(botId, userinfo, helper);
} catch (SQLException e) {
System.out.println("清空旧数据失败");
e.printStackTrace();
}
for(String moduleType:jsonObject.parseArray(jsonObject.getJSONArray("moduleType").toJSONString(), String.class)) {
switch (moduleType) {
Iterator<String> moduleIter = gameElements.keySet().iterator();
StringBuffer mergedConfigText = new StringBuffer();
while(moduleIter.hasNext()) {
moduleName = moduleIter.next();
switch (moduleName) {
case "map":
constructor = new MapConstructor(helper, botId);
break;
case "plot":
case "plots":
constructor = new PlotConstructor(helper, botId);
break;
case "goods":
constructor = new ItemConstructor(helper, botId);
break;
case "globalVarialbe":
constructor = new GlobalVariableConstructor(helper, botId);
break;
case "npc":
constructor = new CharacterConstructor(helper, botId);
break;
default:
continue;
}
constructor.construct(jsonObject);
System.out.println(moduleName);
constructor.construct(gameElements, userinfo);
//先清空再插入
idList.clear();
helper.batchInsertConfigByBotId(botId, constructor.getjConfigList(), idList);
constructor.generateConfigText(idList);
mergedConfigText += constructor.getConfigText();
if(moduleName.equals("map") || moduleName.equals("plots")) {
helper.batchInsertConfigByBotId(botId, constructor.getjConfigList(), idList);
constructor.generateConfigText(idList);
}
mergedConfigText.append(constructor.getConfigText());
}
StringReader sr = new StringReader(mergedConfigText);
StringReader sr = new StringReader(mergedConfigText.toString());
BufferedReader br = new BufferedReader(sr);
ProcessQuestion.buildUserInfo(br, userinfo, true);
UserInfoController.buildUserInfo(br, userinfo, true);
context.setAttribute(userName, userinfo);
ConditionTableController.writeConfigText(userName, userinfo);
helper.destroy();
......@@ -114,10 +131,9 @@ public class GameDesignServlet extends HttpServlet {
}
public boolean clearOldConfig(int botId, List<Integer> idGroup, UserInfo userInfo, SqlHelper helper) throws SQLException, IOException {
boolean deleteFlag = helper.deleteConfigByIdGroup(botId, idGroup);
boolean deleteFlag2 = ConditionTableController.batchDeleteConfig(userInfo.userName, idGroup, userInfo);
public boolean clearOldConfig(int botId, UserInfo userInfo, SqlHelper helper) throws SQLException, IOException {
boolean deleteFlag = helper.deleteConfigByBotId(botId);
return deleteFlag && deleteFlag2;
return deleteFlag;
}
}
......@@ -6,6 +6,7 @@ import entity.UserInfo;
import processing.ChatCacheController;
import processing.MyFunc;
import processing.ProcessQuestion;
import processing.UserInfoController;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
......@@ -25,6 +26,8 @@ public class GetAnswerServlet extends HttpServlet {
/**
* GetAnswerServlet就是处理post请求并返回答案的入口
*/
String NAME_MISSING_ERROR = "没找到bot名?";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
Map<String,String[]> curInfoPoint = new HashMap<>(request.getParameterMap());
......@@ -45,16 +48,16 @@ public class GetAnswerServlet extends HttpServlet {
UserInfo userinfo = null;
UserInfoController uiController = new UserInfoController();
try {
//userinfo = process.userInfoQuery(chatKey,context);
//从servlet根据用户名查询用户信息
userinfo = process.userInfoQuery(request.getParameter("bot_name"),context);
userinfo = uiController.userInfoQuery(request.getParameter("bot_name"),context);
} catch (SQLException e) {
state = "没找到bot名?";
state = NAME_MISSING_ERROR;
e.printStackTrace();
}
//如果historyInfoPoint不存在,就新建一个
if(historyInfoPoint == null) {
String k;
......@@ -64,7 +67,14 @@ public class GetAnswerServlet extends HttpServlet {
k = iit.next();
historyInfoPoint.put(k,"");
}
//变量初始化
iit = userinfo.varInitialMap.keySet().iterator();
while(iit.hasNext()){
k = iit.next();
historyInfoPoint.put(k, userinfo.varInitialMap.get(k));
}
}
//信息点预处理
debugFlag = process.infoPointPreprocessing(curInfoPoint, historyInfoPoint, userinfo, curInfoRecord);
......@@ -115,6 +125,6 @@ public class GetAnswerServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("do get m ethod");
System.out.println("do get method");
}
}
......@@ -3,6 +3,7 @@ package servlet;
import entity.UserInfo;
import processing.MyFunc;
import processing.ProcessQuestion;
import processing.UserInfoController;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
......@@ -38,7 +39,7 @@ public class ReloadConfigServlet extends HttpServlet {
BufferedReader br = new BufferedReader(reader);
try {
userinfo = ProcessQuestion.buildUserInfo(br, userinfo, true);
userinfo = UserInfoController.buildUserInfo(br, userinfo, true);
context.setAttribute(userinfo.userName,userinfo);
}catch(Exception e){
flag = false;
......
......@@ -4,6 +4,7 @@ import entity.Condition;
import entity.UserInfo;
import processing.MyFunc;
import processing.ProcessQuestion;
import processing.UserInfoController;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
......@@ -58,7 +59,7 @@ public class UpdateServlet extends HttpServlet {
userinfo.configStr += line+"\n";
Condition condition = new Condition();
try {
condition = ProcessQuestion.buildCondition(condition,line);
condition = UserInfoController.buildCondition(condition,line);
conditionList = userinfo.conditionTable.get(condition.indexName);
if(conditionList == null)
conditionList = new ArrayList<Condition>();
......
......@@ -3,6 +3,7 @@ package servlet;
import entity.UserInfo;
import processing.ProcessQuestion;
import processing.ConditionTableController;
import processing.UserInfoController;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
......@@ -35,9 +36,19 @@ public class UpdateSingleConfigServlet extends HttpServlet {
case "delete_config":
ConditionTableController.deleteConfig(userName, configId, data, context);
break;
case "insert_variable":
ConditionTableController.updateVariable(userName, configId, data, context);
break;
case "delete_variable":
ConditionTableController.deleteVariable(userName, configId, data, context);
break;
case "update_variable":
ConditionTableController.updateVariable(userName, configId, data, context);
break;
case "extra_info":
ConditionTableController.updateExtraInfo(userName, data, context);
break;
case "batch_update":
batchUpdate(userName, data, context);
......@@ -81,7 +92,7 @@ public class UpdateSingleConfigServlet extends HttpServlet {
public void batchUpdate(String userName, String data, ServletContext context) throws IOException {
BufferedReader br = new BufferedReader(new StringReader(data));
UserInfo userinfo = new UserInfo(userName);
userinfo = ProcessQuestion.buildUserInfo(br, userinfo, true);
userinfo = UserInfoController.buildUserInfo(br, userinfo, true);
context.setAttribute(userinfo.userName,userinfo);
System.out.println("batchupdate");
}
......
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