/* 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.l2jfrozen.gameserver.model.entity.event;
import java.util.Calendar;
import java.util.Collection;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Logger;
import com.l2jfrozen.Config;
import com.l2jfrozen.gameserver.managers.TownManager;
import com.l2jfrozen.gameserver.model.L2Character;
import com.l2jfrozen.gameserver.model.L2World;
import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
import com.l2jfrozen.gameserver.model.zone.L2ZoneType;
import com.l2jfrozen.gameserver.network.clientpackets.Say2;
import com.l2jfrozen.gameserver.network.serverpackets.CreatureSay;
import com.l2jfrozen.gameserver.thread.ThreadPoolManager;
import com.l2jfrozen.util.random.Rnd;
import javolution.util.FastList;
/**
*
* @author Elfocrash
*/
public class HiddenBastard
{
public static FastList<L2PcInstance> _players = new FastList<L2PcInstance>();
public static State state = State.INACTIVE;
public static L2PcInstance bastard = null;
public static L2PcInstance winner = null;
public static enum State { INACTIVE,PICKING,SEARCHING }
protected static final Logger _log = Logger.getLogger(TriviaEventManager.class.getName());
/** Task for event cycles<br> */
private Task _task;
/**
* New instance only by getInstance()<br>
*/
public HiddenBastard()
{
if (Config.ENABLE_HIDDENBASTARD_EVENT)
{
this.scheduleEventStart();
_log.warning("HiddenBastard: Started.");
}
else
{
_log.warning("HiddenBastard: Engine is Disabled.");
}
}
public class Task implements Runnable
{
private long _startTime;
public ScheduledFuture<?> nextRun;
public Task(long startTime)
{
_startTime = startTime;
}
public void setStartTime(long startTime)
{
_startTime = startTime;
}
@Override
public void run()
{
int delay = (int) Math.round((_startTime - System.currentTimeMillis()) / 1000.0);
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
{
if (state != State.INACTIVE)
{
return;
}
Runner();
}
if (delay > 0)
{
nextRun = ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg * 1000);
}
}
}
public void Runner()
{
_task.setStartTime(System.currentTimeMillis() + 1000L *10);
ThreadPoolManager.getInstance().executeTask(_task);
Announce("Hidden Bastard Event Started. In 20 seconds a random player from Giran will be picked to become the hidden bastard.");
state = State.PICKING;
waitSecs(20);
Announce("The event started! Someone in Giran is the Hidden Bastard. Use .bastard command while targeting a player to check if he is the bastard!");
Announce("You have 5 minutes to search for him.");
pickBastard();
wait(4);
while(state == State.SEARCHING)
{
Announce("One minute left! Still searching for him!");
wait(1);
while(state == State.SEARCHING)
{
Announce("No one found the hidden bastard. And that's just sad. " + bastard + "was the bastard.");
bastard.setIsBastard(false);
bastard.setIsImobilised(false);
clear();
this.scheduleEventStart();
}
}
}
/**
* Starts the event
*/
public void scheduleEventStart()
{
try
{
Calendar currentTime = Calendar.getInstance();
Calendar nextStartTime = null;
Calendar testStartTime = null;
for (String timeOfDay : Config.HIDDENBASTARD_INTERVAL)
{
// Creating a Calendar object from the specified interval value
testStartTime = Calendar.getInstance();
testStartTime.setLenient(true);
String[] splitTimeOfDay = timeOfDay.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())
{
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;
}
}
if(nextStartTime != null)
_task = new Task(nextStartTime.getTimeInMillis());
ThreadPoolManager.getInstance().executeTask(_task);
}
catch (Exception e)
{
_log.warning("TriviaEventEngine[TriviaManager.scheduleEventStart()]: Error figuring out a start time. Check TriviaEventInterval in config file.");
}
}
public static void Announce(String msg)
{
Collection<L2PcInstance> plrs = L2World.getInstance().getAllPlayers();
CreatureSay cs = new CreatureSay(1, Say2.ANNOUNCEMENT, "", msg);
for (L2PcInstance pls : plrs)
{
pls.sendPacket(cs);
}
}
public void pickBastard()
{
state = State.SEARCHING;
L2ZoneType zone = TownManager.getInstance().getTown(9);
for (L2Character character : zone.getCharactersInside().values())
if ((character instanceof L2PcInstance) && !((L2PcInstance) character).isInCombat() && !((L2PcInstance) character).isInParty() && !((L2PcInstance) character).isInOlympiadMode() && !((L2PcInstance) character).isCursedWeaponEquipped())
_players.add((L2PcInstance) character);
if(_players.size() > 2)
{
bastard = _players.get(Rnd.get(1, _players.size() - 1));
bastard.setIsBastard(true);
bastard.sendMessage("You are the secret bastard. You are not allowed to move or talk.");
bastard.setIsImobilised(true);
}
else
{
Announce("Non enough players in giran for the event to start.");
state = State.INACTIVE;
this.scheduleEventStart();
clear();
}
}
public static void endAndReward(L2PcInstance player)
{
player.addItem("reward", Config.HIDDENBASTARD_EVENT_REWARD_ID, Config.HIDDENBASTARD_EVENT_REWARD_AMOUNT, player,true);
}
public static void clear()
{
_players.clear();
bastard = null;
winner = null;
state = State.INACTIVE;
}
public void waitSecs(int i)
{
try
{
Thread.sleep(i * 1000);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
public void wait(int i)
{
try
{
Thread.sleep(i * 60000);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
public static HiddenBastard getInstance()
{
return SingletonHolder._instance;
}
private static class SingletonHolder
{
protected static final HiddenBastard _instance = new HiddenBastard();
}
}
/*
* 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.handler.voicedcommandhandlers;
import com.l2jfrozen.gameserver.handler.IVoicedCommandHandler;
import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
import com.l2jfrozen.gameserver.model.entity.event.HiddenBastard;
/**
*
* @author Elfocrash
*/
public class HiddenBastardCmd implements IVoicedCommandHandler
{
private static String[] _voicedCommands =
{
"bastard"
};
@Override
public boolean useVoicedCommand(String command, L2PcInstance activeChar, String target)
{
if(command.equalsIgnoreCase(_voicedCommands[0]))
{
if(!(activeChar.getTarget() instanceof L2PcInstance))
{
activeChar.sendMessage("Only players can be bastards.");
return false;
}
if(activeChar.getTarget() == activeChar)
{
activeChar.sendMessage("You are too good to be the bastard.");
return false;
}
if(activeChar.getTarget() == null)
{
activeChar.sendMessage("You have to target someone.");
return false;
}
if(((L2PcInstance) activeChar.getTarget()) == HiddenBastard.bastard)
{
activeChar.sendMessage("You found him!" + activeChar.getTarget().getName() + " is the bastard.");
HiddenBastard.Announce(activeChar.getName() + " found the hidden bastard who was " + HiddenBastard.bastard);
HiddenBastard.endAndReward(activeChar);
HiddenBastard.bastard.setIsBastard(false);
HiddenBastard.bastard.setIsImobilised(false);
HiddenBastard.clear();
}
else
{
activeChar.sendMessage("No! " + activeChar.getTarget().getName() + " is not the bastard.");
return false;
}
}
return true;
}
@Override
public String[] getVoicedCommandList()
{
return _voicedCommands;
}
}