CharNameTable.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.Logger;
  24. import javolution.util.FastMap;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.L2DatabaseFactory;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. /**
  29. * This class ...
  30. *
  31. * @version $Revision: 1.3.2.2.2.1 $ $Date: 2005/03/27 15:29:18 $
  32. */
  33. public class CharNameTable
  34. {
  35. private static Logger _log = Logger.getLogger(CharNameTable.class.getName());
  36. private final Map<Integer, String> _chars;
  37. private final Map<Integer, Integer> _accessLevels;
  38. private CharNameTable()
  39. {
  40. _chars = new FastMap<Integer, String>();
  41. _accessLevels = new FastMap<Integer, Integer>();
  42. if (Config.CACHE_CHAR_NAMES)
  43. loadAll();
  44. }
  45. public static CharNameTable getInstance()
  46. {
  47. return SingletonHolder._instance;
  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. public final void addName(int objId, String name)
  58. {
  59. if (name != null)
  60. {
  61. if (!name.equalsIgnoreCase(_chars.get(objId)))
  62. _chars.put(objId, name);
  63. }
  64. }
  65. public final void removeName(int objId)
  66. {
  67. _chars.remove(objId);
  68. }
  69. public final int getIdByName(String name)
  70. {
  71. Iterator<Entry<Integer, String>> it = _chars.entrySet().iterator();
  72. while (it.hasNext())
  73. {
  74. Map.Entry<Integer, String> pair = it.next();
  75. if (pair.getValue().equalsIgnoreCase(name))
  76. return pair.getKey();
  77. }
  78. if (Config.CACHE_CHAR_NAMES)
  79. return -1;
  80. int id = -1;
  81. int accessLevel = 0;
  82. Connection con = null;
  83. PreparedStatement statement = null;
  84. try
  85. {
  86. con = L2DatabaseFactory.getInstance().getConnection();
  87. statement = con.prepareStatement("SELECT charId,accesslevel FROM characters WHERE char_name=?");
  88. statement.setString(1, name);
  89. ResultSet rset = statement.executeQuery();
  90. while (rset.next())
  91. {
  92. id = rset.getInt(1);
  93. accessLevel = rset.getInt(2);
  94. }
  95. rset.close();
  96. }
  97. catch (SQLException e)
  98. {
  99. _log.warning("could not check existing char name:" + e.getMessage());
  100. }
  101. finally
  102. {
  103. try
  104. {
  105. statement.close();
  106. }
  107. catch (Exception e) {}
  108. try
  109. {
  110. con.close();
  111. }
  112. catch (Exception e) {}
  113. }
  114. if (id > 0)
  115. {
  116. _chars.put(id, name);
  117. _accessLevels.put(id, accessLevel);
  118. }
  119. return id;
  120. }
  121. public final String getNameById(int id)
  122. {
  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. }
  144. catch (SQLException e)
  145. {
  146. _log.warning("could not check existing char id:" + e.getMessage());
  147. }
  148. finally
  149. {
  150. try
  151. {
  152. statement.close();
  153. }
  154. catch (Exception e) {}
  155. try
  156. {
  157. con.close();
  158. }
  159. catch (Exception e) {}
  160. }
  161. if (name != null && !name.isEmpty())
  162. {
  163. _chars.put(id, name);
  164. _accessLevels.put(id, accessLevel);
  165. }
  166. return name;
  167. }
  168. public final int getAccessLevelById(int objectId)
  169. {
  170. if (getNameById(objectId) != null)
  171. return _accessLevels.get(objectId);
  172. else
  173. return 0;
  174. }
  175. public synchronized boolean doesCharNameExist(String name)
  176. {
  177. boolean result = true;
  178. Connection con = null;
  179. try
  180. {
  181. con = L2DatabaseFactory.getInstance().getConnection();
  182. PreparedStatement statement = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?");
  183. statement.setString(1, name);
  184. ResultSet rset = statement.executeQuery();
  185. result = rset.next();
  186. rset.close();
  187. statement.close();
  188. }
  189. catch (SQLException e)
  190. {
  191. _log.warning("could not check existing charname:" + e.getMessage());
  192. }
  193. finally
  194. {
  195. try
  196. {
  197. con.close();
  198. }
  199. catch (Exception e)
  200. {
  201. }
  202. }
  203. return result;
  204. }
  205. public int accountCharNumber(String account)
  206. {
  207. Connection con = null;
  208. int number = 0;
  209. try
  210. {
  211. con = L2DatabaseFactory.getInstance().getConnection();
  212. PreparedStatement statement = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?");
  213. statement.setString(1, account);
  214. ResultSet rset = statement.executeQuery();
  215. while (rset.next())
  216. {
  217. number = rset.getInt(1);
  218. }
  219. rset.close();
  220. statement.close();
  221. }
  222. catch (SQLException e)
  223. {
  224. _log.warning("could not check existing char number:" + e.getMessage());
  225. }
  226. finally
  227. {
  228. try
  229. {
  230. con.close();
  231. }
  232. catch (Exception e)
  233. {
  234. }
  235. }
  236. return number;
  237. }
  238. private void loadAll()
  239. {
  240. String name;
  241. int id = -1;
  242. int accessLevel = 0;
  243. PreparedStatement statement = null;
  244. Connection con = null;
  245. try
  246. {
  247. con = L2DatabaseFactory.getInstance().getConnection();
  248. statement = con.prepareStatement("SELECT charId,char_name,accesslevel FROM characters");
  249. ResultSet rset = statement.executeQuery();
  250. while (rset.next())
  251. {
  252. id = rset.getInt(1);
  253. name = rset.getString(2);
  254. accessLevel = rset.getInt(3);
  255. _chars.put(id, name);
  256. _accessLevels.put(id, accessLevel);
  257. }
  258. rset.close();
  259. }
  260. catch (SQLException e)
  261. {
  262. _log.warning("could not load char name: " + e.getMessage());
  263. }
  264. finally
  265. {
  266. try
  267. {
  268. statement.close();
  269. }
  270. catch (Exception e)
  271. {}
  272. try
  273. {
  274. con.close();
  275. }
  276. catch (Exception e)
  277. {}
  278. }
  279. }
  280. @SuppressWarnings("synthetic-access")
  281. private static class SingletonHolder
  282. {
  283. protected static final CharNameTable _instance = new CharNameTable();
  284. }
  285. }