Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.84 KB | None | 0 0
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.model.entity;
  16.  
  17. import java.io.BufferedReader;
  18. import java.io.InputStreamReader;
  19. import java.net.URL;
  20. import java.net.URLConnection;
  21. import java.util.Collection;
  22. import java.util.HashMap;
  23.  
  24. import net.sf.l2j.commons.concurrent.ThreadPool;
  25.  
  26. import net.sf.l2j.Config;
  27. import net.sf.l2j.gameserver.model.World;
  28. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  29. import net.sf.l2j.gameserver.util.Broadcast;
  30. /**
  31. * @author Anarchy
  32. *
  33. */
  34. public class VoteRewardTopzone
  35. {
  36. // Configurations.
  37. private static String topzoneUrl = Config.TOPZONE_SERVER_LINK;
  38. private static String page1Url = Config.TOPZONE_FIRST_PAGE_LINK;
  39. private static int voteRewardVotesDifference = Config.TOPZONE_VOTES_DIFFERENCE;
  40. private static int firstPageRankNeeded = Config.TOPZONE_FIRST_PAGE_RANK_NEEDED;
  41. private static int checkTime = 60*1000*Config.TOPZONE_REWARD_CHECK_TIME;
  42.  
  43. // Don't-touch variables.
  44. private static int lastVotes = 0;
  45. private static HashMap<String, Integer> playerIps = new HashMap<>();
  46.  
  47. public static void updateConfigurations()
  48. {
  49. topzoneUrl = Config.TOPZONE_SERVER_LINK;
  50. page1Url = Config.TOPZONE_FIRST_PAGE_LINK;
  51. voteRewardVotesDifference = Config.TOPZONE_VOTES_DIFFERENCE;
  52. firstPageRankNeeded = Config.TOPZONE_FIRST_PAGE_RANK_NEEDED;
  53. checkTime = 60*1000*Config.TOPZONE_REWARD_CHECK_TIME;
  54. }
  55.  
  56. public static void getInstance()
  57. {
  58. System.out.println("Topzone - Vote reward system initialized.");
  59. ThreadPool.scheduleAtFixedRate(new Runnable()
  60. {
  61. @Override
  62. public void run()
  63. {
  64. if (Config.ALLOW_TOPZONE_VOTE_REWARD)
  65. {
  66. reward();
  67. }
  68. else
  69. {
  70. return;
  71. }
  72. }
  73. }, checkTime/2, checkTime);
  74. }
  75.  
  76. static void reward()
  77. {
  78. int firstPageVotes = getFirstPageRankVotes();
  79. int currentVotes = getVotes();
  80.  
  81. if (firstPageVotes == -1 || currentVotes == -1)
  82. {
  83. if (firstPageVotes == -1)
  84. {
  85. System.out.println("There was a problem on getting Topzone votes from server with rank "+firstPageRankNeeded+".");
  86. }
  87. if (currentVotes == -1)
  88. {
  89. System.out.println("There was a problem on getting Topzone server votes.");
  90. }
  91.  
  92. return;
  93. }
  94.  
  95. if (lastVotes == 0)
  96. {
  97. lastVotes = currentVotes;
  98. Broadcast.announceToOnlinePlayers("Topzone: Current vote count is "+currentVotes+".");
  99. Broadcast.announceToOnlinePlayers("Topzone: We need "+((lastVotes+voteRewardVotesDifference)-currentVotes)+" vote(s) for reward.");
  100. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  101. {
  102. System.out.println("Server votes on topzone: "+currentVotes);
  103. System.out.println("Votes needed for reward: "+((lastVotes+voteRewardVotesDifference)-currentVotes));
  104. }
  105. if (firstPageVotes-lastVotes <= 0)
  106. {
  107. Broadcast.announceToOnlinePlayers("Topzone: We are in the top "+firstPageRankNeeded+" of topzone, so the reward will be big.");
  108. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  109. {
  110. System.out.println("Server is on the top "+firstPageRankNeeded+" of topzone.");
  111. }
  112. }
  113. else
  114. {
  115. Broadcast.announceToOnlinePlayers("Topzone: We need "+(firstPageVotes-lastVotes)+" vote(s) to get to the top "+firstPageRankNeeded+" of topzone for big reward.");
  116. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  117. {
  118. System.out.println("Server votes needed for top "+firstPageRankNeeded+": "+(firstPageVotes-lastVotes));
  119. }
  120. }
  121. return;
  122. }
  123.  
  124. if (currentVotes >= lastVotes+voteRewardVotesDifference)
  125. {
  126. Collection<L2PcInstance> pls = World.getInstance().getPlayers();
  127. if (firstPageVotes-currentVotes <= 0)
  128. {
  129. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  130. {
  131. System.out.println("Server votes on topzone: "+currentVotes);
  132. System.out.println("Server is on the top "+firstPageRankNeeded+" of topzone.");
  133. System.out.println("Votes needed for next reward: "+((currentVotes+voteRewardVotesDifference)-currentVotes));
  134. }
  135. Broadcast.announceToOnlinePlayers("Topzone: Everyone has been rewarded with big reward.");
  136. Broadcast.announceToOnlinePlayers("Topzone: Current vote count is "+currentVotes+".");
  137. for (L2PcInstance p : pls)
  138. {
  139. boolean canReward = false;
  140. String pIp = p.getClient().getConnection().getInetAddress().getHostAddress();
  141. if (playerIps.containsKey(pIp))
  142. {
  143. int count = playerIps.get(pIp);
  144. if (count < Config.TOPZONE_DUALBOXES_ALLOWED)
  145. {
  146. playerIps.remove(pIp);
  147. playerIps.put(pIp, count+1);
  148. canReward = true;
  149. }
  150. }
  151. else
  152. {
  153. canReward = true;
  154. playerIps.put(pIp, 1);
  155. }
  156. if (canReward)
  157. {
  158. for (int i = 0; i < Config.TOPZONE_BIG_REWARD.length; i++)
  159. {
  160. p.addItem("Vote reward.", Config.TOPZONE_BIG_REWARD[i][0], Config.TOPZONE_BIG_REWARD[i][1], p, true);
  161. }
  162. }
  163. else
  164. {
  165. p.sendMessage("Already "+Config.TOPZONE_DUALBOXES_ALLOWED+" character(s) of your ip have been rewarded, so this character won't be rewarded.");
  166. }
  167. }
  168. playerIps.clear();
  169. }
  170. else
  171. {
  172. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  173. {
  174. System.out.println("Server votes on topzone: "+currentVotes);
  175. System.out.println("Server votes needed for top "+firstPageRankNeeded+": "+(firstPageVotes-lastVotes));
  176. System.out.println("Votes needed for next reward: "+((currentVotes+voteRewardVotesDifference)-currentVotes));
  177. }
  178. Broadcast.announceToOnlinePlayers("Topzone: Everyone has been rewarded with small reward.");
  179. Broadcast.announceToOnlinePlayers("Topzone: Current vote count is "+currentVotes+".");
  180. Broadcast.announceToOnlinePlayers("Topzone: We need "+(firstPageVotes-currentVotes)+" vote(s) to get to the top "+firstPageRankNeeded+" of topzone for big reward.");
  181. for (L2PcInstance p : pls)
  182. {
  183. boolean canReward = false;
  184. String pIp = p.getClient().getConnection().getInetAddress().getHostAddress();
  185. if (playerIps.containsKey(pIp))
  186. {
  187. int count = playerIps.get(pIp);
  188. if (count < Config.TOPZONE_DUALBOXES_ALLOWED)
  189. {
  190. playerIps.remove(pIp);
  191. playerIps.put(pIp, count+1);
  192. canReward = true;
  193. }
  194. }
  195. else
  196. {
  197. canReward = true;
  198. playerIps.put(pIp, 1);
  199. }
  200. if (canReward)
  201. {
  202. for (int i = 0; i < Config.TOPZONE_SMALL_REWARD.length; i++)
  203. {
  204. p.addItem("Vote reward.", Config.TOPZONE_SMALL_REWARD[i][0], Config.TOPZONE_SMALL_REWARD[i][1], p, true);
  205. }
  206. }
  207. else
  208. {
  209. p.sendMessage("Already "+Config.TOPZONE_DUALBOXES_ALLOWED+" character(s) of your ip have been rewarded, so this character won't be rewarded.");
  210. }
  211. }
  212. playerIps.clear();
  213. }
  214.  
  215. lastVotes = currentVotes;
  216. }
  217. else
  218. {
  219. if (firstPageVotes-currentVotes <= 0)
  220. {
  221. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  222. {
  223. System.out.println("Server votes on topzone: "+currentVotes);
  224. System.out.println("Server is on the top "+firstPageRankNeeded+" of topzone.");
  225. System.out.println("Votes needed for next reward: "+((lastVotes+voteRewardVotesDifference)-currentVotes));
  226. }
  227. Broadcast.announceToOnlinePlayers("Topzone: Current vote count is "+currentVotes+".");
  228. Broadcast.announceToOnlinePlayers("Topzone: We need "+((lastVotes+voteRewardVotesDifference)-currentVotes)+" vote(s) for big reward.");
  229. }
  230. else
  231. {
  232. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  233. {
  234. System.out.println("Server votes on topzone: "+currentVotes);
  235. System.out.println("Server votes needed for top "+firstPageRankNeeded+": "+(firstPageVotes-lastVotes));
  236. System.out.println("Votes needed for next reward: "+((lastVotes+voteRewardVotesDifference)-currentVotes));
  237. }
  238. Broadcast.announceToOnlinePlayers("Topzone: Current vote count is "+currentVotes+".");
  239. Broadcast.announceToOnlinePlayers("Topzone: We need "+((lastVotes+voteRewardVotesDifference)-currentVotes)+" vote(s) for small reward.");
  240. Broadcast.announceToOnlinePlayers("Topzone: We need "+(firstPageVotes-currentVotes)+" vote(s) to get to the top "+firstPageRankNeeded+" of topzone for big reward.");
  241. }
  242. }
  243. }
  244.  
  245. private static int getFirstPageRankVotes()
  246. {
  247. InputStreamReader isr = null;
  248. BufferedReader br = null;
  249.  
  250. try
  251. {
  252. URLConnection con = new URL(page1Url).openConnection();
  253. con.addRequestProperty("User-Agent", "Mozilla/4.76");
  254. isr = new InputStreamReader(con.getInputStream());
  255. br = new BufferedReader(isr);
  256.  
  257. String line;
  258. while ((line = br.readLine()) != null)
  259. {
  260. if (line.contains("<div class=\"slr\">"+firstPageRankNeeded+"<div class=\"votes\">Votes:<br><span>"))
  261. {
  262. int votes = Integer.valueOf(line.split("<div class=\"slr\">"+firstPageRankNeeded+"<div class=\"votes\">Votes:<br><span>")[1].replace("</span></div></div>", ""));
  263. return votes;
  264. }
  265. }
  266.  
  267. br.close();
  268. isr.close();
  269. }
  270. catch (Exception e)
  271. {
  272. System.out.println(e);
  273. System.out.println("Error while getting Hopzone server vote count.");
  274. }
  275.  
  276. return -1;
  277. }
  278.  
  279. private static int getVotes()
  280. {
  281. InputStreamReader isr = null;
  282. BufferedReader br = null;
  283.  
  284. try
  285. {
  286. URLConnection con = new URL(topzoneUrl).openConnection();
  287. con.addRequestProperty("User-Agent", "Mozilla/4.76");
  288. isr = new InputStreamReader(con.getInputStream());
  289. br = new BufferedReader(isr);
  290.  
  291. boolean got = false;
  292.  
  293. String line;
  294. while ((line = br.readLine()) != null)
  295. {
  296. if (line.contains("<div class=\"rank\"><div class=\"votes2\">Votes:<br>") && !got)
  297. {
  298. got = true;
  299. int votes = Integer.valueOf(line.split("<div class=\"rank\"><div class=\"votes2\">Votes:<br>")[1].replace("</div></div>", ""));
  300. return votes;
  301. }
  302. }
  303.  
  304. br.close();
  305. isr.close();
  306. }
  307. catch (Exception e)
  308. {
  309. System.out.println(e);
  310. System.out.println("Error while getting server vote count.");
  311. }
  312.  
  313. return -1;
  314. }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement