CharNameTable.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.sql.Statement;
  21. import java.util.Iterator;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastMap;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. /**
  31. * This class ...
  32. *
  33. * @version $Revision: 1.3.2.2.2.1 $ $Date: 2005/03/27 15:29:18 $
  34. */
  35. public class CharNameTable
  36. {
  37. private static Logger _log = Logger.getLogger(CharNameTable.class.getName());
  38. private final Map<Integer, String> _chars;
  39. private final Map<Integer, Integer> _accessLevels;
  40. protected CharNameTable()
  41. {
  42. _chars = new FastMap<>();
  43. _accessLevels = new FastMap<>();
  44. if (Config.CACHE_CHAR_NAMES)
  45. loadAll();
  46. }
  47. public static CharNameTable getInstance()
  48. {
  49. return SingletonHolder._instance;
  50. }
  51. public final void addName(L2PcInstance player)
  52. {
  53. if (player != null)
  54. {
  55. addName(player.getObjectId(), player.getName());
  56. _accessLevels.put(player.getObjectId(), player.getAccessLevel().getLevel());
  57. }
  58. }
  59. private final void addName(int objectId, String name)
  60. {
  61. if (name != null)
  62. {
  63. if (!name.equals(_chars.get(objectId)))
  64. _chars.put(objectId, name);
  65. }
  66. }
  67. public final void removeName(int objId)
  68. {
  69. _chars.remove(objId);
  70. _accessLevels.remove(objId);
  71. }
  72. public final int getIdByName(String name)
  73. {
  74. if (name == null || name.isEmpty())
  75. return -1;
  76. Iterator<Entry<Integer, String>> it = _chars.entrySet().iterator();
  77. Map.Entry<Integer, String> pair;
  78. while (it.hasNext())
  79. {
  80. pair = it.next();
  81. if (pair.getValue().equalsIgnoreCase(name))
  82. return pair.getKey();
  83. }
  84. if (Config.CACHE_CHAR_NAMES)
  85. return -1;
  86. int id = -1;
  87. int accessLevel = 0;
  88. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  89. PreparedStatement ps = con.prepareStatement("SELECT charId,accesslevel FROM characters WHERE char_name=?"))
  90. {
  91. ps.setString(1, name);
  92. try (ResultSet rs = ps.executeQuery())
  93. {
  94. while (rs.next())
  95. {
  96. id = rs.getInt(1);
  97. accessLevel = rs.getInt(2);
  98. }
  99. }
  100. }
  101. catch (SQLException e)
  102. {
  103. _log.log(Level.WARNING, "Could not check existing char name: " + e.getMessage(), e);
  104. }
  105. if (id > 0)
  106. {
  107. _chars.put(id, name);
  108. _accessLevels.put(id, accessLevel);
  109. return id;
  110. }
  111. return -1; // not found
  112. }
  113. public final String getNameById(int id)
  114. {
  115. if (id <= 0)
  116. return null;
  117. String name = _chars.get(id);
  118. if (name != null)
  119. return name;
  120. if (Config.CACHE_CHAR_NAMES)
  121. return null;
  122. int accessLevel = 0;
  123. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  124. PreparedStatement ps = con.prepareStatement("SELECT char_name,accesslevel FROM characters WHERE charId=?"))
  125. {
  126. ps.setInt(1, id);
  127. try (ResultSet rset = ps.executeQuery())
  128. {
  129. while (rset.next())
  130. {
  131. name = rset.getString(1);
  132. accessLevel = rset.getInt(2);
  133. }
  134. }
  135. }
  136. catch (SQLException e)
  137. {
  138. _log.log(Level.WARNING, "Could not check existing char id: " + e.getMessage(), e);
  139. }
  140. if (name != null && !name.isEmpty())
  141. {
  142. _chars.put(id, name);
  143. _accessLevels.put(id, accessLevel);
  144. return name;
  145. }
  146. return null; //not found
  147. }
  148. public final int getAccessLevelById(int objectId)
  149. {
  150. if (getNameById(objectId) != null)
  151. return _accessLevels.get(objectId);
  152. return 0;
  153. }
  154. public synchronized boolean doesCharNameExist(String name)
  155. {
  156. boolean result = true;
  157. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  158. PreparedStatement ps = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?"))
  159. {
  160. ps.setString(1, name);
  161. try (ResultSet rs = ps.executeQuery())
  162. {
  163. result = rs.next();
  164. }
  165. }
  166. catch (SQLException e)
  167. {
  168. _log.log(Level.WARNING, "Could not check existing charname: " + e.getMessage(), e);
  169. }
  170. return result;
  171. }
  172. public int accountCharNumber(String account)
  173. {
  174. int number = 0;
  175. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  176. PreparedStatement ps = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?"))
  177. {
  178. ps.setString(1, account);
  179. try (ResultSet rset = ps.executeQuery())
  180. {
  181. while (rset.next())
  182. {
  183. number = rset.getInt(1);
  184. }
  185. }
  186. }
  187. catch (SQLException e)
  188. {
  189. _log.log(Level.WARNING, "Could not check existing char number: " + e.getMessage(), e);
  190. }
  191. return number;
  192. }
  193. private void loadAll()
  194. {
  195. String name;
  196. int id = -1;
  197. int accessLevel = 0;
  198. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  199. Statement s = con.createStatement();
  200. ResultSet rs = s.executeQuery("SELECT charId,char_name,accesslevel FROM characters"))
  201. {
  202. while (rs.next())
  203. {
  204. id = rs.getInt(1);
  205. name = rs.getString(2);
  206. accessLevel = rs.getInt(3);
  207. _chars.put(id, name);
  208. _accessLevels.put(id, accessLevel);
  209. }
  210. }
  211. catch (SQLException e)
  212. {
  213. _log.log(Level.WARNING, "Could not load char name: " + e.getMessage(), e);
  214. }
  215. _log.info(getClass().getSimpleName()+": Loaded "+_chars.size()+" char names.");
  216. }
  217. private static class SingletonHolder
  218. {
  219. protected static final CharNameTable _instance = new CharNameTable();
  220. }
  221. }