CharNameTable.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.data.sql.impl;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.Map;
  26. import java.util.Map.Entry;
  27. import java.util.concurrent.ConcurrentHashMap;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. /**
  34. * Loads name and access level for all players.
  35. * @version 2005/03/27
  36. */
  37. public class CharNameTable
  38. {
  39. private static Logger _log = Logger.getLogger(CharNameTable.class.getName());
  40. private final Map<Integer, String> _chars = new ConcurrentHashMap<>();
  41. private final Map<Integer, Integer> _accessLevels = new ConcurrentHashMap<>();
  42. protected CharNameTable()
  43. {
  44. if (Config.CACHE_CHAR_NAMES)
  45. {
  46. loadAll();
  47. }
  48. }
  49. public final void addName(L2PcInstance player)
  50. {
  51. if (player != null)
  52. {
  53. addName(player.getObjectId(), player.getName());
  54. _accessLevels.put(player.getObjectId(), player.getAccessLevel().getLevel());
  55. }
  56. }
  57. private final void addName(int objectId, String name)
  58. {
  59. if (name != null)
  60. {
  61. if (!name.equals(_chars.get(objectId)))
  62. {
  63. _chars.put(objectId, name);
  64. }
  65. }
  66. }
  67. public final void removeName(int objId)
  68. {
  69. _chars.remove(objId);
  70. _accessLevels.remove(objId);
  71. }
  72. public final int getIdByName(String name)
  73. {
  74. if ((name == null) || name.isEmpty())
  75. {
  76. return -1;
  77. }
  78. for (Entry<Integer, String> entry : _chars.entrySet())
  79. {
  80. if (entry.getValue().equalsIgnoreCase(name))
  81. {
  82. return entry.getKey();
  83. }
  84. }
  85. if (Config.CACHE_CHAR_NAMES)
  86. {
  87. return -1;
  88. }
  89. int id = -1;
  90. int accessLevel = 0;
  91. try (Connection con = ConnectionFactory.getInstance().getConnection();
  92. PreparedStatement ps = con.prepareStatement("SELECT charId,accesslevel FROM characters WHERE char_name=?"))
  93. {
  94. ps.setString(1, name);
  95. try (ResultSet rs = ps.executeQuery())
  96. {
  97. while (rs.next())
  98. {
  99. id = rs.getInt(1);
  100. accessLevel = rs.getInt(2);
  101. }
  102. }
  103. }
  104. catch (SQLException e)
  105. {
  106. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing char name: " + e.getMessage(), e);
  107. }
  108. if (id > 0)
  109. {
  110. _chars.put(id, name);
  111. _accessLevels.put(id, accessLevel);
  112. return id;
  113. }
  114. return -1; // not found
  115. }
  116. public final String getNameById(int id)
  117. {
  118. if (id <= 0)
  119. {
  120. return null;
  121. }
  122. String name = _chars.get(id);
  123. if (name != null)
  124. {
  125. return name;
  126. }
  127. if (Config.CACHE_CHAR_NAMES)
  128. {
  129. return null;
  130. }
  131. try (Connection con = ConnectionFactory.getInstance().getConnection();
  132. PreparedStatement ps = con.prepareStatement("SELECT char_name,accesslevel FROM characters WHERE charId=?"))
  133. {
  134. ps.setInt(1, id);
  135. try (ResultSet rset = ps.executeQuery())
  136. {
  137. if (rset.next())
  138. {
  139. name = rset.getString(1);
  140. _chars.put(id, name);
  141. _accessLevels.put(id, rset.getInt(2));
  142. return name;
  143. }
  144. }
  145. }
  146. catch (SQLException e)
  147. {
  148. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing char id: " + e.getMessage(), e);
  149. }
  150. return null; // not found
  151. }
  152. public final int getAccessLevelById(int objectId)
  153. {
  154. if (getNameById(objectId) != null)
  155. {
  156. return _accessLevels.get(objectId);
  157. }
  158. return 0;
  159. }
  160. public synchronized boolean doesCharNameExist(String name)
  161. {
  162. boolean result = true;
  163. try (Connection con = ConnectionFactory.getInstance().getConnection();
  164. PreparedStatement ps = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?"))
  165. {
  166. ps.setString(1, name);
  167. try (ResultSet rs = ps.executeQuery())
  168. {
  169. result = rs.next();
  170. }
  171. }
  172. catch (SQLException e)
  173. {
  174. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing charname: " + e.getMessage(), e);
  175. }
  176. return result;
  177. }
  178. public int getAccountCharacterCount(String account)
  179. {
  180. try (Connection con = ConnectionFactory.getInstance().getConnection();
  181. PreparedStatement ps = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?"))
  182. {
  183. ps.setString(1, account);
  184. try (ResultSet rset = ps.executeQuery())
  185. {
  186. while (rset.next())
  187. {
  188. return rset.getInt(1);
  189. }
  190. }
  191. }
  192. catch (SQLException e)
  193. {
  194. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing char count: " + e.getMessage(), e);
  195. }
  196. return 0;
  197. }
  198. private void loadAll()
  199. {
  200. try (Connection con = ConnectionFactory.getInstance().getConnection();
  201. Statement s = con.createStatement();
  202. ResultSet rs = s.executeQuery("SELECT charId, char_name, accesslevel FROM characters"))
  203. {
  204. while (rs.next())
  205. {
  206. final int id = rs.getInt(1);
  207. _chars.put(id, rs.getString(2));
  208. _accessLevels.put(id, rs.getInt(3));
  209. }
  210. }
  211. catch (SQLException e)
  212. {
  213. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not load char name: " + e.getMessage(), e);
  214. }
  215. _log.info(getClass().getSimpleName() + ": Loaded " + _chars.size() + " char names.");
  216. }
  217. public static CharNameTable getInstance()
  218. {
  219. return SingletonHolder._instance;
  220. }
  221. private static class SingletonHolder
  222. {
  223. protected static final CharNameTable _instance = new CharNameTable();
  224. }
  225. }