Guest User

hopzone

a guest
Mar 20th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.70 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 com.l2jserver.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 javolution.util.FastMap;
  25.      
  26.     import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.Announcements;
  28. import com.l2jserver.gameserver.ThreadPoolManager;
  29. import com.l2jserver.gameserver.model.L2World;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31.     /**
  32.      * @author Anarchy
  33.      *
  34.      */
  35.     public class VoteRewardHopzone
  36.     {
  37.         // Configurations.
  38.         private static String hopzoneUrl = Config.HOPZONE_SERVER_LINK;
  39.         private static String page1Url = Config.HOPZONE_FIRST_PAGE_LINK;
  40.         private static int voteRewardVotesDifference = Config.HOPZONE_VOTES_DIFFERENCE;
  41.         private static int firstPageRankNeeded = Config.HOPZONE_FIRST_PAGE_RANK_NEEDED;
  42.         private static int checkTime = 60*1000*Config.HOPZONE_REWARD_CHECK_TIME;
  43.        
  44.         // Don't-touch variables.
  45.         private static int lastVotes = 0;
  46.         private static HashMap<String, Integer> playerIps = new HashMap<>();
  47.        
  48.         public static void updateConfigurations()
  49.         {
  50.             hopzoneUrl = Config.HOPZONE_SERVER_LINK;
  51.             page1Url = Config.HOPZONE_FIRST_PAGE_LINK;
  52.             voteRewardVotesDifference = Config.HOPZONE_VOTES_DIFFERENCE;
  53.             firstPageRankNeeded = Config.HOPZONE_FIRST_PAGE_RANK_NEEDED;
  54.             checkTime = 60*1000*Config.HOPZONE_REWARD_CHECK_TIME;
  55.         }
  56.        
  57.         public static void getInstance()
  58.         {
  59.             System.out.println("Hopzone - Vote reward system initialized.");
  60.             ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable()
  61.             {
  62.                 @Override
  63.                 public void run()
  64.                 {
  65.                     if (Config.ALLOW_HOPZONE_VOTE_REWARD)
  66.                     {
  67.                         reward();
  68.                     }
  69.                     else
  70.                     {
  71.                         return;
  72.                     }
  73.                 }
  74.             }, checkTime/2, checkTime);
  75.         }
  76.        
  77.         static void reward()
  78.         {
  79.             int firstPageVotes = getFirstPageRankVotes();
  80.             int currentVotes = getVotes();
  81.            
  82.             if (firstPageVotes == -1 || currentVotes == -1)
  83.             {
  84.                 if (firstPageVotes == -1)
  85.                 {
  86.                     System.out.println("There was a problem on getting Hopzone votes from server with rank "+firstPageRankNeeded+".");
  87.                 }
  88.                 if (currentVotes == -1)
  89.                 {
  90.                     System.out.println("There was a problem on getting Hopzone server votes.");
  91.                 }
  92.                
  93.                 return;
  94.             }
  95.            
  96.             if (lastVotes == 0)
  97.             {
  98.                 lastVotes = currentVotes;
  99.                 Announcements.getInstance().announceToAll("Hopzone: Vote count is "+currentVotes+".");
  100.                 Announcements.getInstance().announceToAll("Hopzone: We need "+((lastVotes+voteRewardVotesDifference)-currentVotes)+" vote(s) for reward.");
  101.                 if (Config.ALLOW_HOPZONE_GAME_SERVER_REPORT)
  102.                 {
  103.                     System.out.println("Server votes on hopzone: "+currentVotes);
  104.                     System.out.println("Votes needed for reward: "+((lastVotes+voteRewardVotesDifference)-currentVotes));
  105.                 }
  106.                 if (firstPageVotes-lastVotes <= 0)
  107.                 {
  108.                     Announcements.getInstance().announceToAll("Hopzone: We are in the top "+firstPageRankNeeded+", so the reward will be big.");
  109.                     if (Config.ALLOW_HOPZONE_GAME_SERVER_REPORT)
  110.                     {
  111.                         System.out.println("Server is on the top "+firstPageRankNeeded+" of hopzone.");
  112.                     }
  113.                 }
  114.                 else
  115.                 {
  116.                     Announcements.getInstance().announceToAll("Hopzone: We need "+(firstPageVotes-lastVotes)+" vote(s) to get to the top "+firstPageRankNeeded+" of Hopzone for big reward.");
  117.                     if (Config.ALLOW_HOPZONE_GAME_SERVER_REPORT)
  118.                     {
  119.                         System.out.println("Server votes needed for top "+firstPageRankNeeded+": "+(firstPageVotes-lastVotes));
  120.                     }
  121.                 }
  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_HOPZONE_GAME_SERVER_REPORT)
  131.                     {
  132.                         System.out.println("Server votes on hopzone: "+currentVotes);
  133.                         System.out.println("Server is on the first page of hopzone.");
  134.                         System.out.println("Votes needed for next reward: "+((currentVotes+voteRewardVotesDifference)-currentVotes));
  135.                     }
  136.                     Announcements.getInstance().announceToAll("Hopzone: Everyone has been rewarded with big reward.");
  137.                     Announcements.getInstance().announceToAll("Hopzone: Current vote count is "+currentVotes+".");
  138.                     for (L2PcInstance p : pls)
  139.                     {
  140.                         boolean canReward = false;
  141.                         String pIp = p.getClient().getConnection().getInetAddress().getHostAddress();
  142.                         if (playerIps.containsKey(pIp))
  143.                         {
  144.                             int count = playerIps.get(pIp);
  145.                             if (count < Config.HOPZONE_DUALBOXES_ALLOWED)
  146.                             {
  147.                                 playerIps.remove(pIp);
  148.                                 playerIps.put(pIp, count+1);
  149.                                 canReward = true;
  150.                             }
  151.                         }
  152.                         else
  153.                         {
  154.                             canReward = true;
  155.                             playerIps.put(pIp, 1);
  156.                         }
  157.                         if (canReward)
  158.                         {
  159.                             for (int i : Config.HOPZONE_BIG_REWARD.keySet())
  160.                             {
  161.                                 p.addItem("Vote reward.", i, Config.HOPZONE_BIG_REWARD.get(i), p, true);
  162.                             }
  163.                         }
  164.                         else
  165.                         {
  166.                             p.sendMessage("Already "+Config.HOPZONE_DUALBOXES_ALLOWED+" character(s) of your ip have been rewarded, so this character won't be rewarded.");
  167.                         }
  168.                     }
  169.                     playerIps.clear();
  170.                 }
  171.                 else
  172.                 {
  173.                     if (Config.ALLOW_HOPZONE_GAME_SERVER_REPORT)
  174.                     {
  175.                         System.out.println("Server votes on hopzone: "+currentVotes);
  176.                         System.out.println("Server votes needed for first page: "+(firstPageVotes-lastVotes));
  177.                         System.out.println("Votes needed for next reward: "+((currentVotes+voteRewardVotesDifference)-currentVotes));
  178.                     }
  179.                     Announcements.getInstance().announceToAll("Hopzone: Everyone has been rewarded with small reward.");
  180.                     Announcements.getInstance().announceToAll("Hopzone: Current vote count is "+currentVotes+".");
  181.                     Announcements.getInstance().announceToAll("Hopzone: We need "+(firstPageVotes-currentVotes)+" vote(s) to get to the top "+firstPageRankNeeded+" of Hopzone for big reward.");
  182.                     for (L2PcInstance p : pls)
  183.                     {
  184.                         boolean canReward = false;
  185.                         String pIp = p.getClient().getConnection().getInetAddress().getHostAddress();
  186.                         if (playerIps.containsKey(pIp))
  187.                         {
  188.                             int count = playerIps.get(pIp);
  189.                             if (count < Config.HOPZONE_DUALBOXES_ALLOWED)
  190.                             {
  191.                                 playerIps.remove(pIp);
  192.                                 playerIps.put(pIp, count+1);
  193.                                 canReward = true;
  194.                             }
  195.                         }
  196.                         else
  197.                         {
  198.                             canReward = true;
  199.                             playerIps.put(pIp, 1);
  200.                         }
  201.                         if (canReward)
  202.                         {
  203.                             for (int i : Config.HOPZONE_SMALL_REWARD.keySet())
  204.                             {
  205.                                 p.addItem("Vote reward.", i, Config.HOPZONE_SMALL_REWARD.get(i), p, true);
  206.                             }
  207.                         }
  208.                         else
  209.                         {
  210.                             p.sendMessage("Already "+Config.HOPZONE_DUALBOXES_ALLOWED+" character(s) of your ip have been rewarded, so this character won't be rewarded.");
  211.                         }
  212.                     }
  213.                     playerIps.clear();
  214.                 }
  215.                
  216.                 lastVotes = currentVotes;
  217.             }
  218.             else
  219.             {
  220.                 if (firstPageVotes-currentVotes <= 0)
  221.                 {
  222.                     if (Config.ALLOW_HOPZONE_GAME_SERVER_REPORT)
  223.                     {
  224.                         System.out.println("Server votes on hopzone: "+currentVotes);
  225.                         System.out.println("Server is on the first page of hopzone.");
  226.                         System.out.println("Votes needed for next reward: "+((lastVotes+voteRewardVotesDifference)-currentVotes));
  227.                     }
  228.                     Announcements.getInstance().announceToAll("Hopzone: Current vote count is "+currentVotes+".");
  229.                     Announcements.getInstance().announceToAll("Hopzone: We need "+((lastVotes+voteRewardVotesDifference)-currentVotes)+" vote(s) for big reward.");
  230.                 }
  231.                 else
  232.                 {
  233.                     if (Config.ALLOW_HOPZONE_GAME_SERVER_REPORT)
  234.                     {
  235.                         System.out.println("Server votes on hopzone: "+currentVotes);
  236.                         System.out.println("Server votes needed for first page: "+(firstPageVotes-lastVotes));
  237.                         System.out.println("Votes needed for next reward: "+((lastVotes+voteRewardVotesDifference)-currentVotes));
  238.                     }
  239.                     Announcements.getInstance().announceToAll("Hopzone: Current vote count is "+currentVotes+".");
  240.                     Announcements.getInstance().announceToAll("Hopzone: We need "+((lastVotes+voteRewardVotesDifference)-currentVotes)+" vote(s) for small reward.");
  241.                     Announcements.getInstance().announceToAll("Hopzone: We need "+(firstPageVotes-currentVotes)+" vote(s) to get to the top "+firstPageRankNeeded+" of Hopzone for big reward.");
  242.                 }
  243.             }
  244.         }
  245.        
  246.         private static int getFirstPageRankVotes()
  247.         {
  248.             InputStreamReader isr = null;
  249.             BufferedReader br = null;
  250.            
  251.             try
  252.             {
  253.                 URLConnection con = new URL(page1Url).openConnection();
  254.                 con.addRequestProperty("User-Agent", "Mozilla/4.76");
  255.                 isr = new InputStreamReader(con.getInputStream());
  256.                 br = new BufferedReader(isr);
  257.                
  258.                 String line;
  259.                 int i = 0;
  260.                 while ((line = br.readLine()) != null)
  261.                 {
  262.                     if (line.contains("<span class=\"no\">"+firstPageRankNeeded+"</span>"))
  263.                     {
  264.                         i++;
  265.                     }
  266.                     if (line.contains("<span class=\"rank anonymous tooltip\"") && i == 1)
  267.                     {
  268.                         i = 0;
  269.                         int votes = Integer.valueOf(line.replaceAll("[^\\d]", ""));
  270.                         return votes;
  271.                     }
  272.                 }
  273.                
  274.                 br.close();
  275.                 isr.close();
  276.             }
  277.             catch (Exception e)
  278.             {
  279.                 System.out.println(e);
  280.                 System.out.println("Error while getting Hopzone server vote count.");
  281.             }
  282.            
  283.             return -1;
  284.         }
  285.        
  286.         private static int getVotes()
  287.         {
  288.             InputStreamReader isr = null;
  289.             BufferedReader br = null;
  290.            
  291.             try
  292.             {
  293.                 URLConnection con = new URL(hopzoneUrl).openConnection();
  294.                 con.addRequestProperty("User-Agent", "Mozilla/4.76");
  295.                 isr = new InputStreamReader(con.getInputStream());
  296.                 br = new BufferedReader(isr);
  297.                
  298.                 String line;
  299.                 while ((line = br.readLine()) != null)
  300.                 {
  301.                     if (line.contains("no steal make love")||line.contains("no votes here")||line.contains("bang, you don't have votes")|| line.contains("la vita e bella"))
  302.                     {
  303.                         int votes = Integer.valueOf(line.split(">")[2].replace("</span", ""));
  304.                         return votes;
  305.                     }
  306.                 }
  307.                
  308.                 br.close();
  309.                 isr.close();
  310.             }
  311.             catch (Exception e)
  312.             {
  313.                 System.out.println(e);
  314.                 System.out.println("Error while getting server vote count.");
  315.             }
  316.            
  317.             return -1;
  318.         }
  319.     }
Advertisement
Add Comment
Please, Sign In to add comment