From 8cb0e66bff55f70471efa00bd76478332d7c36ca Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sun, 21 May 2023 20:13:00 -0400 Subject: [PATCH] Refactor to remove abstraction --- src/engine/db/handlers/dbGuildHandler.java | 203 ++++++++++++++------- src/engine/devcmd/cmds/GuildListCmd.java | 105 ----------- src/engine/gameManager/DevCmdManager.java | 1 - 3 files changed, 134 insertions(+), 175 deletions(-) delete mode 100644 src/engine/devcmd/cmds/GuildListCmd.java diff --git a/src/engine/db/handlers/dbGuildHandler.java b/src/engine/db/handlers/dbGuildHandler.java index 49dc05f5..bed92b74 100644 --- a/src/engine/db/handlers/dbGuildHandler.java +++ b/src/engine/db/handlers/dbGuildHandler.java @@ -17,8 +17,7 @@ import engine.server.MBServerStatics; import org.joda.time.DateTime; import org.pmw.tinylog.Logger; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; public class dbGuildHandler extends dbHandlerBase { @@ -29,96 +28,129 @@ public class dbGuildHandler extends dbHandlerBase { } public int BANISH_FROM_GUILD_OFFLINE(final int target, boolean sourceIsGuildLeader) { - if (!sourceIsGuildLeader) //one IC cannot banish another IC - prepareCallable("UPDATE `obj_character` SET `guildUID`=NULL, `guild_isInnerCouncil`=0, `guild_isTaxCollector`=0," - + " `guild_isRecruiter`=0, `guild_isFullMember`=0, `guild_title`=0 WHERE `UID`=? && `guild_isInnerCouncil`=0"); + + String queryString; + int rowCount; + + // Only a Guild Leader can remove inner council + + if (sourceIsGuildLeader) + queryString = "UPDATE `obj_character` SET `guildUID`=NULL, `guild_isInnerCouncil`=0, `guild_isTaxCollector`=0," + + " `guild_isRecruiter`=0, `guild_isFullMember`=0, `guild_title`=0 WHERE `UID`=?"; else - prepareCallable("UPDATE `obj_character` SET `guildUID`=NULL, `guild_isInnerCouncil`=0, `guild_isTaxCollector`=0," - + " `guild_isRecruiter`=0, `guild_isFullMember`=0, `guild_title`=0 WHERE `UID`=?"); - setLong(1, (long) target); - return executeUpdate(); - } + queryString = "UPDATE `obj_character` SET `guildUID`=NULL, `guild_isInnerCouncil`=0, `guild_isTaxCollector`=0," + + " `guild_isRecruiter`=0, `guild_isFullMember`=0, `guild_title`=0 WHERE `UID`=? && `guild_isInnerCouncil`=0"; + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(queryString)) { + + preparedStatement.setLong(1, target); + rowCount = preparedStatement.executeUpdate(); + + } catch (SQLException e) { + Logger.error(e); + return 0; + } + + return rowCount; + } public boolean ADD_TO_BANISHED_FROM_GUILDLIST(int target, long characterID) { - prepareCallable("INSERT INTO `dyn_guild_banishlist` (`GuildID`, `CharacterID`) VALUES (?,?)"); - setLong(1, (long) target); - setLong(2, characterID); - return (executeUpdate() > 0); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_guild_banishlist` (`GuildID`, `CharacterID`) VALUES (?,?)")) { + + preparedStatement.setLong(1, (long) target); + preparedStatement.setLong(2, characterID); + + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } + } public boolean REMOVE_FROM_BANISH_LIST(int target, long characterID) { - prepareCallable("DELETE FROM `dyn_guild_banishlist` (`GuildID`, `CharacterID`) VALUES (?,?)"); - setLong(1, (long) target); - setLong(2, characterID); - return (executeUpdate() > 0); - } - public boolean ADD_TO_GUILDHISTORY(int target, PlayerCharacter pc, DateTime historyDate, GuildHistoryType historyType) { - prepareCallable("INSERT INTO `dyn_character_guildhistory` (`GuildID`, `CharacterID`, `historyDate`, `historyType`) VALUES (?,?,?,?)"); - setLong(1, (long) target); - setLong(2, pc.getObjectUUID()); + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `dyn_guild_banishlist` (`GuildID`, `CharacterID`) VALUES (?,?)")) { - if (historyDate == null) - setNULL(3, java.sql.Types.DATE); - else - setTimeStamp(3, historyDate.getMillis()); - setString(4,historyType.name()); - return (executeUpdate() > 0); - } + preparedStatement.setLong(1, (long) target); + preparedStatement.setLong(2, characterID); - //TODO Need to get this working. - public ArrayList GET_GUILD_HISTORY_OF_PLAYER(final int id) { - prepareCallable("SELECT g.* FROM `obj_guild` g, `dyn_character_guildhistory` l WHERE g.`UID` = l.`GuildID` && l.`CharacterID` = ?"); - setLong(1, (long) id); - return getObjectList(); + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + return false; + } } - public String GET_GUILD_LIST(int guildType) { + public boolean ADD_TO_GUILDHISTORY(int target, PlayerCharacter playerCharacter, DateTime historyDate, GuildHistoryType historyType) { - String newLine = System.getProperty("line.separator"); - String outputStr = null; - ResultSet resultSet; + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_character_guildhistory` (`GuildID`, `CharacterID`, `historyDate`, `historyType`) VALUES (?,?,?,?)")) { - // Setup and execute stored procedure + preparedStatement.setLong(1, target); + preparedStatement.setLong(2, playerCharacter.getObjectUUID()); - prepareCallable("CALL `guild_GETLIST`(?)"); - setInt(1, guildType); - resultSet = executeQuery(); + if (historyDate == null) + preparedStatement.setNull(3, java.sql.Types.DATE); + else + preparedStatement.setTimestamp(3, new Timestamp(historyDate.getMillis())); - // Build formatted string with data from query + preparedStatement.setString(4, historyType.name()); - outputStr += newLine; - outputStr += String.format("%-10s %-30s %-10s %-10s", "UUID", "Name", "GL UUID", "TOL_UUID"); - outputStr += newLine; + return (preparedStatement.executeUpdate() > 0); - try { + } catch (SQLException e) { + Logger.error(e); + return false; + } - while (resultSet.next()) { + } - outputStr += String.format("%-10d %-30s %-10d %-10d", resultSet.getInt(1), - resultSet.getString(2), resultSet.getInt(3), resultSet.getInt(4)); - outputStr += newLine; + //TODO Need to get this working. + public ArrayList GET_GUILD_HISTORY_OF_PLAYER(final int id) { - } + ArrayList guildList = null; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT g.* FROM `obj_guild` g, `dyn_character_guildhistory` l WHERE g.`UID` = l.`GuildID` && l.`CharacterID` = ?")) { + + preparedStatement.setLong(1, id); - // Exception handling + ResultSet rs = preparedStatement.executeQuery(); + guildList = getObjectsFromRs(rs, 20); } catch (SQLException e) { - Logger.error( e.getMessage()); - } finally { - closeCallable(); + Logger.error(e); } - return outputStr; + return guildList; } public ArrayList GET_GUILD_ALLIES(final int id) { - prepareCallable("SELECT g.* FROM `obj_guild` g, `dyn_guild_allianceenemylist` l " - + "WHERE l.isAlliance = 1 && l.OtherGuildID = g.UID && l.GuildID=?"); - setLong(1, (long) id); - return getObjectList(); + + ArrayList guildList = null; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT g.* FROM `obj_guild` g, `dyn_guild_allianceenemylist` l " + + "WHERE l.isAlliance = 1 && l.OtherGuildID = g.UID && l.GuildID=?")) { + + preparedStatement.setLong(1, id); + + ResultSet rs = preparedStatement.executeQuery(); + guildList = getObjectsFromRs(rs, 20); + + } catch (SQLException e) { + Logger.error(e); + } + + return guildList; } @@ -136,16 +168,49 @@ public class dbGuildHandler extends dbHandlerBase { } public ArrayList GET_GUILD_ENEMIES(final int id) { - prepareCallable("SELECT g.* FROM `obj_guild` g, `dyn_guild_allianceenemylist` l " - + "WHERE l.isAlliance = 0 && l.OtherGuildID = g.UID && l.GuildID=?"); - setLong(1, (long) id); - return getObjectList(); + + ArrayList guildList = null; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT g.* FROM `obj_guild` g, `dyn_guild_allianceenemylist` l " + + "WHERE l.isAlliance = 0 && l.OtherGuildID = g.UID && l.GuildID=?")) { + + preparedStatement.setLong(1, id); + + ResultSet rs = preparedStatement.executeQuery(); + guildList = getObjectsFromRs(rs, 20); + + } catch (SQLException e) { + Logger.error(e); + } + + return guildList; + } public ArrayList GET_GUILD_KOS_CHARACTER(final int id) { - prepareCallable("SELECT c.* FROM `obj_character` c, `dyn_guild_characterkoslist` l WHERE c.`char_isActive` = 1 && l.`KOSCharacterID` = c.`UID` && l.`GuildID`=?"); - setLong(1, (long) id); - return getObjectList(); + + ArrayList kosList = null; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT c.* FROM `obj_character` c, `dyn_guild_characterkoslist` l WHERE c.`char_isActive` = 1 && l.`KOSCharacterID` = c.`UID` && l.`GuildID`=?")) { + + preparedStatement.setLong(1, id); + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) { + int playerUUID = rs.getInt(1); + PlayerCharacter kosPlayer = PlayerCharacter.getPlayerCharacter(playerUUID); + + if (kosPlayer != null) + kosList.add(kosPlayer); + } + + } catch (SQLException e) { + Logger.error(e); + } + + return kosList; } public ArrayList GET_GUILD_KOS_GUILD(final int id) { diff --git a/src/engine/devcmd/cmds/GuildListCmd.java b/src/engine/devcmd/cmds/GuildListCmd.java deleted file mode 100644 index 71437576..00000000 --- a/src/engine/devcmd/cmds/GuildListCmd.java +++ /dev/null @@ -1,105 +0,0 @@ -// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . -// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· -// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ -// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ -// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ -// Magicbane Emulator Project © 2013 - 2022 -// www.magicbane.com - - -package engine.devcmd.cmds; - -import engine.devcmd.AbstractDevCmd; -import engine.gameManager.DbManager; -import engine.objects.AbstractGameObject; -import engine.objects.PlayerCharacter; - - -/** - * @author - * Summary: Lists UID, Name and GL UID of either - * Player or NPC sovereign guilds - */ - -public class GuildListCmd extends AbstractDevCmd { - - // Instance variables - - private int _guildType; // 0 = Player : 1 = NPC sovereign guilds - private String outputStr = null; - - public GuildListCmd() { - super("guildlist"); - } - - - // AbstractDevCmd Overridden methods - - @Override - protected void _doCmd(PlayerCharacter pc, String[] args, - AbstractGameObject target) { - - if(validateUserInput(args) == false) { - this.sendUsage(pc); - return; - } - - parseUserInput(args); - - // Execute stored procedure - - outputStr = DbManager.GuildQueries.GET_GUILD_LIST(_guildType); - - // Send results to user - - throwbackInfo(pc, outputStr); - - } - - @Override - protected String _getHelpString() { - return "Lists guild info for sovereign guilds"; - } - - @Override - protected String _getUsageString() { - return "/guildlist npc|player"; - } - - // Class methods - - private static boolean validateUserInput(String[] userInput) { - - int stringIndex; - String commandSet = "npcplayer"; - - // incorrect number of arguments test - - if (userInput.length != 1) - return false; - - // Test of game object type argument - - stringIndex = commandSet.indexOf(userInput[0].toLowerCase()); - - return stringIndex != -1; - } - - private void parseUserInput(String[] userInput) { - - // Build mask from user input - - switch (userInput[0].toLowerCase()) { - case "npc": - _guildType = 1; - break; - case "player": - _guildType = 0; - break; - default: - break; - } - - } - -} diff --git a/src/engine/gameManager/DevCmdManager.java b/src/engine/gameManager/DevCmdManager.java index 74a326d2..8e2ecf82 100644 --- a/src/engine/gameManager/DevCmdManager.java +++ b/src/engine/gameManager/DevCmdManager.java @@ -112,7 +112,6 @@ public enum DevCmdManager { DevCmdManager.registerDevCmd(new DecachePlayerCmd()); DevCmdManager.registerDevCmd(new AuditMobsCmd()); DevCmdManager.registerDevCmd(new ChangeNameCmd()); - DevCmdManager.registerDevCmd(new GuildListCmd()); DevCmdManager.registerDevCmd(new SetGuildCmd()); DevCmdManager.registerDevCmd(new SetOwnerCmd()); DevCmdManager.registerDevCmd(new NetDebugCmd());