From 31394b6fb9d9de3e231e8cfa99963a233bdbffda Mon Sep 17 00:00:00 2001 From: DiogoMurano Date: Sun, 6 Oct 2019 00:12:30 -0300 Subject: [PATCH 1/3] Created getAccount method --- .../commons/account/punishment/Ban.java | 2 + .../commons/account/punishment/Kick.java | 4 + .../commons/account/punishment/Mute.java | 2 + .../commons/backend/model/ModelAccount.java | 4 + .../model/ModelAccountConfiguration.java | 2 + .../backend/model/ModelPunishmentHistory.java | 2 + .../backend/sql/SqlStorageDataAccount.java | 126 +++++++++++++++++- .../commons/translate/Language.java | 14 ++ 8 files changed, 154 insertions(+), 2 deletions(-) diff --git a/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Ban.java b/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Ban.java index c5b7448..5045d39 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Ban.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Ban.java @@ -2,12 +2,14 @@ import br.com.battlebits.commons.account.BattleAccount; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import java.util.UUID; @Getter @AllArgsConstructor +@Builder public class Ban { private String bannedBy; diff --git a/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Kick.java b/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Kick.java index 0b1d88f..7cffcc1 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Kick.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Kick.java @@ -1,8 +1,12 @@ package br.com.battlebits.commons.account.punishment; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; @Getter +@AllArgsConstructor +@Builder public class Kick { private String server; private long kickTime; diff --git a/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Mute.java b/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Mute.java index df5b0ab..2afbca5 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Mute.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/account/punishment/Mute.java @@ -2,12 +2,14 @@ import br.com.battlebits.commons.account.BattleAccount; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import java.util.UUID; @AllArgsConstructor @Getter +@Builder public class Mute { private String mutedBy; diff --git a/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelAccount.java b/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelAccount.java index cda4b4f..fe8a01e 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelAccount.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelAccount.java @@ -6,6 +6,8 @@ import br.com.battlebits.commons.account.Tag; import br.com.battlebits.commons.server.ServerType; import br.com.battlebits.commons.translate.Language; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @@ -14,7 +16,9 @@ import java.util.UUID; @Data +@AllArgsConstructor @NoArgsConstructor +@Builder public class ModelAccount { // INFORMACOES DA CONTA diff --git a/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelAccountConfiguration.java b/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelAccountConfiguration.java index 72521c4..8b1b269 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelAccountConfiguration.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelAccountConfiguration.java @@ -1,11 +1,13 @@ package br.com.battlebits.commons.backend.model; import br.com.battlebits.commons.account.AccountConfiguration; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor +@AllArgsConstructor public class ModelAccountConfiguration { private boolean ignoreAll; private boolean tellEnabled; diff --git a/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelPunishmentHistory.java b/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelPunishmentHistory.java index 28c0c2c..7639d47 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelPunishmentHistory.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelPunishmentHistory.java @@ -4,6 +4,7 @@ import br.com.battlebits.commons.account.punishment.Kick; import br.com.battlebits.commons.account.punishment.Mute; import br.com.battlebits.commons.account.punishment.PunishmentHistory; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -11,6 +12,7 @@ @Data @NoArgsConstructor +@AllArgsConstructor public class ModelPunishmentHistory { private List banHistory; private List muteHistory; diff --git a/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java b/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java index 67f0627..76b4ef4 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java @@ -1,11 +1,23 @@ package br.com.battlebits.commons.backend.sql; import br.com.battlebits.commons.account.BattleAccount; +import br.com.battlebits.commons.account.Group; +import br.com.battlebits.commons.account.Tag; +import br.com.battlebits.commons.account.punishment.Ban; +import br.com.battlebits.commons.account.punishment.Kick; +import br.com.battlebits.commons.account.punishment.Mute; import br.com.battlebits.commons.backend.DataAccount; import br.com.battlebits.commons.backend.model.ModelAccount; +import br.com.battlebits.commons.backend.model.ModelAccountConfiguration; +import br.com.battlebits.commons.backend.model.ModelPunishmentHistory; +import br.com.battlebits.commons.translate.Language; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; public class SqlStorageDataAccount implements DataAccount { @@ -39,9 +51,9 @@ private void setupTables() { "(`id`, `uuid`, `bannedIp`)) ENGINE = InnoDB;"); statement.execute("CREATE TABLE IF NOT EXISTS `accounts_punishment_mutes` (`id` INT NOT NULL AUTO_INCREMENT," + " `uuid` VARCHAR(32) NOT NULL, `mutedBy` VARCHAR(16) NOT NULL, `mutedByUniqueId` VARCHAR(32) NOT " + - "NULL, `mutedIp` VARCHAR(32) NOT NULL, `server` VARCHAR(16) NOT NULL, `banTime` BIGINT(32) NOT NULL," + + "NULL, `mutedIp` VARCHAR(32) NOT NULL, `server` VARCHAR(16) NOT NULL, `muteTime` BIGINT(32) NOT NULL," + " `reason` VARCHAR(128) NOT NULL, `unmuted` TINYINT(1) NOT NULL DEFAULT '0', `unmutedBy` VARCHAR(16)" + - " NOT NULL DEFAULT ' ', `unmutedByUniqueId` BIGINT(32) NOT NULL DEFAULT '0', `unbanTime` BIGINT(32)" + + " NOT NULL DEFAULT ' ', `unmutedByUniqueId` BIGINT(32) NOT NULL DEFAULT '0', `unmuteTime` BIGINT(32)" + " NOT NULL DEFAULT '0', `expire` BIGINT(32) NOT NULL, `duration` BIGINT(32) NOT NULL, PRIMARY KEY " + "(`id`, `uuid`, `mutedIp`)) ENGINE = InnoDB;"); statement.execute("CREATE TABLE IF NOT EXISTS `accounts_punishment_kicks` (`id` INT NOT NULL AUTO_INCREMENT," + @@ -57,6 +69,116 @@ private void setupTables() { @Override public ModelAccount getAccount(UUID uuid) { + try (PreparedStatement stmt = database.prepareStatement("SELECT * FROM `accounts` WHERE `uuid`=?")) { + stmt.setString(1, uuid.toString()); + + ResultSet rs = stmt.executeQuery(); + if (rs.next()) { + /** + * Account + */ + final ModelAccount.ModelAccountBuilder builder = ModelAccount.builder() + .name(rs.getString("name")) + .id(uuid) + .battleCoins(rs.getInt("battleCoins")) + .battleMoney(rs.getInt("battleMoney")) + .xp(rs.getInt("xp")) + .level(rs.getInt("level")) + .doubleXpMultiplier(rs.getInt("doubleXpMultiplier")) + .lastActivatedMultiplier(rs.getInt("lastActivatedMultiplier")) + .tag(Tag.byId(rs.getInt("tag")).orElse(Tag.DEFAULT)) + .lastIpAddress(rs.getString("lastIpAddress")) + .onlineTime(rs.getLong("onlineTime")) + .lastLoggedIn(rs.getLong("lastLoggedIn")) + .firstTimePlaying(rs.getLong("firstTimePlaying")) + .group(Group.byId(rs.getInt("group")).orElse(Group.DEFAULT)) + .language(Language.byId(rs.getInt("language")).orElse(Language.PORTUGUESE)); + + /** + * Configuration + */ + PreparedStatement configuration = database.prepareStatement("SELECT * FROM `accounts_configuration` WHERE `uuid`=?"); + configuration.setString(1, uuid.toString()); + ResultSet rsConfiguration = configuration.executeQuery(); + if (rsConfiguration.next()) { + builder.configuration(new ModelAccountConfiguration(rsConfiguration.getBoolean("ignoreAll"), + rsConfiguration.getBoolean("tellEnabled"), rsConfiguration.getBoolean("staffChatEnabled"), + rsConfiguration.getBoolean("partyInvites"))); + } + rsConfiguration.close(); + + /** + * Bans + */ + PreparedStatement bans = database.prepareStatement("SELECT * FROM `accounts_punishment_bans` WHERE `uuid`=?"); + bans.setString(1, uuid.toString()); + ResultSet rsBans = bans.executeQuery(); + List bansList = new ArrayList<>(); + while (rsBans.next()) { + bansList.add(Ban.builder() + .bannedBy(rs.getString("bannedBy")) + .bannedByUniqueId(UUID.fromString(rs.getString("bannedByUniqueId"))) + .bannedIp(rs.getString("bannedIp")) + .server(rs.getString("server")) + .banTime(rs.getLong("banTime")) + .reason(rs.getString("reason")) + .unbanned(rs.getBoolean("unbanned")) + .unbannedBy(rs.getString("unbannedBy")) + .unbannedByUniqueId(UUID.fromString(rs.getString("unbannedByUniqueId"))) + .unbanTime(rs.getLong("unbanTime")) + .expire(rs.getLong("expire")) + .duration(rs.getLong("duration")) + .build()); + } + rsBans.close(); + + /** + * Mutes + */ + PreparedStatement mutes = database.prepareStatement("SELECT * FROM `accounts_punishment_mutes` WHERE `uuid`=?"); + mutes.setString(1, uuid.toString()); + ResultSet rsMutes = mutes.executeQuery(); + List mutesList = new ArrayList<>(); + while (rsMutes.next()) { + mutesList.add(Mute.builder() + .mutedBy(rs.getString("mutedBy")) + .mutedByUniqueId(UUID.fromString(rs.getString("mutedByUniqueId"))) + .mutedIp(rs.getString("mutedIp")) + .server(rs.getString("server")) + .muteTime(rs.getLong("muteTime")) + .reason(rs.getString("reason")) + .unmuted(rs.getBoolean("unmuted")) + .unmutedBy(rs.getString("unmutedBy")) + .unmutedByUniqueId(UUID.fromString(rs.getString("unmutedByUniqueId"))) + .unmuteTime(rs.getLong("unmuteTime")) + .expire(rs.getLong("expire")) + .duration(rs.getLong("duration")) + .build()); + } + rsMutes.close(); + + /** + * Kicks + */ + PreparedStatement kicks = database.prepareStatement("SELECT * FROM `accounts_punishment_kicks` WHERE `uuid`=?"); + kicks.setString(1, uuid.toString()); + ResultSet rsKicks = kicks.executeQuery(); + List kicksList = new ArrayList<>(); + while (rsKicks.next()) { + kicksList.add(Kick.builder() + .server(rs.getString("server")) + .kickTime(rs.getLong("kickTime")) + .reason(rs.getString("reason")) + .build()); + } + rsKicks.close(); + builder.punishmentHistory(new ModelPunishmentHistory(bansList, mutesList, kicksList)); + return builder.build(); + } + rs.close(); + } catch (SQLException e) { + e.printStackTrace(); + } return null; } diff --git a/commons-core/src/main/java/br/com/battlebits/commons/translate/Language.java b/commons-core/src/main/java/br/com/battlebits/commons/translate/Language.java index fa67e5d..be1963e 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/translate/Language.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/translate/Language.java @@ -1,5 +1,9 @@ package br.com.battlebits.commons.translate; +import br.com.battlebits.commons.account.Group; + +import java.util.Optional; + public enum Language { PORTUGUESE("pt_br.properties"); @@ -17,4 +21,14 @@ public String getFileName() { public String tl(Enum tag, Object... format) { return TranslationCommon.tl(this, tag, format); } + + public static Optional byId(int id) { + for (Language language : values()) { + if(language.ordinal() == id) { + return Optional.of(language); + } + } + return Optional.empty(); + } + } From 2e1ff98172ebdafeb8ff9848a4a93cfd0c4701f1 Mon Sep 17 00:00:00 2001 From: DiogoMurano Date: Sun, 6 Oct 2019 00:20:16 -0300 Subject: [PATCH 2/3] Added newAccount in getAccount method --- .../backend/sql/SqlStorageDataAccount.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java b/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java index 76b4ef4..6a3820c 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java @@ -179,7 +179,28 @@ public ModelAccount getAccount(UUID uuid) { } catch (SQLException e) { e.printStackTrace(); } - return null; + final ModelAccount newAccount = ModelAccount.builder() + .id(uuid) + .battleCoins(0) + .battleMoney(0) + .xp(0) + .level(0) + .doubleXpMultiplier(1) + .lastActivatedMultiplier(1) + .tag(Tag.DEFAULT) + .lastIpAddress(" ") + .onlineTime(0) + .lastLoggedIn(System.currentTimeMillis()) + .firstTimePlaying(System.currentTimeMillis()) + .group(Group.DEFAULT) + .language(Language.PORTUGUESE) + .build(); + saveNewAccount(newAccount); + return newAccount; + } + + private void saveNewAccount(ModelAccount account) { + } @Override From dc69f3b633f1db572587515635793c34c726c0ea Mon Sep 17 00:00:00 2001 From: DiogoMurano Date: Sun, 6 Oct 2019 14:00:58 -0300 Subject: [PATCH 3/3] Created saveAccount method --- .../commons/backend/model/ModelBlocked.java | 2 + .../backend/sql/SqlStorageDataAccount.java | 68 +++++++++++++++++-- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelBlocked.java b/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelBlocked.java index 6707019..ec2a607 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelBlocked.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/backend/model/ModelBlocked.java @@ -1,6 +1,7 @@ package br.com.battlebits.commons.backend.model; import br.com.battlebits.commons.account.Blocked; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -8,6 +9,7 @@ @Data @NoArgsConstructor +@AllArgsConstructor public class ModelBlocked { private UUID uniqueId; diff --git a/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java b/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java index 6a3820c..b28418d 100644 --- a/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java +++ b/commons-core/src/main/java/br/com/battlebits/commons/backend/sql/SqlStorageDataAccount.java @@ -6,9 +6,11 @@ import br.com.battlebits.commons.account.punishment.Ban; import br.com.battlebits.commons.account.punishment.Kick; import br.com.battlebits.commons.account.punishment.Mute; +import br.com.battlebits.commons.account.punishment.PunishmentHistory; import br.com.battlebits.commons.backend.DataAccount; import br.com.battlebits.commons.backend.model.ModelAccount; import br.com.battlebits.commons.backend.model.ModelAccountConfiguration; +import br.com.battlebits.commons.backend.model.ModelBlocked; import br.com.battlebits.commons.backend.model.ModelPunishmentHistory; import br.com.battlebits.commons.translate.Language; @@ -16,9 +18,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; +import java.util.*; public class SqlStorageDataAccount implements DataAccount { @@ -33,7 +33,7 @@ private void setupTables() { statement.execute("CREATE TABLE IF NOT EXISTS `accounts` (`id` INT NOT NULL AUTO_INCREMENT, `uuid` " + "VARCHAR(32) NOT NULL, `name` VARCHAR(16) NOT NULL, `battleCoins` INT(8) NOT NULL DEFAULT '0', " + "`battleMoney` INT(8) NOT NULL DEFAULT '0', `xp` INT(8) NOT NULL DEFAULT '0', `level` INT(8) NOT NULL" + - " DEFAULT '0', `doubleXpMultiplier` INT(8) NOT NULL DEFAULT '0', `lastActivatedMultiplier` INT(8) NOT" + + " DEFAULT '0', `doubleXpMultiplier` INT(8) NOT NULL DEFAULT '0', `lastActivatedMultiplier` BIGINT(32) NOT" + " NULL DEFAULT '0', `tag` INT(2) NOT NULL, `lastIpAddress` VARCHAR(32) NOT NULL, `onlineTime` " + "BIGINT(32) NOT NULL DEFAULT '0', `lastLoggedIn` BIGINT(32) NOT NULL, `firstTimePlaying` BIGINT(32) " + "NOT NULL, `group` INT(2) NOT NULL, `language` INT(2) NOT NULL DEFAULT '0', PRIMARY KEY (`id`, `uuid`," + @@ -85,7 +85,7 @@ public ModelAccount getAccount(UUID uuid) { .xp(rs.getInt("xp")) .level(rs.getInt("level")) .doubleXpMultiplier(rs.getInt("doubleXpMultiplier")) - .lastActivatedMultiplier(rs.getInt("lastActivatedMultiplier")) + .lastActivatedMultiplier(rs.getLong("lastActivatedMultiplier")) .tag(Tag.byId(rs.getInt("tag")).orElse(Tag.DEFAULT)) .lastIpAddress(rs.getString("lastIpAddress")) .onlineTime(rs.getLong("onlineTime")) @@ -172,7 +172,22 @@ public ModelAccount getAccount(UUID uuid) { .build()); } rsKicks.close(); + + /** + * Kicks + */ + PreparedStatement blocks = database.prepareStatement("SELECT * FROM `accounts_blocks` WHERE `uuid`=?"); + kicks.setString(1, uuid.toString()); + ResultSet rsBlocks = blocks.executeQuery(); + Map blocksMap = new HashMap<>(); + while (rsBlocks.next()) { + blocksMap.put(rsBlocks.getString("uuidTarget"), new ModelBlocked(UUID.fromString(rsBlocks + .getString("uuidTarget")), rsBlocks.getLong("blockedTime"))); + } + rsBlocks.close(); + builder.punishmentHistory(new ModelPunishmentHistory(bansList, mutesList, kicksList)); + builder.blockedPlayers(blocksMap); return builder.build(); } rs.close(); @@ -186,7 +201,7 @@ public ModelAccount getAccount(UUID uuid) { .xp(0) .level(0) .doubleXpMultiplier(1) - .lastActivatedMultiplier(1) + .lastActivatedMultiplier(0) .tag(Tag.DEFAULT) .lastIpAddress(" ") .onlineTime(0) @@ -194,18 +209,57 @@ public ModelAccount getAccount(UUID uuid) { .firstTimePlaying(System.currentTimeMillis()) .group(Group.DEFAULT) .language(Language.PORTUGUESE) + .punishmentHistory(new ModelPunishmentHistory(new PunishmentHistory())) + .configuration(new ModelAccountConfiguration(true, true, true, true)) + .blockedPlayers(new HashMap<>()) .build(); saveNewAccount(newAccount); return newAccount; } private void saveNewAccount(ModelAccount account) { + try (PreparedStatement accountStmt = database.prepareStatement("INSERT INTO `accounts` (`uuid`, `name`, `tag`, " + + "`lastIpAddress`, `lastLoggedIn`, `firstTimePlaying`, `group`) VALUES (?, ?, ?, ?, ?, ?, ?)")) { + accountStmt.setString(1, account.getId().toString()); + accountStmt.setString(2, account.getName()); + accountStmt.setInt(3, account.getTag().ordinal()); + accountStmt.setString(4, account.getLastIpAddress()); + accountStmt.setLong(5, account.getLastLoggedIn()); + accountStmt.setLong(6, account.getFirstTimePlaying()); + accountStmt.setLong(7, account.getGroup().ordinal()); + accountStmt.execute(); + PreparedStatement configurationStmt = database.prepareStatement("INSERT INTO `accounts_configuration` " + + "(`uuid`) VALUES (?)"); + configurationStmt.setString(1, account.getId().toString()); + configurationStmt.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } } @Override public void saveAccount(BattleAccount account) { - + try (PreparedStatement stmt = database.prepareStatement("UPDATE `accounts` SET `name`=?, `battleCoins`=?, " + + "`battleMoney`=?, `xp`=?, `level`=?, `doubleXpMultiplier`=?, `lastActivatedMultiplier`=?, `tag`=?, " + + "`lastIpAddress`=?, `onlineTime`=?, `lastLoggedIn`=?, `group`=?, `language`=? WHERE `uuid`=?")) { + stmt.setString(1, account.getName()); + stmt.setInt(2, account.getBattleCoins()); + stmt.setInt(3, account.getBattleMoney()); + stmt.setInt(4, account.getXp()); + stmt.setInt(5, account.getLevel()); + stmt.setInt(6, account.getDoubleXpMultiplier()); + stmt.setLong(7, account.getLastActivatedMultiplier()); + stmt.setInt(8, account.getTag().ordinal()); + stmt.setString(9, account.getLastIpAddress()); + stmt.setLong(10, account.getOnlineTime()); + stmt.setLong(11, account.getLastLoggedIn()); + stmt.setInt(12, account.getGroup().ordinal()); + stmt.setInt(13, account.getLanguage().ordinal()); + stmt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } } @Override