2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.handlers ;
import engine.Enum ;
import engine.gameManager.DbManager ;
import engine.objects.AbstractCharacter ;
import engine.objects.CharacterSkill ;
import engine.objects.PlayerCharacter ;
import engine.server.MBServerStatics ;
import org.pmw.tinylog.Logger ;
2023-05-26 17:46:26 -04:00
import java.sql.* ;
2022-04-30 09:41:17 -04:00
import java.util.concurrent.ConcurrentHashMap ;
public class dbCharacterSkillHandler extends dbHandlerBase {
public dbCharacterSkillHandler () {
this . localClass = CharacterSkill . class ;
this . localObjectType = Enum . GameObjectType . valueOf ( this . localClass . getSimpleName ());
}
public CharacterSkill ADD_SKILL ( CharacterSkill toAdd ) {
2023-05-21 15:48:55 -04:00
CharacterSkill characterSkill = null ;
2022-04-30 09:41:17 -04:00
if ( CharacterSkill . GetOwner ( toAdd ) == null || toAdd . getSkillsBase () == null ) {
Logger . error ( "dbCharacterSkillHandler.ADD_SKILL" , toAdd . getObjectUUID () + " missing owner or skillsBase" );
return null ;
}
2023-05-21 15:48:55 -04:00
try ( Connection connection = DbManager . getConnection ();
2023-05-26 17:46:26 -04:00
PreparedStatement preparedStatement = connection . prepareStatement ( "INSERT INTO `dyn_character_skill` (`CharacterID`, `skillsBaseID`, `trains`) VALUES (?, ?, ?);" , Statement . RETURN_GENERATED_KEYS )) {
2023-05-21 15:48:55 -04:00
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 ;
2022-04-30 09:41:17 -04:00
}
public boolean DELETE_SKILL ( final int objectUUID ) {
2023-05-21 15:48:55 -04:00
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 );
}
2023-05-23 09:17:06 -04:00
return false ;
2022-04-30 09:41:17 -04:00
}
public CharacterSkill GET_SKILL ( final int objectUUID ) {
2023-05-21 15:48:55 -04:00
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 ;
2022-04-30 09:41:17 -04:00
}
public ConcurrentHashMap < String , CharacterSkill > GET_SKILLS_FOR_CHARACTER ( final AbstractCharacter ac ) {
2023-05-21 15:48:55 -04:00
ConcurrentHashMap < String , CharacterSkill > characterSkills = new ConcurrentHashMap <> ( MBServerStatics . CHM_INIT_CAP , MBServerStatics . CHM_LOAD , MBServerStatics . CHM_THREAD_LOW );
2022-04-30 09:41:17 -04:00
if ( ac == null || ( ! ( ac . getObjectType (). equals ( Enum . GameObjectType . PlayerCharacter ))))
2023-05-21 15:48:55 -04:00
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 ();
2022-04-30 09:41:17 -04:00
while ( rs . next ()) {
2023-05-21 15:48:55 -04:00
CharacterSkill cs = new CharacterSkill ( rs , playerCharacter );
2022-04-30 09:41:17 -04:00
if ( cs . getSkillsBase () != null )
2023-05-21 15:48:55 -04:00
characterSkills . put ( cs . getSkillsBase (). getName (), cs );
2022-04-30 09:41:17 -04:00
}
2023-05-21 15:48:55 -04:00
2022-04-30 09:41:17 -04:00
} catch ( SQLException e ) {
2023-05-21 15:48:55 -04:00
Logger . error ( e );
2022-04-30 09:41:17 -04:00
}
2023-05-21 15:48:55 -04:00
return characterSkills ;
2022-04-30 09:41:17 -04:00
}
2023-05-21 15:48:55 -04:00
public void UPDATE_TRAINS ( final CharacterSkill characterSkill ) {
if ( ! characterSkill . isTrained ())
2022-04-30 09:41:17 -04:00
return ;
2023-05-21 15:48:55 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `dyn_character_skill` SET `trains`=? WHERE `UID` = ?" )) {
preparedStatement . setShort ( 1 , ( short ) characterSkill . getNumTrains ());
2023-05-23 10:27:03 -04:00
preparedStatement . setLong ( 2 , characterSkill . getObjectUUID ());
2023-05-21 15:48:55 -04:00
if ( preparedStatement . executeUpdate () != 0 )
characterSkill . syncTrains ();
} catch ( SQLException e ) {
Logger . error ( e );
}
2022-04-30 09:41:17 -04:00
}
2023-05-21 15:48:55 -04:00
public void updateDatabase ( final CharacterSkill characterSkill ) {
if ( characterSkill . getSkillsBase () == null ) {
Logger . error ( "Failed to find skillsBase for Skill " + characterSkill . getObjectUUID ());
2022-04-30 09:41:17 -04:00
return ;
}
2023-05-21 15:48:55 -04:00
if ( CharacterSkill . GetOwner ( characterSkill ) == null ) {
Logger . error ( "Failed to find owner for Skill " + characterSkill . getObjectUUID ());
return ;
}
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 ());
2023-05-23 10:27:03 -04:00
preparedStatement . setLong ( 4 , characterSkill . getObjectUUID ());
2023-05-21 15:48:55 -04:00
if ( preparedStatement . executeUpdate () != 0 )
characterSkill . syncTrains ();
} catch ( SQLException e ) {
Logger . error ( e );
}
2022-04-30 09:41:17 -04:00
}
}