CharNameTable.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.datatables;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.util.Iterator;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.util.L2FastMap;
  30. /**
  31. * This class ...
  32. * @version $Revision: 1.3.2.2.2.1 $ $Date: 2005/03/27 15:29:18 $
  33. */
  34. public class CharNameTable
  35. {
  36. private static Logger _log = Logger.getLogger(CharNameTable.class.getName());
  37. private final Map<Integer, String> _chars = new L2FastMap<>();
  38. private final Map<Integer, Integer> _accessLevels = new L2FastMap<>();
  39. protected CharNameTable()
  40. {
  41. if (Config.CACHE_CHAR_NAMES)
  42. {
  43. loadAll();
  44. }
  45. }
  46. public static CharNameTable getInstance()
  47. {
  48. return SingletonHolder._instance;
  49. }
  50. public final void addName(L2PcInstance player)
  51. {
  52. if (player != null)
  53. {
  54. addName(player.getObjectId(), player.getName());
  55. _accessLevels.put(player.getObjectId(), player.getAccessLevel().getLevel());
  56. }
  57. }
  58. private final void addName(int objectId, String name)
  59. {
  60. if (name != null)
  61. {
  62. if (!name.equals(_chars.get(objectId)))
  63. {
  64. _chars.put(objectId, name);
  65. }
  66. }
  67. }
  68. public final void removeName(int objId)
  69. {
  70. _chars.remove(objId);
  71. _accessLevels.remove(objId);
  72. }
  73. public final int getIdByName(String name)
  74. {
  75. if ((name == null) || name.isEmpty())
  76. {
  77. return -1;
  78. }
  79. Iterator<Entry<Integer, String>> it = _chars.entrySet().iterator();
  80. Map.Entry<Integer, String> pair;
  81. while (it.hasNext())
  82. {
  83. pair = it.next();
  84. if (pair.getValue().equalsIgnoreCase(name))
  85. {
  86. return pair.getKey();
  87. }
  88. }
  89. if (Config.CACHE_CHAR_NAMES)
  90. {
  91. return -1;
  92. }
  93. int id = -1;
  94. int accessLevel = 0;
  95. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  96. PreparedStatement ps = con.prepareStatement("SELECT charId,accesslevel FROM characters WHERE char_name=?"))
  97. {
  98. ps.setString(1, name);
  99. try (ResultSet rs = ps.executeQuery())
  100. {
  101. while (rs.next())
  102. {
  103. id = rs.getInt(1);
  104. accessLevel = rs.getInt(2);
  105. }
  106. }
  107. }
  108. catch (SQLException e)
  109. {
  110. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing char name: " + e.getMessage(), e);
  111. }
  112. if (id > 0)
  113. {
  114. _chars.put(id, name);
  115. _accessLevels.put(id, accessLevel);
  116. return id;
  117. }
  118. return -1; // not found
  119. }
  120. public final String getNameById(int id)
  121. {
  122. if (id <= 0)
  123. {
  124. return null;
  125. }
  126. String name = _chars.get(id);
  127. if (name != null)
  128. {
  129. return name;
  130. }
  131. if (Config.CACHE_CHAR_NAMES)
  132. {
  133. return null;
  134. }
  135. int accessLevel = 0;
  136. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  137. PreparedStatement ps = con.prepareStatement("SELECT char_name,accesslevel FROM characters WHERE charId=?"))
  138. {
  139. ps.setInt(1, id);
  140. try (ResultSet rset = ps.executeQuery())
  141. {
  142. while (rset.next())
  143. {
  144. name = rset.getString(1);
  145. accessLevel = rset.getInt(2);
  146. }
  147. }
  148. }
  149. catch (SQLException e)
  150. {
  151. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing char id: " + e.getMessage(), e);
  152. }
  153. if ((name != null) && !name.isEmpty())
  154. {
  155. _chars.put(id, name);
  156. _accessLevels.put(id, accessLevel);
  157. return name;
  158. }
  159. return null; // not found
  160. }
  161. public final int getAccessLevelById(int objectId)
  162. {
  163. if (getNameById(objectId) != null)
  164. {
  165. return _accessLevels.get(objectId);
  166. }
  167. return 0;
  168. }
  169. public synchronized boolean doesCharNameExist(String name)
  170. {
  171. boolean result = true;
  172. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  173. PreparedStatement ps = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?"))
  174. {
  175. ps.setString(1, name);
  176. try (ResultSet rs = ps.executeQuery())
  177. {
  178. result = rs.next();
  179. }
  180. }
  181. catch (SQLException e)
  182. {
  183. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing charname: " + e.getMessage(), e);
  184. }
  185. return result;
  186. }
  187. public int accountCharNumber(String account)
  188. {
  189. int number = 0;
  190. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  191. PreparedStatement ps = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?"))
  192. {
  193. ps.setString(1, account);
  194. try (ResultSet rset = ps.executeQuery())
  195. {
  196. while (rset.next())
  197. {
  198. number = rset.getInt(1);
  199. }
  200. }
  201. }
  202. catch (SQLException e)
  203. {
  204. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing char number: " + e.getMessage(), e);
  205. }
  206. return number;
  207. }
  208. private void loadAll()
  209. {
  210. String name;
  211. int id = -1;
  212. int accessLevel = 0;
  213. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  214. Statement s = con.createStatement();
  215. ResultSet rs = s.executeQuery("SELECT charId,char_name,accesslevel FROM characters"))
  216. {
  217. while (rs.next())
  218. {
  219. id = rs.getInt(1);
  220. name = rs.getString(2);
  221. accessLevel = rs.getInt(3);
  222. _chars.put(id, name);
  223. _accessLevels.put(id, accessLevel);
  224. }
  225. }
  226. catch (SQLException e)
  227. {
  228. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not load char name: " + e.getMessage(), e);
  229. }
  230. _log.info(getClass().getSimpleName() + ": Loaded " + _chars.size() + " char names.");
  231. }
  232. private static class SingletonHolder
  233. {
  234. protected static final CharNameTable _instance = new CharNameTable();
  235. }
  236. }