08-15-2012, 01:55 PM
Как и обещал скрипт автоматической выдачи награды за голосование в рейтинге vote4.pro
После строки:
вставить:
По всем вопросам обращайтесь в личку, аську или скайп (все в профиле)
Vote4ProManager.java
Код:
package l2rt.gameserver.instancemanager;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;
import java.util.StringTokenizer;
import l2rt.commons.dbutils.DbUtils;
import l2rt.gameserver.Config;
import l2rt.gameserver.ThreadPoolManager;
import l2rt.gameserver.database.DatabaseFactory;
import l2rt.gameserver.model.GameObjectsStorage;
import l2rt.gameserver.model.Player;
import l2rt.gameserver.utils.Log;
/**
*
* @author Vitlav
* @team Revolt-Team
* @site http://revolt-team.com
*
*/
public class Vote4ProManager
{
private static final String SELECT_PLAYER_OBJID = "SELECT obj_Id FROM characters WHERE char_name=?";
private static final String SELECT_CHARACTER_VOTE_4_PRO_DATA = "SELECT * FROM character_vote4_votes WHERE id=? AND date=? AND multipler=?";
private static final String INSERT_VOTE_4_PRO_DATA = "INSERT INTO character_vote4_votes (date, id, nick, multipler) values (?,?,?,?)";
private static final String DELETE_VOTE_4_PRO_DATA = "DELETE FROM character_vote4_votes WHERE date<?";
private static final String SELECT_MULTIPLER_VOTE_4_PRO_DATA = "SELECT multipler FROM character_vote4_votes WHERE id=? AND has_reward=0";
private static final String UPDATE_VOTE_4_PRO_DATA = "UPDATE character_vote4_votes SET has_reward=1 WHERE id=?";
BufferedReader reader;
private static Vote4ProManager _instance;
public static Vote4ProManager getInstance()
{
if(_instance == null && Config.VOTE_4_PRO_MANAGER_ENABLED)
_instance = new Vote4ProManager();
return _instance;
}
public Vote4ProManager()
{
ThreadPoolManager.getInstance().scheduleAtFixedRate(new ConnectAndUpdate(), Config.VOTE_4_PRO_MANAGER_INTERVAL, Config.VOTE_4_PRO_MANAGER_INTERVAL);
ThreadPoolManager.getInstance().scheduleAtFixedRate(new Clean(), Config.VOTE_4_PRO_MANAGER_INTERVAL, Config.VOTE_4_PRO_MANAGER_INTERVAL);
ThreadPoolManager.getInstance().scheduleAtFixedRate(new GiveReward(), Config.VOTE_4_PRO_MANAGER_INTERVAL, Config.VOTE_4_PRO_MANAGER_INTERVAL);
}
public void getPage(String address)
{
try
{
URL url = new URL(address);
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF8"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void parse()
{
try
{
String line;
while((line = reader.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line, "\t. :");
while(st.hasMoreTokens())
try
{
st.nextToken();
int day = Integer.parseInt(st.nextToken());
int month = Integer.parseInt(st.nextToken()) - 1;
int year = Integer.parseInt(st.nextToken());
int hour = Integer.parseInt(st.nextToken());
int minute = Integer.parseInt(st.nextToken());
int second = Integer.parseInt(st.nextToken());
st.nextToken();
st.nextToken();
st.nextToken();
st.nextToken();
String charName = st.nextToken();
int voteType = Integer.parseInt(st.nextToken());
Calendar calendar = Calendar.getInstance();
calendar.set(1, year);
calendar.set(2, month);
calendar.set(5, day);
calendar.set(11, hour);
calendar.set(12, minute);
calendar.set(13, second);
calendar.set(14, 0);
long voteTime = calendar.getTimeInMillis() / 1000;
if(voteTime + Config.VOTE_4_PRO_SAVE_DAYS * 86400 > System.currentTimeMillis() / 1000)
checkAndSave(voteTime, charName, voteType);
}
catch(Exception e)
{}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void checkAndSave(long voteTime, String charName, int voteType)
{
Connection con = null;
PreparedStatement selectObjectStatement = null, selectVote4Statement = null, insertStatement = null;
ResultSet rsetObject = null, rsetVote4 = null;
try
{
con = DatabaseFactory.getInstance().getConnection();
selectObjectStatement = con.prepareStatement(SELECT_PLAYER_OBJID);
selectObjectStatement.setString(1, charName);
rsetObject = selectObjectStatement.executeQuery();
int objId = 0;
if(rsetObject.next())
objId = rsetObject.getInt("obj_Id");
if(objId > 0)
{
selectVote4Statement = con.prepareStatement(SELECT_CHARACTER_VOTE_4_PRO_DATA);
selectVote4Statement.setInt(1, objId);
selectVote4Statement.setLong(2, voteTime);
selectVote4Statement.setInt(3, voteType);
rsetVote4 = selectVote4Statement.executeQuery();
if(!rsetVote4.next())
{
insertStatement = con.prepareStatement(INSERT_VOTE_4_PRO_DATA);
insertStatement.setLong(1, voteTime);
insertStatement.setInt(2, objId);
insertStatement.setString(3, charName);
insertStatement.setInt(4, voteType);
insertStatement.execute();
insertStatement.close();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DbUtils.closeQuietly(con, selectObjectStatement, rsetObject);
DbUtils.closeQuietly(con, selectVote4Statement, rsetVote4);
DbUtils.closeQuietly(con, insertStatement);
}
}
private synchronized void clean()
{
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -Config.VOTE_4_PRO_SAVE_DAYS);
Connection con = null;
PreparedStatement statement = null;
try
{
con = DatabaseFactory.getInstance().getConnection();
statement = con.prepareStatement(DELETE_VOTE_4_PRO_DATA);
statement.setLong(1, calendar.getTimeInMillis() / 1000);
statement.execute();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DbUtils.closeQuietly(con, statement);
}
}
private synchronized void giveReward()
{
Connection con = null;
PreparedStatement selectMultStatement = null, updateStatement = null;
ResultSet rsetMult = null;
try
{
con = DatabaseFactory.getInstance().getConnection();
for(Player player : GameObjectsStorage.getAllPlayers())
{
int objId = player.getObjectId();
int mult = 0;
selectMultStatement = con.prepareStatement(SELECT_MULTIPLER_VOTE_4_PRO_DATA);
selectMultStatement.setInt(1, objId);
rsetMult = selectMultStatement.executeQuery();
while(rsetMult.next())
mult += rsetMult.getInt("multipler");
if(mult > 0)
{
updateStatement = con.prepareStatement(UPDATE_VOTE_4_PRO_DATA);
updateStatement.setInt(1, objId);
updateStatement.executeUpdate();
if(player.isLangRus())
player.sendMessage("Спасибо за Ваш голос в рейтинге Vote4. C наилучшими пожеланиями " + Config.VOTE_4_PRO_SERVER_ADDRESS);
else
player.sendMessage("Thank you for your vote in Vote4 raiting. Best regards " + Config.VOTE_4_PRO_SERVER_ADDRESS);
for(int i = 0; i < Config.VOTE_4_PRO_REWARD.length; i += 2)
if(Config.VOTE_4_PRO_REWARD[i] == -100) // PC Bang
{
player.addPcBangPoints(Config.VOTE_4_PRO_REWARD[i + 1] * mult, false);
Log.add(player.getName() + " | " + player.getObjectId() + " | Vote4 reward item ID | " + Config.VOTE_4_PRO_REWARD[i] + " | Vote4 reward count | " + Config.VOTE_4_PRO_REWARD[i + 1] * mult + " |", "vote4");
}
else if(Config.VOTE_4_PRO_REWARD[i] == -200) // Clan reputation
{
if(player.getClan() != null)
{
player.getClan().incReputation(Config.VOTE_4_PRO_REWARD[i + 1] * mult);
Log.add(player.getName() + " | " + player.getObjectId() + " | Vote4 reward item ID | " + Config.VOTE_4_PRO_REWARD[i] + " | Vote4 reward count | " + Config.VOTE_4_PRO_REWARD[i + 1] * mult + " |", "vote4");
}
else
{
player.getInventory().addItem(Config.VOTE_4_PRO_REWARD_NO_CLAN[i], Config.VOTE_4_PRO_REWARD_NO_CLAN[i + 1] * mult);
Log.add(player.getName() + " | " + player.getObjectId() + " | Vote4 reward item ID | " + Config.VOTE_4_PRO_REWARD_NO_CLAN[i] + " | Vote4 reward count | " + Config.VOTE_4_PRO_REWARD_NO_CLAN[i + 1] * mult + " |", "vote4");
}
}
else if(Config.VOTE_4_PRO_REWARD[i] == -300) // Fame
{
player.setFame(player.getFame() + Config.VOTE_4_PRO_REWARD[i + 1] * mult);
Log.add(player.getName() + " | " + player.getObjectId() + " | Vote4 reward item ID | " + Config.VOTE_4_PRO_REWARD[i] + " | Vote4 reward count | " + Config.VOTE_4_PRO_REWARD[i + 1] * mult + " |", "vote4");
}
else
{
player.getInventory().addItem(Config.VOTE_4_PRO_REWARD[i], Config.VOTE_4_PRO_REWARD[i + 1] * mult);
Log.add(player.getName() + " | " + player.getObjectId() + " | Vote4 reward item ID | " + Config.VOTE_4_PRO_REWARD[i] + " | Vote4 reward count | " + Config.VOTE_4_PRO_REWARD[i + 1] * mult + " |", "vote4");
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DbUtils.closeQuietly(con, selectMultStatement, rsetMult);
DbUtils.closeQuietly(con, updateStatement);
}
}
private class ConnectAndUpdate implements Runnable
{
@Override
public void run()
{
getPage(Config.VOTE_4_PRO_WEB_ADDRESS);
parse();
}
}
private class Clean implements Runnable
{
@Override
public void run()
{
clean();
}
}
private class GiveReward implements Runnable
{
@Override
public void run()
{
giveReward();
}
}
}
GameServer.java
После строки:
Код:
L2TopManager.getInstance();
Код:
Vote4ProManager.getInstance();
Config.java
Код:
public static boolean VOTE_4_PRO_MANAGER_ENABLED;
public static int VOTE_4_PRO_MANAGER_INTERVAL;
public static String VOTE_4_PRO_WEB_ADDRESS;
public static String VOTE_4_PRO_SERVER_ADDRESS;
public static int VOTE_4_PRO_SAVE_DAYS;
public static int[] VOTE_4_PRO_REWARD;
public static int[] VOTE_4_PRO_REWARD_NO_CLAN;
Код:
VOTE_4_PRO_MANAGER_ENABLED = topSettings.getProperty("Vote4ProEnable", false);
VOTE_4_PRO_MANAGER_INTERVAL = topSettings.getProperty("Vote4ProManagerInterval", 300000);
VOTE_4_PRO_WEB_ADDRESS = topSettings.getProperty("Vote4ProUrl", "");
VOTE_4_PRO_SERVER_ADDRESS = topSettings.getProperty("Vote4ProServerAddress", "revolt-team.com");
VOTE_4_PRO_SAVE_DAYS = topSettings.getProperty("Vote4ProSaveDays", 30);
VOTE_4_PRO_REWARD = topSettings.getProperty("Vote4ProReward", new int[0]);
VOTE_4_PRO_REWARD_NO_CLAN = topSettings.getProperty("Vote4ProRewardNoClan", new int[0]);
Для файла конфигурации
Код:
#-------------------------------------------------------------
# Vote4.pro
#-------------------------------------------------------------
# Данная система предназначена для автоматической выдачи бонусов за голосование на Vote4.pro
# Награду выдает только тем игрокам которые в онлайне.
# Включена ли система автоматической выдачи бонусов
Vote4ProEnable = False
# Интервал подключения к Vote4.pro по умолчанию 5 минут, не уменьшайте интервал
Vote4ProManagerInterval = 300000
# Ссылка на получение списка проголосовавших
# Пример: http://vote4.pro/server/4/stat/view/18/1b0a70f72832fbb724ae6f37b6e220ae/last-month.txt
Vote4ProUrl =
# Адрес сайта сервера и/или его название для публикации его в сообщение об успешном голосовании
Vote4ProServerAddress = revolt-team.com
# Сколько дней хранить голоса
Vote4ProSaveDays = 30
# награда в формате id1,count1;id2,count2;
# также можно указать следующие варианты:
# -100 PC Bang
# -200 Clan reputation
# -300 Fame
Vote4ProReward = 57,20;4037,1
# замена награды если нет клана и награда выставлена -200
# количество предметов должно совпадать с количеством в Vote4ProReward
Vote4ProRewardNoClan = 57,20;4037,1
По всем вопросам обращайтесь в личку, аську или скайп (все в профиле)