Advertisement
Guest User

Npc rank for acis 362

a guest
Dec 8th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver.model.actor.instance;
  20.  
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.util.StringTokenizer;
  26. import java.util.logging.Level;
  27.  
  28. import net.sf.l2j.commons.concurrent.ThreadPool;
  29.  
  30. import net.sf.l2j.L2DatabaseFactory;
  31. import net.sf.l2j.gameserver.cache.HtmCache;
  32. import net.sf.l2j.gameserver.model.actor.template.NpcTemplate;
  33. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  34.  
  35. public class L2StatusInstance extends L2NpcInstance
  36. {
  37.  
  38. private class PlayerInfo
  39. {
  40. public PlayerInfo(int pos,String n, int pvps, int pks ,int ontime, Boolean iso)
  41. {
  42. position = pos;
  43. Nick = n;
  44. pvpCount = pvps;
  45. pkCount = pks;
  46. onlineTime = ontime;
  47. isOnline = iso;
  48. }
  49.  
  50. public int position;
  51. public String Nick;
  52. public int pvpCount;
  53. public int pkCount;
  54. public int onlineTime;
  55. public Boolean isOnline;
  56. }
  57.  
  58. //delay interval (in minutes):
  59. private final int delayForCheck = 5;
  60.  
  61. //number of players to be listed
  62. private int pvpListCount = 10;
  63. private int pkListCount = 10;
  64. private int onlineListCount = 10;
  65.  
  66.  
  67. private PlayerInfo [] topPvPList = new PlayerInfo [pvpListCount];
  68. private PlayerInfo [] topPkList = new PlayerInfo [pkListCount];
  69. private PlayerInfo [] topOnlineList = new PlayerInfo [onlineListCount];
  70.  
  71.  
  72. @SuppressWarnings("synthetic-access")
  73. public L2StatusInstance(int objectId, NpcTemplate template)
  74. {
  75. super(objectId, template);
  76. ThreadPool.scheduleAtFixedRate(new RefreshAllLists(), 10000, delayForCheck * 60000);
  77. }
  78.  
  79. private class RefreshAllLists implements Runnable
  80. {
  81. @Override
  82. public void run()
  83. {
  84. ReloadData();
  85. }
  86. }
  87.  
  88. @SuppressWarnings("resource")
  89. void ReloadData()
  90. {
  91. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  92. {
  93. PreparedStatement statement = con.prepareStatement("SELECT char_name, pvpkills, online FROM characters ORDER BY pvpkills DESC, char_name ASC LIMIT 10");
  94. ResultSet result = statement.executeQuery();
  95.  
  96. //refreshing top pvp list
  97. int i = 0; //index of array
  98.  
  99. while (result.next())
  100. {
  101. topPvPList[i] = new PlayerInfo(i+1,result.getString("char_name"),result.getInt("pvpkills"),0,0,result.getBoolean("online"));
  102. i++;
  103. }
  104.  
  105. //refreshing top pk list
  106. statement = con.prepareStatement("SELECT char_name, pkkills, online FROM characters ORDER BY pkkills DESC, char_name ASC LIMIT 10");
  107. result = statement.executeQuery();
  108.  
  109. i = 0; //index of array
  110. while (result.next())
  111. {
  112. topPkList[i] = new PlayerInfo(i+1,result.getString("char_name"),0,result.getInt("pkkills"),0,result.getBoolean("online"));
  113. i++;
  114. }
  115.  
  116. //refreshing top online list
  117. statement = con.prepareStatement("SELECT char_name, onlinetime, online FROM characters ORDER BY onlinetime DESC, char_name ASC LIMIT 10");
  118. result = statement.executeQuery();
  119.  
  120. i = 0; //index of array
  121. while (result.next())
  122. {
  123. topOnlineList[i] = new PlayerInfo(i+1,result.getString("char_name"),0,0,result.getInt("onlinetime"),result.getBoolean("online"));
  124. i++;
  125. }
  126.  
  127. result.close();
  128. statement.close();
  129.  
  130. }
  131. catch (SQLException e)
  132. {
  133. _log.log(Level.WARNING, "ranking (status): could not load statistics informations" + e.getMessage(), e);
  134. }
  135. }
  136.  
  137. @Override
  138. public void onSpawn()
  139. {
  140. ReloadData();
  141. }
  142.  
  143. @Override
  144. public void showChatWindow(L2PcInstance player)
  145. {
  146. GeneratePvPList(player);
  147. }
  148.  
  149. @Override
  150. public void onBypassFeedback(L2PcInstance player, String command)
  151. {
  152. StringTokenizer st = new StringTokenizer(command, " ");
  153. String currentCommand = st.nextToken();
  154.  
  155. if (currentCommand.startsWith("pvplist"))
  156. {
  157. GeneratePvPList(player);
  158. }
  159.  
  160. else if (currentCommand.startsWith("pklist"))
  161. {
  162. GeneratePKList(player);
  163. }
  164. else if (currentCommand.startsWith("onlinelist"))
  165. {
  166. GenerateOnlineList(player);
  167. }
  168.  
  169. super.onBypassFeedback(player, command);
  170. }
  171.  
  172. private void GeneratePvPList(L2PcInstance p)
  173. {
  174. StringBuilder _PVPranking = new StringBuilder();
  175. for (PlayerInfo player : topPvPList)
  176. {
  177. if (player == null) break;
  178.  
  179. _PVPranking.append("<table width=\"290\"><tr>");
  180. _PVPranking.append("<td FIXWIDTH=\"2\" align=\"center\"></td>");
  181. _PVPranking.append("<td FIXWIDTH=\"17\" align=\"center\">"+player.position+"</td>");
  182. _PVPranking.append("<td FIXWIDTH=\"158\" align=\"center\">"+player.Nick+"</td>");
  183. _PVPranking.append("<td FIXWIDTH=\"90\" align=\"center\">"+player.pvpCount+"</td>");
  184. _PVPranking.append("<td FIXWIDTH=\"50\" align=\"center\">"+((player.isOnline) ? "<font color=\"00FF00\">ON</font>" : "<font color=\"CC0000\">OFF</font>")+"</td>");
  185. _PVPranking.append("<td FIXWIDTH=\"2\" align=\"center\"></td>");
  186. _PVPranking.append("</tr></table>");
  187. _PVPranking.append("<img src=\"L2UI.Squaregray\" width=\"300\" height=\"1\">");
  188. }
  189.  
  190. NpcHtmlMessage html = new NpcHtmlMessage(1);
  191. html.setFile(getHtmlPath(getNpcId(), 0));
  192. html.replace("%objectId%", getObjectId());
  193. html.replace("%pvplist%", _PVPranking.toString());
  194. p.sendPacket(html);
  195. }
  196.  
  197. private void GeneratePKList(L2PcInstance p)
  198. {
  199. StringBuilder _PVPranking = new StringBuilder();
  200. for (PlayerInfo player : topPkList)
  201. {
  202. if (player == null) break;
  203.  
  204. _PVPranking.append("<table width=\"290\"><tr>");
  205. _PVPranking.append("<td FIXWIDTH=\"2\" align=\"center\"></td>");
  206. _PVPranking.append("<td FIXWIDTH=\"17\" align=\"center\">"+player.position+"</td>");
  207. _PVPranking.append("<td FIXWIDTH=\"158\" align=\"center\">"+player.Nick+"</td>");
  208. _PVPranking.append("<td FIXWIDTH=\"90\" align=\"center\">"+player.pkCount+"</td>");
  209. _PVPranking.append("<td FIXWIDTH=\"50\" align=\"center\">"+((player.isOnline) ? "<font color=\"00FF00\">ON</font>" : "<font color=\"CC0000\">OFF</font>")+"</td>");
  210. _PVPranking.append("<td FIXWIDTH=\"2\" align=\"center\"></td>");
  211. _PVPranking.append("</tr></table>");
  212. _PVPranking.append("<img src=\"L2UI.Squaregray\" width=\"300\" height=\"1\">");
  213. }
  214.  
  215. NpcHtmlMessage html = new NpcHtmlMessage(1);
  216. html.setFile(getHtmlPath(getNpcId(), 2));
  217. html.replace("%objectId%", getObjectId());
  218. html.replace("%pklist%", _PVPranking.toString());
  219. p.sendPacket(html);
  220. }
  221.  
  222. private void GenerateOnlineList(L2PcInstance p)
  223. {
  224. StringBuilder _PVPranking = new StringBuilder();
  225. for (PlayerInfo player : topOnlineList)
  226. {
  227. if (player == null) break;
  228.  
  229. _PVPranking.append("<table width=\"290\"><tr>");
  230. _PVPranking.append("<td FIXWIDTH=\"2\" align=\"center\"></td>");
  231. _PVPranking.append("<td FIXWIDTH=\"17\" align=\"center\">"+player.position+"</td>");
  232. _PVPranking.append("<td FIXWIDTH=\"158\" align=\"center\">"+player.Nick+"</td>");
  233. _PVPranking.append("<td FIXWIDTH=\"90\" align=\"center\">"+ConverTime(player.onlineTime)+"</td>");
  234. _PVPranking.append("<td FIXWIDTH=\"50\" align=\"center\">"+((player.isOnline) ? "<font color=\"00FF00\">ON</font>" : "<font color=\"CC0000\">OFF</font>")+"</td>");
  235. _PVPranking.append("<td FIXWIDTH=\"2\" align=\"center\"></td>");
  236. _PVPranking.append("</tr></table>");
  237. _PVPranking.append("<img src=\"L2UI.Squaregray\" width=\"300\" height=\"1\">");
  238. }
  239.  
  240. NpcHtmlMessage html = new NpcHtmlMessage(1);
  241. html.setFile(getHtmlPath(getNpcId(), 3));
  242. html.replace("%objectId%", getObjectId());
  243. html.replace("%onlinelist%", _PVPranking.toString());
  244. p.sendPacket(html);
  245. }
  246.  
  247. private static String ConverTime(long seconds)
  248. {
  249. long remainder = seconds;
  250. int days = (int) remainder / (24*3600);
  251. remainder = remainder -(days * 3600 * 24);
  252.  
  253. int hours = (int) (remainder / 3600);
  254. remainder = remainder -(hours * 3600);
  255.  
  256. int minutes = (int) (remainder / 60);
  257. remainder = remainder -(hours * 60);
  258.  
  259. seconds = remainder;
  260.  
  261. String timeInText = "";
  262.  
  263. if (days > 0)
  264. timeInText = days+"<font color=\"LEVEL\">D</font> ";
  265. if (hours > 0)
  266. timeInText = timeInText+ hours+"<font color=\"LEVEL\">H</font> ";
  267. if (minutes >0)
  268. timeInText = timeInText+ minutes+"<font color=\"LEVEL\">M</font>";
  269.  
  270. if (timeInText=="")
  271. {
  272. if(seconds>0)
  273. {
  274. timeInText = seconds+"<font color=\"LEVEL\">S</font>";
  275. }
  276. else
  277. {
  278. timeInText = "N/A";
  279. }
  280. }
  281. return timeInText;
  282. }
  283.  
  284. @Override
  285. public String getHtmlPath(int npcId, int val)
  286. {
  287. String filename;
  288.  
  289. if (val == 0)
  290. filename = "data/html/Status/" + npcId + ".htm";
  291. else
  292. filename = "data/html/Status/" + npcId + "-" + val + ".htm";
  293.  
  294. if (HtmCache.getInstance().isLoadable(filename))
  295. return filename;
  296.  
  297. return "data/html/Status/" + npcId + ".htm";
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement