From 9cf5fda540ff1b553e34cd42e9a41cdcc2169dfb Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Sat, 13 May 2017 01:02:06 -0400 Subject: [PATCH] Update leaderboards system to use more malleable seasons system --- .../leaderboard/LeaderboardRepository.java | 54 ++++--------------- 1 file changed, 11 insertions(+), 43 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/LeaderboardRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/LeaderboardRepository.java index 866a1e5b7..f9b286039 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/LeaderboardRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/LeaderboardRepository.java @@ -18,27 +18,25 @@ import mineplex.serverdata.database.RepositoryBase; public class LeaderboardRepository extends RepositoryBase { - private static final int SEASON = 1; - - private static final String CREATE_TOTAL = "CREATE TABLE accountStatsSeasonalTotal (accountId INT NOT NULL, statId INT NOT NULL, season SMALLINT NOT NULL, value BIGINT NOT NULL, PRIMARY KEY (accountId, statId, season), INDEX valueIndex (value DESC), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (statId) REFERENCES stats(id));"; + private static final String CREATE_TOTAL = "CREATE TABLE accountStatsAllTime (accountId INT NOT NULL, statId INT NOT NULL, value BIGINT NOT NULL, PRIMARY KEY (accountId, statId), INDEX valueIndex (value DESC), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (statId) REFERENCES stats(id));"; + private static final String CREATE_SEASONAL = "CREATE TABLE accountStatsSeasonal (accountId INT NOT NULL, statId INT NOT NULL, seasonId SMALLINT NOT NULL, value BIGINT NOT NULL, PRIMARY KEY (accountId, statId), INDEX valueIndex (value DESC), INDEX seasonIndex (seasonId), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (statId) REFERENCES stats(id), FOREIGN KEY (seasonId) REFERENCES statSeasons(id));"; private static final String CREATE_YEARLY = "CREATE TABLE accountStatsYearly (accountId INT NOT NULL, statId INT NOT NULL, date DATE NOT NULL, value BIGINT NOT NULL, PRIMARY KEY (accountId, statId), INDEX valueIndex (value DESC), INDEX dateIndex (date), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (statId) REFERENCES stats(id));"; private static final String CREATE_MONTHLY = "CREATE TABLE accountStatsMonthly (accountId INT NOT NULL, statId INT NOT NULL, date DATE NOT NULL, value BIGINT NOT NULL, PRIMARY KEY (accountId, statId), INDEX valueIndex (value DESC), INDEX dateIndex (date), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (statId) REFERENCES stats(id));"; private static final String CREATE_WEEKLY = "CREATE TABLE accountStatsWeekly (accountId INT NOT NULL, statId INT NOT NULL, date DATE NOT NULL, value BIGINT NOT NULL, PRIMARY KEY (accountId, statId), INDEX valueIndex (value DESC), INDEX dateIndex (date), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (statId) REFERENCES stats(id));"; private static final String CREATE_DAILY = "CREATE TABLE accountStatsDaily (accountId INT NOT NULL, statId INT NOT NULL, date DATE NOT NULL, value BIGINT NOT NULL, PRIMARY KEY (accountId, statId), INDEX valueIndex (value DESC), INDEX dateIndex (date), FOREIGN KEY (accountId) REFERENCES accounts(id), FOREIGN KEY (statId) REFERENCES stats(id));"; + private static final String CREATE_SEASON = "CREATE TABLE statSeasons (id SMALLINT NOT NULL, seasonName VARCHAR(50) NOT NULL, startDate TIMESTAMP NOT NULL DEFAULT '1969-12-31 18:00:01', endDate TIMESTAMP NOT NULL DEFAULT '1969-12-31 18:00:01', PRIMARY KEY (id), UNIQUE INDEX seasonIndex (seasonName), INDEX startIndex (startDate), INDEX endIndex (endDate));"; - private static final String INSERT_STAT = "INSERT INTO accountStatsSeasonalTotal (accountId, statId, season, value) VALUES (?, ?, " + SEASON + ", ?);"; - private static final String INSERT_STAT_ALL = "INSERT INTO accountStatsSeasonalTotal (accountId, statId, season, value) VALUES (?, ?, 0, ?);"; - private static final String UPDATE_STAT = "UPDATE accountStatsSeasonalTotal SET value=value + ? WHERE accountId=? AND statId=? AND season=" + SEASON + ";"; - private static final String UPDATE_STAT_ALL = "UPDATE accountStatsSeasonalTotal SET value=value + ? WHERE accountId=? AND statId=? AND season=0;"; + private static final String INSERT_STAT = "INSERT INTO accountStatsAllTime (accountId, statId, value) VALUES (?, ?, ?);"; + private static final String UPDATE_STAT = "UPDATE accountStatsAllTime SET value=value + ? WHERE accountId=? AND statId=?;"; - private static final String FETCH_STAT_ALL = "SELECT a.name, sl.value FROM accountStatsSeasonalTotal AS sl INNER JOIN accounts AS a ON a.id=sl.accountId WHERE sl.statId=%STAT% AND season=0 ORDER BY sl.value DESC LIMIT %START%,%LIMIT%;"; + private static final String FETCH_STAT_ALL = "SELECT a.name, sl.value FROM accountStatsAllTime AS sl INNER JOIN accounts AS a ON a.id=sl.accountId WHERE sl.statId=%STAT% ORDER BY sl.value DESC LIMIT %START%,%LIMIT%;"; private static final String FETCH_STAT_YEARLY = "SELECT a.name, sl.value FROM accountStatsYearly AS sl INNER JOIN accounts AS a ON a.id=sl.accountId WHERE YEAR(sl.date) = YEAR(CURDATE()) AND sl.statId=%STAT% ORDER BY sl.value DESC LIMIT %START%,%LIMIT%;"; private static final String FETCH_STAT_MONTHLY = "SELECT a.name, sl.value FROM accountStatsMonthly AS sl INNER JOIN accounts AS a ON a.id=sl.accountId WHERE MONTH(sl.date) = MONTH(CURDATE()) AND YEAR(sl.date) = YEAR(CURDATE()) AND sl.statId=%STAT% ORDER BY sl.value DESC LIMIT %START%,%LIMIT%;"; private static final String FETCH_STAT_WEEKLY = "SELECT a.name, sl.value FROM accountStatsWeekly AS sl INNER JOIN accounts AS a ON a.id=sl.accountId WHERE YEARWEEK(sl.date, 1) = YEARWEEK(CURDATE(), 1) AND sl.statId=%STAT% ORDER BY sl.value DESC LIMIT %START%,%LIMIT%;"; private static final String FETCH_STAT_DAILY = "SELECT a.name, sl.value FROM accountStatsDaily AS sl INNER JOIN accounts AS a ON a.id=sl.accountId WHERE sl.date = CURDATE() AND sl.statId=%STAT% ORDER BY sl.value DESC LIMIT %START%,%LIMIT%;"; - private static final String FETCH_STAT_ALL_SEASON = "SELECT a.name, sl.value FROM accountStatsSeasonalTotal AS sl INNER JOIN accounts AS a ON a.id=sl.accountId WHERE sl.statId=%STAT% AND season=" + SEASON + " ORDER BY sl.value DESC LIMIT %START%,%LIMIT%;"; + private static final String FETCH_STAT_ALL_SEASON = "SELECT a.name, sl.value FROM accountStatsSeasonal AS sl INNER JOIN accounts AS a ON a.id=sl.accountId WHERE sl.statId=%STAT% AND seasonId=(SELECT id FROM statSeasons WHERE now() BETWEEN startDate AND endDate LIMIT 1) ORDER BY sl.value DESC LIMIT %START%,%LIMIT%;"; public LeaderboardRepository(JavaPlugin plugin) { @@ -52,9 +50,7 @@ public class LeaderboardRepository extends RepositoryBase try ( Connection c = getConnection(); PreparedStatement updateStat = c.prepareStatement(UPDATE_STAT); - PreparedStatement updateAllStats = c.prepareStatement(UPDATE_STAT_ALL); PreparedStatement insertStat = c.prepareStatement(INSERT_STAT); - PreparedStatement insertAllStats = c.prepareStatement(INSERT_STAT_ALL); ) { for (Integer accountId : stats.keySet()) @@ -65,38 +61,25 @@ public class LeaderboardRepository extends RepositoryBase updateStat.setInt(2, accountId); updateStat.setInt(3, statId); updateStat.addBatch(); - updateAllStats.setLong(1, stats.get(accountId).get(statId)); - updateAllStats.setInt(2, accountId); - updateAllStats.setInt(3, statId); - updateAllStats.addBatch(); } } - int[] rowsAffected1 = updateStat.executeBatch(); - int[] rowsAffected2 = updateAllStats.executeBatch(); + int[] rowsAffected = updateStat.executeBatch(); int i = 0; for (Integer accountId : stats.keySet()) { for (Integer statId : stats.get(accountId).keySet()) { - if (rowsAffected1[i] < 1) + if (rowsAffected[i] < 1) { insertStat.setInt(1, accountId); insertStat.setInt(2, statId); insertStat.setLong(3, stats.get(accountId).get(statId)); insertStat.addBatch(); } - if (rowsAffected2[i] < 1) - { - insertAllStats.setInt(1, accountId); - insertAllStats.setInt(2, statId); - insertAllStats.setLong(3, stats.get(accountId).get(statId)); - insertAllStats.addBatch(); - } i++; } } insertStat.executeBatch(); - insertAllStats.executeBatch(); } catch (SQLException e) { @@ -110,9 +93,7 @@ public class LeaderboardRepository extends RepositoryBase try ( Connection c = getConnection(); PreparedStatement updateStat = c.prepareStatement(UPDATE_STAT); - PreparedStatement updateAllStats = c.prepareStatement(UPDATE_STAT_ALL); PreparedStatement insertStat = c.prepareStatement(INSERT_STAT); - PreparedStatement insertAllStats = c.prepareStatement(INSERT_STAT_ALL); ) { for (Integer statId : stats.keySet()) @@ -121,34 +102,21 @@ public class LeaderboardRepository extends RepositoryBase updateStat.setInt(2, accountId); updateStat.setInt(3, statId); updateStat.addBatch(); - updateAllStats.setLong(1, stats.get(statId)); - updateAllStats.setInt(2, accountId); - updateAllStats.setInt(3, statId); - updateAllStats.addBatch(); } - int[] rowsAffected1 = updateStat.executeBatch(); - int[] rowsAffected2 = updateAllStats.executeBatch(); + int[] rowsAffected = updateStat.executeBatch(); int i = 0; for (Integer statId : stats.keySet()) { - if (rowsAffected1[i] < 1) + if (rowsAffected[i] < 1) { insertStat.setInt(1, accountId); insertStat.setInt(2, statId); insertStat.setLong(3, stats.get(statId)); insertStat.addBatch(); } - if (rowsAffected2[i] < 1) - { - insertAllStats.setInt(1, accountId); - insertAllStats.setInt(2, statId); - insertAllStats.setLong(3, stats.get(statId)); - insertAllStats.addBatch(); - } i++; } insertStat.executeBatch(); - insertAllStats.executeBatch(); } catch (SQLException e) {