CharNameTable.java 6.5 KB

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