Mineplex2018-withcommit/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GameStatManager.java

105 lines
2.7 KiB
Java
Raw Normal View History

2014-09-22 09:46:36 +02:00
package nautilus.game.arcade.managers;
import java.util.HashMap;
import java.util.UUID;
2014-09-22 09:46:36 +02:00
import mineplex.core.common.util.C;
import nautilus.game.arcade.ArcadeManager;
import nautilus.game.arcade.events.GameStateChangeEvent;
import nautilus.game.arcade.game.Game.GameState;
import nautilus.game.arcade.stats.StatTracker;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
2014-09-22 09:46:36 +02:00
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
2014-09-22 09:46:36 +02:00
public class GameStatManager implements Listener
{
ArcadeManager Manager;
private final HashMap<UUID, Long> _joinTimes = new HashMap<>();
2014-09-22 09:46:36 +02:00
public GameStatManager(ArcadeManager manager)
{
Manager = manager;
Manager.GetPluginManager().registerEvents(this, Manager.GetPlugin());
}
@EventHandler
public void StatEnableDisable(GameStateChangeEvent event)
{
if (!Manager.IsRewardStats())
return;
if (event.GetState() != GameState.Live)
return;
int requirement = (int)((double)event.GetGame().Manager.GetPlayerFull() * 0.75d);
event.GetGame().CanAddStats = (double)event.GetGame().GetPlayers(true).size() >= requirement;
if (!event.GetGame().CanAddStats)
event.GetGame().Announce(C.Bold + "Stats/Achievements Disabled. Requires " + requirement + " Players.");
}
@EventHandler
public void StatRegister(GameStateChangeEvent event)
{
if (!Manager.IsRewardStats())
return;
if (event.GetState() != GameState.Dead)
return;
for (Player player : event.GetGame().GetStats().keySet())
{
for (String stat : event.GetGame().GetStats().get(player).keySet())
{
Manager.GetStatsManager().incrementStat(player, stat, event.GetGame().GetStats().get(player).get(stat));
}
}
}
@EventHandler
public void StatGameRecord(GameStateChangeEvent event)
{
if (event.GetState() == GameState.End)
{
for (StatTracker tracker : event.GetGame().getStatTrackers())
HandlerList.unregisterAll(tracker);
if (event.GetGame().CanAddStats)
{
Manager.saveBasicStats(event.GetGame());
if (Manager.IsTournamentServer())
Manager.saveLeaderboardStats(event.GetGame());
}
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void playTimeStatJoin(PlayerJoinEvent event)
{
_joinTimes.put(event.getPlayer().getUniqueId(), System.currentTimeMillis());
}
@EventHandler
public void playTimeStatQuit(PlayerQuitEvent event)
{
Long joinTime = _joinTimes.remove(event.getPlayer().getUniqueId());
if (joinTime != null)
{
int timeInGame = (int) ((System.currentTimeMillis() - joinTime) / 1000);
Manager.GetStatsManager().incrementStat(event.getPlayer(), "Global.TimeInGame", timeInGame);
}
}
2014-09-22 09:46:36 +02:00
}