2026-07-26 19:08:44 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2026
// https://www.magicbane.com
2022-04-30 09:41:17 -04:00
package engine.db.handlers ;
import engine.gameManager.DbManager ;
2024-04-05 07:59:44 -04:00
import engine.mbEnums ;
2022-04-30 09:41:17 -04:00
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 {
2023-07-15 09:23:48 -04:00
public dbCharacterSkillHandler () {
this . localClass = CharacterSkill . class ;
2024-04-05 07:59:44 -04:00
this . localObjectType = mbEnums . GameObjectType . valueOf ( this . localClass . getSimpleName ());
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public CharacterSkill ADD_SKILL ( CharacterSkill toAdd ) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
CharacterSkill characterSkill = null ;
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
if ( CharacterSkill . GetOwner ( toAdd ) == null || toAdd . getSkillsBase () == null ) {
Logger . error ( "dbCharacterSkillHandler.ADD_SKILL" , toAdd . getObjectUUID () + " missing owner or skillsBase" );
return null ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try ( Connection connection = DbManager . getConnection ();
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
2023-07-15 09:23:48 -04:00
preparedStatement . setLong ( 1 , CharacterSkill . GetOwner ( toAdd ). getObjectUUID ());
preparedStatement . setInt ( 2 , toAdd . getSkillsBase (). getObjectUUID ());
preparedStatement . setInt ( 3 , toAdd . getNumTrains ());
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement . executeUpdate ();
ResultSet rs = preparedStatement . getGeneratedKeys ();
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
if ( rs . next ())
characterSkill = GET_SKILL ( rs . getInt ( 1 ));
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
return characterSkill ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public boolean DELETE_SKILL ( final int objectUUID ) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "DELETE FROM `dyn_character_skill` WHERE `UID` = ?" )) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement . setLong ( 1 , objectUUID );
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
return ( preparedStatement . executeUpdate () > 0 );
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
return false ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public CharacterSkill GET_SKILL ( final int objectUUID ) {
2023-05-21 15:48:55 -04:00
2024-04-05 07:59:44 -04:00
CharacterSkill characterSkill = ( CharacterSkill ) DbManager . getFromCache ( mbEnums . GameObjectType . CharacterSkill , objectUUID );
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
if ( characterSkill != null )
return characterSkill ;
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM `dyn_character_skill` WHERE `UID` = ?" )) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement . setInt ( 1 , objectUUID );
ResultSet rs = preparedStatement . executeQuery ();
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
characterSkill = ( CharacterSkill ) getObjectFromRs ( rs );
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
return characterSkill ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public ConcurrentHashMap < String , CharacterSkill > GET_SKILLS_FOR_CHARACTER ( final AbstractCharacter ac ) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
ConcurrentHashMap < String , CharacterSkill > characterSkills = new ConcurrentHashMap <> ( MBServerStatics . CHM_INIT_CAP , MBServerStatics . CHM_LOAD , MBServerStatics . CHM_THREAD_LOW );
2023-05-21 15:48:55 -04:00
2024-04-05 07:59:44 -04:00
if ( ac == null || ( ! ( ac . getObjectType (). equals ( mbEnums . GameObjectType . PlayerCharacter ))))
2023-07-15 09:23:48 -04:00
return characterSkills ;
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
PlayerCharacter playerCharacter = ( PlayerCharacter ) ac ;
int characterId = playerCharacter . getObjectUUID ();
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM `dyn_character_skill` WHERE `CharacterID` = ?" )) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement . setInt ( 1 , characterId );
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
ResultSet rs = preparedStatement . executeQuery ();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
while ( rs . next ()) {
CharacterSkill cs = new CharacterSkill ( rs , playerCharacter );
if ( cs . getSkillsBase () != null )
characterSkills . put ( cs . getSkillsBase (). getName (), cs );
}
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
return characterSkills ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void UPDATE_TRAINS ( final CharacterSkill characterSkill ) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
if ( ! characterSkill . isTrained ())
return ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `dyn_character_skill` SET `trains`=? WHERE `UID` = ?" )) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement . setShort ( 1 , ( short ) characterSkill . getNumTrains ());
preparedStatement . setLong ( 2 , characterSkill . getObjectUUID ());
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
if ( preparedStatement . executeUpdate () != 0 )
characterSkill . syncTrains ();
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void updateDatabase ( final CharacterSkill characterSkill ) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
if ( characterSkill . getSkillsBase () == null ) {
Logger . error ( "Failed to find skillsBase for Skill " + characterSkill . getObjectUUID ());
return ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( CharacterSkill . GetOwner ( characterSkill ) == null ) {
Logger . error ( "Failed to find owner for Skill " + characterSkill . getObjectUUID ());
return ;
}
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
try ( Connection connection = DbManager . getConnection ();
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `dyn_character_skill` SET `skillsBaseID`=?, `CharacterID`=?, `trains`=? WHERE `UID`=?" )) {
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement . setInt ( 1 , characterSkill . getSkillsBase (). getObjectUUID ());
preparedStatement . setInt ( 2 , CharacterSkill . GetOwner ( characterSkill ). getObjectUUID ());
preparedStatement . setShort ( 3 , ( short ) characterSkill . getNumTrains ());
preparedStatement . setLong ( 4 , characterSkill . getObjectUUID ());
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
if ( preparedStatement . executeUpdate () != 0 )
characterSkill . syncTrains ();
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
} catch ( SQLException e ) {
Logger . error ( e );
}
2023-05-21 15:48:55 -04:00
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
}