2018-07-06 15:30:00 +02:00
package com.eu.habbo.core ;
import com.eu.habbo.Emulator ;
import com.eu.habbo.habbohotel.users.Habbo ;
import com.eu.habbo.messages.incoming.friends.SearchUserEvent ;
import com.eu.habbo.messages.incoming.navigator.SearchRoomsEvent ;
import com.eu.habbo.threading.runnables.AchievementUpdater ;
import com.eu.habbo.util.callback.HTTPPostStatus ;
import com.eu.habbo.util.callback.HTTPVersionCheck ;
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.SQLException ;
import java.sql.Statement ;
public class CleanerThread implements Runnable {
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public static final int DELAY = 10000 ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public static final int RELOAD_HALL_OF_FAME = 1800 ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public static final int RELOAD_NEWS_LIST = 3600 ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public static final int REMOVE_INACTIVE_ROOMS = 120 ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public static final int REMOVE_INACTIVE_GUILDS = 60 ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public static final int REMOVE_INACTIVE_TOURS = 600 ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public static final int SAVE_ERROR_LOGS = 30 ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private static final int CALLBACK_TIME = 60 * 15 ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private static int LAST_HOF_RELOAD = Emulator . getIntUnixTimestamp ( ) ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private static int LAST_NL_RELOAD = Emulator . getIntUnixTimestamp ( ) ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private static int LAST_INACTIVE_ROOMS_CLEARED = Emulator . getIntUnixTimestamp ( ) ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private static int LAST_INACTIVE_GUILDS_CLEARED = Emulator . getIntUnixTimestamp ( ) ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private static int LAST_INACTIVE_TOURS_CLEARED = Emulator . getIntUnixTimestamp ( ) ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private static int LAST_ERROR_LOGS_SAVED = Emulator . getIntUnixTimestamp ( ) ;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private static int LAST_DAILY_REFILL = Emulator . getIntUnixTimestamp ( ) ;
private static int LAST_CALLBACK = Emulator . getIntUnixTimestamp ( ) ;
public CleanerThread ( )
{
databaseCleanup ( ) ;
Emulator . getThreading ( ) . run ( this , DELAY ) ;
Emulator . getThreading ( ) . run ( new AchievementUpdater ( ) ) ;
Emulator . getThreading ( ) . run ( new HTTPVersionCheck ( ) , 10000 ) ;
}
@Override
public void run ( )
{
Emulator . getThreading ( ) . run ( this , DELAY ) ;
int time = Emulator . getIntUnixTimestamp ( ) ;
if ( time - LAST_HOF_RELOAD > RELOAD_HALL_OF_FAME )
{
Emulator . getGameEnvironment ( ) . getHotelViewManager ( ) . getHallOfFame ( ) . reload ( ) ;
LAST_HOF_RELOAD = time ;
}
if ( time - LAST_NL_RELOAD > RELOAD_NEWS_LIST )
{
Emulator . getGameEnvironment ( ) . getHotelViewManager ( ) . getNewsList ( ) . reload ( ) ;
LAST_NL_RELOAD = time ;
}
if ( time - LAST_INACTIVE_ROOMS_CLEARED > REMOVE_INACTIVE_ROOMS )
{
Emulator . getGameEnvironment ( ) . getRoomManager ( ) . clearInactiveRooms ( ) ;
LAST_INACTIVE_ROOMS_CLEARED = time ;
}
if ( time - LAST_INACTIVE_GUILDS_CLEARED > REMOVE_INACTIVE_GUILDS )
{
Emulator . getGameEnvironment ( ) . getGuildManager ( ) . clearInactiveGuilds ( ) ;
Emulator . getGameEnvironment ( ) . getGuildForumManager ( ) . clearInactiveForums ( ) ;
LAST_INACTIVE_GUILDS_CLEARED = time ;
}
if ( time - LAST_INACTIVE_TOURS_CLEARED > REMOVE_INACTIVE_TOURS )
{
Emulator . getGameEnvironment ( ) . getGuideManager ( ) . cleanup ( ) ;
LAST_INACTIVE_TOURS_CLEARED = time ;
}
if ( time - LAST_ERROR_LOGS_SAVED > SAVE_ERROR_LOGS )
{
Emulator . getLogging ( ) . saveLogs ( ) ;
LAST_ERROR_LOGS_SAVED = time ;
}
if ( time - LAST_CALLBACK > CALLBACK_TIME )
{
Emulator . getThreading ( ) . run ( new HTTPPostStatus ( ) ) ;
LAST_CALLBACK = time ;
}
if ( time - LAST_DAILY_REFILL > Emulator . getConfig ( ) . getInt ( " hotel.refill.daily " ) )
{
refillDailyRespects ( ) ;
LAST_DAILY_REFILL = time ;
}
SearchRoomsEvent . cachedResults . clear ( ) ;
SearchUserEvent . cachedResults . clear ( ) ;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
void databaseCleanup ( )
{
refillDailyRespects ( ) ;
int time = Emulator . getIntUnixTimestamp ( ) ;
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) )
{
try ( Statement statement = connection . createStatement ( ) )
{
statement . execute ( " UPDATE users SET online = '0' WHERE online = '1' " ) ;
statement . execute ( " UPDATE rooms SET users = '0' WHERE users > 0 " ) ;
statement . execute ( " DELETE FROM room_mutes WHERE ends < " + time ) ;
statement . execute ( " DELETE FROM room_bans WHERE ends < " + time ) ;
statement . execute ( " DELETE users_favorite_rooms FROM users_favorite_rooms LEFT JOIN rooms ON room_id = rooms.id WHERE rooms.id IS NULL " ) ;
}
try ( PreparedStatement statement = connection . prepareStatement ( " UPDATE users_effects SET total = total - 1 WHERE activation_timestamp < ? AND activation_timestamp != 0 " ) )
{
statement . setInt ( 1 , Emulator . getIntUnixTimestamp ( ) - 86400 ) ;
statement . execute ( ) ;
}
try ( Statement statement = connection . createStatement ( ) )
{
statement . execute ( " DELETE FROM users_effects WHERE total <= 0 " ) ;
}
}
catch ( SQLException e )
{
Emulator . getLogging ( ) . logSQLException ( e ) ;
}
Emulator . getLogging ( ) . logStart ( " Database -> Cleaned! " ) ;
}
public void refillDailyRespects ( )
{
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE users_settings SET daily_respect_points = ?, daily_pet_respect_points = ? " ) )
{
statement . setInt ( 1 , Emulator . getConfig ( ) . getInt ( " hotel.daily.respect " ) ) ;
statement . setInt ( 2 , Emulator . getConfig ( ) . getInt ( " hotel.daily.respect.pets " ) ) ;
statement . executeUpdate ( ) ;
}
catch ( SQLException e )
{
Emulator . getLogging ( ) . logSQLException ( e ) ;
}
if ( Emulator . isReady )
{
for ( Habbo habbo : Emulator . getGameEnvironment ( ) . getHabboManager ( ) . getOnlineHabbos ( ) . values ( ) )
{
habbo . getHabboStats ( ) . petRespectPointsToGive = Emulator . getConfig ( ) . getInt ( " hotel.daily.respect " ) ;
habbo . getHabboStats ( ) . respectPointsToGive = Emulator . getConfig ( ) . getInt ( " hotel.daily.respect.pets " ) ;
}
}
}
}