CharNameTable.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. private 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. if (name == null || name.isEmpty())
  72. return -1;
  73. Iterator<Entry<Integer, String>> it = _chars.entrySet().iterator();
  74. Map.Entry<Integer, String> pair;
  75. while (it.hasNext())
  76. {
  77. pair = it.next();
  78. if (pair.getValue().equalsIgnoreCase(name))
  79. return pair.getKey();
  80. }
  81. if (Config.CACHE_CHAR_NAMES)
  82. return -1;
  83. int id = -1;
  84. int accessLevel = 0;
  85. Connection con = null;
  86. PreparedStatement statement = null;
  87. try
  88. {
  89. con = L2DatabaseFactory.getInstance().getConnection();
  90. statement = con.prepareStatement("SELECT charId,accesslevel FROM characters WHERE char_name=?");
  91. statement.setString(1, name);
  92. ResultSet rset = statement.executeQuery();
  93. while (rset.next())
  94. {
  95. id = rset.getInt(1);
  96. accessLevel = rset.getInt(2);
  97. }
  98. rset.close();
  99. }
  100. catch (SQLException e)
  101. {
  102. _log.warning("could not check existing char name:" + e.getMessage());
  103. }
  104. finally
  105. {
  106. try
  107. {
  108. statement.close();
  109. }
  110. catch (Exception e) {}
  111. try
  112. {
  113. con.close();
  114. }
  115. catch (Exception e) {}
  116. }
  117. if (id > 0)
  118. {
  119. _chars.put(id, name);
  120. _accessLevels.put(id, accessLevel);
  121. return id;
  122. }
  123. return -1; // not found
  124. }
  125. public final String getNameById(int id)
  126. {
  127. if (id <= 0)
  128. return null;
  129. String name = _chars.get(id);
  130. if (name != null)
  131. return name;
  132. if (Config.CACHE_CHAR_NAMES)
  133. return null;
  134. int accessLevel = 0;
  135. Connection con = null;
  136. PreparedStatement statement = null;
  137. try
  138. {
  139. con = L2DatabaseFactory.getInstance().getConnection();
  140. statement = con.prepareStatement("SELECT char_name,accesslevel FROM characters WHERE charId=?");
  141. statement.setInt(1, id);
  142. ResultSet rset = statement.executeQuery();
  143. while (rset.next())
  144. {
  145. name = rset.getString(1);
  146. accessLevel = rset.getInt(2);
  147. }
  148. rset.close();
  149. }
  150. catch (SQLException e)
  151. {
  152. _log.warning("could not check existing char id:" + e.getMessage());
  153. }
  154. finally
  155. {
  156. try
  157. {
  158. statement.close();
  159. }
  160. catch (Exception e) {}
  161. try
  162. {
  163. con.close();
  164. }
  165. catch (Exception e) {}
  166. }
  167. if (name != null && !name.isEmpty())
  168. {
  169. _chars.put(id, name);
  170. _accessLevels.put(id, accessLevel);
  171. return name;
  172. }
  173. return null; //not found
  174. }
  175. public final int getAccessLevelById(int objectId)
  176. {
  177. if (getNameById(objectId) != null)
  178. return _accessLevels.get(objectId);
  179. else
  180. return 0;
  181. }
  182. public synchronized boolean doesCharNameExist(String name)
  183. {
  184. boolean result = true;
  185. Connection con = null;
  186. try
  187. {
  188. con = L2DatabaseFactory.getInstance().getConnection();
  189. PreparedStatement statement = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?");
  190. statement.setString(1, name);
  191. ResultSet rset = statement.executeQuery();
  192. result = rset.next();
  193. rset.close();
  194. statement.close();
  195. }
  196. catch (SQLException e)
  197. {
  198. _log.warning("could not check existing charname:" + e.getMessage());
  199. }
  200. finally
  201. {
  202. try
  203. {
  204. con.close();
  205. }
  206. catch (Exception e)
  207. {
  208. }
  209. }
  210. return result;
  211. }
  212. public int accountCharNumber(String account)
  213. {
  214. Connection con = null;
  215. int number = 0;
  216. try
  217. {
  218. con = L2DatabaseFactory.getInstance().getConnection();
  219. PreparedStatement statement = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?");
  220. statement.setString(1, account);
  221. ResultSet rset = statement.executeQuery();
  222. while (rset.next())
  223. {
  224. number = rset.getInt(1);
  225. }
  226. rset.close();
  227. statement.close();
  228. }
  229. catch (SQLException e)
  230. {
  231. _log.warning("could not check existing char number:" + e.getMessage());
  232. }
  233. finally
  234. {
  235. try
  236. {
  237. con.close();
  238. }
  239. catch (Exception e)
  240. {
  241. }
  242. }
  243. return number;
  244. }
  245. private void loadAll()
  246. {
  247. String name;
  248. int id = -1;
  249. int accessLevel = 0;
  250. PreparedStatement statement = null;
  251. Connection con = null;
  252. try
  253. {
  254. con = L2DatabaseFactory.getInstance().getConnection();
  255. statement = con.prepareStatement("SELECT charId,char_name,accesslevel FROM characters");
  256. ResultSet rset = statement.executeQuery();
  257. while (rset.next())
  258. {
  259. id = rset.getInt(1);
  260. name = rset.getString(2);
  261. accessLevel = rset.getInt(3);
  262. _chars.put(id, name);
  263. _accessLevels.put(id, accessLevel);
  264. }
  265. rset.close();
  266. }
  267. catch (SQLException e)
  268. {
  269. _log.warning("could not load char name: " + e.getMessage());
  270. }
  271. finally
  272. {
  273. try
  274. {
  275. statement.close();
  276. }
  277. catch (Exception e)
  278. {}
  279. try
  280. {
  281. con.close();
  282. }
  283. catch (Exception e)
  284. {}
  285. }
  286. }
  287. @SuppressWarnings("synthetic-access")
  288. private static class SingletonHolder
  289. {
  290. protected static final CharNameTable _instance = new CharNameTable();
  291. }
  292. }