War state, handle time better, ability to have a server timezone (see ClanManager)

This commit is contained in:
Shaun Bennett 2015-02-05 21:36:07 -05:00
parent 0f23533f41
commit d878e3940e
5 changed files with 173 additions and 50 deletions

View File

@ -1,6 +1,7 @@
package mineplex.game.clans.clans;
import java.util.HashSet;
import java.util.TimeZone;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -58,6 +59,8 @@ import mineplex.minecraft.game.core.mechanics.Weapon;
public class ClansManager extends MiniClientPlugin<ClientClan> implements IRelation
{
private static final TimeZone TIME_ZONE = TimeZone.getDefault();
private String _serverName;
private CoreClientManager _clientManager;
@ -504,4 +507,15 @@ public class ClansManager extends MiniClientPlugin<ClientClan> implements IRelat
{
return (energy / 4) + (energy % 4 == 0 ? 0 : 1);
}
/**
* Get the timezone for this server.
* This may be used in the future if we have
* clans servers with varying timezones.
* @return {@link java.util.TimeZone} that this server should run at
*/
public TimeZone getServerTimeZone()
{
return TIME_ZONE;
}
}

View File

@ -36,25 +36,14 @@ public class EnemyData
return _kills;
}
public void addDeath()
{
addScore(-1);
}
public void addKill()
{
addScore(1);
addKills(1);
}
private void addScore(int add)
public void addScore(int add)
{
_score = Math.max(0, Math.min(_score + add, 40));
}
private void addKills(int add)
public void addKill()
{
_kills += add;
_kills++;
}
public boolean isInitiator()

View File

