2018-07-06 13:30:00 +00:00
package com.eu.habbo.habbohotel.rooms ;
import com.eu.habbo.Emulator ;
import com.eu.habbo.core.Disposable ;
2019-05-26 20:00:47 +03:00
import com.eu.habbo.habbohotel.achievements.AchievementManager ;
2020-02-01 14:09:59 +02:00
import com.eu.habbo.habbohotel.gameclients.GameClient ;
2018-07-06 13:30:00 +00:00
import com.eu.habbo.habbohotel.items.SoundTrack ;
import com.eu.habbo.habbohotel.items.interactions.InteractionJukeBox ;
import com.eu.habbo.habbohotel.items.interactions.InteractionMusicDisc ;
import com.eu.habbo.habbohotel.users.Habbo ;
import com.eu.habbo.habbohotel.users.HabboItem ;
2020-02-01 14:09:59 +02:00
import com.eu.habbo.messages.outgoing.inventory.AddHabboItemComposer ;
import com.eu.habbo.messages.outgoing.inventory.InventoryRefreshComposer ;
import com.eu.habbo.messages.outgoing.inventory.RemoveHabboItemComposer ;
2018-09-28 19:25:00 +00:00
import com.eu.habbo.messages.outgoing.rooms.items.jukebox.JukeBoxMySongsComposer ;
import com.eu.habbo.messages.outgoing.rooms.items.jukebox.JukeBoxNowPlayingMessageComposer ;
import com.eu.habbo.messages.outgoing.rooms.items.jukebox.JukeBoxPlayListComposer ;
2018-07-06 13:30:00 +00:00
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.ArrayList ;
import java.util.List ;
2020-02-01 14:09:59 +02:00
import java.util.stream.Collectors ;
2018-07-06 13:30:00 +00:00
2019-05-26 21:14:53 +03:00
public class TraxManager implements Disposable {
2018-07-06 13:30:00 +00:00
private final Room room ;
private final List < InteractionMusicDisc > songs = new ArrayList < > ( 0 ) ;
private int totalLength = 0 ;
2019-05-25 11:24:26 +03:00
private int startedTimestamp = 0 ;
2018-07-06 13:30:00 +00:00
private InteractionMusicDisc currentlyPlaying = null ;
private int playingIndex = 0 ;
2019-05-26 20:00:47 +03:00
private int cycleStartedTimestamp = 0 ;
private Habbo starter = null ;
2018-07-06 13:30:00 +00:00
private boolean disposed = false ;
2019-05-26 21:14:53 +03:00
public TraxManager ( Room room ) {
2018-07-06 13:30:00 +00:00
this . room = room ;
2019-05-26 21:14:53 +03:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " SELECT * FROM room_trax_playlist WHERE room_id = ? " ) ) {
2018-07-06 13:30:00 +00:00
statement . setInt ( 1 , this . room . getId ( ) ) ;
2019-05-26 21:14:53 +03:00
try ( ResultSet set = statement . executeQuery ( ) ) {
while ( set . next ( ) ) {
2020-02-01 14:09:59 +02:00
HabboItem musicDisc = Emulator . getGameEnvironment ( ) . getItemManager ( ) . loadHabboItem ( set . getInt ( " item_id " ) ) ;
if ( ! ( musicDisc instanceof InteractionMusicDisc ) | | musicDisc . getRoomId ( ) ! = - 1 ) {
try ( PreparedStatement stmt = connection . prepareStatement ( " DELETE FROM room_trax_playlist WHERE room_id = ? AND item_id = ? LIMIT 1 " ) ) {
stmt . setInt ( 1 , this . room . getId ( ) ) ;
stmt . setInt ( 2 , musicDisc . getId ( ) ) ;
stmt . execute ( ) ;
} catch ( SQLException e ) {
Emulator . getLogging ( ) . logSQLException ( e ) ;
return ;
}
} else {
2018-07-06 13:30:00 +00:00
SoundTrack track = Emulator . getGameEnvironment ( ) . getItemManager ( ) . getSoundTrack ( ( ( InteractionMusicDisc ) musicDisc ) . getSongId ( ) ) ;
2019-05-26 21:14:53 +03:00
if ( track ! = null ) {
2018-07-06 13:30:00 +00:00
this . songs . add ( ( InteractionMusicDisc ) musicDisc ) ;
this . totalLength + = track . getLength ( ) ;
}
}
}
}
2019-05-26 21:14:53 +03:00
} catch ( SQLException e ) {
2018-07-06 13:30:00 +00:00
Emulator . getLogging ( ) . logSQLException ( e ) ;
}
}
2019-05-26 21:14:53 +03:00
public void cycle ( ) {
if ( this . isPlaying ( ) ) {
if ( this . timePlaying ( ) > = this . totalLength ) {
2018-07-06 13:30:00 +00:00
this . play ( 0 ) ;
//restart
}
2019-05-26 21:14:53 +03:00
if ( this . currentSong ( ) ! = null & & Emulator . getIntUnixTimestamp ( ) > = this . startedTimestamp + this . currentSong ( ) . getLength ( ) ) {
2018-07-06 13:30:00 +00:00
this . play ( ( this . playingIndex + 1 ) % this . songs . size ( ) ) ;
}
}
}
2019-05-26 21:14:53 +03:00
public void play ( int index ) {
2019-05-26 20:00:47 +03:00
this . play ( index , null ) ;
}
2019-05-26 21:14:53 +03:00
public void play ( int index , Habbo starter ) {
if ( this . currentlyPlaying = = null ) {
for ( HabboItem item : this . room . getRoomSpecialTypes ( ) . getItemsOfType ( InteractionJukeBox . class ) ) {
2018-07-06 13:30:00 +00:00
item . setExtradata ( " 1 " ) ;
this . room . updateItem ( item ) ;
}
}
2019-05-26 21:14:53 +03:00
if ( ! this . songs . isEmpty ( ) ) {
2018-07-06 13:30:00 +00:00
index = index % this . songs . size ( ) ;
this . currentlyPlaying = this . songs . get ( index ) ;
2019-05-26 21:14:53 +03:00
if ( this . currentlyPlaying ! = null ) {
2018-07-06 13:30:00 +00:00
this . room . setJukeBoxActive ( true ) ;
this . startedTimestamp = Emulator . getIntUnixTimestamp ( ) ;
this . playingIndex = index ;
2019-05-26 20:00:47 +03:00
if ( starter ! = null ) {
this . starter = starter ;
this . cycleStartedTimestamp = Emulator . getIntUnixTimestamp ( ) ;
}
2018-07-06 13:30:00 +00:00
}
this . room . sendComposer ( new JukeBoxNowPlayingMessageComposer ( Emulator . getGameEnvironment ( ) . getItemManager ( ) . getSoundTrack ( this . currentlyPlaying . getSongId ( ) ) , this . playingIndex , 0 ) . compose ( ) ) ;
2019-05-26 21:14:53 +03:00
} else {
2018-07-06 13:30:00 +00:00
this . stop ( ) ;
}
}
2019-05-26 21:14:53 +03:00
public void stop ( ) {
2019-05-26 20:00:47 +03:00
if ( this . starter ! = null & & this . cycleStartedTimestamp > 0 ) {
AchievementManager . progressAchievement ( this . starter , Emulator . getGameEnvironment ( ) . getAchievementManager ( ) . getAchievement ( " MusicPlayer " ) , ( Emulator . getIntUnixTimestamp ( ) - cycleStartedTimestamp ) / 60 ) ;
}
2018-07-06 13:30:00 +00:00
this . room . setJukeBoxActive ( false ) ;
this . currentlyPlaying = null ;
this . startedTimestamp = 0 ;
2019-05-26 20:00:47 +03:00
this . cycleStartedTimestamp = 0 ;
this . starter = null ;
2018-07-06 13:30:00 +00:00
this . playingIndex = 0 ;
2019-05-26 21:14:53 +03:00
for ( HabboItem item : this . room . getRoomSpecialTypes ( ) . getItemsOfType ( InteractionJukeBox . class ) ) {
2018-07-06 13:30:00 +00:00
item . setExtradata ( " 0 " ) ;
this . room . updateItem ( item ) ;
}
this . room . sendComposer ( new JukeBoxNowPlayingMessageComposer ( null , - 1 , 0 ) . compose ( ) ) ;
}
2019-05-26 21:14:53 +03:00
public SoundTrack currentSong ( ) {
if ( ! this . songs . isEmpty ( ) & & this . playingIndex < this . songs . size ( ) ) {
2018-09-28 19:25:00 +00:00
return Emulator . getGameEnvironment ( ) . getItemManager ( ) . getSoundTrack ( this . songs . get ( this . playingIndex ) . getSongId ( ) ) ;
}
return null ;
2018-07-06 13:30:00 +00:00
}
2020-02-01 14:09:59 +02:00
public void addSong ( InteractionMusicDisc musicDisc , Habbo habbo ) {
SoundTrack track = Emulator . getGameEnvironment ( ) . getItemManager ( ) . getSoundTrack ( musicDisc . getSongId ( ) ) ;
2018-07-06 13:30:00 +00:00
2020-02-01 14:09:59 +02:00
if ( track ! = null ) {
this . totalLength + = track . getLength ( ) ;
this . songs . add ( musicDisc ) ;
2018-07-06 13:30:00 +00:00
2020-02-01 14:09:59 +02:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " INSERT INTO room_trax_playlist (room_id, item_id) VALUES (?, ?) " ) ) {
statement . setInt ( 1 , this . room . getId ( ) ) ;
statement . setInt ( 2 , musicDisc . getId ( ) ) ;
statement . execute ( ) ;
} catch ( SQLException e ) {
Emulator . getLogging ( ) . logSQLException ( e ) ;
return ;
2018-07-06 13:30:00 +00:00
}
2020-02-01 14:09:59 +02:00
this . room . sendComposer ( new JukeBoxPlayListComposer ( this . songs , this . totalLength ) . compose ( ) ) ;
musicDisc . setRoomId ( - 1 ) ;
musicDisc . needsUpdate ( true ) ;
habbo . getInventory ( ) . getItemsComponent ( ) . removeHabboItem ( musicDisc ) ;
habbo . getClient ( ) . sendResponse ( new RemoveHabboItemComposer ( musicDisc . getGiftAdjustedId ( ) ) ) ;
2018-07-06 13:30:00 +00:00
}
2020-02-01 14:09:59 +02:00
this . sendUpdatedSongList ( ) ;
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public void removeSong ( int itemId ) {
2018-07-06 13:30:00 +00:00
InteractionMusicDisc musicDisc = this . fromItemId ( itemId ) ;
2019-05-26 21:14:53 +03:00
if ( musicDisc ! = null ) {
2018-07-06 13:30:00 +00:00
this . songs . remove ( musicDisc ) ;
2019-05-26 21:14:53 +03:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " DELETE FROM room_trax_playlist WHERE room_id = ? AND item_id = ? LIMIT 1 " ) ) {
2018-07-06 13:30:00 +00:00
statement . setInt ( 1 , this . room . getId ( ) ) ;
statement . setInt ( 2 , itemId ) ;
statement . execute ( ) ;
2019-05-26 21:14:53 +03:00
} catch ( SQLException e ) {
2018-07-06 13:30:00 +00:00
Emulator . getLogging ( ) . logSQLException ( e ) ;
return ;
}
this . totalLength - = Emulator . getGameEnvironment ( ) . getItemManager ( ) . getSoundTrack ( musicDisc . getSongId ( ) ) . getLength ( ) ;
2019-05-26 21:14:53 +03:00
if ( this . currentlyPlaying = = musicDisc ) {
2018-07-06 13:30:00 +00:00
this . play ( this . playingIndex ) ;
}
this . room . sendComposer ( new JukeBoxPlayListComposer ( this . songs , this . totalLength ) . compose ( ) ) ;
2020-02-01 14:09:59 +02:00
musicDisc . setRoomId ( 0 ) ;
musicDisc . needsUpdate ( true ) ;
Habbo owner = Emulator . getGameEnvironment ( ) . getHabboManager ( ) . getHabbo ( musicDisc . getUserId ( ) ) ;
if ( owner ! = null ) {
owner . getInventory ( ) . getItemsComponent ( ) . addItem ( musicDisc ) ;
GameClient client = owner . getClient ( ) ;
if ( client ! = null ) {
client . sendResponse ( new AddHabboItemComposer ( musicDisc ) ) ;
client . sendResponse ( new InventoryRefreshComposer ( ) ) ;
}
}
2018-07-06 13:30:00 +00:00
}
2020-02-01 14:09:59 +02:00
this . sendUpdatedSongList ( ) ;
}
public void sendUpdatedSongList ( ) {
this . room . getHabbos ( ) . forEach ( h - > {
GameClient client = h . getClient ( ) ;
if ( client ! = null ) {
client . sendResponse ( new JukeBoxMySongsComposer ( this . myList ( h ) ) ) ;
}
} ) ;
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public int timePlaying ( ) {
2018-07-06 13:30:00 +00:00
return Emulator . getIntUnixTimestamp ( ) - this . startedTimestamp ;
}
2019-05-26 21:14:53 +03:00
public int totalLength ( ) {
2019-03-18 01:22:00 +00:00
return this . totalLength ;
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public List < InteractionMusicDisc > getSongs ( ) {
2019-03-18 01:22:00 +00:00
return this . songs ;
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public boolean isPlaying ( ) {
2018-07-06 13:30:00 +00:00
return this . currentlyPlaying ! = null ;
}
2019-05-26 21:14:53 +03:00
public List < SoundTrack > soundTrackList ( ) {
2018-07-06 13:30:00 +00:00
List < SoundTrack > trax = new ArrayList < > ( this . songs . size ( ) ) ;
2019-05-26 21:14:53 +03:00
for ( InteractionMusicDisc musicDisc : this . songs ) {
2018-07-06 13:30:00 +00:00
SoundTrack track = Emulator . getGameEnvironment ( ) . getItemManager ( ) . getSoundTrack ( musicDisc . getSongId ( ) ) ;
2019-05-26 21:14:53 +03:00
if ( track ! = null ) {
2018-07-06 13:30:00 +00:00
trax . add ( track ) ;
}
}
return trax ;
}
2020-02-01 14:09:59 +02:00
public List < InteractionMusicDisc > myList ( Habbo habbo ) {
return habbo . getInventory ( ) . getItemsComponent ( ) . getItems ( ) . valueCollection ( ) . stream ( )
. filter ( i - > i instanceof InteractionMusicDisc & & i . getRoomId ( ) = = 0 )
. map ( i - > ( InteractionMusicDisc ) i )
. collect ( Collectors . toList ( ) ) ;
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public InteractionMusicDisc fromItemId ( int itemId ) {
for ( InteractionMusicDisc musicDisc : this . songs ) {
if ( musicDisc . getId ( ) = = itemId ) {
2018-07-06 13:30:00 +00:00
return musicDisc ;
}
}
return null ;
}
2019-05-26 21:14:53 +03:00
public void clearPlayList ( ) {
2018-07-06 13:30:00 +00:00
this . songs . clear ( ) ;
2019-05-26 21:14:53 +03:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " DELETE FROM room_trax_playlist WHERE room_id = ? " ) ) {
2018-07-06 13:30:00 +00:00
statement . setInt ( 1 , this . room . getId ( ) ) ;
statement . execute ( ) ;
2019-05-26 21:14:53 +03:00
} catch ( SQLException e ) {
2018-07-06 13:30:00 +00:00
Emulator . getLogging ( ) . logSQLException ( e ) ;
}
}
2019-05-26 21:14:53 +03:00
public void updateCurrentPlayingSong ( Habbo habbo ) {
if ( this . isPlaying ( ) ) {
2018-07-06 13:30:00 +00:00
habbo . getClient ( ) . sendResponse ( new JukeBoxNowPlayingMessageComposer ( Emulator . getGameEnvironment ( ) . getItemManager ( ) . getSoundTrack ( this . currentlyPlaying . getSongId ( ) ) , this . playingIndex , 1000 * ( Emulator . getIntUnixTimestamp ( ) - this . startedTimestamp ) ) ) ;
2019-05-26 21:14:53 +03:00
} else {
2018-07-06 13:30:00 +00:00
habbo . getClient ( ) . sendResponse ( new JukeBoxNowPlayingMessageComposer ( null , - 1 , 0 ) ) ;
}
}
@Override
2019-05-26 21:14:53 +03:00
public void dispose ( ) {
2018-07-06 13:30:00 +00:00
this . disposed = true ;
}
@Override
2019-05-26 21:14:53 +03:00
public boolean disposed ( ) {
2018-07-06 13:30:00 +00:00
return this . disposed ;
}
}