2
0

CharNameTable.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.Iterator;
  26. import java.util.Map;
  27. import java.util.Map.Entry;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.L2DatabaseFactory;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.util.L2FastMap;
  34. /**
  35. * This class ...
  36. * @version $Revision: 1.3.2.2.2.1 $ $Date: 2005/03/27 15:29:18 $
  37. */
  38. public class CharNameTable
  39. {
  40. private static Logger _log = Logger.getLogger(CharNameTable.class.getName());
  41. private final Map<Integer, String> _chars = new L2FastMap<>();
  42. private final Map<Integer, Integer> _accessLevels = new L2FastMap<>();
  43. protected CharNameTable()
  44. {
  45. if (Config.CACHE_CHAR_NAMES)
  46. {
  47. loadAll();
  48. }
  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 objectId, String name)
  59. {
  60. if (name != null)
  61. {
  62. if (!name.equals(_chars.get(objectId)))
  63. {
  64. _chars.put(objectId, name);
  65. }
  66. }
  67. }
  68. public final void removeName(int objId)
  69. {
  70. _chars.remove(objId);
  71. _accessLevels.remove(objId);
  72. }
  73. public final int getIdByName(String name)
  74. {
  75. if ((name == null) || name.isEmpty())
  76. {
  77. return -1;
  78. }
  79. Iterator<Entry<Integer, String>> it = _chars.entrySet().iterator();
  80. Map.Entry<Integer, String> pair;
  81. while (it.hasNext())
  82. {
  83. pair = it.next();
  84. if (pair.getValue().equalsIgnoreCase(name))
  85. {
  86. return pair.getKey();
  87. }
  88. }
  89. if (Config.CACHE_CHAR_NAMES)
  90. {
  91. return -1;
  92. }
  93. int id = -1;
  94. int accessLevel = 0;
  95. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  96. PreparedStatement ps = con.prepareStatement("SELECT charId,accesslevel FROM characters WHERE char_name=?"))
  97. {
  98. ps.setString(1, name);
  99. try (ResultSet rs = ps.executeQuery())
  100. {
  101. while (rs.next())
  102. {
  103. id = rs.getInt(1);
  104. accessLevel = rs.getInt(2);
  105. }
  106. }
  107. }
  108. catch (SQLException e)
  109. {
  110. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing char name: " + e.getMessage(), e);
  111. }
  112. if (id > 0)
  113. {
  114. _chars.put(id, name);
  115. _accessLevels.put(id, accessLevel);
  116. return id;
  117. }
  118. return -1; // not found
  119. }
  120. public final String getNameById(int id)
  121. {
  122. if (id <= 0)
  123. {
  124. return null;
  125. }
  126. String name = _chars.get(id);
  127. if (name != null)
  128. {
  129. return name;
  130. }
  131. if (Config.CACHE_CHAR_NAMES)
  132. {
  133. return null;
  134. }
  135. int accessLevel = 0;
  136. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  137. PreparedStatement ps = con.prepareStatement("SELECT char_name,accesslevel FROM characters WHERE charId=?"))
  138. {
  139. ps.setInt(1, id);
  140. try (ResultSet rset = ps.executeQuery())
  141. {
  142. while (rset.next())
  143. {
  144. name = rset.getString(1);
  145. accessLevel = rset.getInt(2);
  146. }
  147. }
  148. }
  149. catch (SQLException e)
  150. {
  151. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing char id: " + e.getMessage(), e);
  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. {
  165. return _accessLevels.get(objectId);
  166. }
  167. return 0;
  168. }
  169. public synchronized boolean doesCharNameExist(String name)
  170. {
  171. boolean result = true;
  172. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  173. PreparedStatement ps = con.prepareStatement("SELECT account_name FROM characters WHERE char_name=?"))
  174. {
  175. ps.setString(1, name);
  176. try (ResultSet rs = ps.executeQuery())
  177. {
  178. result = rs.next();
  179. }
  180. }
  181. catch (SQLException e)
  182. {
  183. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing charname: " + e.getMessage(), e);
  184. }
  185. return result;
  186. }
  187. public int accountCharNumber(String account)
  188. {
  189. int number = 0;
  190. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  191. PreparedStatement ps = con.prepareStatement("SELECT COUNT(char_name) FROM characters WHERE account_name=?"))
  192. {
  193. ps.setString(1, account);
  194. try (ResultSet rset = ps.executeQuery())
  195. {
  196. while (rset.next())
  197. {
  198. number = rset.getInt(1);
  199. }
  200. }
  201. }
  202. catch (SQLException e)
  203. {
  204. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not check existing char number: " + e.getMessage(), e);
  205. }
  206. return number;
  207. }
  208. private void loadAll()
  209. {
  210. String name;
  211. int id = -1;
  212. int accessLevel = 0;
  213. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  214. Statement s = con.createStatement();
  215. ResultSet rs = s.executeQuery("SELECT charId,char_name,accesslevel FROM characters"))
  216. {
  217. while (rs.next())
  218. {
  219. id = rs.getInt(1);
  220. name = rs.getString(2);
  221. accessLevel = rs.getInt(3);
  222. _chars.put(id, name);
  223. _accessLevels.put(id, accessLevel);
  224. }
  225. }
  226. catch (SQLException e)
  227. {
  228. _log.log(Level.WARNING, getClass().getSimpleName() + ": Could not load char name: " + e.getMessage(), e);
  229. }
  230. _log.info(getClass().getSimpleName() + ": Loaded " + _chars.size() + " char names.");
  231. }
  232. public static CharNameTable getInstance()
  233. {
  234. return SingletonHolder._instance;
  235. }
  236. private static class SingletonHolder
  237. {
  238. protected static final CharNameTable _instance = new CharNameTable();
  239. }
  240. }