CharNameTable.java 7.1 KB

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