2018-07-06 13:30:00 +00:00
package com.eu.habbo.habbohotel.items ;
import com.eu.habbo.Emulator ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.AbstractMap ;
import java.util.HashMap ;
import java.util.Map ;
2019-05-26 21:14:53 +03:00
public class CrackableReward {
2018-07-06 13:30:00 +00:00
public final int itemId ;
public final int count ;
public final Map < Integer , Map . Entry < Integer , Integer > > prizes ;
public final String achievementTick ;
public final String achievementCracked ;
2018-12-22 10:39:00 +00:00
public final int requiredEffect ;
2019-05-16 16:56:20 +03:00
public final int subscriptionDuration ;
public final RedeemableSubscriptionType subscriptionType ;
2019-05-26 21:14:53 +03:00
public int totalChance ;
2018-07-06 13:30:00 +00:00
2019-05-26 21:14:53 +03:00
public CrackableReward ( ResultSet set ) throws SQLException {
2018-07-06 13:30:00 +00:00
this . itemId = set . getInt ( " item_id " ) ;
this . count = set . getInt ( " count " ) ;
this . achievementTick = set . getString ( " achievement_tick " ) ;
this . achievementCracked = set . getString ( " achievement_cracked " ) ;
2018-12-22 10:39:00 +00:00
this . requiredEffect = set . getInt ( " required_effect " ) ;
2019-05-16 16:56:20 +03:00
this . subscriptionDuration = set . getInt ( " subscription_duration " ) ;
this . subscriptionType = RedeemableSubscriptionType . fromString ( set . getString ( " subscription_type " ) ) ;
2018-07-06 13:30:00 +00:00
2019-03-18 01:22:00 +00:00
String [ ] prizes = set . getString ( " prizes " ) . split ( " ; " ) ;
2018-09-28 19:25:00 +00:00
this . prizes = new HashMap < > ( ) ;
2018-07-06 13:30:00 +00:00
2019-05-16 16:56:20 +03:00
if ( set . getString ( " prizes " ) . isEmpty ( ) ) return ;
2018-07-06 13:30:00 +00:00
this . totalChance = 0 ;
2019-05-26 21:14:53 +03:00
for ( String prize : prizes ) {
try {
2018-07-06 13:30:00 +00:00
int itemId = 0 ;
int chance = 100 ;
2019-05-26 21:14:53 +03:00
if ( prize . contains ( " : " ) & & prize . split ( " : " ) . length = = 2 ) {
2019-03-18 01:22:00 +00:00
itemId = Integer . valueOf ( prize . split ( " : " ) [ 0 ] ) ;
chance = Integer . valueOf ( prize . split ( " : " ) [ 1 ] ) ;
2019-08-05 19:35:26 +03:00
} else if ( prize . contains ( " : " ) ) {
Emulator . getLogging ( ) . logErrorLine ( " Invalid configuration of crackable prizes (item id: " + this . itemId + " ). ' " + prize + " ' format should be itemId:chance. " ) ;
2019-05-26 21:14:53 +03:00
} else {
2019-03-18 01:22:00 +00:00
itemId = Integer . valueOf ( prize . replace ( " : " , " " ) ) ;
2018-07-06 13:30:00 +00:00
}
2018-09-28 19:25:00 +00:00
this . prizes . put ( itemId , new AbstractMap . SimpleEntry < > ( this . totalChance , this . totalChance + chance ) ) ;
2018-07-06 13:30:00 +00:00
this . totalChance + = chance ;
2019-05-26 21:14:53 +03:00
} catch ( Exception e ) {
2020-05-03 01:46:07 +02:00
logger . error ( " Caught exception " , e ) ;
2018-07-06 13:30:00 +00:00
}
}
}
2019-05-26 21:14:53 +03:00
public int getRandomReward ( ) {
2019-05-16 16:56:20 +03:00
if ( this . prizes . size ( ) = = 0 ) return 0 ;
2018-07-06 13:30:00 +00:00
int random = Emulator . getRandom ( ) . nextInt ( this . totalChance ) ;
int notFound = 0 ;
2019-05-26 21:14:53 +03:00
for ( Map . Entry < Integer , Map . Entry < Integer , Integer > > set : this . prizes . entrySet ( ) ) {
2018-07-06 13:30:00 +00:00
notFound = set . getKey ( ) ;
2019-05-26 21:14:53 +03:00
if ( random > = set . getValue ( ) . getKey ( ) & & random < set . getValue ( ) . getValue ( ) ) {
2018-07-06 13:30:00 +00:00
return set . getKey ( ) ;
}
}
return notFound ;
}
}