CharNameTable.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 javolution.util.FastMap;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. /**
  31. * This class ...
  32. *
  33. * @version $Revision: 1.3.2.2.2.1 $ $Date: 2005/03/27 15:29:18 $
  34. */
  35. public class CharNameTable
  36. {
  37. private static Logger _log = Logger.getLogger(CharNameTable.class.getName());
  38. private final Map<Integer, String> _chars;
  39. private final Map<Integer, Integer> _accessLevels;
  40. protected CharNameTable()
  41. {
  42. _chars = new FastMap<>();
  43. _accessLevels = new FastMap<>();
  44. if (Config.CACHE_CHAR_NAMES)
  45. loadAll();
  46. }
  47. public static CharNameTable getInstance()
  48. {
  49. return SingletonHolder._instance;
  50. }
  51. public final void addName(L2PcInstance player)
  52. {
  53. if (player != null)
  54. {
  55. addName(player.getObjectId(), player.getName());
  56. _accessLevels.put(player.getObjectId(), player.getAccessLevel().getLevel());
  57. }
  58. }
  59. private final void addName(int objectId, String name)
  60. {
  61. if (name != null)
  62. {
  63. if (!name.equals(_chars.get(objectId)))
  64. _chars.put(objectId, name);
  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. return -1;
  76. Iterator<Entry<Integer, String>> it = _chars.entrySet().iterator();
  77. Map.Entry<Integer, String> pair;
  78. while (it.hasNext())
  79. {
  80. pair = it.next();
  81. if (pair.getValue().equalsIgnoreCase(name))
  82. return pair.getKey();
  83. }
  84. if (Config.CACHE_CHAR_NAMES)
  85. return -1;
  86. int id = -1;
  87. int accessLevel = 0;
  88. Connection con = null;
  89. try
  90. {
  91. con = L2DatabaseFactory.getInstance().getConnection();
  92. try (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. }
  105. catch (SQLException e)
  106. {
  107. _log.log(Level.WARNING, "Could not check existing char name: " + e.getMessage(), e);
  108. }
  109. finally
  110. {
  111. L2DatabaseFactory.close(con);
  112. }
  113. if (id > 0)
  114. {
  115. _chars.put(id, name);
  116. _accessLevels.put(id, accessLevel);
  117. return id;
  118. }
  119. return -1; // not found
  120. }
  121. public final String getNameById(int id)
  122. {
  123. if (id <= 0)
  124. return null;
  125. String name = _chars.get(id);
  126. if (name != null)
  127. return name;
  128. if (Config.CACHE_CHAR_NAMES)
  129. return null;
  130. int accessLevel = 0;
  131. Connection con = null;
  132. try
  133. {
  134. con = L2DatabaseFactory.getInstance().getConnection();
  135. try (PreparedStatement ps = con.prepareStatement("SELECT char_name,accesslevel FROM characters WHERE charId=?"))
  136. {
  137. ps.setInt(1, id);
  138. try (ResultSet rset = ps.executeQuery())
  139. {
  140. while (rset.next())
  141. {
  142. name = rset.getString(1);
  143. accessLevel = rset.getInt(2);
  144. }
  145. }
  146. }
  147. }
  148. catch (SQLException e)
  149. {
  150. _log.log(Level.WARNING, "Could not check existing char id: " + e.getMessage(), e);
  151. }
  152. finally
  153. {
  154. L2DatabaseFactory.close(con);
  155. }
  156. if (name != null && !name.isEmpty())
  157. {
  158. _chars.put(id, name);
  159. _accessLevels.put(id, accessLevel);
  160. return name;
  161. }
  162. return null; //not found
  163. }
  164. public final int getAccessLevelById(int objectId)
  165. {
  166. if (getNameById(objectId) != null)
  167. return _accessLevels.get(objectId);
  168. return 0;
  169. }
  170. public synchronized boolean doesCharNameExist(String name)
  171. {
  172. boolean result = true;
  173. Connection con = null;
  174. try
  175. {
  176. con = L2DatabaseFactory.getInstance().getConnection();
  177. try (PreparedStatement ps = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?"))
  178. {
  179. ps.setString(1, name);
  180. try (ResultSet rs = ps.executeQuery())
  181. {
  182. result = rs.next();
  183. }
  184. }
  185. }
  186. catch (SQLException e)
  187. {
  188. _log.log(Level.WARNING, "Could not check existing charname: " + e.getMessage(), e);
  189. }
  190. finally
  191. {
  192. L2DatabaseFactory.close(con);
  193. }
  194. return result;
  195. }
  196. public int accountCharNumber(String account)
  197. {
  198. Connection con = null;
  199. int number = 0;
  200. try
  201. {
  202. con = L2DatabaseFactory.getInstance().getConnection();
  203. try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?"))
  204. {
  205. ps.setString(1, account);
  206. try (ResultSet rset = ps.executeQuery())
  207. {
  208. while (rset.next())
  209. {
  210. number = rset.getInt(1);
  211. }
  212. }
  213. }
  214. }
  215. catch (SQLException e)
  216. {
  217. _log.log(Level.WARNING, "Could not check existing char number: " + e.getMessage(), e);
  218. }
  219. finally
  220. {
  221. L2DatabaseFactory.close(con);
  222. }
  223. return number;
  224. }
  225. private void loadAll()
  226. {
  227. String name;
  228. int id = -1;
  229. int accessLevel = 0;
  230. Connection con = null;
  231. try
  232. {
  233. con = L2DatabaseFactory.getInstance().getConnection();
  234. try (Statement s = con.createStatement();
  235. ResultSet rs = s.executeQuery("SELECT charId,char_name,accesslevel FROM characters"))
  236. {
  237. while (rs.next())
  238. {
  239. id = rs.getInt(1);
  240. name = rs.getString(2);
  241. accessLevel = rs.getInt(3);
  242. _chars.put(id, name);
  243. _accessLevels.put(id, accessLevel);
  244. }
  245. }
  246. }
  247. catch (SQLException e)
  248. {
  249. _log.log(Level.WARNING, "Could not load char name: " + e.getMessage(), e);
  250. }
  251. finally
  252. {
  253. L2DatabaseFactory.close(con);
  254. }
  255. _log.info(getClass().getSimpleName()+": Loaded "+_chars.size()+" char names.");
  256. }
  257. private static class SingletonHolder
  258. {
  259. protected static final CharNameTable _instance = new CharNameTable();
  260. }
  261. }