2020-01-19 18:18:15 -05:00
package com.eu.habbo.habbohotel.modtool ;
import com.eu.habbo.Emulator ;
import com.eu.habbo.habbohotel.users.Habbo ;
import com.eu.habbo.plugin.events.sanctions.SanctionEvent ;
import gnu.trove.map.hash.THashMap ;
2020-05-04 22:24:09 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2020-01-19 18:18:15 -05:00
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.Date ;
public class ModToolSanctions {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory . getLogger ( ModToolSanctions . class ) ;
2020-01-19 18:18:15 -05:00
private final THashMap < Integer , ArrayList < ModToolSanctionItem > > sanctionHashmap ;
private final THashMap < Integer , ModToolSanctionLevelItem > sanctionLevelsHashmap ;
public ModToolSanctions ( ) {
long millis = System . currentTimeMillis ( ) ;
this . sanctionHashmap = new THashMap < > ( ) ;
this . sanctionLevelsHashmap = new THashMap < > ( ) ;
this . loadModSanctions ( ) ;
2020-05-04 22:24:09 +02:00
LOGGER . info ( " Sanctions Manager -> Loaded! ( " + ( System . currentTimeMillis ( ) - millis ) + " MS) " ) ;
2020-01-19 18:18:15 -05:00
}
public synchronized void loadModSanctions ( ) {
this . sanctionHashmap . clear ( ) ;
this . sanctionLevelsHashmap . clear ( ) ;
this . loadSanctionLevels ( ) ;
}
private void loadSanctionLevels ( ) {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " SELECT * FROM sanction_levels " ) ) {
try ( ResultSet set = statement . executeQuery ( ) ) {
while ( set . next ( ) ) {
this . sanctionLevelsHashmap . put ( set . getInt ( " level " ) , new ModToolSanctionLevelItem ( set . getInt ( " level " ) , set . getString ( " type " ) , set . getInt ( " hour_length " ) , set . getInt ( " probation_days " ) ) ) ;
}
}
} catch ( SQLException e ) {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2020-01-19 18:18:15 -05:00
}
}
public ModToolSanctionLevelItem getSanctionLevelItem ( int sanctionLevel ) {
return this . sanctionLevelsHashmap . get ( sanctionLevel ) ;
}
public THashMap < Integer , ArrayList < ModToolSanctionItem > > getSanctions ( int habboId ) {
synchronized ( this . sanctionHashmap ) {
2020-01-22 17:07:24 +01:00
this . sanctionHashmap . clear ( ) ;
2020-01-19 18:18:15 -05:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " SELECT * FROM sanctions WHERE habbo_id = ? ORDER BY id ASC " ) ) {
statement . setInt ( 1 , habboId ) ;
try ( ResultSet set = statement . executeQuery ( ) ) {
while ( set . next ( ) ) {
if ( this . sanctionHashmap . get ( set . getInt ( " habbo_id " ) ) = = null ) {
this . sanctionHashmap . put ( set . getInt ( " habbo_id " ) , new ArrayList < > ( ) ) ;
}
ModToolSanctionItem item = new ModToolSanctionItem ( set . getInt ( " id " ) , set . getInt ( " habbo_id " ) , set . getInt ( " sanction_level " ) , set . getInt ( " probation_timestamp " ) , set . getBoolean ( " is_muted " ) , set . getInt ( " mute_duration " ) , set . getInt ( " trade_locked_until " ) , set . getString ( " reason " ) ) ;
if ( ! this . sanctionHashmap . get ( set . getInt ( " habbo_id " ) ) . contains ( item ) ) {
this . sanctionHashmap . get ( set . getInt ( " habbo_id " ) ) . add ( item ) ;
}
}
}
} catch ( SQLException e ) {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2020-01-19 18:18:15 -05:00
}
return this . sanctionHashmap ;
}
}
private void insertSanction ( int habboId , int sanctionLevel , int probationTimestamp , String reason , int tradeLockedUntil , boolean isMuted , int muteDuration ) {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " INSERT INTO sanctions (habbo_id, sanction_level, probation_timestamp, reason, trade_locked_until, is_muted, mute_duration) VALUES (?, ?, ?, ?, ?, ?, ?) " ) ) {
statement . setInt ( 1 , habboId ) ;
statement . setInt ( 2 , sanctionLevel ) ;
statement . setInt ( 3 , probationTimestamp ) ;
statement . setString ( 4 , reason ) ;
statement . setInt ( 5 , tradeLockedUntil ) ;
statement . setBoolean ( 6 , isMuted ) ;
statement . setInt ( 7 , muteDuration ) ;
statement . execute ( ) ;
} catch ( SQLException e ) {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2020-01-19 18:18:15 -05:00
}
}
2020-01-22 17:07:24 +01:00
public void updateSanction ( int rowId , int probationTimestamp ) {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE sanctions SET probation_timestamp = ? WHERE id = ? " ) ) {
statement . setInt ( 1 , probationTimestamp ) ;
statement . setInt ( 2 , rowId ) ;
2020-01-19 18:18:15 -05:00
statement . execute ( ) ;
} catch ( SQLException e ) {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2020-01-19 18:18:15 -05:00
}
}
public void updateTradeLockedUntil ( int rowId , int tradeLockedUntil ) {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE sanctions SET trade_locked_until = ? WHERE id = ? " ) ) {
statement . setInt ( 1 , tradeLockedUntil ) ;
statement . setInt ( 2 , rowId ) ;
statement . execute ( ) ;
} catch ( SQLException e ) {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2020-01-19 18:18:15 -05:00
}
}
public void updateMuteDuration ( int rowId , int muteDuration ) {
2020-01-22 00:03:24 +00:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE sanctions SET mute_duration = ? WHERE id = ? " ) ) {
2020-01-19 18:18:15 -05:00
statement . setInt ( 1 , muteDuration ) ;
statement . setInt ( 2 , rowId ) ;
statement . execute ( ) ;
} catch ( SQLException e ) {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2020-01-19 18:18:15 -05:00
}
}
public void run ( int habboId , Habbo self , int sanctionLevel , int cfhTopic , String reason , int tradeLockedUntil , boolean isMuted , int muteDuration ) {
sanctionLevel + + ;
ModToolSanctionLevelItem sanctionLevelItem = getSanctionLevelItem ( sanctionLevel ) ;
insertSanction ( habboId , sanctionLevel , buildProbationTimestamp ( sanctionLevelItem ) , reason , tradeLockedUntil , isMuted , muteDuration ) ;
runSanctionBasedOnLevel ( sanctionLevelItem , habboId , reason , cfhTopic , self , muteDuration ) ;
Emulator . getPluginManager ( ) . fireEvent ( new SanctionEvent ( self , Emulator . getGameEnvironment ( ) . getHabboManager ( ) . getHabbo ( habboId ) , sanctionLevel ) ) ;
}
private int buildProbationTimestamp ( ModToolSanctionLevelItem sanctionLevelItem ) {
return Emulator . getIntUnixTimestamp ( ) + ( sanctionLevelItem . sanctionProbationDays * 24 * 60 * 60 ) ;
}
public int getProbationDays ( ModToolSanctionLevelItem sanctionLevelItem ) {
return sanctionLevelItem . sanctionProbationDays ;
}
private void runSanctionBasedOnLevel ( ModToolSanctionLevelItem sanctionLevelItem , int habboId , String reason , int cfhTopic , Habbo self , int muteDuration ) {
Habbo habbo = Emulator . getGameEnvironment ( ) . getHabboManager ( ) . getHabbo ( habboId ) ;
int muteDurationSeconds = 0 ;
if ( muteDuration > 0 ) {
Date muteDurationDate = new Date ( ( long ) muteDuration * 1000 ) ;
long diff = muteDurationDate . getTime ( ) - Emulator . getDate ( ) . getTime ( ) ;
muteDurationSeconds = Math . toIntExact ( diff / 1000 ) ;
}
switch ( sanctionLevelItem . sanctionType ) {
case " ALERT " : habbo . alert ( reason ) ; break ;
case " BAN " : Emulator . getGameEnvironment ( ) . getModToolManager ( ) . ban ( habboId , self , reason , sanctionLevelItem . sanctionHourLength , ModToolBanType . ACCOUNT , cfhTopic ) ; break ;
2020-01-22 20:56:56 +01:00
case " MUTE " : habbo . mute ( muteDurationSeconds = = 0 ? 3600 : muteDurationSeconds , false ) ; break ;
2020-01-19 18:18:15 -05:00
default : break ;
}
}
public String getSanctionType ( ModToolSanctionLevelItem sanctionLevelItem ) {
return sanctionLevelItem . sanctionType ;
}
public int getTimeOfSanction ( ModToolSanctionLevelItem sanctionLevelItem ) {
return sanctionLevelItem . sanctionHourLength ;
}
}