Advertisement
Guest User

Untitled

a guest
May 22nd, 2012
1,595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.17 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.  
  23. import javolution.util.FastMap;
  24.  
  25. import net.sf.l2j.Config;
  26. import net.sf.l2j.gameserver.Announcements;
  27. import net.sf.l2j.gameserver.ThreadPoolManager;
  28. import net.sf.l2j.gameserver.model.L2World;
  29. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  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 FastMap<String, Integer> playerIps = new FastMap<String, Integer>();
  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("Vote reward system initialized.");
  59. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(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. private 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 votes from server with rank "+firstPageRankNeeded+".");
  86. }
  87. if (currentVotes == -1)
  88. {
  89. System.out.println("There was a problem on getting server votes.");
  90. }
  91.  
  92. return;
  93. }
  94.  
  95. if (lastVotes == 0)
  96. {
  97. lastVotes = currentVotes;
  98. Announcements.getInstance().announceToAll("Vote reward: Current vote count is "+currentVotes+".");
  99. Announcements.getInstance().announceToAll("Vote reward: 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. Announcements.getInstance().announceToAll("Vote reward: We are in the first page 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 first page of topzone.");
  111. }
  112. }
  113. else
  114. {
  115. Announcements.getInstance().announceToAll("Vote reward: We need "+(firstPageVotes-lastVotes)+" vote(s) to get to the first page of topzone for big reward.");
  116. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  117. {
  118. System.out.println("Server votes needed for first page: "+(firstPageVotes-lastVotes));
  119. }
  120. }
  121. Announcements.getInstance().announceToAll("Vote reward: Type .menu to see what the big and what the small reward is.");
  122. return;
  123. }
  124.  
  125. if (currentVotes >= lastVotes+voteRewardVotesDifference)
  126. {
  127. Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
  128. if (firstPageVotes-currentVotes <= 0)
  129. {
  130. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  131. {
  132. System.out.println("Server votes on topzone: "+currentVotes);
  133. System.out.println("Server is on the first page of topzone.");
  134. System.out.println("Votes needed for next reward: "+((currentVotes+voteRewardVotesDifference)-currentVotes));
  135. }
  136. Announcements.getInstance().announceToAll("Vote reward: Everyone has been rewarded with big reward.");
  137. Announcements.getInstance().announceToAll("Vote reward: Current vote count is "+currentVotes+".");
  138. Announcements.getInstance().announceToAll("Vote reward: Type .menu to see what the big and what the small reward is.");
  139. for (L2PcInstance p : pls)
  140. {
  141. boolean canReward = false;
  142. String pIp = p.getClient().getConnection().getInetAddress().getHostAddress();
  143. if (playerIps.containsKey(pIp))
  144. {
  145. int count = playerIps.get(pIp);
  146. if (count < Config.TOPZONE_DUALBOXES_ALLOWED)
  147. {
  148. playerIps.remove(pIp);
  149. playerIps.put(pIp, count+1);
  150. canReward = true;
  151. }
  152. }
  153. else
  154. {
  155. canReward = true;
  156. playerIps.put(pIp, 1);
  157. }
  158. if (canReward)
  159. {
  160. for (int i : Config.TOPZONE_BIG_REWARD.keySet())
  161. {
  162. p.addItem("Vote reward.", i, Config.TOPZONE_BIG_REWARD.get(i), p, true);
  163. }
  164. }
  165. else
  166. {
  167. p.sendMessage("Already "+Config.TOPZONE_DUALBOXES_ALLOWED+" character(s) of your ip have been rewarded, so this character won't be rewarded.");
  168. }
  169. }
  170. playerIps.clear();
  171. }
  172. else
  173. {
  174. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  175. {
  176. System.out.println("Server votes on topzone: "+currentVotes);
  177. System.out.println("Server votes needed for first page: "+(firstPageVotes-lastVotes));
  178. System.out.println("Votes needed for next reward: "+((currentVotes+voteRewardVotesDifference)-currentVotes));
  179. }
  180. Announcements.getInstance().announceToAll("Vote reward: Everyone has been rewarded with small reward.");
  181. Announcements.getInstance().announceToAll("Vote reward: Current vote count is "+currentVotes+".");
  182. Announcements.getInstance().announceToAll("Vote reward: We need "+(firstPageVotes-currentVotes)+" vote(s) to get to the first page of topzone for big reward.");
  183. Announcements.getInstance().announceToAll("Vote reward: Type .menu to see what the big and what the small reward is.");
  184. for (L2PcInstance p : pls)
  185. {
  186. boolean canReward = false;
  187. String pIp = p.getClient().getConnection().getInetAddress().getHostAddress();
  188. if (playerIps.containsKey(pIp))
  189. {
  190. int count = playerIps.get(pIp);
  191. if (count < Config.TOPZONE_DUALBOXES_ALLOWED)
  192. {
  193. playerIps.remove(pIp);
  194. playerIps.put(pIp, count+1);
  195. canReward = true;
  196. }
  197. }
  198. else
  199. {
  200. canReward = true;
  201. playerIps.put(pIp, 1);
  202. }
  203. if (canReward)
  204. {
  205. for (int i : Config.TOPZONE_SMALL_REWARD.keySet())
  206. {
  207. p.addItem("Vote reward.", i, Config.TOPZONE_SMALL_REWARD.get(i), p, true);
  208. }
  209. }
  210. else
  211. {
  212. p.sendMessage("Already "+Config.TOPZONE_DUALBOXES_ALLOWED+" character(s) of your ip have been rewarded, so this character won't be rewarded.");
  213. }
  214. }
  215. playerIps.clear();
  216. }
  217.  
  218. lastVotes = currentVotes;
  219. }
  220. else
  221. {
  222. if (firstPageVotes-currentVotes <= 0)
  223. {
  224. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  225. {
  226. System.out.println("Server votes on topzone: "+currentVotes);
  227. System.out.println("Server is on the first page of topzone.");
  228. System.out.println("Votes needed for next reward: "+((lastVotes+voteRewardVotesDifference)-currentVotes));
  229. }
  230. Announcements.getInstance().announceToAll("Vote reward: Current vote count is "+currentVotes+".");
  231. Announcements.getInstance().announceToAll("Vote reward: We need "+((lastVotes+voteRewardVotesDifference)-currentVotes)+" vote(s) for big reward.");
  232. Announcements.getInstance().announceToAll("Vote reward: Type .menu to see what the big and what the small reward is.");
  233. }
  234. else
  235. {
  236. if (Config.ALLOW_TOPZONE_GAME_SERVER_REPORT)
  237. {
  238. System.out.println("Server votes on topzone: "+currentVotes);
  239. System.out.println("Server votes needed for first page: "+(firstPageVotes-lastVotes));
  240. System.out.println("Votes needed for next reward: "+((lastVotes+voteRewardVotesDifference)-currentVotes));
  241. }
  242. Announcements.getInstance().announceToAll("Vote reward: Current vote count is "+currentVotes+".");
  243. Announcements.getInstance().announceToAll("Vote reward: We need "+((lastVotes+voteRewardVotesDifference)-currentVotes)+" vote(s) for small reward.");
  244. Announcements.getInstance().announceToAll("Vote reward: We need "+(firstPageVotes-currentVotes)+" vote(s) to get to the first page of topzone for big reward.");
  245. Announcements.getInstance().announceToAll("Vote reward: Type .menu to see what the big and what the small reward is.");
  246. }
  247. }
  248. }
  249.  
  250. private static int getFirstPageRankVotes()
  251. {
  252. InputStreamReader isr = null;
  253. BufferedReader br = null;
  254.  
  255. try
  256. {
  257. URLConnection con = new URL(page1Url).openConnection();
  258. con.addRequestProperty("User-Agent", "Mozilla/4.76");
  259. isr = new InputStreamReader(con.getInputStream());
  260. br = new BufferedReader(isr);
  261.  
  262. String line;
  263. int i = 0;
  264. while ((line = br.readLine()) != null)
  265. {
  266. if (line.contains("<td><div align=\"center\">"+firstPageRankNeeded+"</div></td>"))
  267. {
  268. i++;
  269. }
  270. if (line.contains("<td><div align=\"center\">") && i == 1)
  271. {
  272. i++;
  273. }
  274. if (line.contains("<td><div align=\"center\">") && i == 2)
  275. {
  276. i = 0;
  277. int votes = Integer.valueOf(line.split(">")[2].replace("</div", ""));
  278. return votes;
  279. }
  280. }
  281.  
  282. br.close();
  283. isr.close();
  284. }
  285. catch (Exception e)
  286. {
  287. System.out.println(e);
  288. System.out.println("Error while getting server vote count.");
  289. }
  290.  
  291. return -1;
  292. }
  293.  
  294. private static int getVotes()
  295. {
  296. InputStreamReader isr = null;
  297. BufferedReader br = null;
  298.  
  299. try
  300. {
  301. URLConnection con = new URL(topzoneUrl).openConnection();
  302. con.addRequestProperty("User-Agent", "Mozilla/4.76");
  303. isr = new InputStreamReader(con.getInputStream());
  304. br = new BufferedReader(isr);
  305.  
  306. boolean got = false;
  307.  
  308. String line;
  309. while ((line = br.readLine()) != null)
  310. {
  311. if (line.contains("<tr><td><div align=\"center\"><b><font style=\"font-size:14px;color:#018BC1;\"") && !got)
  312. {
  313. got = true;
  314. int votes = Integer.valueOf(line.split("=\"font-size:14px;color:#018BC1;\">")[1].replace("</font></b></div></td></tr>", ""));
  315. return votes;
  316. }
  317. }
  318.  
  319. br.close();
  320. isr.close();
  321. }
  322. catch (Exception e)
  323. {
  324. System.out.println(e);
  325. System.out.println("Error while getting server vote count.");
  326. }
  327.  
  328. return -1;
  329. }
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement