Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. /*
  2.  
  3. * This program is free software: you can redistribute it and/or modify it under
  4.  
  5. * the terms of the GNU General Public License as published by the Free Software
  6.  
  7. * Foundation, either version 3 of the License, or (at your option) any later
  8.  
  9. * version.
  10.  
  11. *
  12.  
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14.  
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16.  
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18.  
  19. * details.
  20.  
  21. *
  22.  
  23. * You should have received a copy of the GNU General Public License along with
  24.  
  25. * this program. If not, see <http://www.gnu.org/licenses/>.
  26.  
  27. */
  28.  
  29. package com.l2jfrozen.gameserver.model.events;
  30.  
  31.  
  32.  
  33. import java.sql.Connection;
  34. import java.sql.PreparedStatement;
  35. import java.sql.ResultSet;
  36. import java.util.Calendar;
  37. import java.util.Date;
  38. import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
  39. import com.l2jfrozen.gameserver.model.entity.Announcements;
  40. import com.l2jfrozen.gameserver.network.serverpackets.NpcHtmlMessage;
  41. import com.l2jfrozen.gameserver.thread.ThreadPoolManager;
  42. import com.l2jfrozen.util.database.L2DatabaseFactory;
  43.  
  44.  
  45.  
  46. /**
  47.  
  48. * @author Elfocrash
  49.  
  50. *
  51.  
  52. */
  53.  
  54. public class PlayerOfTheHour
  55.  
  56. {
  57.  
  58.  
  59.  
  60. public static long getTimeToOclock()
  61.  
  62. {
  63.  
  64. Date d = new Date();
  65.  
  66. Calendar c = Calendar.getInstance();
  67.  
  68. c.setTime(d);
  69.  
  70.  
  71.  
  72.  
  73.  
  74. c.add(Calendar.HOUR, 1);
  75.  
  76.  
  77.  
  78. c.set(Calendar.MINUTE, 0);
  79.  
  80. c.set(Calendar.SECOND, 0);
  81.  
  82. // System.out.print(c.getTime());
  83.  
  84. long howMany = (c.getTimeInMillis()-d.getTime());
  85.  
  86. return howMany;
  87.  
  88. }
  89.  
  90.  
  91.  
  92. public static String getNextHourToDate()
  93.  
  94. {
  95.  
  96. Date d = new Date();
  97.  
  98. Calendar c = Calendar.getInstance();
  99.  
  100. c.setTime(d);
  101.  
  102. c.add(Calendar.HOUR, 1);
  103.  
  104.  
  105.  
  106.  
  107.  
  108. return c.getTime().toString();
  109.  
  110. }
  111.  
  112. public static String getTimeToDate()
  113.  
  114. {
  115.  
  116. Date d = new Date();
  117.  
  118. Calendar c = Calendar.getInstance();
  119.  
  120. c.setTime(d);
  121.  
  122.  
  123.  
  124.  
  125.  
  126. c.add(Calendar.HOUR, 1);
  127.  
  128.  
  129.  
  130. c.set(Calendar.MINUTE, 0);
  131.  
  132. c.set(Calendar.SECOND, 0);
  133.  
  134.  
  135.  
  136.  
  137.  
  138. return c.getTime().toString();
  139.  
  140. }
  141.  
  142. public static int getZonePvp(L2PcInstance activeChar)
  143.  
  144. {
  145.  
  146. int id=0;
  147.  
  148. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  149.  
  150. {
  151.  
  152. PreparedStatement statement = con.prepareStatement("SELECT zone_pvp FROM characters WHERE obj_Id=?");
  153.  
  154. statement.setInt(1, activeChar.getObjectId());
  155.  
  156. ResultSet rset = statement.executeQuery();
  157.  
  158.  
  159.  
  160. while (rset.next())
  161.  
  162. {
  163.  
  164. id = rset.getInt("zone_pvp");
  165.  
  166. }
  167.  
  168. rset.close();
  169.  
  170. statement.close();
  171.  
  172. }
  173.  
  174. catch (Exception e)
  175.  
  176. {
  177.  
  178. }
  179.  
  180. return id;
  181.  
  182. }
  183.  
  184.  
  185.  
  186. public static void addZonePvp(L2PcInstance activeChar)
  187.  
  188. {
  189.  
  190. try(Connection con = L2DatabaseFactory.getInstance().getConnection())
  191.  
  192. {
  193.  
  194. PreparedStatement statement = con.prepareStatement("UPDATE characters SET zone_pvp=? WHERE obj_Id=?");
  195.  
  196. statement.setInt(1, getZonePvp(activeChar) +1);
  197.  
  198. statement.setInt(2, activeChar.getObjectId());
  199.  
  200. statement.executeUpdate();
  201.  
  202. statement.close();
  203.  
  204. }
  205.  
  206. catch(Exception e)
  207.  
  208. {
  209.  
  210.  
  211.  
  212. }
  213.  
  214. }
  215.  
  216.  
  217.  
  218. public static void resetZonePvp()
  219.  
  220. {
  221.  
  222. try(Connection con = L2DatabaseFactory.getInstance().getConnection())
  223.  
  224. {
  225.  
  226. PreparedStatement statement = con.prepareStatement("UPDATE characters SET zone_pvp=0");
  227.  
  228. statement.executeUpdate();
  229.  
  230. statement.close();
  231.  
  232. }
  233.  
  234. catch(Exception e)
  235.  
  236. {
  237.  
  238.  
  239.  
  240. }
  241.  
  242. }
  243.  
  244.  
  245.  
  246. static int getTopZonePvpCount()
  247.  
  248. {
  249.  
  250. int id=0;
  251.  
  252. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  253.  
  254. {
  255.  
  256. PreparedStatement statement = con.prepareStatement("SELECT zone_pvp FROM characters ORDER BY zone_pvp DESC LIMIT 1");
  257.  
  258. ResultSet rset = statement.executeQuery();
  259.  
  260.  
  261.  
  262. while (rset.next())
  263.  
  264. {
  265.  
  266. id = rset.getInt("zone_pvp");
  267.  
  268. }
  269.  
  270. rset.close();
  271.  
  272. statement.close();
  273.  
  274. }
  275.  
  276. catch (Exception e)
  277.  
  278. {
  279.  
  280. }
  281.  
  282. return id;
  283.  
  284. }
  285.  
  286.  
  287.  
  288. static String getTopZonePvpName()
  289.  
  290. {
  291.  
  292. String name = null;
  293.  
  294. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  295.  
  296. {
  297.  
  298. PreparedStatement statement = con.prepareStatement("SELECT char_name FROM characters ORDER BY zone_pvp DESC LIMIT 1");
  299.  
  300. ResultSet rset = statement.executeQuery();
  301.  
  302.  
  303.  
  304. while (rset.next())
  305.  
  306. {
  307.  
  308. name = rset.getString("char_name");
  309.  
  310. }
  311.  
  312. rset.close();
  313.  
  314. statement.close();
  315.  
  316. }
  317.  
  318. catch (Exception e)
  319.  
  320. {
  321.  
  322. }
  323.  
  324. return name;
  325.  
  326. }
  327.  
  328.  
  329.  
  330. private PlayerOfTheHour()
  331.  
  332. {
  333.  
  334.  
  335.  
  336. }
  337.  
  338. public static void getTopHtml(L2PcInstance activeChar)
  339.  
  340. {
  341.  
  342. NpcHtmlMessage htm = new NpcHtmlMessage(0);
  343.  
  344. StringBuilder sb = new StringBuilder("<html><body>");
  345.  
  346. sb.append("<center><br>Top 10 PvP:<br><br1>");
  347.  
  348. Connection con = null;
  349.  
  350. try
  351.  
  352. {
  353.  
  354. con = L2DatabaseFactory.getInstance().getConnection();
  355.  
  356. PreparedStatement stm = con.prepareStatement("SELECT char_name,zone_pvp,accesslevel,online FROM characters ORDER BY zone_pvp DESC LIMIT 10");
  357.  
  358. ResultSet rSet = stm.executeQuery();
  359.  
  360. while (rSet.next())
  361.  
  362. {
  363.  
  364. int accessLevel = rSet.getInt("accesslevel");
  365.  
  366. if (accessLevel > 0)
  367.  
  368. {
  369.  
  370. continue;
  371.  
  372. }
  373.  
  374. int pvpKills = rSet.getInt("zone_pvp");
  375.  
  376. if (pvpKills == 0)
  377.  
  378. {
  379.  
  380. continue;
  381.  
  382. }
  383.  
  384. String pl = rSet.getString("char_name");
  385.  
  386. sb.append("Player: <font color=\"LEVEL\">"+pl+"</font> PvPs: <font color=\"LEVEL\">"+pvpKills+"</font><br1>");
  387.  
  388. }
  389.  
  390. }
  391.  
  392. catch (Exception e)
  393.  
  394. {
  395.  
  396. System.out.println("Error while selecting top 15 pvp from database.");
  397.  
  398. }
  399.  
  400. finally
  401.  
  402. {
  403.  
  404. try
  405.  
  406. {
  407.  
  408. if (con != null)
  409.  
  410. con.close();
  411.  
  412. }
  413.  
  414. catch (Exception e)
  415.  
  416. {}
  417.  
  418. }
  419.  
  420. sb.append("Next round: " + getTimeToDate() +"</body></html>");
  421.  
  422. htm.setHtml(sb.toString());
  423.  
  424. activeChar.sendPacket(htm);
  425.  
  426. }
  427.  
  428.  
  429.  
  430. public static void getInstance()
  431.  
  432. {
  433.  
  434.  
  435.  
  436. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable()
  437.  
  438. {
  439.  
  440. @Override
  441.  
  442.  
  443.  
  444. public void run()
  445.  
  446. {
  447.  
  448.  
  449.  
  450. Announcements.getInstance().announceToAll("The Player of the Hour is " + getTopZonePvpName() + " with "+getTopZonePvpCount()+ " pvps");
  451.  
  452. //TODO Your reward should go here.
  453.  
  454. resetZonePvp();
  455.  
  456. Announcements.getInstance().announceToAll("New Player of the Hour just started. Go to Primeval Isle and show us what you got.");
  457.  
  458. Announcements.getInstance().announceToAll("This round will end at: " + getNextHourToDate() + ".");
  459.  
  460. }
  461.  
  462. }, getTimeToOclock() , 3600000);
  463.  
  464. }
  465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement