Noticias:

Debes de estar registrado para poder ver el contenido indicado. Registrate o Conectate

Menú Principal

Evento Top Pvp

Iniciado por Swarlog, Ago 06, 2022, 02:24 AM

Tema anterior - Siguiente tema

Swarlog

/*
 * 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
 */
package com.l2jfrozen.gameserver.model.entity.event.Topvp;
import java.util.Collection;
import java.util.logging.Level;

import com.l2jfrozen.Config;
import com.l2jfrozen.gameserver.model.actor.instance.L2ItemInstance;
import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
import com.l2jfrozen.gameserver.model.entity.Announcements;

import java.util.Calendar;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Logger;
import com.l2jfrozen.gameserver.network.serverpackets.SystemMessage;

import com.l2jfrozen.gameserver.idfactory.IdFactory;
import com.l2jfrozen.gameserver.network.SystemMessageId;

import com.l2jfrozen.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jfrozen.gameserver.thread.ThreadPoolManager;

import com.l2jfrozen.gameserver.model.L2World;
import com.l2jfrozen.util.CloseUtil;
import com.l2jfrozen.util.database.L2DatabaseFactory;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * @author Vampkiller
 */
public class TopPvp
{
protected static final Logger _log = Logger.getLogger(TopPvp.class.getName());
private static int winner_id;
private static String winner_name;
private static int winner_kills;

/** Task for event cycles<br> */
private TopPvpStartTask _task;

/**
* New instance only by getInstance()<br>
*/
public TopPvp()
{
if (Config.TOP_PVP_ENABLE)
{
this.scheduleEventStart();
_log.info("TopPvpEventEngine: Started.");
}
else
{
_log.info("TopPvpEventEngine: Engine is disabled.");
}
}

/**
* Initialize new/Returns the one and only instance<br><br>
*
* @return TopPvp<br>
*/
public static TopPvp getInstance()
{
return SingletonHolder._instance;
}

/**
* Starts TopPvpStartTask
*/
@SuppressWarnings("null")
public void scheduleEventStart()
{
try
{
Calendar currentTime = Calendar.getInstance();
Calendar nextStartTime = null;
Calendar testStartTime = null;
//String timeOfDay = "23:00";

// Creating a Calendar object from the specified interval value
testStartTime = Calendar.getInstance();
testStartTime.setLenient(true);
String[] splitTimeOfDay = (Config.TOP_PVP_TIME).split(":");
testStartTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitTimeOfDay[0]));
testStartTime.set(Calendar.MINUTE, Integer.parseInt(splitTimeOfDay[1]));
// If the date is in the past, make it the next day (Example: Checking for "1:00", when the time is 23:57.)
if (testStartTime.getTimeInMillis() < currentTime.getTimeInMillis()+60000)
{
testStartTime.add(Calendar.DAY_OF_MONTH, 1);
}
// Check for the test date to be the minimum (smallest in the specified list)
if (nextStartTime == null || testStartTime.getTimeInMillis() < nextStartTime.getTimeInMillis())
{
nextStartTime = testStartTime;
}
System.out.printf ( "Top PvP Event: Schedule next start\n");
_task = new TopPvpStartTask(nextStartTime.getTimeInMillis());
ThreadPoolManager.getInstance().executeTask(_task);
}
catch (Exception e)
{
_log.warning("TopPvpEventEngine[TopPvp.scheduleEventStart()]: Error figuring out a start time. Check TopPvpEventInterval in config file.");
}
}


/**
* Method to start the fight
*/
public void startReward()
{
Connection con = null;
if (Config.TOP_PVP_TAKE_REWARD_FROM_PREVIOUS){
System.out.printf ( "Top PvP Event: Delete items\n");
try
{
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM items WHERE item_id = "+Config.TOP_PVP_REWARD_ID);
ResultSet rset = statement.executeQuery();
while (rset.next())
{
Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers();
{
for (L2PcInstance onlinePlayer : pls)
if (onlinePlayer.isOnline() == 1 && onlinePlayer.getObjectId()==rset.getInt("owner_Id")){
//onlinePlayer.getInventory().destroyItemByItemId("Consume", Config.TOP_PVP_REWARD_ID, Config.TOP_PVP_REWARD_AMOUNT, onlinePlayer, onlinePlayer);
onlinePlayer.destroyItemByItemId("TopPvp", Config.TOP_PVP_REWARD_ID, Config.TOP_PVP_REWARD_AMOUNT, onlinePlayer, false);
/*SystemMessage sm = new SystemMessage(SystemMessageId.S1_DISAPPEARED);
sm.addItemName(rset.getInt("item_id"));*/
SystemMessage sm = new SystemMessage(SystemMessageId.DISSAPEARED_ITEM);
sm.addItemName(Config.TOP_PVP_REWARD_ID);
sm.addNumber(Config.TOP_PVP_REWARD_AMOUNT);
onlinePlayer.sendPacket(sm);
}
}
}

statement = con.prepareStatement("DELETE FROM items WHERE item_id = "+Config.TOP_PVP_REWARD_ID);
statement.execute();
rset.close();
statement.close();
}
catch (SQLException e)
{
if(_log.isLoggable(Level.SEVERE))
_log.log(Level.SEVERE, "SQL exception while Delete previous reward DailyPvp for characters", e);
}
finally
{
CloseUtil.close(con);
}
}
System.out.printf ( "Top PvP Event: Calculate winners\n");
boolean rewarded = false;

String list = "";
con = null;
try
{
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM characters WHERE DailyPvp>0 AND accesslevel=0 AND punish_level=0 order by DailyPvp desc, pvpkills desc limit " + Integer.toString(Config.TOP_PVP_LIST_LENGTH));
ResultSet rset = statement.executeQuery();

if (rset.next()){
int count = 1;
String color_text;
winner_id = rset.getInt("obj_Id");
winner_name = rset.getString("char_name");
winner_kills = rset.getInt("DailyPvp");
color_text = "<font color =\"LEVEL\">";

list += "<tr><td><center>"+ color_text + count + "</font></center></td><td><center>" + color_text +winner_name +"</font></center></td><td><center>"+ color_text + winner_kills + "</font></center></td><td><center>"+ color_text + rset.getInt("pvpkills") + "</font></center></td></tr>";
list += "<tr><td></td><td></td><td></td></tr>";
while (rset.next())
{
count++;
color_text = "<font color =\"FFFFFF\">";
list += "<tr><td><center>"+ color_text + count + "</font></center></td><td><center>" + color_text +rset.getString("char_name") +"</font></center></td><td><center>"+ color_text + rset.getInt("DailyPvp") + "</font></center></td><td><center>"+ color_text + rset.getInt("pvpkills") + "</font></center></td></tr>";
}
}
else
list = "<tr><td><center>-</center></td><td><center>-</center></td><td><center>-</center></td></tr>";

rset.close();
statement.close();
}
catch (SQLException e)
{
if(_log.isLoggable(Level.SEVERE))
_log.log(Level.SEVERE, "SQL exception while Update DailyPvp count for characters", e);
}
finally
{
CloseUtil.close(con);
}


//give reward to online and inform about the results
System.out.printf ( "Top PvP Event: Reset-inform-reward\n");
Collection<L2PcInstance> pls = L2World.getInstance().getAllPlayers();
{
for (L2PcInstance onlinePlayer : pls){
//Announcements.getInstance().announceToAll(onlinePlayer.getName());
//if (onlinePlayer.isOnline() == 1 && onlinePlayer.getPunishLevel() == 0 && !onlinePlayer.isGM())
if (onlinePlayer.isOnline() == 1){
onlinePlayer.setDailyPvp(0);

if(onlinePlayer.getObjectId() == winner_id ) {
//onlinePlayer.getInventory().addItem("TopPvP Reward", Config.TOP_PVP_REWARD_ID, Config.TOP_PVP_REWARD_AMOUNT, onlinePlayer, onlinePlayer);
onlinePlayer.addItem("TopPvP Reward",Config.TOP_PVP_REWARD_ID,Config.TOP_PVP_REWARD_AMOUNT,onlinePlayer,false);
/*SystemMessage systemMessage2 = new SystemMessage(SystemMessageId.EARNED_ITEM);
systemMessage2.addItemName(Config.TOP_PVP_REWARD_ID);*/
SystemMessage systemMessage2 = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
systemMessage2.addItemName(Config.TOP_PVP_REWARD_ID);
systemMessage2.addNumber(Config.TOP_PVP_REWARD_AMOUNT);
onlinePlayer.sendPacket(systemMessage2);
rewarded = true;
}
NpcHtmlMessage notice = new NpcHtmlMessage(1);
notice.setFile("data/html/TopPvp.htm");
notice.replace("%n%", Integer.toString(Config.TOP_PVP_LIST_LENGTH));
notice.replace("%list%", list);
onlinePlayer.sendPacket(notice);
}
}
}


//give reward to offline
if (!rewarded){
L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), Config.TOP_PVP_REWARD_ID);
con = null;
try
{
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("INSERT INTO items VALUES (?, ?, "+Config.TOP_PVP_REWARD_ID+", "+Config.TOP_PVP_REWARD_AMOUNT+", 0, 'INVENTORY', 1, 0,0,NULL, 0, 0, -1)");
statement.setInt(1, winner_id);
statement.setInt(2, item.getObjectId());
statement.execute();
statement.close();
}
catch (SQLException e)
{
if(_log.isLoggable(Level.SEVERE))
_log.log(Level.SEVERE, "SQL exception while Insert reward DailyPvp for characters", e);
}
finally
{
CloseUtil.close(con);
}
}


