From fed9f675d3900b91ab9fc85a53978c4f46967a48 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Sun, 17 Apr 2016 13:51:15 +1000 Subject: [PATCH] Implementation for basic Fountains --- .../core/brawl/fountain/Fountain.java | 52 ++++++++++++++++ .../core/brawl/fountain/FountainManager.java | 31 ++++++++++ .../fountain/FountainRedisRepository.java | 60 +++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/Fountain.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainManager.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainRedisRepository.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/Fountain.java b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/Fountain.java new file mode 100644 index 000000000..cf45e66e7 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/Fountain.java @@ -0,0 +1,52 @@ +package mineplex.core.brawl.fountain; + +import java.util.concurrent.atomic.AtomicLong; + +/** + * A fountain represents an atomic counter that is trying to hit a goal. Once the fountain + * reaches it's goal you can either reset it or let it stay completed for an indefinite time. + * + * @author Shaun Bennett + */ +public class Fountain +{ + // Cached count for the fountain + private AtomicLong _count = new AtomicLong(0); + // The System.currentTimeMillis() when cached count was last updated + private long _cacheLastUpdated; + // The goal that this fountain is trying to reach + private long _goal; + + // Redis repository to store the count + private FountainRedisRepository _redisRepository; + + public Fountain(String dataKey, long goal) + { + _goal = goal; + _redisRepository = new FountainRedisRepository(dataKey); + + updateCount(); + } + + public void updateCount() + { + _count.set(_redisRepository.getCount()); + _cacheLastUpdated = System.currentTimeMillis(); + } + + public long getCount() + { + return _count.get(); + } + + public long getGoal() + { + return _goal; + } + + public double getFillPercent() + { + return Math.min(100, (((double) getCount()) / _goal)); + } + +} \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainManager.java b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainManager.java new file mode 100644 index 000000000..3fd49f97e --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainManager.java @@ -0,0 +1,31 @@ +package mineplex.core.brawl.fountain; + +import mineplex.core.MiniPlugin; +import mineplex.core.updater.UpdateType; +import mineplex.core.updater.event.UpdateEvent; +import org.bukkit.event.EventHandler; +import org.bukkit.plugin.java.JavaPlugin; + +/** + * @author Shaun Bennett + */ +public class FountainManager extends MiniPlugin +{ + private Fountain _gemFountain; + + public FountainManager(JavaPlugin plugin) + { + super("Fountain", plugin); + + _gemFountain = new Fountain("GemFountain", 1000000); + } + + @EventHandler + public void updateFountainCount(UpdateEvent event) + { + if (event.getType() != UpdateType.SLOW) + return; + + _gemFountain.updateCount(); + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainRedisRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainRedisRepository.java new file mode 100644 index 000000000..cbfb51b76 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/brawl/fountain/FountainRedisRepository.java @@ -0,0 +1,60 @@ +package mineplex.core.brawl.fountain; + +import mineplex.serverdata.Region; +import mineplex.serverdata.redis.RedisRepository; +import redis.clients.jedis.Jedis; + +/** + * Redis repository to store the count for {@link Fountain} + * @author Shaun Bennett + */ +public class FountainRedisRepository extends RedisRepository +{ + private String _dataKey; + + public FountainRedisRepository(String dataKey) + { + super(Region.ALL); + + _dataKey = dataKey; + } + + /** + * Get the current count inside the fountain + * @return The current count for the fountain + */ + public long getCount() + { + long count = 0; + + try (Jedis jedis = getResource(false)) + { + count = Long.parseLong(jedis.get(getKey())); + } + + return count; + } + + /** + * Increment the current fountain count by {@code increment} and then return the latest + * count of the fountain. This is handled in an atomic process through redis + * @param increment Amount to increment the fountain by + * @return The new count for the fountain + */ + public long incrementCount(long increment) + { + long count = 0; + + try (Jedis jedis = getResource(true)) + { + count = jedis.incrBy(getKey(), increment); + } + + return count; + } + + private String getKey() + { + return getKey(_dataKey); + } +}