@ -16,6 +16,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 ;
@ -28,86 +30,143 @@ public class dbCharacterPowerHandler extends dbHandlerBase {
}
}
public CharacterPower ADD_CHARACTER_POWER ( CharacterPower toAdd ) {
public CharacterPower ADD_CHARACTER_POWER ( CharacterPower toAdd ) {
CharacterPower characterPower = null ;
if ( CharacterPower . getOwner ( toAdd ) = = null | | toAdd . getPower ( ) = = null ) {
if ( CharacterPower . getOwner ( toAdd ) = = null | | toAdd . getPower ( ) = = null ) {
Logger . error ( "dbCharacterSkillHandler.ADD_Power" , toAdd . getObjectUUID ( ) + " missing owner or powersBase" ) ;
Logger . error ( "dbCharacterSkillHandler.ADD_Power" , toAdd . getObjectUUID ( ) + " missing owner or powersBase" ) ;
return null ;
return null ;
}
}
prepareCallable ( "INSERT INTO `dyn_character_power` (`CharacterID`, `powersBaseToken`, `trains`) VALUES (?, ?, ?);" ) ;
try ( Connection connection = DbManager . getConnection ( ) ;
setLong ( 1 , ( long ) CharacterPower . getOwner ( toAdd ) . getObjectUUID ( ) ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( "INSERT INTO `dyn_character_power` (`CharacterID`, `powersBaseToken`, `trains`) VALUES (?, ?, ?);" ) ) {
setInt ( 2 , toAdd . getPower ( ) . getToken ( ) ) ;
setInt ( 3 , toAdd . getTrains ( ) ) ;
preparedStatement . setLong ( 1 , CharacterPower . getOwner ( toAdd ) . getObjectUUID ( ) ) ;
int powerID = insertGetUUID ( ) ;
preparedStatement . setInt ( 2 , toAdd . getPower ( ) . getToken ( ) ) ;
return GET_CHARACTER_POWER ( powerID ) ;
preparedStatement . setInt ( 3 , toAdd . getTrains ( ) ) ;
preparedStatement . executeUpdate ( ) ;
ResultSet rs = preparedStatement . getGeneratedKeys ( ) ;
if ( rs . next ( ) )
characterPower = GET_CHARACTER_POWER ( rs . getInt ( 1 ) ) ;
} catch ( SQLException e ) {
Logger . error ( e ) ;
return null ;
}
return characterPower ;
}
}
public int DELETE_CHARACTER_POWER ( final int objectUUID ) {
public int DELETE_CHARACTER_POWER ( final int objectUUID ) {
prepareCallable ( "DELETE FROM `dyn_character_power` WHERE `UID` = ?" ) ;
setLong ( 1 , ( long ) objectUUID ) ;
int rowCount = 0 ;
return executeUpdate ( ) ;
try ( Connection connection = DbManager . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( "DELETE FROM `dyn_character_power` WHERE `UID` = ?" ) ) {
preparedStatement . setLong ( 1 , objectUUID ) ;
rowCount = preparedStatement . executeUpdate ( ) ;
} catch ( SQLException e ) {
Logger . error ( e ) ;
}
return rowCount ;
}
}
public CharacterPower GET_CHARACTER_POWER ( int objectUUID ) {
public CharacterPower GET_CHARACTER_POWER ( int objectUUID ) {
CharacterPower cp = ( CharacterPower ) DbManager . getFromCache ( Enum . GameObjectType . CharacterPower , objectUUID ) ;
CharacterPower characterPower = ( CharacterPower ) DbManager . getFromCache ( Enum . GameObjectType . CharacterPower , objectUUID ) ;
if ( cp ! = null )
return cp ;
if ( characterPower ! = null )
prepareCallable ( "SELECT * FROM `dyn_character_power` WHERE `UID` = ?" ) ;
return characterPower ;
setLong ( 1 , ( long ) objectUUID ) ;
return ( CharacterPower ) getObjectSingle ( objectUUID ) ;
try ( Connection connection = DbManager . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM `dyn_character_power` WHERE `UID` = ?" ) ) {
preparedStatement . setLong ( 1 , objectUUID ) ;
ResultSet rs = preparedStatement . executeQuery ( ) ;
characterPower = ( CharacterPower ) getObjectFromRs ( rs ) ;
} catch ( SQLException e ) {
Logger . error ( e ) ;
}
return characterPower ;
}
}
public ConcurrentHashMap < Integer , CharacterPower > GET_POWERS_FOR_CHARACTER ( PlayerCharacter pc ) {
public ConcurrentHashMap < Integer , CharacterPower > GET_POWERS_FOR_CHARACTER ( PlayerCharacter pc ) {
ConcurrentHashMap < Integer , CharacterPower > powers = new ConcurrentHashMap < > ( MBServerStatics . CHM_INIT_CAP , MBServerStatics . CHM_LOAD , MBServerStatics . CHM_THREAD_LOW ) ;
ConcurrentHashMap < Integer , CharacterPower > powers = new ConcurrentHashMap < > ( MBServerStatics . CHM_INIT_CAP , MBServerStatics . CHM_LOAD , MBServerStatics . CHM_THREAD_LOW ) ;
int objectUUID = pc . getObjectUUID ( ) ;
int objectUUID = pc . getObjectUUID ( ) ;
prepareCallable ( "SELECT * FROM `dyn_character_power` WHERE CharacterID = ?" ) ;
setLong ( 1 , ( long ) objectUUID ) ;
try ( Connection connection = DbManager . getConnection ( ) ;
ResultSet rs = executeQuery ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( "SELECT * FROM `dyn_character_power` WHERE CharacterID = ?" ) ) {
try {
preparedStatement . setLong ( 1 , ( long ) objectUUID ) ;
ResultSet rs = preparedStatement . executeQuery ( ) ;
while ( rs . next ( ) ) {
while ( rs . next ( ) ) {
CharacterPower cp = new CharacterPower ( rs , pc ) ;
CharacterPower cp = new CharacterPower ( rs , pc ) ;
if ( cp . getPower ( ) ! = null )
if ( cp . getPower ( ) ! = null )
powers . put ( cp . getPower ( ) . getToken ( ) , cp ) ;
powers . put ( cp . getPower ( ) . getToken ( ) , cp ) ;
}
}
rs . close ( ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
Logger . error ( "CharacterPower.getCharacterPowerForCharacter" , "Exception:" + e . getMessage ( ) ) ;
Logger . error ( e ) ;
} finally {
closeCallable ( ) ;
}
}
return powers ;
return powers ;
}
}
public void UPDATE_TRAINS ( final CharacterPower pow ) {
public void UPDATE_TRAINS ( final CharacterPower characterPower ) {
//skip update if nothing changed
//skip update if nothing changed
if ( ! pow . isTrained ( ) )
if ( ! characterPower . isTrained ( ) )
return ;
return ;
prepareCallable ( "UPDATE `dyn_character_power` SET `trains`=? WHERE `UID`=?" ) ;
try ( Connection connection = DbManager . getConnection ( ) ;
setShort ( 1 , ( short ) pow . getTrains ( ) ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `dyn_character_power` SET `trains`=? WHERE `UID`=?" ) ) {
setInt ( 2 , pow . getObjectUUID ( ) ) ;
executeUpdate ( ) ;
preparedStatement . setShort ( 1 , ( short ) characterPower . getTrains ( ) ) ;
pow . setTrained ( false ) ;
preparedStatement . setInt ( 2 , characterPower . getObjectUUID ( ) ) ;
preparedStatement . execute ( ) ;
} catch ( SQLException e ) {
Logger . error ( e ) ;
}
characterPower . setTrained ( false ) ;
}
}
public void updateDatabase ( final CharacterPower pow ) {
public void updateDatabase ( final CharacterPower pow ) {
if ( pow . getPower ( ) = = null ) {
if ( pow . getPower ( ) = = null ) {
Logger . error ( "Failed to find powersBase for Power " + pow . getObjectUUID ( ) ) ;
Logger . error ( "Failed to find powersBase for Power " + pow . getObjectUUID ( ) ) ;
return ;
return ;
}
}
if ( CharacterPower . getOwner ( pow ) = = null ) {
if ( CharacterPower . getOwner ( pow ) = = null ) {
Logger . error ( "Failed to find owner for Power " + pow . getObjectUUID ( ) ) ;
Logger . error ( "Failed to find owner for Power " + pow . getObjectUUID ( ) ) ;
return ;
return ;
}
}
try ( Connection connection = DbManager . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( "UPDATE `dyn_character_power` SET `PowersBaseToken`=?, `CharacterID`=?, `trains`=? WHERE `UID`=?" ) ) {
preparedStatement . setInt ( 1 , pow . getPower ( ) . getToken ( ) ) ;
preparedStatement . setInt ( 2 , CharacterPower . getOwner ( pow ) . getObjectUUID ( ) ) ;
preparedStatement . setShort ( 3 , ( short ) pow . getTrains ( ) ) ;
preparedStatement . setInt ( 4 , pow . getObjectUUID ( ) ) ;
preparedStatement . execute ( ) ;
} catch ( SQLException e ) {
Logger . error ( e ) ;
}
prepareCallable ( "UPDATE `dyn_character_power` SET `PowersBaseToken`=?, `CharacterID`=?, `trains`=? WHERE `UID`=?" ) ;
setInt ( 1 , pow . getPower ( ) . getToken ( ) ) ;
setInt ( 2 , CharacterPower . getOwner ( pow ) . getObjectUUID ( ) ) ;
setShort ( 3 , ( short ) pow . getTrains ( ) ) ;
setInt ( 4 , pow . getObjectUUID ( ) ) ;
executeUpdate ( ) ;
pow . setTrained ( false ) ;
pow . setTrained ( false ) ;
}
}
}
}