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 objId, String name)
  59. {
  60. if (name != null)
  61. {
  62. if (!name.equalsIgnoreCase(_chars.get(objId)))
  63. _chars.put(objId, 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. PreparedStatement statement = null;
  89. try
  90. {
  91. con = L2DatabaseFactory.getInstance().getConnection();
  92. statement = con.prepareStatement("SELECT charId,accesslevel FROM characters WHERE char_name=?");
  93. statement.setString(1, name);
  94. ResultSet rset = statement.executeQuery();
  95. while (rset.next())
  96. {
  97. id = rset.getInt(1);
  98. accessLevel = rset.getInt(2);
  99. }
  100. rset.close();
  101. statement.close();
  102. }
  103. catch (SQLException e)
  104. {
  105. _log.log(Level.WARNING, "Could not check existing char name: " + e.getMessage(), e);
  106. }
  107. finally
  108. {
  109. L2DatabaseFactory.close(con);
  110. }
  111. if (id > 0)
  112. {
  113. _chars.put(id, name);
  114. _accessLevels.put(id, accessLevel);
  115. return id;
  116. }
  117. return -1; // not found
  118. }
  119. public final String getNameById(int id)
  120. {
  121. if (id <= 0)
  122. return null;
  123. String name = _chars.get(id);
  124. if (name != null)
  125. return name;
  126. if (Config.CACHE_CHAR_NAMES)
  127. return null;
  128. int accessLevel = 0;
  129. Connection con = null;
  130. PreparedStatement statement = null;
  131. try
  132. {
  133. con = L2DatabaseFactory.getInstance().getConnection();
  134. statement = con.prepareStatement("SELECT char_name,accesslevel FROM characters WHERE charId=?");
  135. statement.setInt(1, id);
  136. ResultSet rset = statement.executeQuery();
  137. while (rset.next())
  138. {
  139. name = rset.getString(1);
  140. accessLevel = rset.getInt(2);
  141. }
  142. rset.close();
  143. statement.close();
  144. }
  145. catch (SQLException e)
  146. {
  147. _log.log(Level.WARNING, "Could not check existing char id: " + e.getMessage(), e);
  148. }
  149. finally
  150. {
  151. L2DatabaseFactory.close(con);
  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. return _accessLevels.get(objectId);
  165. else
  166. return 0;
  167. }
  168. public synchronized boolean doesCharNameExist(String name)
  169. {
  170. boolean result = true;
  171. Connection con = null;
  172. try
  173. {
  174. con = L2DatabaseFactory.getInstance().getConnection();
  175. PreparedStatement statement = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?");
  176. statement.setString(1, name);
  177. ResultSet rset = statement.executeQuery();
  178. result = rset.next();
  179. rset.close();
  180. statement.close();
  181. }
  182. catch (SQLException e)
  183. {
  184. _log.log(Level.WARNING, "Could not check existing charname: " + e.getMessage(), e);
  185. }
  186. finally
  187. {
  188. L2DatabaseFactory.close(con);
  189. }
  190. return result;
  191. }
  192. public int accountCharNumber(String account)
  193. {
  194. Connection con = null;
  195. int number = 0;
  196. try
  197. {
  198. con = L2DatabaseFactory.getInstance().getConnection();
  199. PreparedStatement statement = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?");
  200. statement.setString(1, account);
  201. ResultSet rset = statement.executeQuery();
  202. while (rset.next())
  203. {
  204. number = rset.getInt(1);
  205. }
  206. rset.close();
  207. statement.close();
  208. }
  209. catch (SQLException e)
  210. {
  211. _log.log(Level.WARNING, "Could not check existing char number: " + e.getMessage(), e);
  212. }
  213. finally
  214. {
  215. L2DatabaseFactory.close(con);
  216. }
  217. return number;
  218. }
  219. private void loadAll()
  220. {
  221. String name;
  222. int id = -1;
  223. int accessLevel = 0;
  224. PreparedStatement statement = null;
  225. Connection con = null;
  226. try
  227. {
  228. con = L2DatabaseFactory.getInstance().getConnection();
  229. statement = con.prepareStatement("SELECT charId,char_name,accesslevel FROM characters");
  230. ResultSet rset = statement.executeQuery();
  231. while (rset.next())
  232. {
  233. id = rset.getInt(1);
  234. name = rset.getString(2);
  235. accessLevel = rset.getInt(3);
  236. _chars.put(id, name);
  237. _accessLevels.put(id, accessLevel);
  238. }
  239. rset.close();
  240. statement.close();
  241. }
  242. catch (SQLException e)
  243. {
  244. _log.log(Level.WARNING, "Could not load char name: " + e.getMessage(), e);
  245. }
  246. finally
  247. {
  248. L2DatabaseFactory.close(con);
  249. }
  250. _log.info(getClass().getSimpleName()+": Loaded "+_chars.size()+" char names.");
  251. }
  252. @SuppressWarnings("synthetic-access")
  253. private static class SingletonHolder
  254. {
  255. protected static final CharNameTable _instance = new CharNameTable();
  256. }
  257. }