From 7aa63a65b6586f32bc80b30c812ec4beb9c92509 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Mon, 14 Aug 2023 13:40:27 -0400 Subject: [PATCH] Petition db update including table creation. --- src/engine/db/handlers/dbPetitionHandler.java | 77 +++++++++++++++++++ src/engine/gameManager/DbManager.java | 1 + .../handlers/PetitionReceivedMsgHandler.java | 7 +- src/engine/objects/Petition.java | 46 +---------- src/engine/server/world/WorldServer.java | 3 + 5 files changed, 89 insertions(+), 45 deletions(-) create mode 100644 src/engine/db/handlers/dbPetitionHandler.java diff --git a/src/engine/db/handlers/dbPetitionHandler.java b/src/engine/db/handlers/dbPetitionHandler.java new file mode 100644 index 00000000..847984c0 --- /dev/null +++ b/src/engine/db/handlers/dbPetitionHandler.java @@ -0,0 +1,77 @@ +// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . +// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· +// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ +// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ +// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ +// Magicbane Emulator Project © 2013 - 2022 +// www.magicbane.com + + +package engine.db.handlers; + +import engine.Enum; +import engine.gameManager.DbManager; +import engine.objects.Petition; +import org.pmw.tinylog.Logger; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +public class dbPetitionHandler extends dbHandlerBase { + + public dbPetitionHandler() { + + } + + public void CREATE_PETITION_TABLE() { + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SET sql_notes = 0; CREATE TABLE IF NOT EXISTS dyn_petition (\n" + + " petitionNumber INT AUTO_INCREMENT NOT NULL,\n" + + " petitionTime DATETIME,\n" + + " primaryType VARCHAR(50),\n" + + " subType VARCHAR(50),\n" + + " message VARCHAR(255),\n" + + " accountID INT,\n" + + " account VARCHAR(255),\n" + + " characterID INT,\n" + + " `character` VARCHAR(255),\n" + + " location VARCHAR(255),\n" + + " PRIMARY KEY (petitionNumber)\n" + + ") ENGINE = innodb ROW_FORMAT = DEFAULT; SET sql_notes = 1;")) { + + preparedStatement.executeQuery(); + + } catch (SQLException e) { + Logger.error(e); + } + } + + public void WRITE_PETITION_TO_TABLE(Petition petition) { + + try (Connection connection = DbManager.getConnection(); + + //check that table exists + + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_petition` (`petitionTime`, `primaryType`, `subType`, `accountID`, `account`, `characterID`, `character`, `location`, `message`)" + + " VALUES (?,?,?,?,?,?,?,?,?);")) { + + preparedStatement.setTimestamp(1, new java.sql.Timestamp(System.currentTimeMillis())); + preparedStatement.setString(2, Enum.PetitionType.values()[petition.primaryType].name()); + preparedStatement.setString(3, Enum.PetitionSubType.values()[petition.subType].name()); + preparedStatement.setInt(4, petition.reportAccount.getObjectUUID()); + preparedStatement.setString(5, petition.reportAccount.getUname()); + preparedStatement.setInt(6, petition.reportPlayer.getObjectUUID()); + preparedStatement.setString(7, petition.reportPlayer.getFirstName()); + preparedStatement.setString(8, petition.playerLocation.toString()); + preparedStatement.setString(9, petition.message); + + preparedStatement.executeUpdate(); + + } catch (SQLException e) { + Logger.error("CREATE PETITION FAILED: " + e); + } + } + +} diff --git a/src/engine/gameManager/DbManager.java b/src/engine/gameManager/DbManager.java index 74428356..92917a35 100644 --- a/src/engine/gameManager/DbManager.java +++ b/src/engine/gameManager/DbManager.java @@ -74,6 +74,7 @@ public enum DbManager { public static final dbRunegateHandler RunegateQueries = new dbRunegateHandler(); public static final dbPowerHandler PowerQueries = new dbPowerHandler(); + public static final dbPetitionHandler PetitionQueries = new dbPetitionHandler(); private static final EnumMap> objectCache = new EnumMap<>(GameObjectType.class); public static Hasher hasher; private static HikariDataSource connectionPool = null; diff --git a/src/engine/net/client/handlers/PetitionReceivedMsgHandler.java b/src/engine/net/client/handlers/PetitionReceivedMsgHandler.java index 43ee0bc9..9e7897ee 100644 --- a/src/engine/net/client/handlers/PetitionReceivedMsgHandler.java +++ b/src/engine/net/client/handlers/PetitionReceivedMsgHandler.java @@ -1,6 +1,7 @@ package engine.net.client.handlers; import engine.exception.MsgSendException; +import engine.gameManager.DbManager; import engine.net.client.ClientConnection; import engine.net.client.msg.ClientNetMsg; import engine.net.client.msg.PetitionReceivedMsg; @@ -20,13 +21,13 @@ public class PetitionReceivedMsgHandler extends AbstractClientMsgHandler { if (origin == null) return false; - Petition report = new Petition(msg, origin); + Petition petition = new Petition(msg, origin); - if (report == null) + if (petition == null) return false; try { - report.updateDatabase(); + DbManager.PetitionQueries.WRITE_PETITION_TO_TABLE(petition); } catch (Exception e) { return false; } diff --git a/src/engine/objects/Petition.java b/src/engine/objects/Petition.java index 1e43c1c8..3398c2b1 100644 --- a/src/engine/objects/Petition.java +++ b/src/engine/objects/Petition.java @@ -6,22 +6,13 @@ // Magicbane Emulator Project © 2013 - 2022 // www.magicbane.com - package engine.objects; - -import engine.Enum; -import engine.gameManager.DbManager; import engine.math.Vector3fImmutable; import engine.net.client.ClientConnection; import engine.net.client.msg.ClientNetMsg; import engine.net.client.msg.PetitionReceivedMsg; -import org.pmw.tinylog.Logger; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -public class Petition extends AbstractGameObject { +public class Petition { public int primaryType; public int subType; public Account reportAccount; @@ -29,42 +20,13 @@ public class Petition extends AbstractGameObject { public Vector3fImmutable playerLocation; public String message; - public Petition(ClientNetMsg msg, ClientConnection origin){ - this.primaryType = ((PetitionReceivedMsg)msg).getType(); - this.subType = ((PetitionReceivedMsg)msg).getSubType(); + public Petition(ClientNetMsg msg, ClientConnection origin) { + this.primaryType = ((PetitionReceivedMsg) msg).getType(); + this.subType = ((PetitionReceivedMsg) msg).getSubType(); this.reportAccount = origin.getAccount(); this.reportPlayer = origin.getPlayerCharacter(); this.playerLocation = origin.getPlayerCharacter().getLoc(); this.message = ((PetitionReceivedMsg) msg).getMessage(); } - public static String getLocationString(Vector3fImmutable loc){ - return loc.x + "," + loc.y + "," + loc.z; - } - - @Override - public void updateDatabase() { - - try (Connection connection = DbManager.getConnection(); - - //check that table exists - - PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_petition` (`petitionTime`=?, `primaryType`=?, `subType`=?, `accountID`=?, `account`=?, `characterID`=?, `character`=?, `location`=?, `message`=?);")) { - - preparedStatement.setTimestamp(1, new java.sql.Timestamp(System.currentTimeMillis())); - preparedStatement.setString(2, Enum.PetitionType.values()[this.primaryType].name()); - preparedStatement.setString(3, Enum.PetitionSubType.values()[this.subType].name()); - preparedStatement.setInt(4, this.reportAccount.getObjectUUID()); - preparedStatement.setString(5, this.reportAccount.getUname()); - preparedStatement.setInt(6, this.reportPlayer.getObjectUUID()); - preparedStatement.setString(7, this.reportPlayer.getFirstName()); - preparedStatement.setString(8, getLocationString(this.playerLocation)); - preparedStatement.setString(9, this.message); - - preparedStatement.executeUpdate(); - - } catch (SQLException e) { - Logger.error("CREATE PETITION FAILED: " + e); - } - } } diff --git a/src/engine/server/world/WorldServer.java b/src/engine/server/world/WorldServer.java index db9049f6..eafae89b 100644 --- a/src/engine/server/world/WorldServer.java +++ b/src/engine/server/world/WorldServer.java @@ -321,6 +321,9 @@ public class WorldServer { Logger.info("Initializing NPC Profits"); DbManager.NPCQueries.LOAD_NPC_PROFITS(); + Logger.info("Initializing Petition Table"); + DbManager.PetitionQueries.CREATE_PETITION_TABLE(); + Logger.info("Initializing MeshBounds"); MeshBounds.InitializeBuildingBounds();