@ -5,6 +5,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PlayerDeathEvent;
@ -13,7 +14,10 @@ import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTime;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.ClansManager;
@ -24,10 +28,44 @@ public class WarManager extends MiniPlugin
private final ClansManager _clansManager;
private WarState _warState;
public WarManager(JavaPlugin plugin, ClansManager clansManager)
{
super("War Manager", plugin);
_clansManager = clansManager;
_warState = calculateWarState();
}
@EventHandler
public void update(UpdateEvent event)
{
if (event.getType() != UpdateType.SLOWEST)
return;
WarState currentState = calculateWarState();
if (currentState != _warState)
{
WarStateChangeEvent warEvent = new WarStateChangeEvent(_warState, currentState);
UtilServer.getServer().getPluginManager().callEvent(warEvent);
_warState = currentState;
}
}
private WarState calculateWarState()
{
// Get a calendar snapshot of the current time using server timezone
Calendar calendar = Calendar.getInstance(_clansManager.getServerTimeZone());
WarState warState = WarState.WAR;
if (isEnemyTime(calendar))
warState = WarState.FORM_ENEMIES;
else if (isInvadeTime(calendar))
warState = WarState.INVADE;
return warState;
}
public void attemptEnemy(Player player, ClanInfo initiatingClan, ClanInfo otherClan)
@ -39,7 +77,7 @@ public class WarManager extends MiniPlugin
{
String notifyMessage = null;
if (!isEnemyTime())
if (_warState != WarState.FORM_ENEMIES)
{
notifyMessage = "Enemies cannot be formed at this time. Please see mineplex.com/clans for info";
}
@ -75,25 +113,30 @@ public class WarManager extends MiniPlugin
{
if (playerClan.getEnemyData() != null && playerClan.getEnemyData().getEnemyName().equalsIgnoreCase(killerClan.getName()))
{
if (isWarInProgress())
// Only adjust score if we are in WAR or FORM ENEMIES state. Once invasion begins score should never change
// Adjust kill stats no matter what war state we are in (so we track kills during invasion)
if (_warState == WarState.WAR || _warState == WarState.FORM_ENEMIES)
{
playerClan.getEnemyData().addDeath();
killerClan.getEnemyData().addKill();
playerClan.getEnemyData().addScore(-1);
killerClan.getEnemyData().addScore(1);
}
killerClan.getEnemyData().addKill();
}
}
}
/**
* Can enemies be formed at the current time
*
* Enemy forming will happen once a week during a set period
*/
private boolean isEnemyTime()
{
Calendar c = Calendar.getInstance();
/**
* Check if a specific Calendar is currently in enemy time
* If this returns true, isInvadeTime should always return false for the same Calendar
*
* @param c {@link java.util.Calendar} instance that should be checked
*/
private boolean isEnemyTime(Calendar c)
{
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == CREATE_ENEMY_DATE)
@ -104,9 +147,14 @@ public class WarManager extends MiniPlugin
return false;
}
private boolean isInvadeTime()
/**
* Check if a specific Calendar is currently in invade time
* If this returns true, isEnemyTime should always return false for the same Calendar
*
* @param c {@link java.util.Calendar} instance that should be checked
*/
private boolean isInvadeTime(Calendar c)
{
Calendar c = Calendar.getInstance();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == INVADE_ENEMY_DATE)
@ -116,24 +164,22 @@ public class WarManager extends MiniPlugin
return false;
}
private boolean isWarInProgress()
{
return !isInvadeTime();
}
/**
* Get the starting time of when enemies can be formed next
* @return The enemy start time in the form of {@link java.util.Date}
*/
private Date getNextEnemyTime()
{
Calendar c = Calendar.getInstance();
Calendar c = Calendar.getInstance(_clansManager.getServerTimeZone());
int currDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
c.set(Calendar.DAY_OF_WEEK, CREATE_ENEMY_DATE);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.add(Calendar.DATE, 7);
if (currDayOfWeek >= CREATE_ENEMY_DATE)
c.add(Calendar.DATE, 7);
return c.getTime();
}
@ -144,17 +190,37 @@ public class WarManager extends MiniPlugin
*/
private Date getNextInvadeTime()
{
Calendar c = Calendar.getInstance();
Calendar c = Calendar.getInstance(_clansManager.getServerTimeZone());
int currDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
c.set(Calendar.DAY_OF_WEEK, INVADE_ENEMY_DATE);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.add(Calendar.DATE, 7);
if (currDayOfWeek >= INVADE_ENEMY_DATE)
c.add(Calendar.DATE, 7);
return c.getTime();
}
/**
* Get the current War State of the server
* War state determines what events are going on
* with respect to the current war.
*/
public WarState getWarState()
{
return _warState;
}
@EventHandler
public void stateChange(WarStateChangeEvent event)
{
WarState state = event.getNewState();
Bukkit.broadcastMessage(F.main("Clans", "War state changed: " + F.elem(state.getDescription())));
}
/**
* Send the current server time information to the player
*/
@ -162,34 +228,29 @@ public class WarManager extends MiniPlugin
{
LinkedList<String> messageList = new LinkedList<String>();
messageList.add(F.main("Clans", "Server Time;"));
messageList.add(F.main("Clans", "Server Time"));
Date currDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy");
dateFormat.setTimeZone(_clansManager.getServerTimeZone());
messageList.add(F.value("Server Time", dateFormat.format(currDate)));
if (isInvadeTime())
{
messageList.add(F.elem("Winning clans can invade enemies right now"));
}
else
messageList.add(F.value("Status", _warState.getDescription()));
if (_warState != WarState.INVADE)
{
long next = getNextInvadeTime().getTime();
long currTime = System.currentTimeMillis();
messageList.add(F.value("Enemy Invasion", UtilTime.convertString(next - currTime, 1, UtilTime.TimeUnit.FIT)));
messageList.add(F.value("Invasion", UtilTime.convertString(next - currTime, 1, UtilTime.TimeUnit.FIT)));
}
if (isEnemyTime())
{
messageList.add(F.elem("Enemies can be formed at this time"));
}
else
if (_warState != WarState.FORM_ENEMIES)
{
long next = getNextEnemyTime().getTime();
long currTime = System.currentTimeMillis();
messageList.add(F.value("Can Form Enemies In", UtilTime.convertString(next - currTime, 1, UtilTime.TimeUnit.FIT)));
messageList.add(F.value("Enemy Reset", UtilTime.convertString(next - currTime, 1, UtilTime.TimeUnit.FIT)));
}
return messageList;

View File

@ -0,0 +1,20 @@
package mineplex.game.clans.clans.war;
public enum WarState
{
FORM_ENEMIES("Enemies can now be formed"),
WAR("War is in progress"),
INVADE("Winning teams can invade enemies");
private String _description;
WarState(String description)
{
_description = description;
}
public String getDescription()
{
return _description;
}
}

View File

@ -0,0 +1,39 @@
package mineplex.game.clans.clans.war;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class WarStateChangeEvent extends Event
{
private static final HandlerList handlers = new HandlerList();
private WarState _oldState;
private WarState _newState;
public WarStateChangeEvent(WarState oldState, WarState newState)
{
_oldState = oldState;
_newState = newState;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public WarState getOldState()
{
return _oldState;
}
public WarState getNewState()
{
return _newState;
}
}