Browse Source

Created getObjectsFromRs()

master
MagicBot 2 years ago
parent
commit
7e511f590d
  1. 60
      src/engine/db/handlers/dbBaseClassHandler.java
  2. 28
      src/engine/db/handlers/dbHandlerBase.java
  3. 53
      src/engine/db/handlers/dbPlayerCharacterHandler.java

60
src/engine/db/handlers/dbBaseClassHandler.java

@ -13,7 +13,12 @@ import engine.Enum;
import engine.Enum.GameObjectType; import engine.Enum.GameObjectType;
import engine.gameManager.DbManager; import engine.gameManager.DbManager;
import engine.objects.BaseClass; 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; import java.util.ArrayList;
public class dbBaseClassHandler extends dbHandlerBase { public class dbBaseClassHandler extends dbHandlerBase {
@ -27,24 +32,61 @@ public class dbBaseClassHandler extends dbHandlerBase {
if (id == 0) if (id == 0)
return null; return null;
BaseClass baseClass = (BaseClass) DbManager.getFromCache(GameObjectType.BaseClass, id); BaseClass baseClass = (BaseClass) DbManager.getFromCache(GameObjectType.BaseClass, id);
if (baseClass != null) if (baseClass != null)
return baseClass; return baseClass;
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `obj_account` WHERE `UID`=?")) {
preparedStatement.setLong(1, id);
prepareCallable("SELECT * FROM `static_rune_baseclass` WHERE `ID` = ?;"); ResultSet rs = preparedStatement.executeQuery();
setInt(1, id); baseClass = (BaseClass) getObjectFromRs(rs);
return (BaseClass) getObjectSingle(id);
} catch (SQLException e) {
Logger.error(e);
}
return baseClass;
} }
public ArrayList<BaseClass> GET_BASECLASS_FOR_RACE(final int id) { public ArrayList<BaseClass> 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); ArrayList<BaseClass> baseClasses = new ArrayList<>();
return getObjectList();
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<BaseClass> GET_ALL_BASE_CLASSES() {
ArrayList<BaseClass> 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);
} }
public ArrayList<BaseClass> GET_ALL_BASE_CLASSES(){ return baseClasses;
prepareCallable("SELECT * FROM `static_rune_baseclass`;");
return getObjectList();
} }
} }

28
src/engine/db/handlers/dbHandlerBase.java

@ -297,6 +297,7 @@ public abstract class dbHandlerBase {
try { try {
if (rs.next()) { if (rs.next()) {
abstractGameObject = localClass.getConstructor(ResultSet.class).newInstance(rs); abstractGameObject = localClass.getConstructor(ResultSet.class).newInstance(rs);
DbManager.addToCache(abstractGameObject); DbManager.addToCache(abstractGameObject);
} }
} catch (Exception e) { } catch (Exception e) {
@ -306,6 +307,33 @@ public abstract class dbHandlerBase {
return abstractGameObject; return abstractGameObject;
} }
protected <T extends AbstractGameObject> ArrayList<T> getObjectsFromRs(ResultSet rs, int listSize) {
ArrayList<T> 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 <T extends AbstractGameObject> AbstractGameObject getObjectSingle(int id) { protected <T extends AbstractGameObject> AbstractGameObject getObjectSingle(int id) {
return getObjectSingle(id, false, true); return getObjectSingle(id, false, true);
} }

53
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<Integer> toDelete) {
return false;
}
public ArrayList<PlayerCharacter> GET_ALL_PLAYERCHARACTERS() {
prepareCallable("SELECT * FROM `obj_character`");
return getObjectList();
}
public ArrayList<PlayerCharacter> 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<PlayerCharacter> GET_CHARACTERS_FOR_ACCOUNT(final int id) { public ArrayList<PlayerCharacter> 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';"); 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); setLong(1, (long) id);
@ -111,22 +95,6 @@ public class dbPlayerCharacterHandler extends dbHandlerBase {
* and cached. * 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<Integer, String> GET_IGNORE_LIST(final int objectUUID, final boolean skipActiveCheck) { public ConcurrentHashMap<Integer, String> GET_IGNORE_LIST(final int objectUUID, final boolean skipActiveCheck) {
ConcurrentHashMap<Integer, String> out = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); ConcurrentHashMap<Integer, String> 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); return (PlayerCharacter) getObjectSingle(objectUUID);
} }
public boolean INSERT_CHARACTER_IGNORE(final PlayerCharacter pc, final ArrayList<Integer> 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) { public boolean IS_CHARACTER_NAME_UNIQUE(final String firstName) {
boolean unique = true; boolean unique = true;
prepareCallable("SELECT `char_firstname` FROM `obj_character` WHERE `char_isActive`=1 && `char_firstname`=?"); 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); 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) { 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`=?"); prepareCallable("UPDATE `obj_character` SET `char_strMod`=?, `char_dexMod`=?, `char_conMod`=?, `char_intMod`=?, `char_spiMod`=? WHERE `UID`=?");
setInt(1, pc.getStrMod()); setInt(1, pc.getStrMod());

Loading…
Cancel
Save