2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.catalog ;
import com.eu.habbo.Emulator ;
import com.eu.habbo.habbohotel.items.FurnitureType ;
import com.eu.habbo.habbohotel.items.Item ;
import com.eu.habbo.messages.ISerialize ;
import com.eu.habbo.messages.ServerMessage ;
import gnu.trove.set.hash.THashSet ;
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.HashMap ;
2019-05-26 20:14:53 +02:00
public class CatalogItem implements ISerialize , Runnable , Comparable < CatalogItem > {
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
int id ;
2019-05-26 20:14:53 +02:00
int limitedStack ;
2019-03-18 02:22:00 +01:00
private int pageId ;
private String itemId ;
private String name ;
private int credits ;
private int points ;
private short pointsType ;
private int amount ;
private boolean allowGift = false ;
private int limitedSells ;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private String extradata ;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private boolean clubOnly ;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private boolean haveOffer ;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int offerId ;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private boolean needsUpdate ;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
private int orderNumber ;
2018-11-17 14:28:00 +01:00
2019-03-18 02:22:00 +01:00
private HashMap < Integer , Integer > bundle ;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public CatalogItem ( ResultSet set ) throws SQLException {
2018-07-06 15:30:00 +02:00
this . load ( set ) ;
this . needsUpdate = false ;
}
2019-05-26 20:14:53 +02:00
public static boolean haveOffer ( CatalogItem item ) {
if ( ! item . haveOffer )
return false ;
if ( item . getAmount ( ) ! = 1 )
return false ;
if ( item . isLimited ( ) )
return false ;
if ( item . bundle . size ( ) > 1 )
return false ;
if ( item . getName ( ) . toLowerCase ( ) . startsWith ( " cf_ " ) | | item . getName ( ) . toLowerCase ( ) . startsWith ( " cfc_ " ) )
return false ;
for ( Item i : item . getBaseItems ( ) ) {
if ( i . getName ( ) . toLowerCase ( ) . startsWith ( " cf_ " ) | | i . getName ( ) . toLowerCase ( ) . startsWith ( " cfc_ " ) | | i . getName ( ) . toLowerCase ( ) . startsWith ( " rentable_bot " ) )
return false ;
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
return ! item . getName ( ) . toLowerCase ( ) . startsWith ( " rentable_bot_ " ) ;
}
public void update ( ResultSet set ) throws SQLException {
2018-07-06 15:30:00 +02:00
this . load ( set ) ;
}
2019-05-26 20:14:53 +02:00
private void load ( ResultSet set ) throws SQLException {
this . id = set . getInt ( " id " ) ;
this . pageId = set . getInt ( " page_id " ) ;
this . itemId = set . getString ( " item_Ids " ) ;
this . name = set . getString ( " catalog_name " ) ;
this . credits = set . getInt ( " cost_credits " ) ;
this . points = set . getInt ( " cost_points " ) ;
this . pointsType = set . getShort ( " points_type " ) ;
this . amount = set . getInt ( " amount " ) ;
2018-07-06 15:30:00 +02:00
this . limitedStack = set . getInt ( " limited_stack " ) ;
this . limitedSells = set . getInt ( " limited_sells " ) ;
2019-05-26 20:14:53 +02:00
this . extradata = set . getString ( " extradata " ) ;
this . clubOnly = set . getBoolean ( " club_only " ) ;
this . haveOffer = set . getBoolean ( " have_offer " ) ;
this . offerId = set . getInt ( " offer_id " ) ;
2018-11-17 14:28:00 +01:00
this . orderNumber = set . getInt ( " order_number " ) ;
2018-07-06 15:30:00 +02:00
this . bundle = new HashMap < > ( ) ;
this . loadBundle ( ) ;
}
2019-05-26 20:14:53 +02:00
public int getId ( ) {
2018-07-06 15:30:00 +02:00
return this . id ;
}
2019-05-26 20:14:53 +02:00
public int getPageId ( ) {
2018-07-06 15:30:00 +02:00
return this . pageId ;
}
2019-05-26 20:14:53 +02:00
public void setPageId ( int pageId ) {
2018-07-06 15:30:00 +02:00
this . pageId = pageId ;
}
2019-05-26 20:14:53 +02:00
public String getItemId ( ) {
2018-07-06 15:30:00 +02:00
return this . itemId ;
}
2019-05-26 20:14:53 +02:00
public void setItemId ( String itemId ) {
2018-07-06 15:30:00 +02:00
this . itemId = itemId ;
}
2019-05-26 20:14:53 +02:00
public String getName ( ) {
2018-07-06 15:30:00 +02:00
return this . name ;
}
2019-05-26 20:14:53 +02:00
public int getCredits ( ) {
2018-07-06 15:30:00 +02:00
return this . credits ;
}
2019-05-26 20:14:53 +02:00
public int getPoints ( ) {
2018-07-06 15:30:00 +02:00
return this . points ;
}
2019-05-26 20:14:53 +02:00
public int getPointsType ( ) {
2018-07-06 15:30:00 +02:00
return this . pointsType ;
}
2019-05-26 20:14:53 +02:00
public int getAmount ( ) {
2018-07-06 15:30:00 +02:00
return this . amount ;
}
2019-05-26 20:14:53 +02:00
public int getLimitedStack ( ) {
2018-07-06 15:30:00 +02:00
return this . limitedStack ;
}
2019-05-26 20:14:53 +02:00
public int getLimitedSells ( ) {
2018-07-06 15:30:00 +02:00
CatalogLimitedConfiguration ltdConfig = Emulator . getGameEnvironment ( ) . getCatalogManager ( ) . getLimitedConfig ( this ) ;
2019-05-26 20:14:53 +02:00
if ( ltdConfig ! = null ) {
2018-07-06 15:30:00 +02:00
return this . limitedStack - ltdConfig . available ( ) ;
}
return this . limitedStack ;
}
2019-05-26 20:14:53 +02:00
public String getExtradata ( ) {
2018-07-06 15:30:00 +02:00
return this . extradata ;
}
2019-05-26 20:14:53 +02:00
public boolean isClubOnly ( ) {
2018-07-06 15:30:00 +02:00
return this . clubOnly ;
}
2019-05-26 20:14:53 +02:00
public boolean isHaveOffer ( ) {
2018-07-06 15:30:00 +02:00
return this . haveOffer ;
}
2019-05-26 20:14:53 +02:00
public int getOfferId ( ) {
2018-07-06 15:30:00 +02:00
return this . offerId ;
}
2019-05-26 20:14:53 +02:00
public boolean isLimited ( ) {
2018-07-06 15:30:00 +02:00
return this . limitedStack > 0 ;
}
2019-05-26 20:14:53 +02:00
private int getOrderNumber ( ) {
2018-11-17 14:28:00 +01:00
return this . orderNumber ;
}
2019-05-26 20:14:53 +02:00
public synchronized void sellRare ( ) {
2018-07-06 15:30:00 +02:00
this . limitedSells + + ;
this . needsUpdate = true ;
2019-05-26 20:14:53 +02:00
if ( this . limitedSells = = this . limitedStack ) {
2018-07-06 15:30:00 +02:00
Emulator . getGameEnvironment ( ) . getCatalogManager ( ) . moveCatalogItem ( this , Emulator . getConfig ( ) . getInt ( " catalog.ltd.page.soldout " ) ) ;
}
Emulator . getThreading ( ) . run ( this ) ;
}
2019-05-26 20:14:53 +02:00
public THashSet < Item > getBaseItems ( ) {
2018-07-06 15:30:00 +02:00
THashSet < Item > items = new THashSet < > ( ) ;
2019-05-26 20:14:53 +02:00
if ( ! this . itemId . isEmpty ( ) ) {
2018-07-06 15:30:00 +02:00
String [ ] itemIds = this . itemId . split ( " ; " ) ;
2019-05-26 20:14:53 +02:00
for ( String itemId : itemIds ) {
2018-07-06 15:30:00 +02:00
if ( itemId . isEmpty ( ) )
continue ;
2019-05-26 20:14:53 +02:00
if ( itemId . contains ( " : " ) ) {
2018-07-06 15:30:00 +02:00
itemId = itemId . split ( " : " ) [ 0 ] ;
}
2019-03-18 02:22:00 +01:00
int identifier ;
2019-05-26 20:14:53 +02:00
try {
2018-09-28 21:25:00 +02:00
2019-05-26 20:14:53 +02:00
identifier = Integer . parseInt ( itemId ) ;
} catch ( Exception e ) {
2020-05-03 01:46:07 +02:00
logger . info ( " Invalid value ( " + itemId + " ) for items_base column for catalog_item id ( " + this . id + " ). Value must be integer or of the format of integer:amount;integer:amount " ) ;
2018-09-28 21:25:00 +02:00
continue ;
}
2019-05-26 20:14:53 +02:00
if ( identifier > 0 ) {
2018-07-06 15:30:00 +02:00
Item item = Emulator . getGameEnvironment ( ) . getItemManager ( ) . getItem ( identifier ) ;
if ( item ! = null )
items . add ( item ) ;
}
}
}
return items ;
}
2019-05-26 20:14:53 +02:00
public int getItemAmount ( int id ) {
if ( this . bundle . containsKey ( id ) )
2018-07-06 15:30:00 +02:00
return this . bundle . get ( id ) ;
else
return this . amount ;
}
2019-05-26 20:14:53 +02:00
public HashMap < Integer , Integer > getBundle ( ) {
2018-07-06 15:30:00 +02:00
return this . bundle ;
}
2019-05-26 20:14:53 +02:00
public void loadBundle ( ) {
2018-07-06 15:30:00 +02:00
int intItemId ;
2019-05-26 20:14:53 +02:00
if ( this . itemId . contains ( " ; " ) ) {
try {
2018-07-06 15:30:00 +02:00
String [ ] itemIds = this . itemId . split ( " ; " ) ;
2019-05-26 20:14:53 +02:00
for ( String itemId : itemIds ) {
if ( itemId . contains ( " : " ) ) {
2018-07-06 15:30:00 +02:00
String [ ] data = itemId . split ( " : " ) ;
2019-05-26 20:14:53 +02:00
if ( data . length > 1 & & Integer . parseInt ( data [ 0 ] ) > 0 & & Integer . parseInt ( data [ 1 ] ) > 0 ) {
2018-07-06 15:30:00 +02:00
this . bundle . put ( Integer . parseInt ( data [ 0 ] ) , Integer . parseInt ( data [ 1 ] ) ) ;
}
2019-05-26 20:14:53 +02:00
} else {
if ( ! itemId . isEmpty ( ) ) {
2018-07-06 15:30:00 +02:00
intItemId = ( Integer . parseInt ( itemId ) ) ;
this . bundle . put ( intItemId , 1 ) ;
}
}
}
2019-05-26 20:14:53 +02:00
} catch ( Exception e ) {
2019-03-18 02:22:00 +01:00
Emulator . getLogging ( ) . logDebugLine ( " Failed to load " + this . itemId ) ;
2020-05-03 01:46:07 +02:00
logger . error ( " Caught exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
} else {
try {
2018-07-06 15:30:00 +02:00
Item item = Emulator . getGameEnvironment ( ) . getItemManager ( ) . getItem ( Integer . valueOf ( this . itemId ) ) ;
2019-05-26 20:14:53 +02:00
if ( item ! = null ) {
2018-07-06 15:30:00 +02:00
this . allowGift = item . allowGift ( ) ;
}
2019-05-26 20:14:53 +02:00
} catch ( Exception e ) {
2018-07-06 15:30:00 +02:00
}
}
}
@Override
2019-05-26 20:14:53 +02:00
public void serialize ( ServerMessage message ) {
2018-07-06 15:30:00 +02:00
message . appendInt ( this . getId ( ) ) ;
message . appendString ( this . getName ( ) ) ;
message . appendBoolean ( false ) ;
message . appendInt ( this . getCredits ( ) ) ;
message . appendInt ( this . getPoints ( ) ) ;
message . appendInt ( this . getPointsType ( ) ) ;
message . appendBoolean ( this . allowGift ) ; //Can gift
THashSet < Item > items = this . getBaseItems ( ) ;
message . appendInt ( items . size ( ) ) ;
2019-05-26 20:14:53 +02:00
for ( Item item : items ) {
2018-07-06 15:30:00 +02:00
message . appendString ( item . getType ( ) . code . toLowerCase ( ) ) ;
2019-05-26 20:14:53 +02:00
if ( item . getType ( ) = = FurnitureType . BADGE ) {
2018-07-06 15:30:00 +02:00
message . appendString ( item . getName ( ) ) ;
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
message . appendInt ( item . getSpriteId ( ) ) ;
2019-05-26 20:14:53 +02:00
if ( this . getName ( ) . contains ( " wallpaper_single " ) | | this . getName ( ) . contains ( " floor_single " ) | | this . getName ( ) . contains ( " landscape_single " ) ) {
2018-07-06 15:30:00 +02:00
message . appendString ( this . getName ( ) . split ( " _ " ) [ 2 ] ) ;
2019-05-26 20:14:53 +02:00
} else if ( item . getName ( ) . contains ( " bot " ) & & item . getType ( ) = = FurnitureType . ROBOT ) {
2018-07-06 15:30:00 +02:00
boolean lookFound = false ;
2019-05-26 20:14:53 +02:00
for ( String s : this . getExtradata ( ) . split ( " ; " ) ) {
if ( s . startsWith ( " figure: " ) ) {
2018-07-06 15:30:00 +02:00
lookFound = true ;
message . appendString ( s . replace ( " figure: " , " " ) ) ;
break ;
}
}
2019-05-26 20:14:53 +02:00
if ( ! lookFound ) {
2018-07-06 15:30:00 +02:00
message . appendString ( this . getExtradata ( ) ) ;
}
2019-05-26 20:14:53 +02:00
} else if ( item . getType ( ) = = FurnitureType . ROBOT ) {
2018-07-06 15:30:00 +02:00
message . appendString ( this . getExtradata ( ) ) ;
2019-05-26 20:14:53 +02:00
} else if ( item . getName ( ) . equalsIgnoreCase ( " poster " ) ) {
2018-07-06 15:30:00 +02:00
message . appendString ( this . getExtradata ( ) ) ;
2019-05-26 20:14:53 +02:00
} else if ( this . getName ( ) . startsWith ( " SONG " ) ) {
2018-07-06 15:30:00 +02:00
message . appendString ( this . getExtradata ( ) ) ;
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
message . appendString ( " " ) ;
}
message . appendInt ( this . getItemAmount ( item . getId ( ) ) ) ;
message . appendBoolean ( this . isLimited ( ) ) ;
2019-05-26 20:14:53 +02:00
if ( this . isLimited ( ) ) {
2018-07-06 15:30:00 +02:00
message . appendInt ( this . getLimitedStack ( ) ) ;
message . appendInt ( this . getLimitedStack ( ) - this . getLimitedSells ( ) ) ;
}
}
}
2018-10-07 00:28:00 +02:00
message . appendInt ( this . clubOnly ) ;
2018-07-06 15:30:00 +02:00
message . appendBoolean ( haveOffer ( this ) ) ;
message . appendBoolean ( false ) ; //unknown
message . appendString ( this . name + " .png " ) ;
}
@Override
2019-05-26 20:14:53 +02:00
public void run ( ) {
if ( this . needsUpdate ) {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE catalog_items SET limited_sells = ?, page_id = ? WHERE id = ? " ) ) {
2018-07-06 15:30:00 +02:00
statement . setInt ( 1 , this . getLimitedSells ( ) ) ;
statement . setInt ( 2 , this . pageId ) ;
statement . setInt ( 3 , this . getId ( ) ) ;
statement . execute ( ) ;
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
}
this . needsUpdate = false ;
}
}
@SuppressWarnings ( " NullableProblems " )
@Override
2019-05-26 20:14:53 +02:00
public int compareTo ( CatalogItem catalogItem ) {
if ( CatalogManager . SORT_USING_ORDERNUM ) {
2018-11-17 14:28:00 +01:00
return this . getOrderNumber ( ) - catalogItem . getOrderNumber ( ) ;
2019-05-26 20:14:53 +02:00
} else {
2018-11-17 14:28:00 +01:00
return this . getId ( ) - catalogItem . getId ( ) ;
}
2018-07-06 15:30:00 +02:00
}
}