From 439ef96bac74c27edd5a539c409a5e0a200057ee Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 16 Dec 2019 09:50:56 +0100 Subject: [PATCH] Adding sessions --- src/de/steamwar/bungeecore/ArenaMode.java | 8 ------ src/de/steamwar/bungeecore/Broadcaster.java | 3 +- src/de/steamwar/bungeecore/BungeeCore.java | 6 ++-- .../bungeecore/listeners/SessionManager.java | 28 +++++++++++++++++++ src/de/steamwar/bungeecore/sql/Session.java | 19 +++++++++++++ 5 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 src/de/steamwar/bungeecore/listeners/SessionManager.java create mode 100644 src/de/steamwar/bungeecore/sql/Session.java diff --git a/src/de/steamwar/bungeecore/ArenaMode.java b/src/de/steamwar/bungeecore/ArenaMode.java index ded82fc..f2df5bc 100644 --- a/src/de/steamwar/bungeecore/ArenaMode.java +++ b/src/de/steamwar/bungeecore/ArenaMode.java @@ -5,14 +5,6 @@ import net.md_5.bungee.config.Configuration; import java.util.*; public class ArenaMode { - /*AirShip, - WarShip, - WarGear, - MiniWarGear, - WarGear7, - GearUp, - EisZeit, - UNKNOWN;*/ private static Map byChat = new HashMap<>(); private static Map byInternal = new HashMap<>(); diff --git a/src/de/steamwar/bungeecore/Broadcaster.java b/src/de/steamwar/bungeecore/Broadcaster.java index 6d81deb..49df73f 100644 --- a/src/de/steamwar/bungeecore/Broadcaster.java +++ b/src/de/steamwar/bungeecore/Broadcaster.java @@ -11,7 +11,8 @@ class Broadcaster { Broadcaster(){ ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> { - BungeeCore.broadcast(BungeeCore.CHAT_PREFIX + broadCastMsgs[lastBroadCast]); + if(!ProxyServer.getInstance().getPlayers().isEmpty()) + BungeeCore.broadcast(BungeeCore.CHAT_PREFIX + broadCastMsgs[lastBroadCast]); lastBroadCast++; if(lastBroadCast == broadCastMsgs.length){ lastBroadCast = 0; diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index b40836d..933db5d 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -1,10 +1,7 @@ package de.steamwar.bungeecore; import de.steamwar.bungeecore.commands.*; -import de.steamwar.bungeecore.listeners.BanListener; -import de.steamwar.bungeecore.listeners.ChatListener; -import de.steamwar.bungeecore.listeners.ConnectionListener; -import de.steamwar.bungeecore.listeners.PollSystem; +import de.steamwar.bungeecore.listeners.*; import de.steamwar.bungeecore.listeners.mods.Forge; import de.steamwar.bungeecore.listeners.mods.LabyMod; import de.steamwar.bungeecore.sql.SQL; @@ -86,6 +83,7 @@ public class BungeeCore extends Plugin { new EventStarter(); new Broadcaster(); + new SessionManager(); getProxy().getScheduler().schedule(this, () -> { SteamwarUser.clearCache(); diff --git a/src/de/steamwar/bungeecore/listeners/SessionManager.java b/src/de/steamwar/bungeecore/listeners/SessionManager.java new file mode 100644 index 0000000..1aefae7 --- /dev/null +++ b/src/de/steamwar/bungeecore/listeners/SessionManager.java @@ -0,0 +1,28 @@ +package de.steamwar.bungeecore.listeners; + +import de.steamwar.bungeecore.sql.Session; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.event.PlayerDisconnectEvent; +import net.md_5.bungee.api.event.PostLoginEvent; +import net.md_5.bungee.event.EventHandler; + +import java.util.HashMap; +import java.util.Map; + +public class SessionManager extends BasicListener { + + private Map sessions = new HashMap<>(); + + @EventHandler + public void onPostLogin(PostLoginEvent event){ + sessions.put(event.getPlayer(), new Session(SteamwarUser.get(event.getPlayer()).getId())); + } + + @EventHandler + public void onDisconnect(PlayerDisconnectEvent e){ + Session session = sessions.remove(e.getPlayer()); + if(session != null) + session.stopSession(); + } +} diff --git a/src/de/steamwar/bungeecore/sql/Session.java b/src/de/steamwar/bungeecore/sql/Session.java new file mode 100644 index 0000000..68bf026 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/Session.java @@ -0,0 +1,19 @@ +package de.steamwar.bungeecore.sql; + +import java.sql.Timestamp; +import java.time.Instant; + +public class Session { + + private final int userID; + private Timestamp startTime; + + public Session(int userID){ + this.userID = userID; + startTime = Timestamp.from(Instant.now()); + } + + public void stopSession(){ + SQL.update("INSERT INTO Session (UserID, StartTime, EndTime) VALUES ("+ userID + ", '" + startTime.toString() + "', NOW())"); + } +}