Browse Source

Refactor to remove abstraction.

master
MagicBot 2 years ago
parent
commit
ca5cfed82f
  1. 160
      src/engine/db/handlers/dbCharacterSkillHandler.java
  2. 2
      src/engine/db/handlers/dbItemBaseHandler.java

160
src/engine/db/handlers/dbCharacterSkillHandler.java

@ -17,6 +17,8 @@ import engine.objects.PlayerCharacter; @@ -17,6 +17,8 @@ import engine.objects.PlayerCharacter;
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;
@ -29,88 +31,146 @@ public class dbCharacterSkillHandler extends dbHandlerBase { @@ -29,88 +31,146 @@ public class dbCharacterSkillHandler extends dbHandlerBase {
}
public CharacterSkill ADD_SKILL(CharacterSkill toAdd) {
CharacterSkill characterSkill = null;
if (CharacterSkill.GetOwner(toAdd) == null || toAdd.getSkillsBase() == null) {
Logger.error("dbCharacterSkillHandler.ADD_SKILL", toAdd.getObjectUUID() + " missing owner or skillsBase");
return null;
}
prepareCallable("INSERT INTO `dyn_character_skill` (`CharacterID`, `skillsBaseID`, `trains`) VALUES (?, ?, ?);");
setLong(1, (long)CharacterSkill.GetOwner(toAdd).getObjectUUID());
setInt(2, toAdd.getSkillsBase().getObjectUUID());
setInt(3, toAdd.getNumTrains());
int skillID = insertGetUUID();
return GET_SKILL(skillID);
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_character_skill` (`CharacterID`, `skillsBaseID`, `trains`) VALUES (?, ?, ?);")) {
preparedStatement.setLong(1, CharacterSkill.GetOwner(toAdd).getObjectUUID());
preparedStatement.setInt(2, toAdd.getSkillsBase().getObjectUUID());
preparedStatement.setInt(3, toAdd.getNumTrains());
preparedStatement.executeUpdate();
ResultSet rs = preparedStatement.getGeneratedKeys();
if (rs.next())
characterSkill = GET_SKILL(rs.getInt(1));
} catch (SQLException e) {
Logger.error(e);
}
return characterSkill;
}
public boolean DELETE_SKILL(final int objectUUID) {
prepareCallable("DELETE FROM `dyn_character_skill` WHERE `UID` = ?");
setLong(1, (long)objectUUID);
return (executeUpdate() != 0);
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `dyn_character_skill` WHERE `UID` = ?")) {
preparedStatement.setLong(1, objectUUID);
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
return false;
}
}
public CharacterSkill GET_SKILL(final int objectUUID) {
CharacterSkill skill = (CharacterSkill) DbManager.getFromCache(Enum.GameObjectType.CharacterSkill, objectUUID);
if (skill != null)
return skill;
prepareCallable("SELECT * FROM `dyn_character_skill` WHERE `UID` = ?");
setInt(1, objectUUID);
return (CharacterSkill) getObjectSingle(objectUUID);
CharacterSkill characterSkill = (CharacterSkill) DbManager.getFromCache(Enum.GameObjectType.CharacterSkill, objectUUID);
if (characterSkill != null)
return characterSkill;
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `dyn_character_skill` WHERE `UID` = ?")) {
preparedStatement.setInt(1, objectUUID);
ResultSet rs = preparedStatement.executeQuery();
characterSkill = (CharacterSkill) getObjectFromRs(rs);
} catch (SQLException e) {
Logger.error(e);
}
return characterSkill;
}
public ConcurrentHashMap<String, CharacterSkill> GET_SKILLS_FOR_CHARACTER(final AbstractCharacter ac) {
ConcurrentHashMap<String, CharacterSkill> skills = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
ConcurrentHashMap<String, CharacterSkill> characterSkills = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
if (ac == null || (!(ac.getObjectType().equals(Enum.GameObjectType.PlayerCharacter))))
return skills;
PlayerCharacter pc = (PlayerCharacter) ac;
int objectUUID = pc.getObjectUUID();
prepareCallable("SELECT * FROM `dyn_character_skill` WHERE `CharacterID` = ?");
setInt(1, objectUUID);
try {
ResultSet rs = executeQuery();
return characterSkills;
PlayerCharacter playerCharacter = (PlayerCharacter) ac;
int characterId = playerCharacter.getObjectUUID();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `dyn_character_skill` WHERE `CharacterID` = ?")) {
preparedStatement.setInt(1, characterId);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
CharacterSkill cs = new CharacterSkill(rs, pc);
CharacterSkill cs = new CharacterSkill(rs, playerCharacter);
if (cs.getSkillsBase() != null)
skills.put(cs.getSkillsBase().getName(), cs);
characterSkills.put(cs.getSkillsBase().getName(), cs);
}
rs.close();
} catch (SQLException e) {
Logger.error("CharacterSkill.getCharacterSkillForCharacter", e);
} finally {
closeCallable();
Logger.error(e);
}
return skills;
return characterSkills;
}
public void UPDATE_TRAINS(final CharacterSkill cs) {
if (!cs.isTrained())
public void UPDATE_TRAINS(final CharacterSkill characterSkill) {
if (!characterSkill.isTrained())
return;
prepareCallable("UPDATE `dyn_character_skill` SET `trains`=? WHERE `UID` = ?");
setShort(1, (short)cs.getNumTrains());
setLong(2, (long)cs.getObjectUUID());
if (executeUpdate() != 0)
cs.syncTrains();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `dyn_character_skill` SET `trains`=? WHERE `UID` = ?")) {
preparedStatement.setShort(1, (short) characterSkill.getNumTrains());
preparedStatement.setLong(2, (long) characterSkill.getObjectUUID());
if (preparedStatement.executeUpdate() != 0)
characterSkill.syncTrains();
} catch (SQLException e) {
Logger.error(e);
}
}
public void updateDatabase(final CharacterSkill cs) {
if (cs.getSkillsBase() == null) {
Logger.error("Failed to find skillsBase for Skill " + cs.getObjectUUID());
public void updateDatabase(final CharacterSkill characterSkill) {
if (characterSkill.getSkillsBase() == null) {
Logger.error("Failed to find skillsBase for Skill " + characterSkill.getObjectUUID());
return;
}
if (CharacterSkill.GetOwner(cs) == null) {
Logger.error("Failed to find owner for Skill " + cs.getObjectUUID());
if (CharacterSkill.GetOwner(characterSkill) == null) {
Logger.error("Failed to find owner for Skill " + characterSkill.getObjectUUID());
return;
}
prepareCallable("UPDATE `dyn_character_skill` SET `skillsBaseID`=?, `CharacterID`=?, `trains`=? WHERE `UID`=?");
setInt(1, cs.getSkillsBase().getObjectUUID());
setInt(2, CharacterSkill.GetOwner(cs).getObjectUUID());
setShort(3, (short)cs.getNumTrains());
setLong(4, (long)cs.getObjectUUID());
if (executeUpdate() != 0)
cs.syncTrains();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `dyn_character_skill` SET `skillsBaseID`=?, `CharacterID`=?, `trains`=? WHERE `UID`=?")) {
preparedStatement.setInt(1, characterSkill.getSkillsBase().getObjectUUID());
preparedStatement.setInt(2, CharacterSkill.GetOwner(characterSkill).getObjectUUID());
preparedStatement.setShort(3, (short) characterSkill.getNumTrains());
preparedStatement.setLong(4, (long) characterSkill.getObjectUUID());
if (preparedStatement.executeUpdate() != 0)
characterSkill.syncTrains();
} catch (SQLException e) {
Logger.error(e);
}
}
}

2
src/engine/db/handlers/dbItemBaseHandler.java

@ -94,7 +94,7 @@ public class dbItemBaseHandler extends dbHandlerBase { @@ -94,7 +94,7 @@ public class dbItemBaseHandler extends dbHandlerBase {
Logger.error(e);
}
Logger.info("read: " + recordsRead + "cached: " + ItemBase.getUUIDCache().size());
Logger.info("read: " + recordsRead + " cached: " + ItemBase.getUUIDCache().size());
}
public HashMap<Integer, ArrayList<Integer>> LOAD_RUNES_FOR_NPC_AND_MOBS() {

Loading…
Cancel
Save