diff options
Diffstat (limited to 'src/main/java/dev/tylerm/khs/database')
10 files changed, 937 insertions, 0 deletions
diff --git a/src/main/java/dev/tylerm/khs/database/Database.java b/src/main/java/dev/tylerm/khs/database/Database.java new file mode 100644 index 0000000..42c4798 --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/Database.java @@ -0,0 +1,107 @@ +package dev.tylerm.khs.database; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.UUID; + +import com.google.common.io.ByteStreams; + +import dev.tylerm.khs.Main; +import dev.tylerm.khs.configuration.Config; +import dev.tylerm.khs.database.connections.DatabaseConnection; +import dev.tylerm.khs.database.connections.MySQLConnection; +import dev.tylerm.khs.database.connections.SQLiteConnection; + +public class Database { + + private final GameDataTable playerInfo; + private final NameDataTable nameInfo; + private final InventoryTable inventoryInfo; + private final DatabaseConnection connection; + + public Database(){ + + if(Config.databaseType.equalsIgnoreCase("SQLITE")) { + Main.getInstance().getLogger().info("SQLITE database chosen"); + connection = new SQLiteConnection(); + } else if(Config.databaseType.equalsIgnoreCase("MYSQL")) { + Main.getInstance().getLogger().info("MYSQL database chosen"); + connection = new MySQLConnection(); + } else { + throw new IllegalArgumentException("Invalid database type: " + Config.databaseType); + } + + playerInfo = new GameDataTable(this); + + nameInfo = new NameDataTable(this); + + inventoryInfo = new InventoryTable(this); + + LegacyTable legacyTable = new LegacyTable(this); + if(legacyTable.exists()){ + if(legacyTable.copyData()){ + if(!legacyTable.drop()){ + Main.getInstance().getLogger().severe("Failed to drop old legacy table: player_info. Some data may be duplicated!"); + } + } + } + } + + public GameDataTable getGameData(){ + return playerInfo; + } + + public NameDataTable getNameData() { return nameInfo; } + + public InventoryTable getInventoryData() { return inventoryInfo; } + + protected Connection connect() { + Connection conn = null; + try { + conn = connection.connect(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe(e.getMessage()); + e.printStackTrace(); + } + return conn; + } + + protected byte[] encodeUUID(UUID uuid) { + try { + byte[] bytes = new byte[16]; + ByteBuffer.wrap(bytes) + .putLong(uuid.getMostSignificantBits()) + .putLong(uuid.getLeastSignificantBits()); + InputStream is = new ByteArrayInputStream(bytes); + byte[] result = new byte[is.available()]; + if (is.read(result) == -1) { + Main.getInstance().getLogger().severe("IO Error: Failed to read bytes from input stream"); + return new byte[0]; + } + return result; + } catch (IOException e) { + Main.getInstance().getLogger().severe("IO Error: " + e.getMessage()); + return new byte[0]; + } + } + + @SuppressWarnings("UnstableApiUsage") + protected UUID decodeUUID(byte[] bytes) { + InputStream is = new ByteArrayInputStream(bytes); + ByteBuffer buffer = ByteBuffer.allocate(16); + try { + buffer.put(ByteStreams.toByteArray(is)); + ((Buffer)buffer).flip(); + return new UUID(buffer.getLong(), buffer.getLong()); + } catch (IOException e) { + Main.getInstance().getLogger().severe("IO Error: " + e.getMessage()); + } + return null; + } + +} diff --git a/src/main/java/dev/tylerm/khs/database/GameDataTable.java b/src/main/java/dev/tylerm/khs/database/GameDataTable.java new file mode 100644 index 0000000..ed54baa --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/GameDataTable.java @@ -0,0 +1,221 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2021-2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package dev.tylerm.khs.database; + +import dev.tylerm.khs.Main; +import dev.tylerm.khs.database.util.PlayerInfo; +import dev.tylerm.khs.game.Board; +import dev.tylerm.khs.game.util.WinType; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.sql.*; +import java.util.*; + +public class GameDataTable { + + private final Map<UUID, PlayerInfo> CACHE = new HashMap<>(); + private final Database database; + + protected GameDataTable(Database database) { + + String sql = "CREATE TABLE IF NOT EXISTS hs_data (\n" + + " uuid BINARY(16) PRIMARY KEY,\n" + + " hider_wins int NOT NULL,\n" + + " seeker_wins int NOT NULL,\n" + + " hider_games int NOT NULL,\n" + + " seeker_games int NOT NULL,\n" + + " hider_kills int NOT NULL,\n" + + " seeker_kills int NOT NULL,\n" + + " hider_deaths int NOT NULL,\n" + + " seeker_deaths int NOT NULL\n" + + ");"; + + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + + this.database = database; + } + + @Nullable + public PlayerInfo getInfo(@Nullable UUID uuid) { + if (uuid == null) return null; + if(CACHE.containsKey(uuid)) return CACHE.get(uuid); + String sql = "SELECT * FROM hs_data WHERE uuid = ?;"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, database.encodeUUID(uuid)); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + PlayerInfo info = new PlayerInfo( + uuid, + rs.getInt("hider_wins"), + rs.getInt("seeker_wins"), + rs.getInt("hider_games"), + rs.getInt("seeker_games"), + rs.getInt("hider_kills"), + rs.getInt("seeker_kills"), + rs.getInt("hider_deaths"), + rs.getInt("seeker_deaths") + ); + rs.close(); + CACHE.put(uuid, info); + return info; + } + rs.close(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + return null; + } + + @Nullable + public PlayerInfo getInfoRanking(@NotNull String order, int place) { + String sql = "SELECT * FROM hs_data ORDER BY "+order+" DESC LIMIT 1 OFFSET ?;"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setInt(1, place-1); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + UUID uuid = database.decodeUUID(rs.getBytes("uuid")); + PlayerInfo info = new PlayerInfo( + uuid, + rs.getInt("hider_wins"), + rs.getInt("seeker_wins"), + rs.getInt("hider_games"), + rs.getInt("seeker_games"), + rs.getInt("hider_kills"), + rs.getInt("seeker_kills"), + rs.getInt("hider_deaths"), + rs.getInt("seeker_deaths") + ); + rs.close(); + CACHE.put(uuid, info); + return info; + } + rs.close(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + return null; + } + + @Nullable + public List<PlayerInfo> getInfoPage(int page) { + String sql = "SELECT * FROM hs_data ORDER BY (hider_wins + seeker_wins) DESC LIMIT 10 OFFSET ?;"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setInt(1, (page-1)*10); + ResultSet rs = statement.executeQuery(); + List<PlayerInfo> infoList = new ArrayList<>(); + while(rs.next()) { + PlayerInfo info = new PlayerInfo( + database.decodeUUID(rs.getBytes("uuid")), + rs.getInt("hider_wins"), + rs.getInt("seeker_wins"), + rs.getInt("hider_games"), + rs.getInt("seeker_games"), + rs.getInt("hider_kills"), + rs.getInt("seeker_kills"), + rs.getInt("hider_deaths"), + rs.getInt("seeker_deaths") + ); + infoList.add(info); + } + rs.close(); + return infoList; + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + return null; + } + + @Nullable + public Integer getRanking(@NotNull String order, @NotNull UUID uuid) { + String sql = "SELECT count(*) AS total FROM hs_data WHERE "+order+" >= (SELECT "+order+" FROM hs_data WHERE uuid = ?) AND "+order+" > 0;"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, database.encodeUUID(uuid)); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + return rs.getInt("total"); + } + rs.close(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + return null; + } + + public void addWins( + @NotNull Board board, + @NotNull List<UUID> uuids, + @NotNull List<UUID> winners, + @NotNull Map<UUID,Integer> hider_kills, + @NotNull Map<UUID,Integer> hider_deaths, + @NotNull Map<UUID,Integer> seeker_kills, + @NotNull Map<UUID,Integer> seeker_deaths, + @NotNull WinType type + ) { + for(UUID uuid : uuids) { + PlayerInfo info = getInfo(uuid); + if(info == null){ + info = new PlayerInfo(uuid, 0, 0, 0, 0, 0, 0, 0, 0); + } + updateInfo( + database.encodeUUID(info.getUniqueId()), + info.getHiderWins() + (winners.contains(uuid) && type == WinType.HIDER_WIN ? 1 : 0), + info.getSeekerWins() + (winners.contains(uuid) && type == WinType.SEEKER_WIN ? 1 : 0), + info.getHiderGames() + (board.isHider(uuid) || (board.isSeeker(uuid) && winners.contains(uuid)) ? 1 : 0), + info.getSeekerGames() + (board.isSeeker(uuid) && winners.contains(uuid) ? 1 : 0), + info.getHiderKills() + hider_kills.getOrDefault(uuid, 0), + info.getSeekerKills() + seeker_kills.getOrDefault(uuid, 0), + info.getHiderDeaths() + hider_deaths.getOrDefault(uuid, 0), + info.getSeekerDeaths() + seeker_deaths.getOrDefault(uuid, 0) + ); + } + } + + protected void updateInfo(byte[] uuid, int hider_wins, int seeker_wins, int hider_games, int seeker_games, int hider_kills, int seeker_kills, int hider_deaths, int seeker_deaths){ + String sql = "REPLACE INTO hs_data (uuid, hider_wins, seeker_wins, hider_games, seeker_games, hider_kills, seeker_kills, hider_deaths, seeker_deaths) VALUES (?,?,?,?,?,?,?,?,?)"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, uuid); + statement.setInt(2, hider_wins); + statement.setInt(3, seeker_wins); + statement.setInt(4, hider_games); + statement.setInt(5, seeker_games); + statement.setInt(6, hider_kills); + statement.setInt(7, seeker_kills); + statement.setInt(8, hider_deaths); + statement.setInt(9, seeker_deaths); + statement.execute(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } finally { + CACHE.remove(database.decodeUUID(uuid)); + } + } + +} diff --git a/src/main/java/dev/tylerm/khs/database/InventoryTable.java b/src/main/java/dev/tylerm/khs/database/InventoryTable.java new file mode 100644 index 0000000..3fc62fb --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/InventoryTable.java @@ -0,0 +1,103 @@ +package dev.tylerm.khs.database; + +import dev.tylerm.khs.Main; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.io.BukkitObjectInputStream; +import org.bukkit.util.io.BukkitObjectOutputStream; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.sql.*; +import java.util.UUID; + +public class InventoryTable { + + private final Database database; + + protected InventoryTable(Database database) { + + String sql = "CREATE TABLE IF NOT EXISTS hs_inventory (\n" + + " uuid BINARY(16) NOT NULL,\n" + + " inventory TEXT NOT NULL,\n" + + " PRIMARY KEY (uuid)\n" + + ");"; + + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + + this.database = database; + } + + @Nullable + public ItemStack[] getInventory(@NotNull UUID uuid) { + String sql = "SELECT * FROM hs_inventory WHERE uuid = ?;"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, database.encodeUUID(uuid)); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + String data = rs.getString("inventory"); + if(data == null) return null; + return itemStackArrayFromBase64(data); + } + rs.close(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + Main.getInstance().getLogger().severe("IO Error: " + e.getMessage()); + e.printStackTrace(); + } + return null; + } + + public void saveInventory(@NotNull UUID uuid, @NotNull ItemStack[] itemArray) { + String sql = "REPLACE INTO hs_inventory (uuid, inventory) VALUES (?,?)"; + String data = itemStackArrayToBase64(itemArray); + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, database.encodeUUID(uuid)); + statement.setString(2, data); + statement.execute(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + } + + private String itemStackArrayToBase64(ItemStack[] itemArray) throws IllegalStateException { + try { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream); + + dataOutput.writeObject(itemArray); + + dataOutput.close(); + + return Base64Coder.encodeLines(outputStream.toByteArray()); + } catch (Exception e) { + throw new IllegalStateException("Error whilst saving items, Please contact the developer", e); + } + } + + private ItemStack[] itemStackArrayFromBase64(String data) throws IOException { + try { + ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); + BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); + + ItemStack[] itemArray = (ItemStack[]) dataInput.readObject(); + + dataInput.close(); + return itemArray; + } catch (ClassNotFoundException e) { + throw new IOException("Error whilst loading items, Please contact the developer", e); + } + } + +} diff --git a/src/main/java/dev/tylerm/khs/database/LegacyTable.java b/src/main/java/dev/tylerm/khs/database/LegacyTable.java new file mode 100644 index 0000000..7bf079c --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/LegacyTable.java @@ -0,0 +1,101 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package dev.tylerm.khs.database; + +import dev.tylerm.khs.database.util.LegacyPlayerInfo; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +public class LegacyTable { + + private final Database database; + private final boolean exists; + + protected LegacyTable(Database database) { + + String sql = "SELECT * FROM player_info LIMIT 1;"; + + boolean check; + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + ResultSet resultSet = statement.executeQuery(sql); + check = resultSet.next(); + } catch (SQLException e) { + check = false; + } + + this.exists = check; + this.database = database; + } + + public boolean exists(){ + return exists; + } + + public boolean copyData(){ + String sql = "SELECT * FROM player_info;"; + List<LegacyPlayerInfo> legacyPlayerInfoList = new ArrayList<>(); + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + ResultSet resultSet = statement.executeQuery(sql); + while(resultSet.next()){ + legacyPlayerInfoList.add(new LegacyPlayerInfo( + resultSet.getBytes("uuid"), + resultSet.getInt("hider_wins"), + resultSet.getInt("seeker_wins"), + resultSet.getInt("games_played") + )); + } + resultSet.close(); + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + for(LegacyPlayerInfo legacyInfo : legacyPlayerInfoList){ + database.getGameData().updateInfo( + legacyInfo.getUniqueId(), + legacyInfo.getHiderWins(), + legacyInfo.getSeekerWins(), + legacyInfo.getGamesPlayer() - legacyInfo.getSeekerWins(), + legacyInfo.getSeekerWins(), + 0, + 0, + 0, + 0 + ); + } + return true; + } + + public boolean drop(){ + String sql = "DROP table player_info"; + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + statement.execute(sql); + return true; + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + +} diff --git a/src/main/java/dev/tylerm/khs/database/NameDataTable.java b/src/main/java/dev/tylerm/khs/database/NameDataTable.java new file mode 100644 index 0000000..1ce7143 --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/NameDataTable.java @@ -0,0 +1,113 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package dev.tylerm.khs.database; + +import dev.tylerm.khs.Main; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.sql.*; +import java.util.UUID; + +public class NameDataTable { + + private final Database database; + + protected NameDataTable(Database database) { + + String sql = "CREATE TABLE IF NOT EXISTS hs_names (\n" + + " uuid BINARY(16) NOT NULL,\n" + + " name VARCHAR(48) NOT NULL,\n" + + " PRIMARY KEY (uuid,name)\n" + + ");"; + + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + + this.database = database; + } + + @Nullable + public String getName(@NotNull UUID uuid) { + String sql = "SELECT * FROM hs_names WHERE uuid = ?;"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, database.encodeUUID(uuid)); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + return rs.getString("name"); + } + rs.close(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + OfflinePlayer retry = Bukkit.getOfflinePlayer(uuid); + if(retry != null && retry.getName() != null){ + this.update(uuid, retry.getName()); + return retry.getName(); + } + return null; + } + + @SuppressWarnings("deprecation") + @Nullable + public UUID getUUID(@NotNull String name) { + String sql = "SELECT * FROM hs_names WHERE name = ?;"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setString(1, name); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + return database.decodeUUID(rs.getBytes("uuid")); + } + rs.close(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + OfflinePlayer retry = Bukkit.getOfflinePlayer(name); + if(retry != null){ + this.update(retry.getUniqueId(), name); + return retry.getUniqueId(); + } + return null; + } + + public boolean update(@NotNull UUID uuid, @NotNull String name){ + String sql = "REPLACE INTO hs_names (uuid, name) VALUES (?,?)"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, database.encodeUUID(uuid)); + statement.setString(2, name); + statement.execute(); + statement.close(); + return true; + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + return false; + } + } + +} diff --git a/src/main/java/dev/tylerm/khs/database/connections/DatabaseConnection.java b/src/main/java/dev/tylerm/khs/database/connections/DatabaseConnection.java new file mode 100644 index 0000000..7dd8c01 --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/connections/DatabaseConnection.java @@ -0,0 +1,29 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package dev.tylerm.khs.database.connections; + +import java.sql.Connection; +import java.sql.SQLException; + +public interface DatabaseConnection { + + Connection connect() throws SQLException; + +} diff --git a/src/main/java/dev/tylerm/khs/database/connections/MySQLConnection.java b/src/main/java/dev/tylerm/khs/database/connections/MySQLConnection.java new file mode 100644 index 0000000..0f7ce30 --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/connections/MySQLConnection.java @@ -0,0 +1,65 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package dev.tylerm.khs.database.connections; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import dev.tylerm.khs.Main; +import dev.tylerm.khs.configuration.Config; + +import java.sql.Connection; +import java.sql.SQLException; + +public class MySQLConnection implements DatabaseConnection { + + private final HikariDataSource ds; + + public MySQLConnection(){ + + HikariConfig config = new HikariConfig(); + + Main.getInstance().getLogger().info("Database host: " + Config.databaseHost); + Main.getInstance().getLogger().info("Database port: " + Config.databasePort); + Main.getInstance().getLogger().info("Database user: " + Config.databaseUser); + Main.getInstance().getLogger().info("Database pass: xxxxxxxxxxx"); + Main.getInstance().getLogger().info("Database name: " + Config.databaseName); + + + config.setDriverClassName(org.mariadb.jdbc.Driver.class.getName()); + config.setJdbcUrl("jdbc:mariadb://"+ Config.databaseHost+":"+ Config.databasePort+"/"+ Config.databaseName.trim()); + config.addDataSourceProperty("cachePrepStmts", "true"); + config.addDataSourceProperty("prepStmtCacheSize", "250"); + config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); + config.addDataSourceProperty("user", Config.databaseUser); + config.addDataSourceProperty("password", Config.databasePass); + config.addDataSourceProperty("autoCommit", "true"); + config.setAutoCommit(true); + config.setMaximumPoolSize(20); + + ds = new HikariDataSource(config); + + } + + @Override + public Connection connect() throws SQLException { + return ds.getConnection(); + } + +} diff --git a/src/main/java/dev/tylerm/khs/database/connections/SQLiteConnection.java b/src/main/java/dev/tylerm/khs/database/connections/SQLiteConnection.java new file mode 100644 index 0000000..70a31fd --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/connections/SQLiteConnection.java @@ -0,0 +1,64 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package dev.tylerm.khs.database.connections; + +import dev.tylerm.khs.Main; +import org.sqlite.SQLiteConfig; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class SQLiteConnection implements DatabaseConnection { + + private final File databaseFile; + private final SQLiteConfig config; + + public SQLiteConnection(){ + + try { + Class.forName("org.sqlite.JDBC"); + } catch (ClassNotFoundException e) { + Main.getInstance().getLogger().severe(e.getMessage()); + throw new RuntimeException(e.getMessage()); + } + + databaseFile = new File(Main.getInstance().getDataFolder(), "database.db"); + + config = new SQLiteConfig(); + config.setSynchronous(SQLiteConfig.SynchronousMode.NORMAL); + config.setTempStore(SQLiteConfig.TempStore.MEMORY); + } + + @Override + public Connection connect() { + Connection conn = null; + try { + String url = "jdbc:sqlite:"+databaseFile.getPath(); + conn = DriverManager.getConnection(url, config.toProperties()); + } catch (SQLException e) { + Main.getInstance().getLogger().severe(e.getMessage()); + e.printStackTrace(); + } + return conn; + } + +} diff --git a/src/main/java/dev/tylerm/khs/database/util/LegacyPlayerInfo.java b/src/main/java/dev/tylerm/khs/database/util/LegacyPlayerInfo.java new file mode 100644 index 0000000..bbf6e55 --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/util/LegacyPlayerInfo.java @@ -0,0 +1,50 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2021-2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package dev.tylerm.khs.database.util; + +public class LegacyPlayerInfo { + + private final byte[] uniqueId; + private final int hiderWins; + private final int seekerWins; + private final int gamesPlayed; + + public LegacyPlayerInfo(byte[] uniqueId, int hiderWins, int seekerWins, int gamesPlayed) { + this.uniqueId = uniqueId; + this.hiderWins = hiderWins; + this.seekerWins = seekerWins; + this.gamesPlayed = gamesPlayed; + } + + public byte[] getUniqueId() { + return uniqueId; + } + + public int getHiderWins() { + return hiderWins; + } + + public int getSeekerWins() { + return seekerWins; + } + + public int getGamesPlayer() { return gamesPlayed; } + +} diff --git a/src/main/java/dev/tylerm/khs/database/util/PlayerInfo.java b/src/main/java/dev/tylerm/khs/database/util/PlayerInfo.java new file mode 100644 index 0000000..555a954 --- /dev/null +++ b/src/main/java/dev/tylerm/khs/database/util/PlayerInfo.java @@ -0,0 +1,84 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2021-2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package dev.tylerm.khs.database.util; + +import java.util.UUID; + +public class PlayerInfo { + + private final UUID uniqueId; + private final int hiderWins; + private final int seekerWins; + private final int hiderGames; + private final int seekerGames; + private final int hiderKills; + private final int seekerKills; + private final int hiderDeaths; + private final int seekerDeaths; + + public PlayerInfo(UUID uniqueId, int hiderWins, int seekerWins, int hiderGames, int seekerGames, int hiderKills, int seekerKills, int hiderDeaths, int seekerDeaths) { + this.uniqueId = uniqueId; + this.hiderWins = hiderWins; + this.seekerWins = seekerWins; + this.hiderGames = hiderGames; + this.seekerGames = seekerGames; + this.hiderKills = hiderKills; + this.seekerKills = seekerKills; + this.hiderDeaths = hiderDeaths; + this.seekerDeaths = seekerDeaths; + } + + public UUID getUniqueId() { + return uniqueId; + } + + public int getHiderWins() { + return hiderWins; + } + + public int getSeekerWins() { + return seekerWins; + } + + public int getHiderGames() { + return hiderGames; + } + + public int getSeekerGames() { + return seekerGames; + } + + public int getHiderKills() { + return hiderKills; + } + + public int getSeekerKills() { + return seekerKills; + } + + public int getHiderDeaths() { + return hiderDeaths; + } + + public int getSeekerDeaths() { + return seekerDeaths; + } + +} |