From 6a368454ecd3a32e199898da27dcefbfcda32b95 Mon Sep 17 00:00:00 2001 From: CoderTim Date: Wed, 18 Feb 2015 21:31:38 -0500 Subject: [PATCH] Clans database refactoring --- .../Mineplex.Game.Clans.iml | 2 ++ .../mineplex/game/clans/clans/ClanInfo.java | 19 ++++++++------- .../clans/clans/ClansDataAccessLayer.java | 3 ++- .../mineplex/game/clans/clans/ClansGame.java | 4 +++- .../clans/repository/ClanRepository.java | 24 ++++++++----------- .../repository/tokens/ClanEnemyToken.java | 4 ++-- .../clans/repository/tokens/ClanToken.java | 5 ++-- 7 files changed, 32 insertions(+), 29 deletions(-) diff --git a/Plugins/Mineplex.Game.Clans/Mineplex.Game.Clans.iml b/Plugins/Mineplex.Game.Clans/Mineplex.Game.Clans.iml index 9f3f37b52..82d2e5104 100644 --- a/Plugins/Mineplex.Game.Clans/Mineplex.Game.Clans.iml +++ b/Plugins/Mineplex.Game.Clans/Mineplex.Game.Clans.iml @@ -13,5 +13,7 @@ + + \ No newline at end of file diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClanInfo.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClanInfo.java index 7544c2efe..0dda88280 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClanInfo.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClanInfo.java @@ -1,5 +1,6 @@ package mineplex.game.clans.clans; +import java.sql.Timestamp; import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedList; @@ -35,8 +36,8 @@ public class ClanInfo private boolean _admin = false; - private long _dateCreated = 0; - private long _lastOnline = 0; + private Timestamp _dateCreated = null; + private Timestamp _lastOnline = null; // Loaded from Client private NautHashMap _memberMap = new NautHashMap(); @@ -193,7 +194,7 @@ public class ClanInfo // Age stringList.add(F.value("Age", - UtilTime.convertString(System.currentTimeMillis() - _dateCreated, 1, TimeUnit.FIT))); + UtilTime.convertString(System.currentTimeMillis() - _dateCreated.getTime(), 1, TimeUnit.FIT))); // Home if (Clans.getClanUtility().relPC(caller, this) == ClanRelation.SELF) @@ -371,17 +372,17 @@ public class ClanInfo return _requestMap; } - public long getDateCreated() + public Timestamp getDateCreated() { return _dateCreated; } - public long getLastOnline() + public Timestamp getLastOnline() { return _lastOnline; } - public void setLastOnline(long lastOnline) + public void setLastOnline(Timestamp lastOnline) { _lastOnline = lastOnline; } @@ -401,7 +402,7 @@ public class ClanInfo if (UtilPlayer.isOnline(cur)) return true; - return System.currentTimeMillis() - _lastOnline < Clans.getOnlineTime(); + return System.currentTimeMillis() - _lastOnline.getTime() < Clans.getOnlineTime(); } public String getProtected() @@ -410,12 +411,12 @@ public class ClanInfo if (UtilPlayer.isOnline(cur)) return C.cRed + "No - Clan Members are Online"; - if (System.currentTimeMillis() - _lastOnline > Clans.getOnlineTime()) + if (System.currentTimeMillis() - _lastOnline.getTime() > Clans.getOnlineTime()) return C.cGreen + "Yes - Clan Members are Offline"; return C.cGold + "No, " - + UtilTime.convertString(Clans.getOnlineTime() - (System.currentTimeMillis() - _lastOnline), 1, + + UtilTime.convertString(Clans.getOnlineTime() - (System.currentTimeMillis() - _lastOnline.getTime()), 1, TimeUnit.FIT) + " to Protection"; } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansDataAccessLayer.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansDataAccessLayer.java index 5d29c90e9..c05809249 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansDataAccessLayer.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansDataAccessLayer.java @@ -1,5 +1,6 @@ package mineplex.game.clans.clans; +import java.sql.Timestamp; import java.util.Date; import org.bukkit.Chunk; @@ -157,7 +158,7 @@ public class ClansDataAccessLayer public void enemy(ClanInfo clan, ClanInfo otherClan, String player) { _repository.addEnemy(clan.getId(), otherClan.getId()); - Date currDate = new Date(); + Timestamp currDate = new Timestamp(System.currentTimeMillis()); ClanEnemyToken clanEnemyToken = new ClanEnemyToken(); clanEnemyToken.Initiator = true; diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansGame.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansGame.java index 50c91bec2..717fb383b 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansGame.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansGame.java @@ -1,5 +1,7 @@ package mineplex.game.clans.clans; +import java.sql.Timestamp; + import org.bukkit.Effect; import org.bukkit.GameMode; import org.bukkit.Location; @@ -516,7 +518,7 @@ public class ClansGame extends MiniPlugin ClanInfo clan = Clans.getClanUtility().getClanByPlayer(event.getPlayer()); if (clan == null) return; - clan.setLastOnline(System.currentTimeMillis()); + clan.setLastOnline(new Timestamp(System.currentTimeMillis())); } @EventHandler(priority = EventPriority.LOWEST) diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/ClanRepository.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/ClanRepository.java index 029890464..109d4354e 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/ClanRepository.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/ClanRepository.java @@ -2,8 +2,8 @@ package mineplex.game.clans.clans.repository; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; import java.util.Collection; -import java.util.Date; import java.util.UUID; import org.bukkit.plugin.java.JavaPlugin; @@ -13,7 +13,7 @@ import mineplex.core.database.RepositoryBase; import mineplex.core.database.ResultSetCallable; import mineplex.core.database.column.ColumnBoolean; import mineplex.core.database.column.ColumnInt; -import mineplex.core.database.column.ColumnLong; +import mineplex.core.database.column.ColumnTimestamp; import mineplex.core.database.column.ColumnVarChar; import mineplex.game.clans.clans.ClanInfo; import mineplex.game.clans.clans.repository.tokens.ClanAllianceToken; @@ -24,10 +24,10 @@ import mineplex.game.clans.clans.repository.tokens.ClanToken; public class ClanRepository extends RepositoryBase { - private static String CREATE_CLAN_TABLE = "CREATE TABLE IF NOT EXISTS clans (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(100), name VARCHAR(100), description VARCHAR(140), home VARCHAR(140), admin BOOL, dateCreated LONG, lastOnline LONG, energy INT, PRIMARY KEY (id), INDEX clanName (name));"; + private static String CREATE_CLAN_TABLE = "CREATE TABLE IF NOT EXISTS clans (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(100), name VARCHAR(100), description VARCHAR(140), home VARCHAR(140), admin BIT(1), dateCreated DATETIME, lastOnline DATETIME, energy INT, PRIMARY KEY (id), INDEX clanName (name));"; private static String CREATE_ACCOUNT_CLAN_TABLE = "CREATE TABLE IF NOT EXISTS accountClan (id INT NOT NULL AUTO_INCREMENT, accountId INT, clanId INT, clanRole VARCHAR(140), PRIMARY KEY (id), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (clanId) REFERENCES clans(id), INDEX clanIdIndex (clanId));"; - private static String CREATE_CLAN_TERRITORY_TABLE = "CREATE TABLE IF NOT EXISTS clanTerritory (id INT NOT NULL AUTO_INCREMENT, clanId INT, serverName VARCHAR(100), chunk VARCHAR(100), safe BOOL, PRIMARY KEY (id), FOREIGN KEY (clanId) REFERENCES clans(id), INDEX clanIdIndex (clanId, serverName));"; - private static String CREATE_CLAN_ALLIANCE_TABLE = "CREATE TABLE IF NOT EXISTS clanAlliances (id INT NOT NULL AUTO_INCREMENT, clanId INT, otherClanId INT, trusted BOOL, PRIMARY KEY (id), FOREIGN KEY (otherClanId) REFERENCES clans(id), FOREIGN KEY (clanId) REFERENCES clans(id), INDEX clanIdIndex (clanId));"; + private static String CREATE_CLAN_TERRITORY_TABLE = "CREATE TABLE IF NOT EXISTS clanTerritory (id INT NOT NULL AUTO_INCREMENT, clanId INT, serverName VARCHAR(100), chunk VARCHAR(100), safe BIT(1), PRIMARY KEY (id), FOREIGN KEY (clanId) REFERENCES clans(id), INDEX clanIdIndex (clanId, serverName));"; + private static String CREATE_CLAN_ALLIANCE_TABLE = "CREATE TABLE IF NOT EXISTS clanAlliances (id INT NOT NULL AUTO_INCREMENT, clanId INT, otherClanId INT, trusted BIT(1), PRIMARY KEY (id), FOREIGN KEY (otherClanId) REFERENCES clans(id), FOREIGN KEY (clanId) REFERENCES clans(id), INDEX clanIdIndex (clanId));"; private static String RETRIEVE_START_CLAN_INFO = "SELECT c.id, c.name, c.description, c.home, c.admin, c.energy, c.dateCreated, c.lastOnline, ct.chunk, ct.safe FROM clans AS c LEFT JOIN clanTerritory AS ct ON ct.clanId = c.id WHERE c.serverName = ?;"; private static String RETRIEVE_CLAN_MEMBER_INFO = "SELECT c.name, a.name, a.uuid, clanRole FROM accountClan AS ac INNER JOIN accounts AS a ON a.id = ac.accountId INNER JOIN clans AS c on c.id = ac.clanId WHERE c.serverName = ?;"; @@ -91,8 +91,8 @@ public class ClanRepository extends RepositoryBase token.Home = resultSet.getString(4); token.Admin = resultSet.getBoolean(5); token.Energy = resultSet.getInt(6); - token.DateCreated = resultSet.getLong(7); - token.LastOnline = resultSet.getLong(8); + token.DateCreated = resultSet.getTimestamp(7); + token.LastOnline = resultSet.getTimestamp(8); ClanTerritoryToken territoryToken = new ClanTerritoryToken(); territoryToken.ClanName = token.Name; @@ -173,7 +173,7 @@ public class ClanRepository extends RepositoryBase int otherClanScore = resultSet.getInt(4); int clanKills = resultSet.getInt(5); int otherClanKills = resultSet.getInt(6); - Date timeFormed = resultSet.getTimestamp(7); + Timestamp timeFormed = resultSet.getTimestamp(7); clanToken.EnemyName = otherClanName; clanToken.Score = clanScore; @@ -204,10 +204,6 @@ public class ClanRepository extends RepositoryBase public void deleteClan(int clanId) { - executeUpdate(DELETE_CLAN_MEMBERS, new ColumnInt("clanid", clanId)); - executeUpdate(DELETE_CLAN_TERRITORIES, new ColumnInt("clanid", clanId)); - executeUpdate(DELETE_CLAN_ALLIANCES, new ColumnInt("clanid", clanId), new ColumnInt("clanid", clanId)); - executeUpdate(DELETE_CLAN_ENEMY, new ColumnInt("clanid", clanId)); executeUpdate(DELETE_CLAN, new ColumnInt("clanid", clanId)); } @@ -273,9 +269,9 @@ public class ClanRepository extends RepositoryBase executeUpdate(DELETE_CLAN_TERRITORY, new ColumnInt("clanId", clanId), new ColumnVarChar("serverName", 100, _serverName), new ColumnVarChar("chunk", 100, chunk)); } - public void updateClan(int clanId, String name, String desc, String home, boolean admin, int energy, long lastOnline) + public void updateClan(int clanId, String name, String desc, String home, boolean admin, int energy, Timestamp lastOnline) { - executeUpdate(UPDATE_CLAN, new ColumnVarChar("name", 100, name), new ColumnVarChar("desc", 100, desc), new ColumnVarChar("home", 100, home), new ColumnBoolean("admin", admin), new ColumnInt("energy", energy), new ColumnLong("lastOnline", lastOnline), new ColumnInt("clanId", clanId)); + executeUpdate(UPDATE_CLAN, new ColumnVarChar("name", 100, name), new ColumnVarChar("desc", 100, desc), new ColumnVarChar("home", 100, home), new ColumnBoolean("admin", admin), new ColumnInt("energy", energy), new ColumnTimestamp("lastOnline", lastOnline), new ColumnInt("clanId", clanId)); } public void updateEnemy(int clanId, int otherClanId, int clanScore, int otherClanScore, int clanKills, int otherClanKills) diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/tokens/ClanEnemyToken.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/tokens/ClanEnemyToken.java index 13389612e..3aa201763 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/tokens/ClanEnemyToken.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/tokens/ClanEnemyToken.java @@ -1,6 +1,6 @@ package mineplex.game.clans.clans.repository.tokens; -import java.util.Date; +import java.sql.Timestamp; public class ClanEnemyToken { @@ -8,5 +8,5 @@ public class ClanEnemyToken public String EnemyName; public int Score; public int Kills; - public Date TimeFormed; + public Timestamp TimeFormed; } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/tokens/ClanToken.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/tokens/ClanToken.java index 0cc1a7f8e..f9881aa51 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/tokens/ClanToken.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/repository/tokens/ClanToken.java @@ -1,5 +1,6 @@ package mineplex.game.clans.clans.repository.tokens; +import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; @@ -11,8 +12,8 @@ public class ClanToken public String Home; public boolean Admin; public int Energy; - public long DateCreated; - public long LastOnline; + public Timestamp DateCreated; + public Timestamp LastOnline; public ClanEnemyToken EnemyToken;