2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.handlers ;
import engine.gameManager.DbManager ;
import engine.objects.Account ;
import engine.objects.PlayerCharacter ;
import engine.session.CSSession ;
import engine.util.StringUtils ;
import org.pmw.tinylog.Logger ;
import java.net.InetAddress ;
2023-05-21 17:27:45 -04:00
import java.sql.Connection ;
import java.sql.PreparedStatement ;
2022-04-30 09:41:17 -04:00
import java.sql.ResultSet ;
import java.sql.SQLException ;
public class dbCSSessionHandler extends dbHandlerBase {
public dbCSSessionHandler ( ) {
this . localClass = CSSession . class ;
this . localObjectType = engine . Enum . GameObjectType . valueOf ( this . localClass . getSimpleName ( ) ) ;
}
public boolean ADD_CSSESSION ( String secKey , Account acc , InetAddress inet , String machineID ) {
2023-05-21 17:27:45 -04:00
try ( Connection connection = DbManager . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( " INSERT INTO `dyn_session` (`secretKey`, `accountID`, `discordAccount`, `sessionIP`, machineID) VALUES (?,?,?,INET_ATON(?),?) " ) ) {
preparedStatement . setString ( 1 , secKey ) ;
preparedStatement . setLong ( 2 , acc . getObjectUUID ( ) ) ;
preparedStatement . setString ( 3 , acc . discordAccount ) ;
preparedStatement . setString ( 4 , StringUtils . InetAddressToClientString ( inet ) ) ;
preparedStatement . setString ( 5 , machineID ) ;
return ( preparedStatement . executeUpdate ( ) > 0 ) ;
} catch ( SQLException e ) {
Logger . error ( e ) ;
}
2023-05-23 09:17:06 -04:00
return false ;
2023-05-21 17:27:45 -04:00
}
2022-04-30 09:41:17 -04:00
public boolean DELETE_UNUSED_CSSESSION ( String secKey ) {
2023-05-21 17:27:45 -04:00
try ( Connection connection = DbManager . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( " DELETE FROM `dyn_session` WHERE `secretKey`=? && `characterID` IS NULL " ) ) {
preparedStatement . setString ( 1 , secKey ) ;
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 boolean DELETE_CSSESSION ( String secKey ) {
2023-05-21 17:27:45 -04:00
try ( Connection connection = DbManager . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( " DELETE FROM `dyn_session` WHERE `secretKey`=? " ) ) {
preparedStatement . setString ( 1 , secKey ) ;
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 boolean UPDATE_CSSESSION ( String secKey , int charID ) {
2023-05-21 17:27:45 -04:00
try ( Connection connection = DbManager . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( " UPDATE `dyn_session` SET `characterID`=? WHERE `secretKey`=? " ) ) {
preparedStatement . setInt ( 1 , charID ) ;
preparedStatement . setString ( 2 , secKey ) ;
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 CSSession GET_CSSESSION ( String secKey ) {
2023-05-21 17:27:45 -04:00
2022-04-30 09:41:17 -04:00
CSSession css = null ;
2023-05-21 17:27:45 -04:00
try ( Connection connection = DbManager . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( " SELECT `accountID`, `characterID`, `machineID` FROM `dyn_session` WHERE `secretKey`=? " ) ) {
2022-04-30 09:41:17 -04:00
2023-05-21 17:27:45 -04:00
preparedStatement . setString ( 1 , secKey ) ;
ResultSet rs = preparedStatement . executeQuery ( ) ;
if ( rs . next ( ) )
2023-05-23 10:50:00 -04:00
css = new CSSession ( secKey , DbManager . AccountQueries . GET_ACCOUNT ( rs . getInt ( " accountID " ) ) , PlayerCharacter . getPlayerCharacter ( rs . getInt ( " characterID " ) ) , rs . getString ( " machineID " ) ) ;
2023-05-21 17:27:45 -04:00
2022-04-30 09:41:17 -04:00
} catch ( SQLException e ) {
2023-05-21 17:27:45 -04:00
Logger . error ( e ) ;
2022-04-30 09:41:17 -04:00
}
2023-05-21 17:27:45 -04:00
2022-04-30 09:41:17 -04:00
return css ;
}
}