UIKeysSettings.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  29. import com.l2jserver.gameserver.data.xml.impl.UIData;
  30. /**
  31. * UI Keys Settings class.
  32. * @author mrTJO, Zoey76
  33. */
  34. public class UIKeysSettings
  35. {
  36. private static final Logger _log = Logger.getLogger(UIKeysSettings.class.getName());
  37. private final int _playerObjId;
  38. private Map<Integer, List<ActionKey>> _storedKeys;
  39. private Map<Integer, List<Integer>> _storedCategories;
  40. private boolean _saved = true;
  41. public UIKeysSettings(int playerObjId)
  42. {
  43. _playerObjId = playerObjId;
  44. loadFromDB();
  45. }
  46. public void storeAll(Map<Integer, List<Integer>> catMap, Map<Integer, List<ActionKey>> keyMap)
  47. {
  48. _saved = false;
  49. _storedCategories = catMap;
  50. _storedKeys = keyMap;
  51. }
  52. public void storeCategories(Map<Integer, List<Integer>> catMap)
  53. {
  54. _saved = false;
  55. _storedCategories = catMap;
  56. }
  57. public Map<Integer, List<Integer>> getCategories()
  58. {
  59. return _storedCategories;
  60. }
  61. public void storeKeys(Map<Integer, List<ActionKey>> keyMap)
  62. {
  63. _saved = false;
  64. _storedKeys = keyMap;
  65. }
  66. public Map<Integer, List<ActionKey>> getKeys()
  67. {
  68. return _storedKeys;
  69. }
  70. public void loadFromDB()
  71. {
  72. getCatsFromDB();
  73. getKeysFromDB();
  74. }
  75. /**
  76. * Save Categories and Mapped Keys into GameServer DataBase
  77. */
  78. public void saveInDB()
  79. {
  80. String query;
  81. if (_saved)
  82. {
  83. return;
  84. }
  85. // TODO(Zoey76): Refactor this to use batch.
  86. query = "REPLACE INTO character_ui_categories (`charId`, `catId`, `order`, `cmdId`) VALUES ";
  87. for (int category : _storedCategories.keySet())
  88. {
  89. int order = 0;
  90. for (int key : _storedCategories.get(category))
  91. {
  92. query += "(" + _playerObjId + ", " + category + ", " + (order++) + ", " + key + "),";
  93. }
  94. }
  95. query = query.substring(0, query.length() - 1) + "; ";
  96. try (Connection con = ConnectionFactory.getInstance().getConnection();
  97. PreparedStatement statement = con.prepareStatement(query))
  98. {
  99. statement.execute();
  100. }
  101. catch (Exception e)
  102. {
  103. _log.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
  104. }
  105. query = "REPLACE INTO character_ui_actions (`charId`, `cat`, `order`, `cmd`, `key`, `tgKey1`, `tgKey2`, `show`) VALUES";
  106. for (List<ActionKey> keyLst : _storedKeys.values())
  107. {
  108. int order = 0;
  109. for (ActionKey key : keyLst)
  110. {
  111. query += key.getSqlSaveString(_playerObjId, order++) + ",";
  112. }
  113. }
  114. query = query.substring(0, query.length() - 1) + ";";
  115. try (Connection con = ConnectionFactory.getInstance().getConnection();
  116. PreparedStatement statement = con.prepareStatement(query))
  117. {
  118. statement.execute();
  119. }
  120. catch (Exception e)
  121. {
  122. _log.log(Level.WARNING, "Exception: saveInDB(): " + e.getMessage(), e);
  123. }
  124. _saved = true;
  125. }
  126. public void getCatsFromDB()
  127. {
  128. if (_storedCategories != null)
  129. {
  130. return;
  131. }
  132. _storedCategories = new HashMap<>();
  133. try (Connection con = ConnectionFactory.getInstance().getConnection();
  134. PreparedStatement ps = con.prepareStatement("SELECT * FROM character_ui_categories WHERE `charId` = ? ORDER BY `catId`, `order`"))
  135. {
  136. ps.setInt(1, _playerObjId);
  137. try (ResultSet rs = ps.executeQuery())
  138. {
  139. while (rs.next())
  140. {
  141. UIData.addCategory(_storedCategories, rs.getInt("catId"), rs.getInt("cmdId"));
  142. }
  143. }
  144. }
  145. catch (Exception e)
  146. {
  147. _log.log(Level.WARNING, "Exception: getCatsFromDB(): " + e.getMessage(), e);
  148. }
  149. if (_storedCategories.isEmpty())
  150. {
  151. _storedCategories = UIData.getInstance().getCategories();
  152. }
  153. }
  154. public void getKeysFromDB()
  155. {
  156. if (_storedKeys != null)
  157. {
  158. return;
  159. }
  160. _storedKeys = new HashMap<>();
  161. try (Connection con = ConnectionFactory.getInstance().getConnection();
  162. PreparedStatement ps = con.prepareStatement("SELECT * FROM character_ui_actions WHERE `charId` = ? ORDER BY `cat`, `order`"))
  163. {
  164. ps.setInt(1, _playerObjId);
  165. try (ResultSet rs = ps.executeQuery())
  166. {
  167. while (rs.next())
  168. {
  169. int cat = rs.getInt("cat");
  170. int cmd = rs.getInt("cmd");
  171. int key = rs.getInt("key");
  172. int tgKey1 = rs.getInt("tgKey1");
  173. int tgKey2 = rs.getInt("tgKey2");
  174. int show = rs.getInt("show");
  175. UIData.addKey(_storedKeys, cat, new ActionKey(cat, cmd, key, tgKey1, tgKey2, show));
  176. }
  177. }
  178. }
  179. catch (Exception e)
  180. {
  181. _log.log(Level.WARNING, "Exception: getKeysFromDB(): " + e.getMessage(), e);
  182. }
  183. if (_storedKeys.isEmpty())
  184. {
  185. _storedKeys = UIData.getInstance().getKeys();
  186. }
  187. }
  188. public boolean isSaved()
  189. {
  190. return _saved;
  191. }
  192. }