package servlet; import com.alibaba.fastjson.JSONObject; import db.SqlHelper; import entity.UserInfo; import gameDesign.*; import processing.MyFunc; import processing.ConditionTableController; import 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.BufferedReader; import java.io.IOException; 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, projectId, dataId; String userName = null, moduleName; ServletContext context = this.getServletContext(); boolean flag; SqlHelper helper = null; List idList = new ArrayList<>(); ElementConstructor constructor = null; try { helper = new SqlHelper(); } catch (SQLException e) { e.printStackTrace(); } //读取并解析json数据 String jsonStr = MyFunc.readJSONString(request); if (jsonStr != null && !jsonStr.equals("")) { JSONObject jsonObject = JSONObject.parseObject(jsonStr); 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) { UserInfoController.userConfigInit(userName, context); } UserInfo userinfo = (UserInfo) context.getAttribute(userName); //UserInfo userinfo = new UserInfo(userName); //清空之前生成的配置条目 try { flag = clearOldConfig(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": constructor = new MapConstructor(helper, botId); break; 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; } System.out.println(moduleName); constructor.construct(gameElements, userinfo); //先清空再插入 idList.clear(); helper.batchInsertVariableByBotId(botId, constructor); helper.batchInsertConfigByBotId(botId, constructor, idList); constructor.generateConfigText(idList); mergedConfigText.append(constructor.getConfigText()); } StringReader sr = new StringReader(mergedConfigText.toString()); BufferedReader br = new BufferedReader(sr); UserInfoController.buildUserInfo(br, userinfo, true); context.setAttribute(userName, userinfo); ConditionTableController.writeConfigText(userName, userinfo); helper.destroy(); } response.setCharacterEncoding("utf-8"); response.setContentType("text/plain"); response.setStatus(111); PrintWriter out = response.getWriter(); out.println("完成\\\n"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public boolean clearOldConfig(int botId, UserInfo userInfo, SqlHelper helper) throws SQLException, IOException { boolean deleteConfigFlag = helper.deleteConfigByBotId(botId); boolean deleteVariableFlag = helper.deleteVariableByBotId(botId); return deleteConfigFlag && deleteVariableFlag; } }