con = null;
try
{
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("UPDATE characters SET DailyPvp=0");
statement.execute();
statement.close();
}
catch (SQLException e)
{
//if(_log.isLoggable(Level.SEVERE))
_log.log(Level.WARNING, "SQL exception while Update DailyPvp count for characters "+ e.getMessage(), e);
}
finally
{
CloseUtil.close(con);
}

}


public void skipDelay()
{
if (_task.nextRun.cancel(false))
{
_task.setStartTime(System.currentTimeMillis());
ThreadPoolManager.getInstance().executeTask(_task);
}
}

/**
* Class for TopPvp cycles
*/
class TopPvpStartTask implements Runnable
{
private long _startTime;
public ScheduledFuture<?> nextRun;

public TopPvpStartTask(long startTime)
{
_startTime = startTime;
}

public void setStartTime(long startTime)
{
_startTime = startTime;
}

/**
* @see java.lang.Runnable#run()
*/
@Override
public void run()
{
int delay = (int) Math.round((_startTime - System.currentTimeMillis()) / 1000.0);

if (delay > 0)
{
this.announce(delay);
}

int nextMsg = 0;
if (delay > 3600)
{
nextMsg = delay - 3600;
}
else if (delay > 1800)
{
nextMsg = delay - 1800;
}
else if (delay > 900)
{
nextMsg = delay - 900;
}
else if (delay > 600)
{
nextMsg = delay - 600;
}
else if (delay > 300)
{
nextMsg = delay - 300;
}
else if (delay > 60)
{
nextMsg = delay - 60;
}
else if (delay > 5)
{
nextMsg = delay - 5;
}
else if (delay > 0)
{
nextMsg = delay;
}
else
{

TopPvp.this.startReward();
TopPvp.this.scheduleEventStart();

}

if (delay > 0)
{
nextRun = ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg * 1000);
}
}

private void announce(long time)
{
if (time >= 3600 && time % 3600 == 0)
{
Announcements.getInstance().announceToAll("Top PvP Event: " + (time / 60 / 60) + " hour(s) until event is finished!");
}
else if (time >= 60)
{
Announcements.getInstance().announceToAll("Top PvP Event: " + (time / 60) + " minute(s) until the event is finished!");
}
else
{
Announcements.getInstance().announceToAll("Top PvP Event: " + time + " second(s) until the event is finished!");
}
}
}


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