summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/net/tylermurphy/hideAndSeek/database')
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/Database.java99
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java219
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/LegacyTable.java104
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java112
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java136
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/connections/DatabaseConnection.java (renamed from src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java)20
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java57
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/connections/SQLiteConnection.java64
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/util/LegacyPlayerInfo.java56
-rw-r--r--src/main/java/net/tylermurphy/hideAndSeek/database/util/PlayerInfo.java84
10 files changed, 764 insertions, 187 deletions
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java b/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java
index 8001368..0489b5d 100644
--- a/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java
+++ b/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java
@@ -21,70 +21,95 @@ package net.tylermurphy.hideAndSeek.database;
import com.google.common.io.ByteStreams;
import net.tylermurphy.hideAndSeek.Main;
-import org.sqlite.SQLiteConfig;
-import sun.font.ScriptRun;
+import net.tylermurphy.hideAndSeek.database.connections.DatabaseConnection;
+import net.tylermurphy.hideAndSeek.database.connections.MySQLConnection;
+import net.tylermurphy.hideAndSeek.database.connections.SQLiteConnection;
import java.io.ByteArrayInputStream;
-import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.sql.Connection;
-import java.sql.DriverManager;
import java.sql.SQLException;
-
-import java.sql.Statement;
import java.util.UUID;
+import static net.tylermurphy.hideAndSeek.configuration.Config.databaseType;
+
public class Database {
- private static final File databaseFile = new File(Main.data, "database.db");
+ private final GameDataTable playerInfo;
+ private final NameDataTable nameInfo;
+ private final DatabaseConnection connection;
+
+ public Database(){
+
+ if(databaseType.equals("SQLITE")) {
+ connection = new SQLiteConnection();
+ } else {
+ connection = new MySQLConnection();
+ }
+
+ playerInfo = new GameDataTable(this);
- public static PlayerInfoTable playerInfo;
- private static SQLiteConfig config;
+ nameInfo = new NameDataTable(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;
+ }
- protected static Connection connect() {
+ public NameDataTable getNameData() { return nameInfo; }
+
+ protected Connection connect() {
Connection conn = null;
try {
- String url = "jdbc:sqlite:"+databaseFile;
- conn = DriverManager.getConnection(url, config.toProperties());
+ conn = connection.connect();
} catch (SQLException e) {
- Main.plugin.getLogger().severe(e.getMessage());
+ Main.getInstance().getLogger().severe(e.getMessage());
+ e.printStackTrace();
}
return conn;
}
- protected static InputStream convertUniqueId(UUID uuid) {
- byte[] bytes = new byte[16];
- ByteBuffer.wrap(bytes)
- .putLong(uuid.getMostSignificantBits())
- .putLong(uuid.getLeastSignificantBits());
- return new ByteArrayInputStream(bytes);
+ 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];
+ }
}
- protected static UUID convertBinaryStream(InputStream stream) {
+ protected UUID decodeUUID(byte[] bytes) {
+ InputStream is = new ByteArrayInputStream(bytes);
ByteBuffer buffer = ByteBuffer.allocate(16);
try {
- buffer.put(ByteStreams.toByteArray(stream));
+ buffer.put(ByteStreams.toByteArray(is));
buffer.flip();
return new UUID(buffer.getLong(), buffer.getLong());
- } catch (IOException ignored) {}
- return null;
- }
-
- public static void init(){
- try {
- Class.forName("org.sqlite.JDBC");
- } catch (ClassNotFoundException e) {
- Main.plugin.getLogger().severe(e.getMessage());
- throw new RuntimeException(e.getMessage());
+ } catch (IOException e) {
+ Main.getInstance().getLogger().severe("IO Error: " + e.getMessage());
}
-
- config = new SQLiteConfig();
- config.setSynchronous(SQLiteConfig.SynchronousMode.NORMAL);
- config.setTempStore(SQLiteConfig.TempStore.MEMORY);
-
- playerInfo = new PlayerInfoTable();
+ return null;
}
}
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java
new file mode 100644
index 0000000..ce392a9
--- /dev/null
+++ b/src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java
@@ -0,0 +1,219 @@
+/*
+ * 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 net.tylermurphy.hideAndSeek.database;
+
+import net.tylermurphy.hideAndSeek.Main;
+import net.tylermurphy.hideAndSeek.database.util.PlayerInfo;
+import net.tylermurphy.hideAndSeek.game.Board;
+import net.tylermurphy.hideAndSeek.game.util.WinType;
+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();
+ connection.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(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();
+ connection.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();
+ connection.close();
+ return infoList;
+ } catch (SQLException e) {
+ Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage());
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ @Nullable
+ public Integer getRanking(String order, 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(Board board, List<UUID> uuids, List<UUID> winners, Map<String,Integer> hider_kills, Map<String,Integer> hider_deaths, Map<String,Integer> seeker_kills, Map<String,Integer> seeker_deaths, 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) && !board.getFirstSeeker().getUniqueId().equals(uuid)) ? 1 : 0),
+ info.getSeekerGames() + (board.getFirstSeeker().getUniqueId().equals(uuid) ? 1 : 0),
+ info.getHiderKills() + hider_kills.getOrDefault(uuid.toString(), 0),
+ info.getSeekerKills() + seeker_kills.getOrDefault(uuid.toString(), 0),
+ info.getHiderDeaths() + hider_deaths.getOrDefault(uuid.toString(), 0),
+ info.getSeekerDeaths() + seeker_deaths.getOrDefault(uuid.toString(), 0)
+ );
+ }
+ }
+
+ protected boolean 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){
+ boolean success;
+ String sql = "INSERT OR 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();
+ statement.close();
+ success = true;
+ } catch (SQLException e) {
+ Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage());
+ e.printStackTrace();
+ success = false;
+ } finally {
+ CACHE.remove(uuid);
+ }
+ return success;
+ }
+
+}
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/LegacyTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/LegacyTable.java
new file mode 100644
index 0000000..580c2f5
--- /dev/null
+++ b/src/main/java/net/tylermurphy/hideAndSeek/database/LegacyTable.java
@@ -0,0 +1,104 @@
+/*
+ * 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 net.tylermurphy.hideAndSeek.database;
+
+import net.tylermurphy.hideAndSeek.Main;
+import net.tylermurphy.hideAndSeek.database.Database;
+import net.tylermurphy.hideAndSeek.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("wins"),
+ 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/net/tylermurphy/hideAndSeek/database/NameDataTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java
new file mode 100644
index 0000000..e5ac4bb
--- /dev/null
+++ b/src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java
@@ -0,0 +1,112 @@
+/*
+ * 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 net.tylermurphy.hideAndSeek.database;
+
+import net.tylermurphy.hideAndSeek.Main;
+import org.bukkit.Bukkit;
+import org.bukkit.OfflinePlayer;
+import org.jetbrains.annotations.Nullable;
+
+import java.lang.management.BufferPoolMXBean;
+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(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){
+ this.update(uuid, retry.getName());
+ return retry.getName();
+ }
+ return null;
+ }
+
+ @Nullable
+ public UUID getUUID(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(UUID uuid, String name){
+ String sql = "INSERT OR 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/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java
deleted file mode 100644
index 7b9b476..0000000
--- a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java
+++ /dev/null
@@ -1,136 +0,0 @@
-/*
- * This file is part of Kenshins Hide and Seek
- *
- * Copyright (c) 2021 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 net.tylermurphy.hideAndSeek.database;
-
-import net.tylermurphy.hideAndSeek.Main;
-import net.tylermurphy.hideAndSeek.configuration.Config;
-import net.tylermurphy.hideAndSeek.util.WinType;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.sql.*;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
-
-public class PlayerInfoTable {
-
- protected PlayerInfoTable(){
-
- String sql = "CREATE TABLE IF NOT EXISTS player_info (\n"
- + " uuid BINARY(16) PRIMARY KEY,\n"
- + " wins int NOT NULL,\n"
- + " seeker_wins int NOT NULL,\n"
- + " hider_wins int NOT NULL,\n"
- + " games_played int NOT NULL\n"
- + ");";
-
- try(Connection connection = Database.connect(); Statement statement = connection.createStatement()){
- statement.executeUpdate(sql);
- } catch (SQLException e){
- Main.plugin.getLogger().severe("SQL Error: " + e.getMessage());
- }
- }
-
- public PlayerInfo getInfo(UUID uuid){
- String sql = "SELECT * FROM player_info WHERE uuid = ?;";
- try(Connection connection = Database.connect(); PreparedStatement statement = connection.prepareStatement(sql)){
- InputStream is = Database.convertUniqueId(uuid);
- byte[] bytes = new byte[is.available()];
- if(is.read(bytes) == -1){
- throw new IOException("Failed to read bytes from input stream");
- }
- statement.setBytes(1, bytes);
- ResultSet rs = statement.executeQuery();
- if(rs.next()){
- rs.close();
- connection.close();
- return new PlayerInfo(
- uuid,
- rs.getInt("wins"),
- rs.getInt("seeker_wins"),
- rs.getInt("hider_wins"),
- rs.getInt("games_played")
- );
- }
- rs.close();
- } catch (SQLException e){
- Main.plugin.getLogger().severe("SQL Error: " + e.getMessage());
- } catch (IOException e) {
- Main.plugin.getLogger().severe("IO Error: " + e.getMessage());
- e.printStackTrace();
- }
- return new PlayerInfo(uuid, 0, 0, 0, 0);
- }
-
- public List<PlayerInfo> getInfoPage(int page){
- String sql = "SELECT * FROM player_info ORDER BY 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.convertBinaryStream(new ByteArrayInputStream(rs.getBytes("uuid"))),
- rs.getInt("wins"),
- rs.getInt("seeker_wins"),
- rs.getInt("hider_wins"),
- rs.getInt("games_played")
- );
- infoList.add(info);
- }
- rs.close();
- connection.close();
- return infoList;
- } catch (SQLException e){
- Main.plugin.getLogger().severe("SQL Error: " + e.getMessage());
- }
- return null;
- }
-
- public void addWins(List<UUID> uuids, List<UUID> winners, WinType type){
- for(UUID uuid : uuids){
- String sql = "INSERT OR REPLACE INTO player_info (uuid, wins, seeker_wins, hider_wins, games_played) VALUES (?,?,?,?,?)";
- PlayerInfo info = getInfo(uuid);
- try(Connection connection = Database.connect(); PreparedStatement statement = connection.prepareStatement(sql)){
- InputStream is = Database.convertUniqueId(uuid);
- byte[] bytes = new byte[is.available()];
- if(is.read(bytes) == -1){
- throw new IOException("Failed to read bytes from input stream");
- }
- statement.setBytes(1, bytes);
- statement.setInt(2, info.wins + (winners.contains(uuid) ? 1 : 0));
- statement.setInt(3, info.seeker_wins + (winners.contains(uuid) && type == WinType.SEEKER_WIN ? 1 : 0));
- statement.setInt(4, info.hider_wins + (winners.contains(uuid) && type == WinType.HIDER_WIN ? 1 : 0));
- statement.setInt(5, info.games_played + 1);
- statement.execute();
- } catch (SQLException e){
- Main.plugin.getLogger().severe("SQL Error: " + e.getMessage());
- e.printStackTrace();
- return;
- } catch (IOException e) {
- Main.plugin.getLogger().severe("IO Error: " + e.getMessage());
- e.printStackTrace();
- }
- }
- }
-
-}
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/DatabaseConnection.java
index 8464b54..466bda5 100644
--- a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java
+++ b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/DatabaseConnection.java
@@ -1,7 +1,7 @@
/*
* This file is part of Kenshins Hide and Seek
*
- * Copyright (c) 2021 Tyler Murphy.
+ * 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
@@ -17,21 +17,13 @@
*
*/
-package net.tylermurphy.hideAndSeek.database;
+package net.tylermurphy.hideAndSeek.database.connections;
-import java.util.UUID;
+import java.sql.Connection;
+import java.sql.SQLException;
-public class PlayerInfo {
+public interface DatabaseConnection {
- public UUID uuid;
- public int wins, hider_wins, seeker_wins, games_played;
-
- public PlayerInfo(UUID uuid, int wins, int hider_wins, int seeker_wins, int games_played){
- this.uuid = uuid;
- this.wins = wins;
- this.hider_wins = hider_wins;
- this.seeker_wins = seeker_wins;
- this.games_played = games_played;
- }
+ Connection connect() throws SQLException;
}
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java
new file mode 100644
index 0000000..b7c1b1d
--- /dev/null
+++ b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java
@@ -0,0 +1,57 @@
+/*
+ * 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 net.tylermurphy.hideAndSeek.database.connections;
+
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import static net.tylermurphy.hideAndSeek.configuration.Config.*;
+
+public class MySQLConnection implements DatabaseConnection {
+
+ private final HikariDataSource ds;
+
+ public MySQLConnection(){
+
+ HikariConfig config = new HikariConfig();
+
+ config.setJdbcUrl("jdbc:mariadb://"+databaseHost+":"+databasePort+"/"+databaseName);
+ config.addDataSourceProperty("cachePrepStmts", "true");
+ config.addDataSourceProperty("prepStmtCacheSize", "250");
+ config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
+ config.addDataSourceProperty("user", databaseUser);
+ config.addDataSourceProperty("password",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/net/tylermurphy/hideAndSeek/database/connections/SQLiteConnection.java b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/SQLiteConnection.java
new file mode 100644
index 0000000..1a03106
--- /dev/null
+++ b/src/main/java/net/tylermurphy/hideAndSeek/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 net.tylermurphy.hideAndSeek.database.connections;
+
+import net.tylermurphy.hideAndSeek.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;
+ 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/net/tylermurphy/hideAndSeek/database/util/LegacyPlayerInfo.java b/src/main/java/net/tylermurphy/hideAndSeek/database/util/LegacyPlayerInfo.java
new file mode 100644
index 0000000..5b59779
--- /dev/null
+++ b/src/main/java/net/tylermurphy/hideAndSeek/database/util/LegacyPlayerInfo.java
@@ -0,0 +1,56 @@
+/*
+ * 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 net.tylermurphy.hideAndSeek.database.util;
+
+import java.util.UUID;
+
+public class LegacyPlayerInfo {
+
+ private final byte[] uniqueId;
+ private final int totalWins;
+ private final int hiderWins;
+ private final int seekerWins;
+ private final int gamesPlayed;
+
+ public LegacyPlayerInfo(byte[] uniqueId, int totalWins, int hiderWins, int seekerWins, int gamesPlayed) {
+ this.uniqueId = uniqueId;
+ this.totalWins = totalWins;
+ this.hiderWins = hiderWins;
+ this.seekerWins = seekerWins;
+ this.gamesPlayed = gamesPlayed;
+ }
+
+ public byte[] getUniqueId() {
+ return uniqueId;
+ }
+
+ public int getTotalWins() { return totalWins; }
+
+ public int getHiderWins() {
+ return hiderWins;
+ }
+
+ public int getSeekerWins() {
+ return seekerWins;
+ }
+
+ public int getGamesPlayer() { return gamesPlayed; }
+
+}
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/util/PlayerInfo.java b/src/main/java/net/tylermurphy/hideAndSeek/database/util/PlayerInfo.java
new file mode 100644
index 0000000..96fe9d8
--- /dev/null
+++ b/src/main/java/net/tylermurphy/hideAndSeek/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 net.tylermurphy.hideAndSeek.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;
+ }
+
+}