Jump to content

[Share]AutoVoteReward Topzone


Recommended Posts

Hallo This Java Code In By Vampirekiller L2Shax  Server

 

net.sf.l2j.gameserver.model

add New Class Name AutoVoteRewardHandler.java

 

package net.sf.l2j.gameserver.model;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import net.sf.l2j.Config;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.ThreadPoolManager;
import net.sf.l2j.gameserver.datatables.ItemTable;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.network.SystemMessageId;
import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
import net.sf.l2j.gameserver.util.Broadcast;

public class AutoVoteRewardHandler
{
  private final int initialCheck = Config.VOTE_INIT_CHECK;

  private final int delayForCheck = Config.VOTE_DELAY;

  private String server_name = "";

  private final int votesRequiredForReward = Config.VOTE_DIFF;

  private static int lastVoteCount = 0;

  private AutoVoteRewardHandler(Object object)
  {
    System.out.println("Vote Reward System Initiated.");
    ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(null), initialCheck, delayForCheck);
  }

  private int getVotes()
  {
    URL url = null;
    InputStreamReader isr = null;
    BufferedReader in = null;
    try
    {
      url = new URL(Config.VOTE_URL);

      if (Config.VOTE_DEBUG_PARSE) System.out.println("HTML Parse-------------------------------------");

      isr = new InputStreamReader(url.openStream());
      in = new BufferedReader(isr);
      String inputLine;
      while ((inputLine = in.readLine()) != null)
      {
        if (Config.VOTE_DEBUG_PARSE) System.out.println(inputLine);
        if (inputLine.contains(">Votes<")) {
          inputLine = in.readLine();
          if (Config.VOTE_DEBUG_PARSE) System.out.println("-------------->" + inputLine.split(">")[5].replace("</font", ""));
          int i = Integer.valueOf(inputLine.split(">")[5].replace("</font", "")).intValue();
          return i;
        }
      }
    }
    catch (IOException e)
    {
      ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(null), initialCheck, delayForCheck);
      System.out.println("Server Votes: Site Down");
      if (Config.VOTE_DEBUG) e.printStackTrace();
    }
    finally
    {
      try
      {
        in.close();
      }
      catch (IOException e)
      {
      }
      try {
        isr.close();
      }
      catch (IOException e) {
      }
    }
    return 0;
  }

  private int getVotesForFirstPage(int topn)
  {
    URL url = null;
    InputStreamReader isr = null;
    BufferedReader in = null;
    try
    {
      url = new URL(Config.VOTE_LIST);
      isr = new InputStreamReader(url.openStream());
      in = new BufferedReader(isr);

      if (Config.VOTE_DEBUG_PARSE) System.out.println("HTML First Page Parse-------------------------------------");
      String inputLine;
      while ((inputLine = in.readLine()) != null)
      {
        if (Config.VOTE_DEBUG_PARSE) System.out.println(inputLine);
        if (inputLine.contains("<td><div align=\"center\">" + topn + "</div></td>")) {
          for (int i = 0; i < 3; i++) inputLine = in.readLine();
          server_name = inputLine.split("\"")[1];
          if (Config.VOTE_DEBUG_PARSE) System.out.println("First page parse ServerName-->" + server_name);
          for (int i = 0; i < 2; i++) inputLine = in.readLine();
          if (Config.VOTE_DEBUG_PARSE) System.out.println("First page parse FirstPage Last Server Votes-->" + inputLine.split(">")[2].replace("</div", ""));
          int i = Integer.valueOf(inputLine.split(">")[2].replace("</div", "")).intValue();
          return i;
        }
      }
    }
    catch (IOException e)
    {
      ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(null), initialCheck, delayForCheck);
      System.out.println("Server Votes: Site Down");
      if (Config.VOTE_DEBUG) e.printStackTrace();
    }
    finally
    {
      try
      {
        in.close();
      }
      catch (IOException e)
      {
      }
      try {
        isr.close();
      }
      catch (IOException e) {
      }
    }
    return 0;
  }

  public static void setLastVoteCount(int voteCount)
  {
    lastVoteCount = voteCount;
  }

  public static void onShutdown() throws SQLException {
    Connection con = null;
    try
    {
      con = L2DatabaseFactory.getInstance().getConnection();
      PreparedStatement statement = con.prepareStatement("UPDATE Votes SET count = " + lastVoteCount);

      statement.execute();
      statement.close();
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
    	con.close();
    }
  }

  public static int getLastVoteCount()
  {
    return lastVoteCount;
  }

  public static void onStartup() throws SQLException {
    int count = 0;
    Connection con = null;
    try
    {
      con = L2DatabaseFactory.getInstance().getConnection();
      PreparedStatement statement = con.prepareStatement("SELECT * FROM Votes");

      ResultSet rset = statement.executeQuery();
      while (rset.next())
      {
        count = rset.getInt("count");
      }

      rset.close();
      statement.close();
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    finally
    {
    	con.close();
    }

    setLastVoteCount(count);
  }

  public static AutoVoteRewardHandler getInstance()
  {
    return SingletonHolder._instance;
  }

  private static class SingletonHolder
  {
    protected static final AutoVoteRewardHandler _instance = new AutoVoteRewardHandler(null);
  }

  private class AutoReward
    implements Runnable
  {
    private AutoReward(Object object)
    {
    }

    public void run()
    {
      int votes = AutoVoteRewardHandler.this.getVotes();
      int votes_frs = AutoVoteRewardHandler.this.getVotesForFirstPage(Config.VOTE_TOP);
      if ((votes < AutoVoteRewardHandler.getLastVoteCount()) || (AutoVoteRewardHandler.getLastVoteCount() <= 0)) {
        AutoVoteRewardHandler.setLastVoteCount(votes);
      }
      System.out.println("Server Votes: " + votes + " - For First Page: " + (votes_frs - votes));
      if (Config.VOTE_DEBUG) {
        System.out.println("URL: " + Config.VOTE_URL);
        System.out.println("vote diff: " + AutoVoteRewardHandler.this.votesRequiredForReward);
        for (int[] reward : Config.VOTE_REWARDS)
          System.out.println("Reward Low : " + reward[0] + "-" + reward[1]);
        for (int[] reward : Config.VOTE_REWARDS_FIRST_PAGE)
          System.out.println("Reward High : " + reward[0] + "-" + reward[1]);
      }
      if (!Config.VOTE_DEBUG) {
        if ((votes != 0) && (AutoVoteRewardHandler.getLastVoteCount() != 0) && (votes >= AutoVoteRewardHandler.getLastVoteCount() + AutoVoteRewardHandler.this.votesRequiredForReward))
        {
          Connection con = null;
          try
          {
            con = L2DatabaseFactory.getInstance().getConnection();
            PreparedStatement statement = con.prepareStatement("SELECT\tc.obj_Id,\tc.char_name FROM \tcharacters AS c LEFT JOIN \taccounts AS a ON \tc.account_name = a.login WHERE \tc.online > 0 ORDER BY \tc.level DESC");

            ResultSet rset = statement.executeQuery();
            L2PcInstance player = null;

            if ((votes < votes_frs) || ((votes == votes_frs) && (!Config.VOTE_URL.equals(AutoVoteRewardHandler.this.server_name)))) {
              Broadcast.toAllOnlinePlayers(new CreatureSay(1, 3, "Vote System", Config.VOTE_TEXT_SMALL_WIN));
              System.out.println("Server Votes: SMALL Rewarded at " + votes);
            }while (rset.next())
            {
              player = L2World.getInstance().getPlayer(rset.getInt("obj_Id"));
              if ((player == null) ||
             (player.isOffline())) continue;
              for (int[] reward : Config.VOTE_REWARDS)
              {
                player.addItem("Vote Reward", reward[0], reward[1], player, false);
                SystemMessage systemMessage = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
                systemMessage.addItemName(reward[0]);
                systemMessage.addNumber(reward[1]);
                player.sendPacket(systemMessage);
              continue;
              
              }
              if ((votes <= votes_frs) && ((votes != votes_frs) || (!Config.VOTE_URL.equals(AutoVoteRewardHandler.this.server_name)))) break;
              Broadcast.toAllOnlinePlayers(new CreatureSay(1, 3, "Vote System", Config.VOTE_TEXT_BIG_WIN));
              System.out.println("Server Votes: BIG Rewarded at " + votes);
              while (rset.next())
              {
                player = L2World.getInstance().getPlayer(rset.getInt("obj_Id"));
                if ((player == null) || 
                  (player.isOffline())) continue;
                for (int[] reward : Config.VOTE_REWARDS_FIRST_PAGE)
                {
                  player.addItem("Vote Reward", reward[0], reward[1], player, false);
                  SystemMessage systemMessage = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
                  systemMessage.addItemName(reward[0]);
                  systemMessage.addNumber(reward[1]);
                  player.sendPacket(systemMessage);
                }

              }

            }

            rset.close();
            statement.close();
          }
          catch (SQLException e)
          {
            e.printStackTrace();
          }
          finally
          {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

          }

          AutoVoteRewardHandler.setLastVoteCount(AutoVoteRewardHandler.getLastVoteCount() + AutoVoteRewardHandler.this.votesRequiredForReward);
        }

        if ((votes < votes_frs) || ((votes == votes_frs) && (!Config.VOTE_URL.equals(AutoVoteRewardHandler.this.server_name))))
        {
          String msg = Config.VOTE_TEXT_CURRENT;
          msg = msg.replaceAll("%votes%", Integer.toString(votes));
          msg = msg.replaceAll("%votesRequiredForReward%", Integer.toString(AutoVoteRewardHandler.getLastVoteCount() + AutoVoteRewardHandler.this.votesRequiredForReward - votes));
          int count = 0;
          for (int[] reward : Config.VOTE_REWARDS)
          {
            msg = msg + reward[1] + " " + ItemTable.getInstance().getTemplate(reward[0]).getName();
            count++;
            if (count != Config.VOTE_REWARDS.size())
              msg = msg + ", ";
          }
          msg = msg + ")";

          Broadcast.toAllOnlinePlayers(new CreatureSay(1, 3, "Vote System", msg));

          msg = Config.VOTE_TEXT_FOR_FIRSTPAGE;
          msg = msg.replaceAll("%votes_frs%", Integer.toString(votes_frs - votes + 1));
          count = 0;
          for (int[] reward : Config.VOTE_REWARDS_FIRST_PAGE)
          {
            msg = msg + reward[1] + " " + ItemTable.getInstance().getTemplate(reward[0]).getName();
            count++;
            if (count != Config.VOTE_REWARDS_FIRST_PAGE.size())
              msg = msg + ", ";
          }
          msg = msg + ")";
          Broadcast.toAllOnlinePlayers(new CreatureSay(1, 3, "Vote System", msg));
        }
        else if ((votes > votes_frs) || ((votes == votes_frs) && (Config.VOTE_URL.equals(AutoVoteRewardHandler.this.server_name)))) {
          String msg = "Votes now: " + votes + ". " + (AutoVoteRewardHandler.getLastVoteCount() + AutoVoteRewardHandler.this.votesRequiredForReward - votes) + " Votes left at TopZone until next reward (";

          int count = 0;
          for (int[] reward : Config.VOTE_REWARDS_FIRST_PAGE)
          {
            msg = msg + reward[1] + " " + ItemTable.getInstance().getTemplate(reward[0]).getName();
            count++;
            if (count != Config.VOTE_REWARDS_FIRST_PAGE.size())
              msg = msg + ", ";
          }
          msg = msg + ")";

          Broadcast.toAllOnlinePlayers(new CreatureSay(1, 3, "Vote System", msg));
        }
      }
    }
  }
}

 

net.sf.l2j Config.java

  public static boolean VOTE_ENABLE;
  public static boolean VOTE_DEBUG;
  public static boolean VOTE_DEBUG_PARSE;
  public static String VOTE_URL;
  public static int VOTE_INIT_CHECK;
  public static int VOTE_DELAY;
  public static int VOTE_DIFF;
  public static List<int[]> VOTE_REWARDS;
  public static int VOTE_TOP;
  public static String VOTE_LIST;
  public static List<int[]> VOTE_REWARDS_FIRST_PAGE;
  public static String VOTE_TEXT_SMALL_WIN;
  public static String VOTE_TEXT_BIG_WIN;
  public static String VOTE_TEXT_CURRENT;
  public static String VOTE_TEXT_FOR_FIRSTPAGE;

 

    		      VOTE_ENABLE = Boolean.parseBoolean(votereward.getProperty("VoteEnable", "false"));
    		      VOTE_DEBUG = Boolean.parseBoolean(votereward.getProperty("VoteDebug", "false"));
    		      VOTE_DEBUG_PARSE = Boolean.parseBoolean(votereward.getProperty("VoteDebugParse", "false"));
    		      VOTE_URL = votereward.getProperty("VoteUrl", "");
    		      VOTE_INIT_CHECK = Integer.parseInt(votereward.getProperty("InitCheck", "0"));
    		      VOTE_DELAY = Integer.parseInt(votereward.getProperty("Delay", "0"));
    		      VOTE_DIFF = Integer.parseInt(votereward.getProperty("VoteDiff", "10"));
    		      VOTE_TOP = Integer.parseInt(votereward.getProperty("VoteTop", "10"));
    		      VOTE_REWARDS = new ArrayList<int[]>();

    		      VOTE_TEXT_SMALL_WIN = votereward.getProperty("VoteTextSmallWin", "");
    		      VOTE_TEXT_BIG_WIN = votereward.getProperty("VoteTextBigWin", "");
    		      VOTE_TEXT_CURRENT = votereward.getProperty("VoteCurrent", "");
    		      VOTE_TEXT_FOR_FIRSTPAGE = votereward.getProperty("VoteTextForFirstpage", "");

    		      VOTE_LIST = votereward.getProperty("VoteList", "");
    		      VOTE_REWARDS_FIRST_PAGE = new ArrayList<int[]>();

    		      String[] propertySplit = votereward.getProperty("VoteRewards", "100001,50").split(";");
    		      for (String reward : propertySplit)
    		      {
    		        String[] rewardSplit = reward.split(",");
    		        if (rewardSplit.length != 2) {
    		          _log.warning(StringUtil.concat(new String[] { "VoteEngine[Config.load()]: invalid config property -> VoteReward \"", reward, "\"" }));
    		        }
    		        else {
    		          try
    		          {
    		            VOTE_REWARDS.add(new int[] { Integer.parseInt(rewardSplit[0]), Integer.parseInt(rewardSplit[1]) });
    		          }
    		          catch (NumberFormatException nfe)
    		          {
    		            if (!reward.isEmpty()) {
    		              _log.warning(StringUtil.concat(new String[] { "VoteEngine[Config.load()]: invalid config property -> VoteReward \"", reward, "\"" }));
    		            }
    		          }
    		        }
    		      }
    		      propertySplit = votereward.getProperty("VoteRewardsFirstPage", "100001,50").split(";");
    		      for (String reward : propertySplit)
    		      {
    		        String[] rewardSplit = reward.split(",");
    		        if (rewardSplit.length != 2) {
    		          _log.warning(StringUtil.concat(new String[] { "VoteEngine[Config.load()]: invalid config property -> VoteReward \"", reward, "\"" }));
    		        }
    		        else {
    		          try
    		          {
    		            VOTE_REWARDS_FIRST_PAGE.add(new int[] { Integer.parseInt(rewardSplit[0]), Integer.parseInt(rewardSplit[1]) });
    		          }
    		          catch (NumberFormatException nfe)
    		          {
    		            if (!reward.isEmpty()) {
    		              _log.warning(StringUtil.concat(new String[] { "VoteEngine[Config.load()]: invalid config property -> VoteReward \"", reward, "\"" }));
    		            }
    		          }
    		        }
    		      }

Custom Config

 

#Big Reward TopZone
#Vote Reward System By L2Shax

VoteEnable = False

VoteDebug = false

VoteDebugParse = false

#url to hopzone page
VoteUrl = http://l2topzone.com/lineage2/server-info/6764/L2Luxury.html

#60 * 1000(1000milliseconds = 1 second) = 60seconds
InitCheck = 300000

Working I Test
Delay = 600000

VoteDiff = 1

VoteRewards = 813,5;

VoteTop = 1

VoteList = http://l2topzone.com/lineage2/server-list/top.html

VoteRewardsFirstPage = 813,25;


VoteTextSmallWin = Votes achieved! We are not in first page of TopZone. You will get the SMALL reward in few seconds.

VoteTextBigWin = Votes achieved! We are in first page of TopZone!!! You will get the BIG reward in few seconds.

VoteCurrent = Votes now: %votes%. %votesRequiredForReward% Votes left at TopZone until next reward (

VoteTextForFirstpage = %votes_frs% Votes left at TopZone until the server be on first page and the rewards will be (

Link to comment
Share on other sites

  • 5 weeks later...
  • 3 weeks later...

put the rightful credits that guy only adapted it.

That's the code i took the idea from Setekh. Then re-coded it in a more illegible and cleaner way.

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...
  • 5 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...