2018-07-06 15:30:00 +02:00
package com.eu.habbo.messages.rcon ;
import com.eu.habbo.Emulator ;
import com.eu.habbo.habbohotel.users.Habbo ;
import com.google.gson.Gson ;
2020-05-04 22:24:09 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-07-06 15:30:00 +02:00
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.SQLException ;
2019-05-26 20:14:53 +02:00
public class GiveCredits extends RCONMessage < GiveCredits . JSONGiveCredits > {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory . getLogger ( GiveCredits . class ) ;
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public GiveCredits ( ) {
2018-07-06 15:30:00 +02:00
super ( JSONGiveCredits . class ) ;
}
@Override
2019-05-26 20:14:53 +02:00
public void handle ( Gson gson , JSONGiveCredits object ) {
2018-07-06 15:30:00 +02:00
Habbo habbo = Emulator . getGameEnvironment ( ) . getHabboManager ( ) . getHabbo ( object . user_id ) ;
2019-05-26 20:14:53 +02:00
if ( habbo ! = null ) {
2018-07-06 15:30:00 +02:00
habbo . giveCredits ( object . credits ) ;
2019-05-26 20:14:53 +02:00
} else {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE users SET credits = credits + ? WHERE id = ? LIMIT 1 " ) ) {
2018-07-06 15:30:00 +02:00
statement . setInt ( 1 , object . credits ) ;
statement . setInt ( 2 , object . user_id ) ;
statement . execute ( ) ;
2019-05-26 20:14:53 +02:00
} catch ( SQLException e ) {
2018-07-06 15:30:00 +02:00
this . status = RCONMessage . SYSTEM_ERROR ;
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
this . message = " offline " ;
}
}
2019-05-26 20:14:53 +02:00
static class JSONGiveCredits {
2018-10-07 00:28:00 +02:00
public int user_id ;
public int credits ;
2018-07-06 15:30:00 +02:00
}
}