Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature mysql #34

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,7 +16,9 @@
import java.util.UUID;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ModelAccount {
// INFORMACOES DA CONTA

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package br.com.battlebits.commons.backend.model;

import br.com.battlebits.commons.account.Blocked;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ModelBlocked {

private UUID uniqueId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ModelPunishmentHistory {
private List<Ban> banHistory;
private List<Mute> muteHistory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
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.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;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;
import java.util.*;

public class SqlStorageDataAccount implements DataAccount {

Expand All @@ -21,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`," +
Expand All @@ -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," +
Expand All @@ -57,12 +69,197 @@ private void setupTables() {

@Override
public ModelAccount getAccount(UUID uuid) {
return null;
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.getLong("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<Ban> 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<Mute> 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<Kick> 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();

/**
* Kicks
*/
PreparedStatement blocks = database.prepareStatement("SELECT * FROM `accounts_blocks` WHERE `uuid`=?");
kicks.setString(1, uuid.toString());
ResultSet rsBlocks = blocks.executeQuery();
Map<String, ModelBlocked> 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();
} catch (SQLException e) {
e.printStackTrace();
}
final ModelAccount newAccount = ModelAccount.builder()
.id(uuid)
.battleCoins(0)
.battleMoney(0)
.xp(0)
.level(0)
.doubleXpMultiplier(1)
.lastActivatedMultiplier(0)
.tag(Tag.DEFAULT)
.lastIpAddress(" ")
.onlineTime(0)
.lastLoggedIn(System.currentTimeMillis())
.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
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -17,4 +21,14 @@ public String getFileName() {
public String tl(Enum<?> tag, Object... format) {
return TranslationCommon.tl(this, tag, format);
}

public static Optional<Language> byId(int id) {
for (Language language : values()) {
if(language.ordinal() == id) {
return Optional.of(language);
}
}
return Optional.empty();
}

}