2018-07-06 15:30:00 +02:00
package com.eu.habbo.core ;
import com.eu.habbo.Emulator ;
2019-05-11 00:45:07 +02:00
import com.eu.habbo.habbohotel.guilds.forums.ForumThread ;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.users.Habbo ;
import com.eu.habbo.messages.incoming.friends.SearchUserEvent ;
import com.eu.habbo.messages.incoming.navigator.SearchRoomsEvent ;
2022-04-10 18:56:28 +02:00
import com.eu.habbo.messages.outgoing.users.UserDataComposer ;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.threading.runnables.AchievementUpdater ;
2020-05-03 01:46:07 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2019-04-30 21:47:43 +02:00
2018-07-06 15:30:00 +02:00
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.SQLException ;
import java.sql.Statement ;
2018-10-07 00:28:00 +02:00
import java.util.Map ;
2018-07-06 15:30:00 +02:00
public class CleanerThread implements Runnable {
2020-05-03 01:46:07 +02:00
private static final Logger LOGGER = LoggerFactory . getLogger ( CleanerThread . class ) ;
2018-07-08 23:32:00 +02:00
2020-05-03 01:46:07 +02:00
private static final int DELAY = 10000 ;
private static final int RELOAD_HALL_OF_FAME = 1800 ;
private static final int RELOAD_NEWS_LIST = 3600 ;
private static final int REMOVE_INACTIVE_ROOMS = 120 ;
private static final int REMOVE_INACTIVE_GUILDS = 60 ;
private static final int REMOVE_INACTIVE_TOURS = 600 ;
private static final int SAVE_ERROR_LOGS = 30 ;
private static final int CLEAR_CACHED_VALUES = 60 * 60 ;
2019-05-26 20:14:53 +02:00
private static final int CALLBACK_TIME = 60 * 15 ;
2018-07-06 15:30:00 +02:00
private static int LAST_HOF_RELOAD = Emulator . getIntUnixTimestamp ( ) ;
private static int LAST_NL_RELOAD = Emulator . getIntUnixTimestamp ( ) ;
private static int LAST_INACTIVE_ROOMS_CLEARED = Emulator . getIntUnixTimestamp ( ) ;
private static int LAST_INACTIVE_GUILDS_CLEARED = Emulator . getIntUnixTimestamp ( ) ;
private static int LAST_INACTIVE_TOURS_CLEARED = Emulator . getIntUnixTimestamp ( ) ;
private static int LAST_ERROR_LOGS_SAVED = Emulator . getIntUnixTimestamp ( ) ;
private static int LAST_DAILY_REFILL = Emulator . getIntUnixTimestamp ( ) ;
private static int LAST_CALLBACK = Emulator . getIntUnixTimestamp ( ) ;
2018-10-07 00:28:00 +02:00
private static int LAST_HABBO_CACHE_CLEARED = Emulator . getIntUnixTimestamp ( ) ;
2019-05-26 20:14:53 +02:00
public CleanerThread ( ) {
2019-03-18 02:22:00 +01:00
this . databaseCleanup ( ) ;
2018-07-06 15:30:00 +02:00
Emulator . getThreading ( ) . run ( this , DELAY ) ;
Emulator . getThreading ( ) . run ( new AchievementUpdater ( ) ) ;
2019-05-26 20:14:53 +02:00
// Emulator.getThreading().run(new HTTPVersionCheck(), 10000);
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public void run ( ) {
2018-07-06 15:30:00 +02:00
Emulator . getThreading ( ) . run ( this , DELAY ) ;
int time = Emulator . getIntUnixTimestamp ( ) ;
2019-05-26 20:14:53 +02:00
if ( time - LAST_HOF_RELOAD > RELOAD_HALL_OF_FAME ) {
2018-07-06 15:30:00 +02:00
Emulator . getGameEnvironment ( ) . getHotelViewManager ( ) . getHallOfFame ( ) . reload ( ) ;
LAST_HOF_RELOAD = time ;
}
2019-05-26 20:14:53 +02:00
if ( time - LAST_NL_RELOAD > RELOAD_NEWS_LIST ) {
2018-07-06 15:30:00 +02:00
Emulator . getGameEnvironment ( ) . getHotelViewManager ( ) . getNewsList ( ) . reload ( ) ;
LAST_NL_RELOAD = time ;
}
2019-05-26 20:14:53 +02:00
if ( time - LAST_INACTIVE_ROOMS_CLEARED > REMOVE_INACTIVE_ROOMS ) {
2018-07-06 15:30:00 +02:00
Emulator . getGameEnvironment ( ) . getRoomManager ( ) . clearInactiveRooms ( ) ;
LAST_INACTIVE_ROOMS_CLEARED = time ;
}
2019-05-26 20:14:53 +02:00
if ( time - LAST_INACTIVE_GUILDS_CLEARED > REMOVE_INACTIVE_GUILDS ) {
2018-07-06 15:30:00 +02:00
Emulator . getGameEnvironment ( ) . getGuildManager ( ) . clearInactiveGuilds ( ) ;
2019-05-11 00:45:07 +02:00
ForumThread . clearCache ( ) ;
2018-07-06 15:30:00 +02:00
LAST_INACTIVE_GUILDS_CLEARED = time ;
}
2019-05-26 20:14:53 +02:00
if ( time - LAST_INACTIVE_TOURS_CLEARED > REMOVE_INACTIVE_TOURS ) {
2018-07-06 15:30:00 +02:00
Emulator . getGameEnvironment ( ) . getGuideManager ( ) . cleanup ( ) ;
LAST_INACTIVE_TOURS_CLEARED = time ;
}
2019-05-26 20:14:53 +02:00
if ( time - LAST_ERROR_LOGS_SAVED > SAVE_ERROR_LOGS ) {
2020-05-03 01:46:07 +02:00
Emulator . getDatabaseLogger ( ) . save ( ) ;
2018-07-06 15:30:00 +02:00
LAST_ERROR_LOGS_SAVED = time ;
}
2019-05-26 20:14:53 +02:00
if ( time - LAST_CALLBACK > CALLBACK_TIME ) {
// Emulator.getThreading().run(new HTTPPostStatus());
2018-07-06 15:30:00 +02:00
LAST_CALLBACK = time ;
}
2019-05-26 20:14:53 +02:00
if ( time - LAST_DAILY_REFILL > Emulator . getConfig ( ) . getInt ( " hotel.refill.daily " ) ) {
2019-03-18 02:22:00 +01:00
this . refillDailyRespects ( ) ;
2018-07-06 15:30:00 +02:00
LAST_DAILY_REFILL = time ;
}
2019-05-26 20:14:53 +02:00
if ( time - LAST_HABBO_CACHE_CLEARED > CLEAR_CACHED_VALUES ) {
2019-03-18 02:22:00 +01:00
this . clearCachedValues ( ) ;
2018-10-07 00:28:00 +02:00
LAST_HABBO_CACHE_CLEARED = time ;
}
2018-07-06 15:30:00 +02:00
SearchRoomsEvent . cachedResults . clear ( ) ;
SearchUserEvent . cachedResults . clear ( ) ;
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
void databaseCleanup ( ) {
2019-03-18 02:22:00 +01:00
this . refillDailyRespects ( ) ;
2018-07-06 15:30:00 +02:00
int time = Emulator . getIntUnixTimestamp ( ) ;
2019-05-26 20:14:53 +02:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ) {
try ( Statement statement = connection . createStatement ( ) ) {
2018-07-06 15:30:00 +02:00
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 " ) ;
}
2019-12-26 21:37:25 +01:00
try ( PreparedStatement statement = connection . prepareStatement ( " UPDATE users_effects SET total = total - 1 WHERE activation_timestamp + duration < ? AND activation_timestamp > 0 AND duration > 0 " ) ) {
statement . setInt ( 1 , Emulator . getIntUnixTimestamp ( ) ) ;
2018-07-06 15:30:00 +02:00
statement . execute ( ) ;
}
2019-05-26 20:14:53 +02:00
try ( Statement statement = connection . createStatement ( ) ) {
2018-07-06 15:30:00 +02:00
statement . execute ( " DELETE FROM users_effects WHERE total <= 0 " ) ;
}
2019-05-26 20:14:53 +02:00
} catch ( SQLException e ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
2020-05-03 01:46:07 +02:00
LOGGER . info ( " Database -> Cleaned! " ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
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 = ? " ) ) {
2018-07-06 15:30:00 +02:00
statement . setInt ( 1 , Emulator . getConfig ( ) . getInt ( " hotel.daily.respect " ) ) ;
statement . setInt ( 2 , Emulator . getConfig ( ) . getInt ( " hotel.daily.respect.pets " ) ) ;
statement . executeUpdate ( ) ;
2019-05-26 20:14:53 +02:00
} catch ( SQLException e ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
if ( Emulator . isReady ) {
for ( Habbo habbo : Emulator . getGameEnvironment ( ) . getHabboManager ( ) . getOnlineHabbos ( ) . values ( ) ) {
2021-01-23 19:51:56 +01:00
habbo . getHabboStats ( ) . respectPointsToGive = Emulator . getConfig ( ) . getInt ( " hotel.daily.respect " ) ;
habbo . getHabboStats ( ) . petRespectPointsToGive = Emulator . getConfig ( ) . getInt ( " hotel.daily.respect.pets " ) ;
2022-04-10 18:56:28 +02:00
habbo . getClient ( ) . sendResponse ( new UserDataComposer ( habbo ) ) ;
2018-07-06 15:30:00 +02:00
}
}
}
2018-10-07 00:28:00 +02:00
2019-05-26 20:14:53 +02:00
private void clearCachedValues ( ) {
2019-03-18 02:22:00 +01:00
Habbo habbo ;
2019-05-26 20:14:53 +02:00
for ( Map . Entry < Integer , Habbo > map : Emulator . getGameEnvironment ( ) . getHabboManager ( ) . getOnlineHabbos ( ) . entrySet ( ) ) {
2018-10-07 00:28:00 +02:00
habbo = map . getValue ( ) ;
2019-05-26 20:14:53 +02:00
try {
if ( habbo ! = null ) {
2018-10-07 00:28:00 +02:00
habbo . clearCaches ( ) ;
}
2019-05-26 20:14:53 +02:00
} catch ( Exception e ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught exception " , e ) ;
2018-10-07 00:28:00 +02:00
}
}
}
2018-07-06 15:30:00 +02:00
}