2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package discord ;
import engine.Enum ;
import engine.gameManager.ConfigManager ;
import org.pmw.tinylog.Logger ;
import java.sql.* ;
import java.util.ArrayList ;
2023-01-15 11:29:24 -05:00
import java.util.HashMap ;
2022-04-30 09:41:17 -04:00
import java.util.List ;
public class Database {
public static Boolean online ;
static {
try {
Class . forName ( " com.mysql.cj.jdbc.Driver " ) . newInstance ( ) ;
} catch ( InstantiationException | ClassNotFoundException | IllegalAccessException e ) {
// TODO Auto-generated catch block
Logger . error ( e . toString ( ) ) ;
online = false ;
}
}
2023-01-15 11:29:24 -05:00
// Load and instance the JDBC Driver
public String sqlURI ;
2022-04-30 09:41:17 -04:00
public void configureDatabase ( ) {
// Build connection string from JSON object.
sqlURI = " jdbc:mysql:// " ;
sqlURI + = ConfigManager . MB_DATABASE_ADDRESS . getValue ( ) + ':' + ConfigManager . MB_DATABASE_PORT . getValue ( ) ;
2023-01-15 11:29:24 -05:00
sqlURI + = '/' + ConfigManager . MB_DATABASE_NAME . getValue ( ) + '?' ;
2022-04-30 09:41:17 -04:00
sqlURI + = " useServerPrepStmts=true " ;
sqlURI + = " &cachePrepStmts=false " ;
sqlURI + = " &cacheCallableStmts=true " ;
sqlURI + = " &characterEncoding=utf8 " ;
online = true ;
}
public boolean updateAccountPassword ( String discordAccountID , String newPassword ) {
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement updateStatement = connection . prepareStatement ( " call discordUpdatePassword(?, ?) " ) ) {
2022-04-30 09:41:17 -04:00
2023-05-17 15:11:26 -04:00
updateStatement . setString ( 1 , discordAccountID ) ;
updateStatement . setString ( 2 , newPassword ) ;
2022-04-30 09:41:17 -04:00
2023-05-17 15:11:26 -04:00
updateStatement . executeUpdate ( ) ;
2022-04-30 09:41:17 -04:00
return true ;
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
return false ;
}
}
public boolean updateAccountStatus ( String discordAccountID , Enum . AccountStatus accountStatus ) {
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement updateStatement = connection . prepareStatement ( " update obj_account set `status` = ? where `discordAccount` = ? " ) ) {
2022-04-30 09:41:17 -04:00
2023-05-17 15:11:26 -04:00
updateStatement . setString ( 1 , accountStatus . name ( ) ) ;
updateStatement . setString ( 2 , discordAccountID ) ;
2022-04-30 09:41:17 -04:00
2023-05-17 15:11:26 -04:00
updateStatement . executeUpdate ( ) ;
2022-04-30 09:41:17 -04:00
return true ;
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
return false ;
}
}
public boolean registerDiscordAccount ( String discordAccountID , String discordUserName , String discordPassword ) {
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ) {
2023-05-17 15:11:26 -04:00
PreparedStatement registerStatement = connection . prepareStatement ( " call discordAccountRegister(?, ?, ?) " ) ;
2022-04-30 09:41:17 -04:00
2023-05-17 15:11:26 -04:00
registerStatement . setString ( 1 , discordAccountID ) ;
registerStatement . setString ( 2 , discordUserName ) ;
registerStatement . setString ( 3 , discordPassword ) ;
2022-04-30 09:41:17 -04:00
2023-05-17 15:11:26 -04:00
registerStatement . execute ( ) ;
2022-04-30 09:41:17 -04:00
return true ;
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
return false ;
}
}
public List < DiscordAccount > getDiscordAccounts ( String discordAccountID ) {
DiscordAccount discordAccount ;
List < DiscordAccount > discordAccounts = new ArrayList < > ( ) ;
String queryString = " SELECT * FROM obj_account where discordAccount = ? " ;
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement accountQuery = connection . prepareStatement ( queryString ) ; ) {
2022-04-30 09:41:17 -04:00
// Discord account name based lookup
accountQuery . setString ( 1 , discordAccountID ) ;
ResultSet rs = accountQuery . executeQuery ( ) ;
while ( rs . next ( ) ) {
discordAccount = new DiscordAccount ( ) ;
discordAccount . discordAccount = rs . getString ( " discordAccount " ) ;
discordAccount . gameAccountName = rs . getString ( " acct_uname " ) ;
discordAccount . status = Enum . AccountStatus . valueOf ( rs . getString ( " status " ) ) ;
discordAccount . isDiscordAdmin = rs . getByte ( " discordAdmin " ) ; // Registration date cannot be null
Timestamp registrationDate = rs . getTimestamp ( " registrationDate " ) ;
discordAccount . registrationDate = registrationDate . toLocalDateTime ( ) ;
// Load last Update Request datetime
Timestamp lastUpdateRequest = rs . getTimestamp ( " lastUpdateRequest " ) ;
if ( lastUpdateRequest ! = null )
discordAccount . lastUpdateRequest = lastUpdateRequest . toLocalDateTime ( ) ;
else
discordAccount . lastUpdateRequest = null ;
discordAccounts . add ( discordAccount ) ;
}
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
}
return discordAccounts ;
}
public String getTrashDetail ( ) {
String outString = " accountName characterName machineID ip count \ n " ;
outString + = " --------------------------------------------- \ n " ;
String queryString = " SELECT * FROM dyn_trash_detail; " ;
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement trashQuery = connection . prepareStatement ( queryString ) ) {
2022-04-30 09:41:17 -04:00
ResultSet rs = trashQuery . executeQuery ( ) ;
while ( rs . next ( ) ) {
outString + = rs . getString ( " accountName " ) + " " ;
outString + = rs . getString ( " characterName " ) + " " ;
outString + = rs . getString ( " machineID " ) + " " ;
outString + = rs . getString ( " ip " ) + " " ;
outString + = rs . getInt ( " count " ) + " \ n " ;
}
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
}
return outString ;
}
public String getTrashList ( ) {
String outString = " " ;
int counter = 0 ;
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement trashQuery = connection . prepareStatement ( " SELECT DISTINCT `characterName` FROM dyn_trash_detail; " ) ) {
2022-04-30 09:41:17 -04:00
ResultSet rs = trashQuery . executeQuery ( ) ;
while ( rs . next ( ) ) {
2023-01-15 11:29:24 -05:00
outString + = rs . getString ( " characterName " ) ;
2022-04-30 09:41:17 -04:00
counter + + ;
if ( counter > 2 ) {
outString + = " \ n " ;
2023-01-15 11:29:24 -05:00
counter = 0 ;
} else
2022-04-30 09:41:17 -04:00
outString + = " " ;
}
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
}
if ( outString . length ( ) > 1500 )
2023-01-15 11:29:24 -05:00
return outString . substring ( 0 , 1500 ) ;
else
return outString ;
2022-04-30 09:41:17 -04:00
}
2023-01-15 11:29:24 -05:00
2022-04-30 09:41:17 -04:00
public int getTrashCount ( ) {
int trashCount = 0 ;
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement trashQuery = connection . prepareStatement ( " SELECT count(distinct characterName) FROM dyn_trash_detail; " ) ) {
2022-04-30 09:41:17 -04:00
ResultSet rs = trashQuery . executeQuery ( ) ;
while ( rs . next ( ) ) {
trashCount = rs . getInt ( 1 ) ;
}
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
}
return trashCount ;
}
2023-01-16 06:24:24 -05:00
public void setAdminEventAsRead ( int adminEvent ) {
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement updateAdminEvent = connection . prepareStatement ( " UPDATE dyn_admin_log SET `SentFlag` = 1 WHERE `entry` = ? " ) ) {
2023-01-16 06:24:24 -05:00
updateAdminEvent . setInt ( 1 , adminEvent ) ;
updateAdminEvent . executeUpdate ( ) ;
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
online = false ;
}
}
2023-01-15 11:29:24 -05:00
public HashMap < Integer , String > getAdminEvents ( ) {
HashMap < Integer , String > outMap = new HashMap < > ( ) ;
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement adminLogQuery = connection . prepareStatement ( " SELECT * from dyn_admin_log where `SentFlag` = 0 " ) ) {
2023-01-15 11:29:24 -05:00
ResultSet rs = adminLogQuery . executeQuery ( ) ;
String workString ;
while ( rs . next ( ) ) {
workString = " ___Admin Event___ \ n " +
" Character: " + rs . getString ( " charName " ) + " \ n " +
" Event: " + rs . getString ( " eventString " ) ;
outMap . put ( rs . getInt ( " entry " ) , workString ) ;
}
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
}
return outMap ;
}
public String getTrashFile ( ) {
2022-04-30 09:41:17 -04:00
String outString = " machineID : count \ n " ;
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement trashQuery = connection . prepareStatement ( " SELECT * FROM dyn_trash; " ) ) {
2022-04-30 09:41:17 -04:00
ResultSet rs = trashQuery . executeQuery ( ) ;
while ( rs . next ( ) ) {
outString + = rs . getString ( " machineID " ) + " : " ;
outString + = rs . getInt ( " count " ) + " \ n " ;
}
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
}
return outString ;
}
public List < DiscordAccount > getAccountsByDiscordName ( String accountName , Boolean exact ) {
DiscordAccount discordAccount ;
List < DiscordAccount > discordAccounts = new ArrayList < > ( ) ;
String searchString ;
String queryString ;
if ( exact . equals ( true ) )
searchString = accountName + " #% " ;
else
searchString = accountName + " %#% " ;
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement nameQuery = connection . prepareStatement ( " SELECT * FROM obj_account where `acct_uname` LIKE ? " ) ) {
2022-04-30 09:41:17 -04:00
nameQuery . setString ( 1 , searchString ) ;
ResultSet rs = nameQuery . executeQuery ( ) ;
while ( rs . next ( ) ) {
discordAccount = new DiscordAccount ( ) ;
discordAccount . discordAccount = rs . getString ( " discordAccount " ) ;
discordAccount . gameAccountName = rs . getString ( " acct_uname " ) ;
discordAccount . status = Enum . AccountStatus . valueOf ( rs . getString ( " status " ) ) ;
// Registration date cannot be null
Timestamp registrationDate = rs . getTimestamp ( " registrationDate " ) ;
discordAccount . registrationDate = registrationDate . toLocalDateTime ( ) ;
// Load last Update Request datetime
Timestamp lastUpdateRequest = rs . getTimestamp ( " lastUpdateRequest " ) ;
if ( lastUpdateRequest ! = null )
discordAccount . lastUpdateRequest = lastUpdateRequest . toLocalDateTime ( ) ;
else
discordAccount . lastUpdateRequest = null ;
discordAccounts . add ( discordAccount ) ;
}
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
}
return discordAccounts ;
}
2023-05-19 07:57:20 -04:00
public String getPopulationString ( ) {
2022-04-30 09:41:17 -04:00
String popString = " " ;
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement getPopString = connection . prepareStatement ( " CALL GET_POPULATION_STRING() " ) ; ) {
2022-04-30 09:41:17 -04:00
ResultSet rs = getPopString . executeQuery ( ) ;
if ( rs . next ( ) )
popString = rs . getString ( " popstring " ) ;
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
}
return popString ;
}
public void invalidateLoginCache ( String discordAccountID ) {
try ( Connection connection = DriverManager . getConnection ( sqlURI , ConfigManager . MB_DATABASE_USER . getValue ( ) ,
2023-05-17 15:11:26 -04:00
ConfigManager . MB_DATABASE_PASS . getValue ( ) ) ;
PreparedStatement invalidateAccounts = connection . prepareStatement ( " INSERT IGNORE INTO login_cachelist (`UID`) SELECT `UID` from `obj_account` WHERE `discordAccount` = ? " ) ) {
2022-04-30 09:41:17 -04:00
invalidateAccounts . setString ( 1 , discordAccountID ) ;
invalidateAccounts . executeUpdate ( ) ;
} catch ( SQLException e ) {
Logger . error ( e . toString ( ) ) ;
2023-01-15 11:29:24 -05:00
online = false ;
2022-04-30 09:41:17 -04:00
}
}
}