From d878e3940e8b0b4c8f752447d3dd588cffc8d30b Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Thu, 5 Feb 2015 21:36:07 -0500 Subject: [PATCH] War state, handle time better, ability to have a server timezone (see ClanManager) --- .../game/clans/clans/ClansManager.java | 14 ++ .../mineplex/game/clans/clans/EnemyData.java | 17 +-- .../game/clans/clans/war/WarManager.java | 133 +++++++++++++----- .../game/clans/clans/war/WarState.java | 20 +++ .../clans/clans/war/WarStateChangeEvent.java | 39 +++++ 5 files changed, 173 insertions(+), 50 deletions(-) create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarState.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarStateChangeEvent.java diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java index 850256d08..dd85820e3 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java @@ -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 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 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; + } } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/EnemyData.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/EnemyData.java index 726fc11c3..fb9bb9d03 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/EnemyData.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/EnemyData.java @@ -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() diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarManager.java index ec7e64f24..1d5437992 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarManager.java @@ -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 messageList = new LinkedList(); - 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; diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarState.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarState.java new file mode 100644 index 000000000..e8bac93ea --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarState.java @@ -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; + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarStateChangeEvent.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarStateChangeEvent.java new file mode 100644 index 000000000..b525f63c5 --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/war/WarStateChangeEvent.java @@ -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; + } + +} \ No newline at end of file