package com.qiwu.chatbotbackend.servlet; import com.alibaba.fastjson.JSONObject; import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.entity.JConfig; import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.gameDesign.*; import com.qiwu.chatbotbackend.processing.MyFunc; import com.qiwu.chatbotbackend.processing.ConditionTableController; import com.qiwu.chatbotbackend.processing.UserInfoController; 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.*; import java.sql.SQLException; import java.util.*; @WebServlet(name = "GameDesignServlet", urlPatterns = {"/gameDesign"}) public class GameDesignServlet extends HttpServlet { String POST_URL = "http://localhost:1111/conditionProcess"; //分组及其相应的id,id初始化为0, 新建分组成功后id会更新。 HashMap categoryMap = new HashMap(){ { put("地图", 0); put("剧情", 0); put("物品", 0); put("NPC", 0); put("全局变量", 0); } }; //每个condition的三元组两端需要补充的关键词,比如npc名字、npc属性等 HashMap> relationKeywords = new HashMap>(); //特定的relation的三元组不会被添加到relationKeywords HashSet exceptRelation = new HashSet(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Integer botId, projectId, dataId, returnStatus = 1; String botAccount = null, moduleName, userName, moduleName2, returnMessage = "Success"; ServletContext context = this.getServletContext(); JSONObject json = null, root = null; Boolean flag; SqlHelper helper = null; List idList = new ArrayList<>(); ElementConstructor constructor = new ElementConstructor(); try { helper = new SqlHelper(); } catch (SQLException e) { e.printStackTrace(); } //读取并解析json数据 String jsonStr = MyFunc.readJSONString(request); if (jsonStr != null && !jsonStr.equals("")) { JSONObject receiveParaneters = JSONObject.parseObject(jsonStr); dataId = (Integer) receiveParaneters.get("id"); JSONObject gameElements = helper.queryGameElementsByBotId(dataId); System.out.println(gameElements.toJSONString()); botId = (Integer)gameElements.get("botId"); botAccount = (String) gameElements.get("botAccount"); userName = (String)gameElements.get("userName"); System.out.println("botId:" + botId + ",botAccount:" + botAccount); //假设重建用户信息 if (context.getAttribute(botAccount) == null) { UserInfoController.userConfigInit(botAccount, context); } UserInfo userinfo = (UserInfo) context.getAttribute(botAccount); //UserInfo userinfo = new UserInfo(botAccount); //清空之前生成的配置条目 try { flag = botInit(botId, userinfo, helper); } catch (SQLException e) { System.out.println("清空旧数据失败"); e.printStackTrace(); } Iterator moduleIter = gameElements.keySet().iterator(); StringBuffer mergedConfigText = new StringBuffer(); while (moduleIter.hasNext()) { moduleName = moduleIter.next(); switch (moduleName) { case "map": moduleName2 = "地图"; constructor = new MapConstructor(helper, botId, exceptRelation); constructor.construct(gameElements, userinfo); break; case "plots": moduleName2 = "剧情"; constructor = new PlotConstructor(helper, botId, exceptRelation); constructor.construct(gameElements, userinfo); root = ((PlotConstructor) constructor).getId2j().get((String) gameElements.get("start")); break; case "goods": moduleName2 = "物品"; constructor = new ItemConstructor(helper, botId, exceptRelation); constructor.construct(gameElements, userinfo); break; case "globalVarialbe": moduleName2 = "全局变量"; constructor = new GlobalVariableConstructor(helper, botId, exceptRelation); constructor.construct(gameElements, userinfo); break; case "npc": moduleName2 = "NPC"; constructor = new CharacterConstructor(helper, botId, exceptRelation); constructor.construct(gameElements, userinfo); break; default: continue; } if (constructor.getOperationStatus() == 1) { System.out.println(moduleName); //先清空再插入 idList.clear(); helper.batchInsertVariableByBotId(botId, constructor); List jsonList = new ArrayList(); for(JConfig jConfig: constructor.getjConfigList()){ json = jConfig.toJson(constructor.getConditionDescription()); constructor.parseKeyword(json, relationKeywords, exceptRelation); jsonList.add(json); } helper.batchInsertConfigByBotId(botId, jsonList, idList, categoryMap.get(moduleName2)); constructor.postProcessing(idList, categoryMap.get(moduleName2)); constructor.generateConfigText(idList); mergedConfigText.append(constructor.getConfigText()); }else { returnStatus = 0; returnMessage = constructor.getMessage(); break; } } if(returnStatus == 1) { StringReader sr = new StringReader(mergedConfigText.toString()); BufferedReader br = new BufferedReader(sr); UserInfoController.buildUserInfo(br, userinfo, true); context.setAttribute(botAccount, userinfo); ConditionTableController.writeConfigText(botAccount, userinfo); String postResult = actionRegenerate(root, gameElements, userinfo); if(!postResult.equals("success")){ returnStatus = 0; returnMessage = postResult; } } helper.destroy(); }else { returnStatus = 0; returnMessage = "请求解析失败"; } JSONObject responseJSONObject = new JSONObject(); responseJSONObject.put("status", returnStatus); responseJSONObject.put("message", returnMessage); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); 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 { OutputStream out = response.getOutputStream(); out.write("ssss".getBytes()); out.flush(); } public boolean botInit(int botId, UserInfo userInfo, SqlHelper helper) throws SQLException, IOException { boolean deleteConfigFlag = helper.deleteConfigByBotId(botId); boolean deleteVariableFlag = helper.deleteVariableByBotId(botId); boolean deleteCategoryFlag = helper.deleteCategoryByBotId(botId); boolean batchInsertCategoryFlag = helper.batchInsertCategoryByBotId(botId, categoryMap); return deleteConfigFlag && deleteVariableFlag; } public String actionRegenerate(JSONObject root, JSONObject gameElements, UserInfo userInfo){ String startCondition; String botName = (String)gameElements.get("botName"); JSONObject returnMessage = null; JSONObject actionPostData = new JSONObject(); System.out.println("root"+ root.toJSONString()); startCondition = (String)root.get("conditionName"); actionPostData.put("startCondition", startCondition); actionPostData.put("botName_yhm", (String)gameElements.get("botName")); actionPostData.put("botName", botName); actionPostData.put("botId", (Integer)gameElements.get("botId")); actionPostData.put("type", "game_plot_init"); actionPostData.put("welcome", (String)gameElements.get("plotStartText")); actionPostData.put("relationKeywords", relationKeywords); System.out.println(actionPostData.toJSONString()); userInfo.extraInfo.put("activeAction", startCondition); try { returnMessage = MyFunc.postJSON(POST_URL, actionPostData); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if((Integer)returnMessage.get("status") == 1) { return "success"; }else { System.out.println(returnMessage.get("parameter")); System.out.println("语义关系修改失败"); return (String)returnMessage.get("parameter"); } } }