CharNameTable.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.util.Iterator;
  21. import java.util.Map;
  22. import java.util.Map.Entry;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastMap;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. /**
  30. * This class ...
  31. *
  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;
  38. private final Map<Integer, Integer> _accessLevels;
  39. private CharNameTable()
  40. {
  41. _chars = new FastMap<Integer, String>();
  42. _accessLevels = new FastMap<Integer, Integer>();
  43. if (Config.CACHE_CHAR_NAMES)
  44. loadAll();
  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. _chars.put(objectId, name);
  64. }
  65. }
  66. public final void removeName(int objId)
  67. {
  68. _chars.remove(objId);
  69. _accessLevels.remove(objId);
  70. }
  71. public final int getIdByName(String name)
  72. {
  73. if (name == null || name.isEmpty())
  74. return -1;
  75. Iterator<Entry<Integer, String>> it = _chars.entrySet().iterator();
  76. Map.Entry<Integer, String> pair;
  77. while (it.hasNext())
  78. {
  79. pair = it.next();
  80. if (pair.getValue().equalsIgnoreCase(name))
  81. return pair.getKey();
  82. }
  83. if (Config.CACHE_CHAR_NAMES)
  84. return -1;
  85. int id = -1;
  86. int accessLevel = 0;
  87. Connection con = null;
  88. try
  89. {
  90. con = L2DatabaseFactory.getInstance().getConnection();
  91. PreparedStatement statement = con.prepareStatement("SELECT charId,accesslevel FROM characters WHERE char_name=?");
  92. statement.setString(1, name);
  93. ResultSet rset = statement.executeQuery();
  94. while (rset.next())
  95. {
  96. id = rset.getInt(1);
  97. accessLevel = rset.getInt(2);
  98. }
  99. rset.close();
  100. statement.close();
  101. }
  102. catch (SQLException e)
  103. {
  104. _log.log(Level.WARNING, "Could not check existing char name: " + e.getMessage(), e);
  105. }
  106. finally
  107. {
  108. L2DatabaseFactory.close(con);
  109. }
  110. if (id > 0)
  111. {
  112. _chars.put(id, name);
  113. _accessLevels.put(id, accessLevel);
  114. return id;
  115. }
  116. return -1; // not found
  117. }
  118. public final String getNameById(int id)
  119. {
  120. if (id <= 0)
  121. return null;
  122. String name = _chars.get(id);
  123. if (name != null)
  124. return name;
  125. if (Config.CACHE_CHAR_NAMES)
  126. return null;
  127. int accessLevel = 0;
  128. Connection con = null;
  129. PreparedStatement statement = null;
  130. try
  131. {
  132. con = L2DatabaseFactory.getInstance().getConnection();
  133. statement = con.prepareStatement("SELECT char_name,accesslevel FROM characters WHERE charId=?");
  134. statement.setInt(1, id);
  135. ResultSet rset = statement.executeQuery();
  136. while (rset.next())
  137. {
  138. name = rset.getString(1);
  139. accessLevel = rset.getInt(2);
  140. }
  141. rset.close();
  142. statement.close();
  143. }
  144. catch (SQLException e)
  145. {
  146. _log.log(Level.WARNING, "Could not check existing char id: " + e.getMessage(), e);
  147. }
  148. finally
  149. {
  150. L2DatabaseFactory.close(con);
  151. }
  152. if (name != null && !name.isEmpty())
  153. {
  154. _chars.put(id, name);
  155. _accessLevels.put(id, accessLevel);
  156. return name;
  157. }
  158. return null; //not found
  159. }
  160. public final int getAccessLevelById(int objectId)
  161. {
  162. if (getNameById(objectId) != null)
  163. return _accessLevels.get(objectId);
  164. return 0;
  165. }
  166. public synchronized boolean doesCharNameExist(String name)
  167. {
  168. boolean result = true;
  169. Connection con = null;
  170. try
  171. {
  172. con = L2DatabaseFactory.getInstance().getConnection();
  173. PreparedStatement statement = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?");
  174. statement.setString(1, name);
  175. ResultSet rset = statement.executeQuery();
  176. result = rset.next();
  177. rset.close();
  178. statement.close();
  179. }
  180. catch (SQLException e)
  181. {
  182. _log.log(Level.WARNING, "Could not check existing charname: " + e.getMessage(), e);
  183. }
  184. finally
  185. {
  186. L2DatabaseFactory.close(con);
  187. }
  188. return result;
  189. }
  190. public int accountCharNumber(String account)
  191. {
  192. Connection con = null;
  193. int number = 0;
  194. try
  195. {
  196. con = L2DatabaseFactory.getInstance().getConnection();
  197. PreparedStatement statement = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?");
  198. statement.setString(1, account);
  199. ResultSet rset = statement.executeQuery();
  200. while (rset.next())
  201. {
  202. number = rset.getInt(1);
  203. }
  204. rset.close();
  205. statement.close();
  206. }
  207. catch (SQLException e)
  208. {
  209. _log.log(Level.WARNING, "Could not check existing char number: " + e.getMessage(), e);
  210. }
  211. finally
  212. {
  213. L2DatabaseFactory.close(con);
  214. }
  215. return number;
  216. }
  217. private void loadAll()
  218. {
  219. String name;
  220. int id = -1;
  221. int accessLevel = 0;
  222. PreparedStatement statement = null;
  223. Connection con = null;
  224. try
  225. {
  226. con = L2DatabaseFactory.getInstance().getConnection();
  227. statement = con.prepareStatement("SELECT charId,char_name,accesslevel FROM characters");
  228. ResultSet rset = statement.executeQuery();
  229. while (rset.next())
  230. {
  231. id = rset.getInt(1);
  232. name = rset.getString(2);
  233. accessLevel = rset.getInt(3);
  234. _chars.put(id, name);
  235. _accessLevels.put(id, accessLevel);
  236. }
  237. rset.close();
  238. statement.close();
  239. }
  240. catch (SQLException e)
  241. {
  242. _log.log(Level.WARNING, "Could not load char name: " + e.getMessage(), e);
  243. }
  244. finally
  245. {
  246. L2DatabaseFactory.close(con);
  247. }
  248. _log.info(getClass().getSimpleName()+": Loaded "+_chars.size()+" char names.");
  249. }
  250. @SuppressWarnings("synthetic-access")
  251. private static class SingletonHolder
  252. {
  253. protected static final CharNameTable _instance = new CharNameTable();
  254. }
  255. }