Advertisement
Guest User

Andrew

a guest
Jul 24th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 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. import java.util.logging.Logger;
  14.  
  15. import com.l2jfrozen.Config;
  16. import com.l2jfrozen.gameserver.model.L2World;
  17. import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
  18. import com.l2jfrozen.gameserver.model.entity.Announcements;
  19. import com.l2jfrozen.gameserver.powerpak.PowerPakConfig;
  20. import com.l2jfrozen.gameserver.thread.ThreadPoolManager;
  21.  
  22. public class AutoVoteRewardHandler
  23. {
  24. protected static final Logger _log = Logger.getLogger(AutoVoteRewardHandler.class.getName());
  25.  
  26. private int hopzoneVotesCount = 0;
  27. private int topzoneVotesCount = 0;
  28. protected List<String> already_rewarded;
  29.  
  30. protected static boolean topzone = false;
  31. protected static boolean hopzone = false;
  32.  
  33. private AutoVoteRewardHandler()
  34. {
  35. _log.info("Vote Reward System Initiated.");
  36.  
  37. if(hopzone){
  38. int hopzone_votes = getHopZoneVotes();
  39.  
  40. if(hopzone_votes == -1){
  41. hopzone_votes = 0;
  42. }
  43.  
  44. setHopZoneVoteCount(hopzone_votes);
  45. }
  46.  
  47. if(topzone){
  48. int topzone_votes = getTopZoneVotes();
  49.  
  50. if(topzone_votes == -1){
  51. topzone_votes = 0;
  52. }
  53.  
  54. setTopZoneVoteCount(topzone_votes);
  55. }
  56.  
  57. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(), PowerPakConfig.VOTES_SYSYEM_INITIAL_DELAY, PowerPakConfig.VOTES_SYSYEM_STEP_DELAY);
  58. }
  59.  
  60. protected class AutoReward implements Runnable
  61. {
  62. @Override
  63. public void run()
  64. {
  65. int minutes = (PowerPakConfig.VOTES_SYSYEM_STEP_DELAY/1000)/60;
  66.  
  67. if(hopzone){
  68. int hopzone_votes = getHopZoneVotes();
  69.  
  70. if(hopzone_votes != -1){
  71. _log.info("[AutoVoteReward] Server HOPZONE Votes: " + hopzone_votes);
  72. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Actual HOPZONE Votes are " + hopzone_votes + "...");
  73.  
  74. if (hopzone_votes != 0 && hopzone_votes >= getHopZoneVoteCount() + PowerPakConfig.VOTES_FOR_REWARD)
  75. {
  76. already_rewarded = new ArrayList<String>();
  77.  
  78. Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers();
  79.  
  80. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Great Work! Check your inventory for Reward!!");
  81.  
  82. //L2ItemInstance item;
  83. for (L2PcInstance player : pls)
  84. {
  85. if (player != null && !player.isOffline() && player.isOnline()==1)
  86. {
  87. if(player._active_boxes<=1 || (player._active_boxes>1 && checkSingleBox(player))){
  88.  
  89. Set<Integer> items = PowerPakConfig.VOTES_REWARDS_LIST.keySet();
  90. for (Integer i : items)
  91. {
  92. //item = player.getInventory().getItemByItemId(i);
  93.  
  94. //TODO: check on maxstack for item
  95. player.addItem("reward", i, PowerPakConfig.VOTES_REWARDS_LIST.get(i), player, true);
  96.  
  97. }
  98.  
  99. }
  100. }
  101. }
  102. setHopZoneVoteCount(hopzone_votes);
  103. }
  104. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Next HOPZONE Reward in "+minutes+" minutes at " + (getHopZoneVoteCount() + PowerPakConfig.VOTES_FOR_REWARD) + " Votes!!");
  105. //site web
  106. Announcements.getInstance().gameAnnounceToAll("[SiteWeb] "+PowerPakConfig.SERVER_WEB_SITE);
  107.  
  108. }
  109.  
  110. }
  111.  
  112. if(topzone && hopzone && PowerPakConfig.VOTES_SYSYEM_STEP_DELAY>0)
  113. try
  114. {
  115. Thread.sleep(PowerPakConfig.VOTES_SYSYEM_STEP_DELAY/2);
  116. }
  117. catch(InterruptedException e)
  118. {
  119. if(Config.ENABLE_ALL_EXCEPTIONS)
  120. e.printStackTrace();
  121. }
  122.  
  123. if(topzone){
  124. int topzone_votes = getTopZoneVotes();
  125.  
  126. if(topzone_votes != -1){
  127.  
  128. _log.info("[AutoVoteReward] Server TOPZONE Votes: " + topzone_votes);
  129. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Actual TOPZONE Votes are " + topzone_votes + "...");
  130.  
  131. if (topzone_votes != 0 && topzone_votes >= getTopZoneVoteCount() + PowerPakConfig.VOTES_FOR_REWARD)
  132. {
  133. already_rewarded = new ArrayList<String>();
  134.  
  135. Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers();
  136.  
  137. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Great Work! Check your inventory for Reward!!");
  138.  
  139. //L2ItemInstance item;
  140. for (L2PcInstance player : pls)
  141. {
  142. if (player != null && !player.isOffline() && player.isOnline()==1)
  143. {
  144. if(player._active_boxes<=1 || (player._active_boxes>1 && checkSingleBox(player))){
  145.  
  146. Set<Integer> items = PowerPakConfig.VOTES_REWARDS_LIST.keySet();
  147. for (Integer i : items)
  148. {
  149. //item = player.getInventory().getItemByItemId(i);
  150.  
  151. //TODO: check on maxstack for item
  152. player.addItem("reward", i, PowerPakConfig.VOTES_REWARDS_LIST.get(i), player, true);
  153.  
  154. }
  155.  
  156. }
  157. }
  158. }
  159. setTopZoneVoteCount(topzone_votes);
  160. }
  161.  
  162. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] Next TOPZONE Reward in "+minutes+" minutes at " + (getTopZoneVoteCount() + PowerPakConfig.VOTES_FOR_REWARD) + " Votes!!");
  163. //site web
  164. Announcements.getInstance().gameAnnounceToAll("[SiteWeb] "+PowerPakConfig.SERVER_WEB_SITE);
  165.  
  166. }
  167.  
  168.  
  169. }
  170.  
  171. }
  172. }
  173.  
  174. protected boolean checkSingleBox(L2PcInstance player){
  175.  
  176. if(player.getClient()!=null && player.getClient().getConnection()!=null && !player.getClient().getConnection().isClosed() && !player.isOffline()){
  177.  
  178. String playerip = player.getClient().getConnection().getInetAddress().getHostAddress();
  179.  
  180. if(already_rewarded.contains(playerip))
  181. return false;
  182. already_rewarded.add(playerip);
  183. return true;
  184. }
  185.  
  186. //if no connection (maybe offline shop) dnt reward
  187. return false;
  188. }
  189.  
  190. protected int getHopZoneVotes()
  191. {
  192. int votes = -1;
  193. URL url = null;
  194. URLConnection con = null;
  195. InputStream is = null;
  196. InputStreamReader isr = null;
  197. BufferedReader in = null;
  198. try
  199. {
  200. url = new URL(PowerPakConfig.VOTES_SITE_HOPZONE_URL);
  201. con = url.openConnection();
  202. con.addRequestProperty("User-Agent", "Mozilla/4.76");
  203. is = con.getInputStream();
  204. isr = new InputStreamReader(is);
  205. in = new BufferedReader(isr);
  206. String inputLine;
  207. while ((inputLine = in.readLine()) != null)
  208. {
  209. if (inputLine.contains("rank anonymous tooltip"))
  210. {
  211. votes = Integer.valueOf(inputLine.split(">")[2].replace("</span", ""));
  212. break;
  213. }
  214. }
  215. }
  216. catch (Exception e)
  217. {
  218. _log.info("[AutoVoteReward] Server HOPZONE is offline or something is wrong in link");
  219. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] HOPZONE is offline. We will check reward as it will be online again");
  220. //e.printStackTrace();
  221. }
  222. finally
  223. {
  224. if(in!=null)
  225. try
  226. {
  227. in.close();
  228. }
  229. catch(IOException e1)
  230. {
  231. e1.printStackTrace();
  232. }
  233. if(isr!=null)
  234. try
  235. {
  236. isr.close();
  237. }
  238. catch(IOException e1)
  239. {
  240. e1.printStackTrace();
  241. }
  242. if(is!=null)
  243. try
  244. {
  245. is.close();
  246. }
  247. catch(IOException e1)
  248. {
  249. e1.printStackTrace();
  250. }
  251.  
  252. }
  253. return votes;
  254. }
  255.  
  256. protected int getTopZoneVotes()
  257. {
  258. int votes = -1;
  259. URL url = null;
  260. URLConnection con = null;
  261. InputStream is = null;
  262. InputStreamReader isr = null;
  263. BufferedReader in = null;
  264. try
  265. {
  266. url = new URL(PowerPakConfig.VOTES_SITE_TOPZONE_URL);
  267. con = url.openConnection();
  268. con.addRequestProperty("User-Agent", "Mozilla/4.76");
  269. is = con.getInputStream();
  270. isr = new InputStreamReader(is);
  271. in = new BufferedReader(isr);
  272. String inputLine;
  273. while ((inputLine = in.readLine()) != null)
  274. {
  275. if (inputLine.contains("Votes"))
  276. {
  277. String votesLine = inputLine;
  278.  
  279. votes = Integer.valueOf(votesLine.split(">")[3].replace("</div", ""));
  280. break;
  281. }
  282. }
  283. }
  284. catch (Exception e)
  285. {
  286. _log.warning("[AutoVoteReward] Server TOPZONE is offline or something is wrong in link");
  287. Announcements.getInstance().gameAnnounceToAll("[AutoVoteReward] TOPZONE is offline. We will check reward as it will be online again");
  288. //e.printStackTrace();
  289. }
  290. finally
  291. {
  292. if(in!=null)
  293. try
  294. {
  295. in.close();
  296. }
  297. catch(IOException e1)
  298. {
  299. e1.printStackTrace();
  300. }
  301. if(isr!=null)
  302. try
  303. {
  304. isr.close();
  305. }
  306. catch(IOException e1)
  307. {
  308. e1.printStackTrace();
  309. }
  310. if(is!=null)
  311. try
  312. {
  313. is.close();
  314. }
  315. catch(IOException e1)
  316. {
  317. e1.printStackTrace();
  318. }
  319. }
  320. return votes;
  321. }
  322.  
  323. protected void setHopZoneVoteCount(int voteCount)
  324. {
  325. hopzoneVotesCount = voteCount;
  326. }
  327.  
  328. protected int getHopZoneVoteCount()
  329. {
  330. return hopzoneVotesCount;
  331. }
  332.  
  333. protected void setTopZoneVoteCount(int voteCount)
  334. {
  335. topzoneVotesCount = voteCount;
  336. }
  337.  
  338. protected int getTopZoneVoteCount()
  339. {
  340. return topzoneVotesCount;
  341. }
  342.  
  343. public static AutoVoteRewardHandler getInstance()
  344. {
  345. if(PowerPakConfig.VOTES_SITE_HOPZONE_URL != null && !PowerPakConfig.VOTES_SITE_HOPZONE_URL.equals("")){
  346. hopzone = true;
  347. }
  348.  
  349. if(PowerPakConfig.VOTES_SITE_TOPZONE_URL != null && !PowerPakConfig.VOTES_SITE_TOPZONE_URL.equals("")){
  350. topzone = true;
  351. }
  352.  
  353. if(topzone || hopzone)
  354. return SingletonHolder._instance;
  355. return null;
  356. }
  357.  
  358. @SuppressWarnings("synthetic-access")
  359. private static class SingletonHolder
  360. {
  361. protected static final AutoVoteRewardHandler _instance = new AutoVoteRewardHandler();
  362. }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement