From 7e511f590dc68461bdbfa8ff332a005afe02fbcd Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sat, 20 May 2023 19:02:56 -0400 Subject: [PATCH] Created getObjectsFromRs() --- .../db/handlers/dbBaseClassHandler.java | 60 ++++++++++++++++--- src/engine/db/handlers/dbHandlerBase.java | 28 +++++++++ .../db/handlers/dbPlayerCharacterHandler.java | 53 ---------------- 3 files changed, 79 insertions(+), 62 deletions(-) diff --git a/src/engine/db/handlers/dbBaseClassHandler.java b/src/engine/db/handlers/dbBaseClassHandler.java index 3ae0f098..ce4e1d41 100644 --- a/src/engine/db/handlers/dbBaseClassHandler.java +++ b/src/engine/db/handlers/dbBaseClassHandler.java @@ -13,7 +13,12 @@ import engine.Enum; import engine.Enum.GameObjectType; import engine.gameManager.DbManager; import engine.objects.BaseClass; +import org.pmw.tinylog.Logger; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; public class dbBaseClassHandler extends dbHandlerBase { @@ -27,24 +32,61 @@ public class dbBaseClassHandler extends dbHandlerBase { if (id == 0) return null; + BaseClass baseClass = (BaseClass) DbManager.getFromCache(GameObjectType.BaseClass, id); + if (baseClass != null) return baseClass; + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `obj_account` WHERE `UID`=?")) { + + preparedStatement.setLong(1, id); + + ResultSet rs = preparedStatement.executeQuery(); + baseClass = (BaseClass) getObjectFromRs(rs); + + } catch (SQLException e) { + Logger.error(e); + } - prepareCallable("SELECT * FROM `static_rune_baseclass` WHERE `ID` = ?;"); - setInt(1, id); - return (BaseClass) getObjectSingle(id); + return baseClass; } public ArrayList GET_BASECLASS_FOR_RACE(final int id) { - prepareCallable("SELECT b.* FROM `static_rune_baseclass` b, `static_rune_racebaseclass` r WHERE b.`ID` = r.`BaseClassID` && r.`RaceID` = ?"); - setInt(1, id); - return getObjectList(); + + ArrayList baseClasses = new ArrayList<>(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT b.* FROM `static_rune_baseclass` b, `static_rune_racebaseclass` r WHERE b.`ID` = r.`BaseClassID` && r.`RaceID` = ?")) { + + preparedStatement.setInt(1, id); + + ResultSet rs = preparedStatement.executeQuery(); + baseClasses = getObjectsFromRs(rs, 20); + + } catch (SQLException e) { + Logger.error(e); + } + + return baseClasses; } - public ArrayList GET_ALL_BASE_CLASSES(){ - prepareCallable("SELECT * FROM `static_rune_baseclass`;"); - return getObjectList(); + public ArrayList GET_ALL_BASE_CLASSES() { + + ArrayList baseClasses = new ArrayList<>(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_rune_baseclass`;")) { + + ResultSet rs = preparedStatement.executeQuery(); + baseClasses = getObjectsFromRs(rs, 20); + + } catch (SQLException e) { + Logger.error(e); + } + + return baseClasses; + } } diff --git a/src/engine/db/handlers/dbHandlerBase.java b/src/engine/db/handlers/dbHandlerBase.java index 67271be3..53eea3c2 100644 --- a/src/engine/db/handlers/dbHandlerBase.java +++ b/src/engine/db/handlers/dbHandlerBase.java @@ -297,6 +297,7 @@ public abstract class dbHandlerBase { try { if (rs.next()) { abstractGameObject = localClass.getConstructor(ResultSet.class).newInstance(rs); + DbManager.addToCache(abstractGameObject); } } catch (Exception e) { @@ -306,6 +307,33 @@ public abstract class dbHandlerBase { return abstractGameObject; } + protected ArrayList getObjectsFromRs(ResultSet rs, int listSize) { + + ArrayList objectList = new ArrayList<>(listSize); + + try { + while (rs.next()) { + + int id = rs.getInt(1); + + if (DbManager.inCache(localObjectType, id)) { + objectList.add((T) DbManager.getFromCache(localObjectType, id)); + } else { + AbstractGameObject toAdd = localClass.getConstructor(ResultSet.class).newInstance(rs); + DbManager.addToCache(toAdd); + objectList.add((T) toAdd); + + if (toAdd != null && toAdd instanceof AbstractWorldObject) + ((AbstractWorldObject) toAdd).runAfterLoad(); + } + } + } catch (Exception e) { + Logger.error(e); + } + return objectList; + + } + protected AbstractGameObject getObjectSingle(int id) { return getObjectSingle(id, false, true); } diff --git a/src/engine/db/handlers/dbPlayerCharacterHandler.java b/src/engine/db/handlers/dbPlayerCharacterHandler.java index 1fb8e394..3dfeb6ae 100644 --- a/src/engine/db/handlers/dbPlayerCharacterHandler.java +++ b/src/engine/db/handlers/dbPlayerCharacterHandler.java @@ -76,22 +76,6 @@ public class dbPlayerCharacterHandler extends dbHandlerBase { } } - public static boolean DELETE_CHARACTER_IGNORE(final PlayerCharacter pc, final ArrayList toDelete) { - - return false; - } - - public ArrayList GET_ALL_PLAYERCHARACTERS() { - prepareCallable("SELECT * FROM `obj_character`"); - return getObjectList(); - } - - public ArrayList GET_CHARACTERS_FOR_ACCOUNT(final int id, boolean forceFromDB) { - prepareCallable("SELECT `obj_character`.*, `object`.`parent` FROM `object` INNER JOIN `obj_character` ON `obj_character`.`UID` = `object`.`UID` WHERE `object`.`parent`=? && `obj_character`.`char_isActive`='1';"); - setLong(1, (long) id); - return getObjectList(10, forceFromDB); - } - public ArrayList GET_CHARACTERS_FOR_ACCOUNT(final int id) { prepareCallable("SELECT `obj_character`.*, `object`.`parent` FROM `object` INNER JOIN `obj_character` ON `obj_character`.`UID` = `object`.`UID` WHERE `object`.`parent`=? && `obj_character`.`char_isActive`='1';"); setLong(1, (long) id); @@ -111,22 +95,6 @@ public class dbPlayerCharacterHandler extends dbHandlerBase { * and cached. * */ - public String GET_FIRST_NAME(final int objectUUID) { - prepareCallable("SELECT `char_firstname` from `obj_character` WHERE `UID` = ? LIMIT 1"); - setLong(1, (long) objectUUID); - String firstName = ""; - try { - ResultSet rs = executeQuery(); - if (rs.next()) { - firstName = rs.getString("char_firstname"); - } - } catch (SQLException e) { - Logger.error( e); - } finally { - closeCallable(); - } - return firstName; - } public ConcurrentHashMap GET_IGNORE_LIST(final int objectUUID, final boolean skipActiveCheck) { ConcurrentHashMap out = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); @@ -165,20 +133,6 @@ public class dbPlayerCharacterHandler extends dbHandlerBase { return (PlayerCharacter) getObjectSingle(objectUUID); } - public boolean INSERT_CHARACTER_IGNORE(final PlayerCharacter pc, final ArrayList toAdd) { - boolean allWorked = true; - prepareCallable("INSERT INTO `dyn_character_ignore` (`characterUID`, `ignoringUID`) VALUES (?, ?)"); - setLong(1, (long) pc.getObjectUUID()); - for (int id : toAdd) { - setLong(2, (long) id); - if (executeUpdate(false) == 0) { - allWorked = false; - } - } - closeCallable(); - return allWorked; - } - public boolean IS_CHARACTER_NAME_UNIQUE(final String firstName) { boolean unique = true; prepareCallable("SELECT `char_firstname` FROM `obj_character` WHERE `char_isActive`=1 && `char_firstname`=?"); @@ -247,13 +201,6 @@ public class dbPlayerCharacterHandler extends dbHandlerBase { return (executeUpdate() != 0); } - public boolean UPDATE_CHARACTER_STAT(final PlayerCharacter pc, String stat, short amount) { - prepareCallable("UPDATE `obj_character` SET `" + stat + "`=? WHERE `UID`=?"); - setInt(1, pc.getExp()); - setLong(2, (long) pc.getObjectUUID()); - return (executeUpdate() != 0); - } - public boolean UPDATE_CHARACTER_STATS(final PlayerCharacter pc) { prepareCallable("UPDATE `obj_character` SET `char_strMod`=?, `char_dexMod`=?, `char_conMod`=?, `char_intMod`=?, `char_spiMod`=? WHERE `UID`=?"); setInt(1, pc.getStrMod());