📄 multigroupmessagerouter.java
字号:
ChatList myChatList = player.getChatList();
if( myChatList == null )
return; // no chats in the room
// 4 - We send the CHAT ROOMS available to our client...
Hashtable chatRooms = myChatList.getChatRooms();
synchronized( chatRooms ) {
Iterator it = chatRooms.values().iterator();
while( it.hasNext() ) {
ChatRoom cRoom = (ChatRoom) it.next();
player.sendMessage( new ChatRoomCreatedMessage( cRoom.getPrimaryKey(),
cRoom.getName(), cRoom.getCreatorPrimaryKey() ) );
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To remove a player from this group.
*
* Call this method when a player is removed from the map. If the player is arriving
* from another room call movePlayer.
*
* @param player player to remove
* @return true if the player was removed successfully, false if an error occured.
*/
public boolean removePlayer( Player player ) {
// 1 - We remove this player from our list
if( !super.removePlayer(player) )
return false; // non-existent player
// 2 - We send remove messages to local & near players
sendMessage( new RemovePlayerFromRoomMessage( player.getPrimaryKey(), thisRoom.getLocation() ),
null,
EXTENDED_GROUP );
// 3 - Remove from the chat
PlayerImpl playerImpl = (PlayerImpl) player;
if( !playerImpl.getCurrentChatPrimaryKey().equals( ChatRoom.DEFAULT_CHAT ) ) {
RemPlayerFromChatRoomMsgBehaviour remPlayerFromChat
= new RemPlayerFromChatRoomMsgBehaviour( player.getPrimaryKey(),
playerImpl.getCurrentChatPrimaryKey() );
try{
remPlayerFromChat.doBehaviour( player );
}catch( Exception e ) {
Debug.signal( Debug.ERROR, this, e );
playerImpl.setCurrentChatPrimaryKey( ChatRoom.DEFAULT_CHAT );
}
}
else
sendMessage( new RemPlayerFromChatRoomMessage( player.getPrimaryKey(), ChatRoom.DEFAULT_CHAT ) );
return true;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To remove all the players of this group. The default implementation of this method
* just removes all the players WITHOUT sending any messages.
*/
public void removeAllPlayers() {
super.removeAllPlayers();
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To find a player by its primary key. We first search in the local group and then
* extend our search to near groups.
*
* @param primaryKey player to find
* @return null if not found, the player otherwise
*/
public Player getPlayer( String primaryKey ) {
Player p = (Player) players.get( primaryKey );
if(p!=null)
return p;
// we extend the search to near rooms
for( int i=0; i<nearRooms.length; i++ ) {
p = (Player) nearRooms[i].getMessageRouter().getPlayers().get( primaryKey );
if(p!=null)
return p;
}
return null; // not found
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To move a player from this group to another. The player location is changed.
*
* @param player player to move
* @return true if the player was moved successfully, false if an error occured.
*/
public boolean movePlayer( Player player, WotlasLocation targetLocation ) {
// 1 - We remove the player from our router...
if( !super.removePlayer(player) )
return false; // non-existent player
// 2 - Check the target room...
int targetRoomID = targetLocation.getRoomID();
Room targetRoom = null;
for( int i=0; i<nearRooms.length; i++ )
if( nearRooms[i].getRoomID()==targetRoomID ) {
targetRoom = nearRooms[i];
break;
}
if(targetRoom==null) {
Debug.signal( Debug.ERROR, this, "Target room not found !" );
return false;
}
// 3 - Send approriate Remove messages
RemovePlayerFromRoomMessage rMsg = new RemovePlayerFromRoomMessage(
player.getPrimaryKey(), player.getLocation() );
for( int i=0; i<nearRooms.length; i++ )
if( nearRooms[i].getRoomID()!=targetRoomID )
nearRooms[i].getMessageRouter().sendMessage(rMsg);
sendMessage( new RemPlayerFromChatRoomMessage( player.getPrimaryKey(), ChatRoom.DEFAULT_CHAT ) );
// 4 - Send appropriate Location changes & Messages
player.setLocation( targetRoom.getLocation() );
targetRoom.getMessageRouter().getPlayers().put(player.getPrimaryKey(),player);
LocationChangeMessage lMsg = new LocationChangeMessage( player.getPrimaryKey(),
player.getLocation(), 0, 0, 0.0f );
sendMessage( lMsg ); // to this room
targetRoom.getMessageRouter().sendMessage( lMsg, player ); // to the target room
// 5 - We ask the moved player to clean his ghosts
player.sendMessage( new CleanGhostsMessage( player.getPrimaryKey(), player.getLocation() ) );
// 6 - Send appropriate Add Player Messages
// to the neighbours of the target room where we are now
// We also send remaining Players & Doors Messages to our player
AddPlayerToRoomMessage aMsg = new AddPlayerToRoomMessage( null, player );
if(targetRoom.getRoomLinks()!=null)
for( int i=0; i<targetRoom.getRoomLinks().length; i++ ) {
Room otherRoom = targetRoom.getRoomLinks()[i].getRoom1();
if( otherRoom==targetRoom )
otherRoom = targetRoom.getRoomLinks()[i].getRoom2();
if( otherRoom==thisRoom )
continue;
player.sendMessage( new DoorsStateMessage( otherRoom ) );
player.sendMessage( new RoomPlayerDataMessage( otherRoom, player ) );
Hashtable otherPlayers = otherRoom.getMessageRouter().getPlayers();
synchronized( otherPlayers ) {
Iterator it = otherPlayers.values().iterator();
while( it.hasNext() ) {
Player p = (Player) it.next();
aMsg.setOtherPlayer(p); // needed for the LieManager to know who is
p.sendMessage( aMsg ); // asking for the player's name....
}
}
}
// 7 - We send CHAT DATA to the added player
( (MultiGroupMessageRouter) targetRoom.getMessageRouter() ).updateChatInformation( (PlayerImpl) player );
return true;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To send a list of messages to the specified group with the exception of a player.
* @param msg message to send to the group
* @param exceptThisPlayer player to except from the send of messages, if the
* given player is null the message will be sent to everyone in the selected
* groups.
* @param groupOption gives the groups to send the message to. See the constants
* defined in this class : LOCAL_GROUP, EXTENDED_GROUP, EXC_EXTENDED_GROUP
*/
public void sendMessages( NetMessage msg[], Player exceptThisPlayer, byte groupOption ) {
if( groupOption!=EXC_EXTENDED_GROUP ) {
// We send the messages to the local group.
synchronized( players ) {
Iterator it = players.values().iterator();
while( it.hasNext() ) {
Player p = (Player) it.next();
if( p!=exceptThisPlayer )
for( int i=0; i< msg.length; i++ )
p.sendMessage( msg[i] );
}
}
}
if( groupOption!=LOCAL_GROUP ) {
// We send the messages to near groups.
for( int i=0; i<nearRooms.length; i++ )
nearRooms[i].getMessageRouter().sendMessages( msg, exceptThisPlayer, LOCAL_GROUP );
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -