/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package com.l2jserver.gameserver.model;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javolution.util.FastList;
import com.l2jserver.Config;
import com.l2jserver.gameserver.Announcements;
import com.l2jserver.gameserver.GmListTable;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.datatables.SpawnTable;
import com.l2jserver.gameserver.model.L2ItemInstance;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.L2Npc;
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.network.serverpackets.ExShowScreenMessage;
/**
*
* @author DreamzOr 10-02-2011 L2 Hardstyle.com
*
*/
public class AutoVoteRewardHandler
{
private static Logger _log = Logger.getLogger(AutoVoteRewardHandler.class.getName());
private int hopzoneVotesCount = 0;
private int topzoneVotesCount = 0;
private int gamesitesVotesCount = 0;
private static final int RINGS_MANAGER = 10011;
private int initialCheck = Config.VOTES_SYSTEM_INITIAL_DELAY * 1000;
private int initialCheckRings = 300 * 1000;
private int delayForCheck = Config.VOTES_SYSTEM_STEP_DELAY * 1000;
private int delayForCheckRings = 3600 * 1000;
private int tenminutes = 1200 * 1000;
private int initialchecktenminutes = (initialCheck + tenminutes);
private int oneminute = 1740 * 1000;
private int initialcheckoneminute = (initialCheck + oneminute);
private List<L2Spawn> _managers = new FastList<L2Spawn>();
private static boolean topzone = false;
private static boolean hopzone = false;
private static boolean gamesites = false;
private List<String> already_rewarded;
private List<String> gamesites_urls = new ArrayList<String>();
private AutoVoteRewardHandler()
{
if (Config.AUTOVOTEREWARD_ENABLED)
{
System.out.println("======================================");
System.out.println(" Vote Reward System Initiated ");
System.out.println("======================================");
if(hopzone)
{
int hopzone_votes = getHopZoneVotes();
if(hopzone_votes == -1)
{
hopzone_votes = 0;
}
setHopZoneVoteCount(hopzone_votes);
}
if(topzone)
{
int topzone_votes = getTopZoneVotes();
if(topzone_votes == -1)
{
topzone_votes = 0;
}
setTopZoneVoteCount(topzone_votes);
}
if(gamesites)
{
int gamesites_votes = getGameSitesVotes();
if(gamesites_votes == -1)
{
gamesites_votes = 0;
}
setGameSitesVoteCount(gamesites_votes);
}
ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoReward(), initialCheck, delayForCheck);
ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new AutoRingsManager(), initialCheckRings, delayForCheckRings);
ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Autochecktenminutes(), initialchecktenminutes, delayForCheck);
ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Autocheckoneminute(), initialcheckoneminute, delayForCheck);
}
else
{
System.out.println("======================================");
System.out.println(" Vote Reward System Disabled ");
System.out.println("======================================");
}
for (L2Spawn spawn : SpawnTable.getInstance().getSpawnTable())
{
if (spawn != null && spawn.getNpcid() == RINGS_MANAGER)
_managers.add(spawn);
}
}
private class AutoReward implements Runnable
{
@Override
public void run()
{
if (Config.AUTOVOTEREWARD_ENABLED)
{
if (Config.VOTE_TOPZONE_COUNT == 0 || Config.VOTE_HOPZONE_ID == 0 || Config.VOTE_HOPZONE_ID == 0 || Config.VOTE_HOPZONE_COUNT == 0 || Config.VOTE_GAMESITES_COUNT == 0 || Config.VOTE_GAMESITES_ID == 0)
{
GmListTable.broadcastMessageToGMs("The rewards aren't Identified. Please take a look.");
return;
}
int topzone_votes = getTopZoneVotes();
System.out.println("==================================");
System.out.println(" Topzone Last Vote Count:"+ topzone_votes);
System.out.println(" Topzone Reward Votes:"+ (getTopZoneVoteCount() + Config.VOTES_FOR_REWARD));
System.out.println("==================================");
int hopzone_votes = getHopZoneVotes();
System.out.println("==================================");
System.out.println(" Hopzone Last Vote Count:"+ hopzone_votes);
System.out.println(" Hopzone Reward Votes:"+ (getHopZoneVoteCount() + Config.VOTES_FOR_REWARD));
System.out.println("==================================");
int gamesites_votes = getGameSitesVotes();
System.out.println("==================================");
System.out.println(" Gamesites Last Vote Count:"+ gamesites_votes);
System.out.println(" Gamesites Reward Votes:"+ (getGameSitesVoteCount() + Config.VOTES_FOR_REWARD));
System.out.println("==================================");
if(topzone)
{
if(topzone_votes != -1)
{
if (topzone_votes != 0 && topzone_votes >= getTopZoneVoteCount() + Config.VOTES_FOR_REWARD)
{
already_rewarded = new ArrayList<String>();
Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
//L2ItemInstance item;
for (L2PcInstance player : pls)
{
CreatureSay cs = new CreatureSay(player.getObjectId(), Say2.TELL, "[Topzone]", "You have more then 1 client logged. You can only be rewarded for 1 client.");
CreatureSay thanks = new CreatureSay(player.getObjectId(), Say2.TELL, "[Topzone]", "Thank you for voting for us.");
CreatureSay maxcount = new CreatureSay(player.getObjectId(), Say2.TELL, "[Topzone]", "You have reached our limit for vote reward items!");
CreatureSay bagfull = new CreatureSay(player.getObjectId(), Say2.TELL, "[Topzone]", "You have your iventory full. Please check it!");
if (player != null && !player.getClient().isDetached() && checkSingleBox(player))
{
L2ItemInstance item1 = player.getInventory().getItemByItemId(Config.VOTE_TOPZONE_ID);
if (item1 == null || item1.getCount() < Config.MAX_REWARD_COUNT_FOR_STACK_TOPZONE)
{
if (player.getAppearance().getNameColor() != Config.OFFLINE_NAME_COLOR)
{
player.addItem("reward", Config.VOTE_TOPZONE_ID, Config.VOTE_TOPZONE_COUNT, player, true);
player.sendPacket(thanks);
}
}
else if (item1.getCount() > Config.MAX_REWARD_COUNT_FOR_STACK_TOPZONE)
{
player.sendPacket(maxcount);
}
else
{
player.sendPacket(bagfull);
}
}
else if (!checkSingleBox(player))
{
player.sendPacket(cs);
}
}
setTopZoneVoteCount(topzone_votes);
}
}
if (getTopZoneVoteCount() == 0)
{
setTopZoneVoteCount(topzone_votes);
}
}
if(hopzone)
{
if(hopzone_votes != -1)
{
if (hopzone_votes != 0 && hopzone_votes >= getHopZoneVoteCount() + Config.VOTES_FOR_REWARD)
{
already_rewarded = new ArrayList<String>();
Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
//L2ItemInstance item;
for (L2PcInstance player : pls)
{
CreatureSay cs = new CreatureSay(player.getObjectId(), Say2.TELL, "[Hopzone]", "You have more then 1 client logged. You can only be rewarded for 1 client!");
CreatureSay thanks = new CreatureSay(player.getObjectId(), Say2.TELL, "[Hopzone]", "Thank you for voting for us.");
CreatureSay maxcount = new CreatureSay(player.getObjectId(), Say2.TELL, "[Hopzone]", "You have reached our limit for vote reward items!");
CreatureSay bagfull = new CreatureSay(player.getObjectId(), Say2.TELL, "[Hopzone]", "You have your iventory full. Please check it!");
if (player != null && !player.getClient().isDetached() && checkSingleBox(player))
{
L2ItemInstance item1 = player.getInventory().getItemByItemId(Config.VOTE_HOPZONE_ID);
if (item1 == null || item1.getCount() < Config.MAX_REWARD_COUNT_FOR_STACK_HOPZONE)
{
if (player.getAppearance().getNameColor() != Config.OFFLINE_NAME_COLOR)
{
player.addItem("reward", Config.VOTE_HOPZONE_ID, Config.VOTE_HOPZONE_COUNT, player, true);
player.sendPacket(thanks);
}
}
else if (item1.getCount() > Config.MAX_REWARD_COUNT_FOR_STACK_HOPZONE)
{
player.sendPacket(maxcount);
}
else
{
player.sendPacket(bagfull);
}
}
else if (!checkSingleBox(player))
{
player.sendPacket(cs);
}
}
setHopZoneVoteCount(hopzone_votes);
}
}
if (getHopZoneVoteCount() == 0)
{
setHopZoneVoteCount(hopzone_votes);
}
}
if(gamesites)
{
if(gamesites_votes != -1)
{
if (gamesites_votes != 0 && gamesites_votes >= getGameSitesVoteCount() + Config.VOTES_FOR_REWARD)
{
already_rewarded = new ArrayList<String>();
Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
//L2ItemInstance item;
for (L2PcInstance player : pls)
{
CreatureSay cs = new CreatureSay(player.getObjectId(), Say2.TELL, "[Gamesites]", "You have more then 1 client logged. You can only be rewarded for 1 client.");
CreatureSay thanks = new CreatureSay(player.getObjectId(), Say2.TELL, "[Gamesites]", "Thank you for voting for us.");
CreatureSay maxcount = new CreatureSay(player.getObjectId(), Say2.TELL, "[Gamesites]", "You have reached our limit for vote reward items!");
CreatureSay bagfull = new CreatureSay(player.getObjectId(), Say2.TELL, "[Gamesites]", "You have your iventory full. Please check it!");
if (player != null && !player.getClient().isDetached() && checkSingleBox(player))
{
L2ItemInstance item1 = player.getInventory().getItemByItemId(Config.VOTE_GAMESITES_ID);
if (item1 == null || item1.getCount() < Config.MAX_REWARD_COUNT_FOR_STACK_GAMESITES)
{
if (player.getAppearance().getNameColor() != Config.OFFLINE_NAME_COLOR)
{
player.addItem("reward", Config.VOTE_GAMESITES_ID, Config.VOTE_GAMESITES_COUNT, player, true);
player.sendPacket(thanks);
}
}
else if (item1.getCount() > Config.MAX_REWARD_COUNT_FOR_STACK_GAMESITES)
{
player.sendPacket(maxcount);
}
else
{
player.sendPacket(bagfull);
}
}
else if (!checkSingleBox(player))
{
player.sendPacket(cs);
}
}
setGameSitesVoteCount(gamesites_votes);
}
}
if (getGameSitesVoteCount() == 0)
{
setGameSitesVoteCount(gamesites_votes);
}
}
Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
for (L2PcInstance player : pls)
{
player.sendPacket(new ExShowScreenMessage("TOPZONE: " + topzone_votes + "/" + (getTopZoneVoteCount() + Config.VOTES_FOR_REWARD) + " Votes | HOPZONE: " + hopzone_votes + "/" + (getHopZoneVoteCount() + Config.VOTES_FOR_REWARD) + " Votes | GAMESITES: " + gamesites_votes + "/" + (getGameSitesVoteCount() + Config.VOTES_FOR_REWARD) + " Votes", 10000));
}
Announcements.getInstance().announceToAll("TOPZONE: " + topzone_votes + "/" + (getTopZoneVoteCount() + Config.VOTES_FOR_REWARD) + " Votes | HOPZONE: " + hopzone_votes + "/" + (getHopZoneVoteCount() + Config.VOTES_FOR_REWARD) + " Votes | GAMESITES: " + gamesites_votes + "/" + (getGameSitesVoteCount() + Config.VOTES_FOR_REWARD) + " Votes");
Announcements.getInstance().announceToAll("Next vote check in " + (Config.VOTES_SYSTEM_STEP_DELAY / 60) + " minutes!");
if(topzone && hopzone && gamesites && Config.VOTES_SYSTEM_STEP_DELAY>0)
{
try
{
Thread.sleep(Config.VOTES_SYSTEM_STEP_DELAY/2);
}
catch(InterruptedException e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
}
}
}
}
}
private class Autochecktenminutes implements Runnable
{
@Override
public void run()
{
Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
for (L2PcInstance player : pls)
{
player.sendPacket(new ExShowScreenMessage("Next vote check in 10 minutes!", 10000));
}
Announcements.getInstance().announceToAll("Next vote check in 10 minutes!");
}
}
private class AutoRingsManager implements Runnable
{
@Override
public void run()
{
L2Npc manager;
for (L2Spawn spawn : _managers)
{
manager = spawn.getLastSpawn();
if (manager != null)
{
CreatureSay packet = new CreatureSay(manager.getObjectId(), Say2.SHOUT, manager.getName(), "L2 Hardstylers from Aden to Elmore come meet me at Giran Castle Town to buy your Clan Ring and Master Ring for defeating your enemies!");
manager.broadcastPacket(packet);
}
}
}
}
private class Autocheckoneminute implements Runnable
{
@Override
public void run()
{
Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers().values();
for (L2PcInstance player : pls)
{
player.sendPacket(new ExShowScreenMessage("Next vote check in 1 minute!", 10000));
}
Announcements.getInstance().announceToAll("Next vote check in 1 minute!");
}
}
private boolean checkSingleBox(L2PcInstance player)
{
if(player.getClient()!=null && player.getClient().getConnection()!=null && !player.getClient().getConnection().isClosed() && player.isOnline())
{
String playerip = player.getClient().getConnection().getInetAddress().getHostAddress();
if(already_rewarded.contains(playerip))
return false;
else
{
already_rewarded.add(playerip);
return true;
}
}
//if no connection (maybe offline shop) dnt reward
return false;
}
private int getGameSitesVotes()
{
gamesites_urls.add("http://www.gamesites200.com/lineage2/");
gamesites_urls.add("http://www.gamesites200.com/lineage2/index2.shtml");
gamesites_urls.add("http://www.gamesites200.com/lineage2/index3.shtml");
gamesites_urls.add("http://www.gamesites200.com/lineage2/index4.shtml");
int votes = -1;
URL url = null;
InputStreamReader isr = null;
BufferedReader in = null;
for (String urls : gamesites_urls)
{
try
{
url = new URL(urls);
isr = new InputStreamReader(url.openStream());
in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
{
if (inputLine.contains("Freya Rates 50x|Enchant Rate: 33%|Safe Enchant: 4 (armor and weapon)| Buffs 2 Hours | Cov & Manus 1 Hour | Retail Events | PVP Server</span></td><td"))
{
try
{
try
{
return Integer.valueOf(inputLine.split(">")[21].replace("</td", ""));
}
catch (Exception e)
{
return Integer.valueOf(inputLine.split(">")[25].replace("</td", ""));
}
}
catch (Exception e)
{
try
{
return Integer.valueOf(inputLine.split(">")[17].replace("</td", ""));
}
catch (Exception ee)
{
return Integer.valueOf(inputLine.split(">")[38].replace("</td", ""));
}
}
finally
{
try
{
if(in!=null)
in.close();
}
catch (Exception e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
}
try
{
if(isr!=null)
isr.close();
}
catch (Exception e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
_log.info("[AutoVoteReward] Server GAMESITES is offline or something is wrong in link");
//e.printStackTrace();
}
finally
{
try
{
if(in!=null)
in.close();
}
catch (Exception e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
}
try
{
if(isr!=null)
isr.close();
}
catch (Exception e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
}
}
}
return votes;
}
private int getHopZoneVotes()
{
int votes = -1;
URL url = null;
InputStreamReader isr = null;
BufferedReader in = null;
try
{
url = new URL(Config.VOTES_SITE_HOPZONE_URL);
URLConnection con = url.openConnection();
con.addRequestProperty("User-Agent", "Mozilla/4.76");
isr = new InputStreamReader(con.getInputStream());
in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
{
// for top-zone
//if (inputLine.contains("<tr><td><div align=\"center\"><b><font style=\"font-size:14px;color:#018BC1;\""))
//{
//return Integer.valueOf(inputLine.split(">")[5].replace("</font", ""));
//}
//for hopzone
if (inputLine.contains("rank anonymous tooltip"))
{
return Integer.valueOf(inputLine.split(">")[2].replace("</span", ""));
}
}
}
catch (Exception e)
{
e.printStackTrace();
_log.info("[AutoVoteReward] Server HOPZONE is offline or something is wrong in link");
//e.printStackTrace();
}
finally
{
try
{
if(in!=null)
in.close();
}
catch (Exception e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
}
try
{
if(isr!=null)
isr.close();
}
catch (Exception e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
}
}
return votes;
}
private int getTopZoneVotes()
{
int votes = -1;
URL url = null;
InputStreamReader isr = null;
BufferedReader in = null;
try
{
url = new URL(Config.VOTES_SITE_TOPZONE_URL);
isr = new InputStreamReader(url.openStream());
in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
{
if (inputLine.contains("Votes"))
{
String votesLine = in.readLine() ;
votes = Integer.valueOf(votesLine.split(">")[5].replace("</font", ""));
break;
}
}
}
catch (Exception e)
{
_log.info("[AutoVoteReward] Server TOPZONE is offline or something is wrong in link");
//e.printStackTrace();
}
finally
{
try
{
if(in!=null)
in.close();
}
catch (Exception e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
}
try
{
if(isr!=null)
isr.close();
}
catch (Exception e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
}
}
return votes;
}
/**
* @return
*/
private void setHopZoneVoteCount(int voteCount)
{
hopzoneVotesCount = voteCount;
}
private int getHopZoneVoteCount()
{
return hopzoneVotesCount;
}
private void setTopZoneVoteCount(int voteCount)
{
topzoneVotesCount = voteCount;
}
private int getTopZoneVoteCount()
{
return topzoneVotesCount;
}
private void setGameSitesVoteCount(int voteCount)
{
gamesitesVotesCount = voteCount;
}
private int getGameSitesVoteCount()
{
return gamesitesVotesCount;
}
public static AutoVoteRewardHandler getInstance()
{
if(Config.VOTES_SITE_HOPZONE_URL != null && !Config.VOTES_SITE_HOPZONE_URL.equals(""))
{
hopzone = true;
}
if(Config.VOTES_SITE_TOPZONE_URL != null && !Config.VOTES_SITE_TOPZONE_URL.equals(""))
{
topzone = true;
}
if(Config.VOTES_SITE_GAMESITES_URL != null && !Config.VOTES_SITE_GAMESITES_URL.equals(""))
{
gamesites = true;
}
if(topzone || hopzone || gamesites)
return SingletonHolder._instance;
else
return null;
}
@SuppressWarnings("synthetic-access")
private static class SingletonHolder
{
protected static final AutoVoteRewardHandler _instance = new AutoVoteRewardHandler();
}
}