Jump to content
  • 0

[bug] vote reward


Question

Posted

I have a vote reward, but is bugged i don't know why it shows 0 Votes and i have 25 votes

 

package com.l2jserver.gameserver.instancemanager;

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 com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.Announcements;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.model.L2ItemInstance;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.clientpackets.Say2;
import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
import com.l2jserver.gameserver.util.Broadcast;

public class AutoVoteRewardHandler
{
private final String TOPZONE = "http://l2topzone.com/";
// 60 * 1000(1000milliseconds = 1 second) = 60seconds
private final int initialCheck = 60 * 1000;
// 1800 * 1000(1000milliseconds = 1 second) = 1800seconds = 30minutes
private final int delayForCheck = 1800 * 1000;
private final int[] itemId = { 9627, 9142 };
   private final int[] itemCount = { 2, 500 };
private final int[] maxStack = { 2, 500 };
private final int votesRequiredForReward = 10;
// do not change
private int lastVoteCount = 0;

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

private class AutoReward implements Runnable
{
	public void run()
	{
		int votes = getVotes();
		System.out.println("Server Votes: " + votes);
		if (votes != 0 && getLastVoteCount() != 0 && votes >= getLastVoteCount() + votesRequiredForReward)
		{
			Connection con = null;
			try
			{
				con = L2DatabaseFactory.getInstance().getConnection();
				PreparedStatement statement = con.prepareStatement("SELECT c.charId, c.char_name FROM characters AS c LEFT JOIN accounts AS a ON c.account_name = a.login WHERE c.online > 0 GROUP BY a.lastIP ORDER BY c.level DESC");
				ResultSet rset = statement.executeQuery();
				L2PcInstance player = null;
				L2ItemInstance item = null;
				while (rset.next())
				{
					player = L2World.getInstance().getPlayer(rset.getInt("charId"));
					if (player != null && !player.getClient().isDetached())
					{
						for (int i = 0; i < itemId.length; i++)
						{
							item = player.getInventory().getItemByItemId(itemId[i]);
							if (item == null || item.getCount() < maxStack[i])
								player.addItem("reward", itemId[i], itemCount[i], player, true);
						}
					}
				}
				statement.close();
		}
			catch (SQLException e)
		{
				e.printStackTrace();
			}
			finally
			{
				L2DatabaseFactory.close(con);
			}

			setLastVoteCount(getLastVoteCount() + votesRequiredForReward);
		}
		Broadcast.toAllOnlinePlayers(new CreatureSay(1, Say2.ANNOUNCEMENT, "Announcements:", "Server Votes: " + votes + " | Next Reward on " + (getLastVoteCount() + votesRequiredForReward) + " Votes.)"));
		if (getLastVoteCount() == 0)
			setLastVoteCount(votes);
	}
}

private int getVotes()
{
	URL url = null;
	InputStreamReader isr = null;
	BufferedReader in = null;
	try
	{
		url = new URL(TOPZONE);
		isr = new InputStreamReader(url.openStream());
		in = new BufferedReader(isr);
		String inputLine;
		while ((inputLine = in.readLine()) != null)
		{
			if (inputLine.contains("rank anonymous tooltip"))
			{
				return Integer.valueOf(inputLine.split(">")[2].replace("</span", ""));
			}
		}
	}
	catch (IOException e)
	{
		e.printStackTrace();
	}
	finally
	{
		try
		{
			in.close();
		}
		catch (IOException e)
		{}
		try
		{
			isr.close();
		}
		catch (IOException e)
		{}
	}
	return 0;
}

private void setLastVoteCount(int voteCount)
{
	lastVoteCount = voteCount;
}

private int getLastVoteCount()
{
	return lastVoteCount;
}

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

@SuppressWarnings("synthetic-access")
private static class SingletonHolder
{
	protected static final AutoVoteRewardHandler _instance = new AutoVoteRewardHandler();
}
}

11 answers to this question

Recommended Posts

  • 0
Posted

put this sql in server

-- ----------------------------
-- Table structure for `votes`
-- ----------------------------
DROP TABLE IF EXISTS `votes`;
CREATE TABLE `votes` (
  `id` int(6) NOT NULL,
  `vote` int(6) NOT NULL,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of votes
-- ----------------------------
INSERT INTO votes VALUES ('1', '0');

an tell as

  • 0
Posted

Dirty fix, will work for a while:

			while ((inputLine = in.readLine()) != null)
		{
			if (inputLine.contains("								<tr><td><div align=\"center\"><b><font style=\"font-size:14px;color:#018BC1;\">"))
			{
				return Integer.valueOf(inputLine.replace("								<tr><td><div align=\"center\"><b><font style=\"font-size:14px;color:#018BC1;\">", "").replace("</font></b></div></td></tr>", ""));
			}
		}

 

You need to copy the if statement and replace it in your script.

 

Please don't bump your topic 3 times in a row the same hour.

 

@l22expert do you have any idea of what you posted?

 

  • 0
Posted

Show me full current script, I tested it with first server in list and it gave me 2761 votes.

 

You did something wrong.

  • 0
Posted
package com.l2jserver.gameserver.instancemanager;

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 com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.model.L2ItemInstance;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.clientpackets.Say2;
import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
import com.l2jserver.gameserver.util.Broadcast;

public class AutoVoteRewardHandler
{
private final String TOPZONE = "http://l2topzone.com";
// 60 * 1000(1000milliseconds = 1 second) = 60seconds
private final int initialCheck = 60 * 1000;
// 1800 * 1000(1000milliseconds = 1 second) = 1800seconds = 30minutes
private final int delayForCheck = 1800 * 1000;
private final int[] itemId = { 9627, 9142 };
    private final int[] itemCount = { 2, 500 };
private final int[] maxStack = { 2, 500 };
private final int votesRequiredForReward = 10;
// do not change
private int lastVoteCount = 0;

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

private class AutoReward implements Runnable
{
	public void run()
	{
		int votes = getVotes();
		System.out.println("Server Votes: " + votes);
		if (votes != 0 && getLastVoteCount() != 0 && votes >= getLastVoteCount() + votesRequiredForReward)
		{
			Connection con = null;
			try
			{
				con = L2DatabaseFactory.getInstance().getConnection();
				PreparedStatement statement = con.prepareStatement("SELECT c.charId, c.char_name FROM characters AS c LEFT JOIN accounts AS a ON c.account_name = a.login WHERE c.online > 0 GROUP BY a.lastIP ORDER BY c.level DESC");
				ResultSet rset = statement.executeQuery();
				L2PcInstance player = null;
				L2ItemInstance item = null;
				while (rset.next())
				{
					player = L2World.getInstance().getPlayer(rset.getInt("charId"));
					if (player != null && !player.getClient().isDetached())
					{
						for (int i = 0; i < itemId.length; i++)
						{
							item = player.getInventory().getItemByItemId(itemId[i]);
							if (item == null || item.getCount() < maxStack[i])
								player.addItem("reward", itemId[i], itemCount[i], player, true);
						}
					}
				}
				statement.close();
		}
			catch (SQLException e)
		{
				e.printStackTrace();
			}
			finally
			{
				L2DatabaseFactory.close(con);
			}

			setLastVoteCount(getLastVoteCount() + votesRequiredForReward);
		}
		Broadcast.toAllOnlinePlayers(new CreatureSay(1, Say2.ANNOUNCEMENT, "Announcements:", "Server Votes: " + votes + " | Next Reward on " + (getLastVoteCount() + votesRequiredForReward) + " Votes.)"));
		if (getLastVoteCount() == 0)
			setLastVoteCount(votes);
	}
}

private int getVotes()
{
	URL url = null;
	InputStreamReader isr = null;
	BufferedReader in = null;
	try
	{
		url = new URL(TOPZONE);
		isr = new InputStreamReader(url.openStream());
		in = new BufferedReader(isr);
		String inputLine;
		while ((inputLine = in.readLine()) != null)
		{
			if (inputLine.contains("								<tr><td><div align=\"center\"><b><font style=\"font-size:14px;color:#018BC1;\">"))
			{
				return Integer.valueOf(inputLine.replace("								<tr><td><div align=\"center\"><b><font style=\"font-size:14px;color:#018BC1;\">", "").replace("</font></b></div></td></tr>", ""));
			}
		}
	}
	catch (IOException e)
	{
		e.printStackTrace();
	}
	finally
	{
		try
		{
			in.close();
		}
		catch (IOException e)
		{}
		try
		{
			isr.close();
		}
		catch (IOException e)
		{}
	}
	return 0;
}

private void setLastVoteCount(int voteCount)
{
	lastVoteCount = voteCount;
}

private int getLastVoteCount()
{
	return lastVoteCount;
}

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

@SuppressWarnings("synthetic-access")
private static class SingletonHolder
{
	protected static final AutoVoteRewardHandler _instance = new AutoVoteRewardHandler();
}
}

  • 0
Posted

The code is fine, the problem maybe in URL, test with this http://l2topzone.com/lineage2/server-info/3425/L2Worldx20&x1000.html

Should give you 2800+ votes.

 

Also for testing change private final int delayForCheck = 1800 * 1000; to private final int delayForCheck = 30 * 1000; thats initial delay.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock