Browse Source

Refactor to remove abstraction.

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

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

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

Loading…
Cancel
Save