Advertisement
scraw

AutoVoteReward

Jan 15th, 2021
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.47 KB | None | 0 0
  1. package com.l2jfrozen.gameserver.handler;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.util.ArrayList;
  10. import java.util.Collection;
  11. import java.util.List;
  12. import java.util.Set;
  13.  
  14. import org.apache.log4j.Level;
  15. import org.apache.log4j.Logger;
  16.  
  17. import com.gargoylesoftware.htmlunit.BrowserVersion;
  18. import com.gargoylesoftware.htmlunit.WebClient;
  19. import com.gargoylesoftware.htmlunit.html.HtmlPage;
  20. import com.l2jfrozen.Config;
  21. import com.l2jfrozen.gameserver.model.L2World;
  22. import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jfrozen.gameserver.model.entity.Announcements;
  24. import com.l2jfrozen.gameserver.powerpak.PowerPakConfig;
  25. import com.l2jfrozen.gameserver.thread.ThreadPoolManager;
  26.  
  27. /**
  28. * @author L2jFrozen <br>
  29. * <br>
  30. * Network working with L2jFrozen AutoVoteReward: <br>
  31. * Hopzone -> http://l2.hopzone.net/ <br>
  32. * Topzone -> http://l2topzone.com/ <br>
  33. * L2network -> http://l2network.eu/ <br>
  34. */
  35.  
  36. public class AutoVoteRewardHandler
  37. {
  38. protected static final Logger LOGGER = Logger.getLogger(AutoVoteRewardHandler.class);
  39.  
  40. protected List<String> already_rewarded;
  41.  
  42. private int _l2networkVotesCount = 0;
  43. private int _hopzoneVotesCount = 0;
  44. private int _topzoneVotesCount = 0;
  45. protected List<String> _already_rewarded;
  46.  
  47. protected static boolean _l2network = false;
  48. protected static boolean _topzone = false;
  49. protected static boolean _hopzone = false;
  50.  
  51. private AutoVoteRewardHandler()
  52. {
  53. LOGGER.info("Vote Reward System Initiated.");
  54.  
  55. if (_hopzone)
  56. {
  57. int hopzone_votes = getHopZoneVotes();
  58.  
  59. if (hopzone_votes == -1)
  60. {
  61. hopzone_votes = 0;
  62. }
  63.  
  64. setHopZoneVoteCount(hopzone_votes);
  65. }
  66.  
  67. if (_l2network)
  68. {
  69. int l2network_votes = getL2NetworkVotes();
  70.  
  71. if (l2network_votes == -1)
  72. {
  73. l2network_votes = 0;
  74. }
  75.  
  76. setL2NetworkVoteCount(l2network_votes);
  77. }
  78.  
  79. if (_topzone)
  80. {
  81. int topzone_votes = getTopZoneVotes();
  82.  
  83. if (topzone_votes == -1)
  84. {
  85. topzone_votes = 0;
  86. }
  87.  
  88. setTopZoneVoteCount(topzone_votes);
  89. }
  90.  
  91. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(), PowerPakConfig.VOTES_SYSYEM_INITIAL_DELAY, PowerPakConfig.VOTES_SYSYEM_STEP_DELAY);
  92. }
  93.  
  94. protected class AutoReward implements Runnable
  95. {
  96. @Override
  97. public void run()
  98. {
  99. final int minutes = (PowerPakConfig.VOTES_SYSYEM_STEP_DELAY / 1000) / 60;
  100.  
  101. if (_hopzone)
  102. {
  103. final int hopzone_votes = getHopZoneVotes();
  104.  
  105. if (hopzone_votes != -1)
  106. {
  107. LOGGER.info("[AutoVoteReward] Server HOPZONE Votes: " + hopzone_votes);
  108. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Actual HOPZONE Votes are " + hopzone_votes + "...");
  109.  
  110. if (hopzone_votes != 0 && hopzone_votes >= getHopZoneVoteCount() + PowerPakConfig.VOTES_FOR_REWARD)
  111. {
  112. _already_rewarded = new ArrayList<>();
  113.  
  114. final Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers();
  115.  
  116. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Great Work! Check your inventory for Reward!!");
  117.  
  118. // L2ItemInstance item;
  119. for (final L2PcInstance player : pls)
  120. {
  121. if (player != null && !player.isInOfflineMode() && player.isOnline() == 1)
  122. {
  123. if (player._active_boxes <= 1 || (player._active_boxes > 1 && checkSingleBox(player)))
  124. {
  125.  
  126. final Set<Integer> items = PowerPakConfig.VOTES_REWARDS_LIST.keySet();
  127. for (final Integer i : items)
  128. {
  129. // item = player.getInventory().getItemByItemId(i);
  130.  
  131. // TODO: check on maxstack for item
  132. player.addItem("reward", i, PowerPakConfig.VOTES_REWARDS_LIST.get(i), player, true);
  133.  
  134. }
  135.  
  136. }
  137. }
  138. }
  139. setHopZoneVoteCount(hopzone_votes);
  140. }
  141. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Next HOPZONE Reward in " + minutes + " minutes at " + (getHopZoneVoteCount() + PowerPakConfig.VOTES_FOR_REWARD) + " Votes!!");
  142. // site web
  143. Announcements.getInstance().gameAnnounceToAll("[SiteWeb] " + PowerPakConfig.SERVER_WEB_SITE);
  144.  
  145. }
  146.  
  147. }
  148.  
  149. if (_topzone && _hopzone && PowerPakConfig.VOTES_SYSYEM_STEP_DELAY > 0)
  150. {
  151. try
  152. {
  153. Thread.sleep(PowerPakConfig.VOTES_SYSYEM_STEP_DELAY / 2);
  154. }
  155. catch (final InterruptedException e)
  156. {
  157. if (Config.ENABLE_ALL_EXCEPTIONS)
  158. e.printStackTrace();
  159. }
  160. }
  161.  
  162. if (_topzone)
  163. {
  164. final int topzone_votes = getTopZoneVotes();
  165.  
  166. if (topzone_votes != -1)
  167. {
  168. LOGGER.info("[AutoVoteReward] Server TOPZONE Votes: " + topzone_votes);
  169. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Actual TOPZONE Votes are " + topzone_votes + "...");
  170.  
  171. if (topzone_votes != 0 && topzone_votes >= getTopZoneVoteCount() + PowerPakConfig.VOTES_FOR_REWARD)
  172. {
  173. _already_rewarded = new ArrayList<>();
  174.  
  175. final Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers();
  176.  
  177. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Great Work! Check your inventory for Reward!!");
  178.  
  179. // L2ItemInstance item;
  180. for (final L2PcInstance player : pls)
  181. {
  182. if (player != null && !player.isInOfflineMode() && player.isOnline() == 1)
  183. {
  184. if (player._active_boxes <= 1 || (player._active_boxes > 1 && checkSingleBox(player)))
  185. {
  186. final Set<Integer> items = PowerPakConfig.VOTES_REWARDS_LIST.keySet();
  187. for (final Integer i : items)
  188. {
  189. // item = player.getInventory().getItemByItemId(i);
  190.  
  191. // TODO: check on maxstack for item
  192. player.addItem("reward", i, PowerPakConfig.VOTES_REWARDS_LIST.get(i), player, true);
  193. }
  194. }
  195. }
  196. }
  197. setTopZoneVoteCount(topzone_votes);
  198. }
  199.  
  200. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Next TOPZONE Reward in " + minutes + " minutes at " + (getTopZoneVoteCount() + PowerPakConfig.VOTES_FOR_REWARD) + " Votes!!");
  201. // site web
  202. Announcements.getInstance().gameAnnounceToAll("[SiteWeb] " + PowerPakConfig.SERVER_WEB_SITE);
  203. }
  204. }
  205.  
  206. if (_topzone && _hopzone && _l2network && PowerPakConfig.VOTES_SYSYEM_STEP_DELAY > 0)
  207. {
  208. try
  209. {
  210. Thread.sleep(PowerPakConfig.VOTES_SYSYEM_STEP_DELAY / 2);
  211. }
  212. catch (final InterruptedException e)
  213. {
  214. if (Config.ENABLE_ALL_EXCEPTIONS)
  215. e.printStackTrace();
  216. }
  217. }
  218.  
  219. if (_l2network)
  220. {
  221. final int l2network_votes = getL2NetworkVotes();
  222.  
  223. if (l2network_votes != -1)
  224. {
  225. LOGGER.info("[AutoVoteReward] Server L2NETWORK Votes: " + l2network_votes);
  226. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Actual L2Network Votes are " + l2network_votes + "...");
  227.  
  228. if (l2network_votes != 0 && l2network_votes >= getL2NetworkVoteCount() + PowerPakConfig.VOTES_FOR_REWARD)
  229. {
  230. already_rewarded = new ArrayList<>();
  231.  
  232. final Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers();
  233.  
  234. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Great Work! Check your inventory for Reward!!");
  235.  
  236. // L2ItemInstance item;
  237. for (final L2PcInstance player : pls)
  238. {
  239. if (player != null && !player.isInOfflineMode() && player.isOnline() == 1)
  240. {
  241. if (player._active_boxes <= 1 || (player._active_boxes > 1 && checkSingleBox(player)))
  242. {
  243. final Set<Integer> items = PowerPakConfig.VOTES_REWARDS_LIST.keySet();
  244. for (final Integer i : items)
  245. {
  246. // item = player.getInventory().getItemByItemId(i);
  247.  
  248. // TODO: check on maxstack for item
  249. player.addItem("reward", i, PowerPakConfig.VOTES_REWARDS_LIST.get(i), player, true);
  250. }
  251. }
  252. }
  253. }
  254. setL2NetworkVoteCount(l2network_votes);
  255. }
  256. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Next L2Network Reward in " + minutes + " minutes at " + (getL2NetworkVoteCount() + PowerPakConfig.VOTES_FOR_REWARD) + " Votes!!");
  257. // site web
  258. Announcements.getInstance().gameAnnounceToAll("[SiteWeb] " + PowerPakConfig.SERVER_WEB_SITE);
  259. }
  260. }
  261. }
  262. }
  263.  
  264. // Check boxes
  265. protected boolean checkSingleBox(final L2PcInstance player)
  266. {
  267. if (player == null)
  268. return false;
  269.  
  270. if (player.getClient() != null && player.getClient().getConnection() != null && !player.getClient().getConnection().isClosed() && player.isOnline() == 1 && !player.isInOfflineMode())
  271. {
  272. final String playerip = player.getClient().getConnection().getInetAddress().getHostAddress();
  273.  
  274. if (_already_rewarded.contains(playerip))
  275. return false;
  276. _already_rewarded.add(playerip);
  277. return true;
  278. }
  279.  
  280. // if no connection (maybe offline shop) dnt reward
  281. return false;
  282. }
  283.  
  284. protected int getHopZoneVotes()
  285. {
  286. int votes = -1;
  287.  
  288. try
  289. {
  290. final WebClient webClient = new WebClient(BrowserVersion.CHROME);
  291. webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
  292. webClient.getOptions().setThrowExceptionOnScriptError(false);
  293. webClient.getOptions().setPrintContentOnFailingStatusCode(false);
  294.  
  295. final HtmlPage page = webClient.getPage(PowerPakConfig.VOTES_SITE_HOPZONE_URL);
  296.  
  297. final String fullPage = page.asXml();
  298. final int constrainA = fullPage.indexOf("rank anonymous tooltip") + 24;
  299. String voteSection = fullPage.substring(constrainA);
  300. final int constrainB = voteSection.indexOf("span") - 2;
  301. voteSection = voteSection.substring(0, constrainB).trim();
  302. votes = Integer.parseInt(voteSection);
  303.  
  304. // Try to free all the freaking resources
  305. page.cleanUp();
  306. webClient.getJavaScriptEngine().shutdown();
  307. webClient.closeAllWindows();
  308. }
  309. catch (final Exception e)
  310. {
  311. LOGGER.warn("[AutoVoteReward] Server HOPZONE is offline or something is wrong in link", e);
  312. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] HOPZONE is offline. We will check reward as it will be online again");
  313. }
  314.  
  315. return votes;
  316. }
  317.  
  318. protected int getTopZoneVotes()
  319. {
  320. int votes = -1;
  321. URL url = null;
  322. URLConnection con = null;
  323. InputStream is = null;
  324. InputStreamReader isr = null;
  325. BufferedReader in = null;
  326.  
  327. try
  328. {
  329. url = new URL(PowerPakConfig.VOTES_SITE_TOPZONE_URL);
  330. con = url.openConnection();
  331. con.addRequestProperty("User-Agent", "L2TopZone");
  332. is = con.getInputStream();
  333. isr = new InputStreamReader(is);
  334. in = new BufferedReader(isr);
  335. String inputLine;
  336. while ((inputLine = in.readLine()) != null)
  337. {
  338. if (inputLine.contains("Votes"))
  339. {
  340. votes = Integer.valueOf(inputLine.split(">")[3].replace("</div", ""));
  341. break;
  342. }
  343. }
  344. }
  345. catch (final Exception e)
  346. {
  347. LOGGER.warn("[AutoVoteReward] Server TOPZONE is offline or something is wrong in link");
  348. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] TOPZONE is offline. We will check reward as it will be online again");
  349. // e.printStackTrace();
  350. }
  351. finally
  352. {
  353. if (in != null)
  354. try
  355. {
  356. in.close();
  357. }
  358. catch (final IOException e1)
  359. {
  360. e1.printStackTrace();
  361. }
  362. if (isr != null)
  363. try
  364. {
  365. isr.close();
  366. }
  367. catch (final IOException e1)
  368. {
  369. e1.printStackTrace();
  370. }
  371. if (is != null)
  372. try
  373. {
  374. is.close();
  375. }
  376. catch (final IOException e1)
  377. {
  378. e1.printStackTrace();
  379. }
  380. }
  381. return votes;
  382. }
  383.  
  384. protected int getL2NetworkVotes()
  385. {
  386. int votes = -1;
  387. URL url = null;
  388. URLConnection con = null;
  389. InputStream is = null;
  390. InputStreamReader isr = null;
  391. BufferedReader in = null;
  392.  
  393. try
  394. {
  395. url = new URL(PowerPakConfig.VOTES_SITE_L2NETWORK_URL);
  396. con = url.openConnection();
  397. con.addRequestProperty("User-Agent", "L2Network");
  398. is = con.getInputStream();
  399. isr = new InputStreamReader(is);
  400. in = new BufferedReader(isr);
  401. String inputLine;
  402. while ((inputLine = in.readLine()) != null)
  403. {
  404. if (inputLine.contains("color:#e7ebf2"))
  405. {
  406. votes = Integer.valueOf(inputLine.split(">")[2].replace("</b", ""));
  407. break;
  408. }
  409. }
  410. }
  411. catch (final Exception e)
  412. {
  413. LOGGER.warn("[AutoVoteReward] Server L2NETWORK is offline or something is wrong in link");
  414. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] L2Network is offline. We will check reward as it will be online again");
  415. // e.printStackTrace();
  416. }
  417. finally
  418. {
  419. if (in != null)
  420. try
  421. {
  422. in.close();
  423. }
  424. catch (final IOException e1)
  425. {
  426. e1.printStackTrace();
  427. }
  428. if (isr != null)
  429. try
  430. {
  431. isr.close();
  432. }
  433. catch (final IOException e1)
  434. {
  435. e1.printStackTrace();
  436. }
  437. if (is != null)
  438. try
  439. {
  440. is.close();
  441. }
  442. catch (final IOException e1)
  443. {
  444. e1.printStackTrace();
  445. }
  446. }
  447. return votes;
  448. }
  449.  
  450. protected void setHopZoneVoteCount(final int voteCount)
  451. {
  452. _hopzoneVotesCount = voteCount;
  453. }
  454.  
  455. protected int getHopZoneVoteCount()
  456. {
  457. return _hopzoneVotesCount;
  458. }
  459.  
  460. protected void setTopZoneVoteCount(final int voteCount)
  461. {
  462. _topzoneVotesCount = voteCount;
  463. }
  464.  
  465. protected int getTopZoneVoteCount()
  466. {
  467. return _topzoneVotesCount;
  468. }
  469.  
  470. protected void setL2NetworkVoteCount(final int voteCount)
  471. {
  472. _l2networkVotesCount = voteCount;
  473. }
  474.  
  475. protected int getL2NetworkVoteCount()
  476. {
  477. return _l2networkVotesCount;
  478. }
  479.  
  480. public static AutoVoteRewardHandler getInstance()
  481. {
  482. Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
  483.  
  484. if (PowerPakConfig.VOTES_SITE_HOPZONE_URL != null && !PowerPakConfig.VOTES_SITE_HOPZONE_URL.equals(""))
  485. _hopzone = true;
  486.  
  487. if (PowerPakConfig.VOTES_SITE_TOPZONE_URL != null && !PowerPakConfig.VOTES_SITE_TOPZONE_URL.equals(""))
  488. _topzone = true;
  489.  
  490. if (PowerPakConfig.VOTES_SITE_L2NETWORK_URL != null && !PowerPakConfig.VOTES_SITE_L2NETWORK_URL.equals(""))
  491. _l2network = true;
  492.  
  493. if (_topzone || _hopzone || _l2network)
  494. return SingletonHolder._instance;
  495.  
  496. return null;
  497. }
  498.  
  499. @SuppressWarnings("synthetic-access")
  500. private static class SingletonHolder
  501. {
  502. protected static final AutoVoteRewardHandler _instance = new AutoVoteRewardHandler();
  503. }
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement