📄 multigroupmessagerouter.java
字号:
/*
* Light And Shadow. A Persistent Universe based on Robert Jordan's Wheel of Time Books.
* Copyright (C) 2001-2002 WOTLAS Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package wotlas.server.router;
import wotlas.server.PlayerImpl;
import wotlas.server.message.chat.*;
import wotlas.common.*;
import wotlas.common.universe.*;
import wotlas.common.router.*;
import wotlas.common.chat.*;
import wotlas.common.message.description.*;
import wotlas.common.message.movement.*;
import wotlas.common.message.chat.*;
import wotlas.libs.net.NetMessage;
import wotlas.utils.Debug;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Iterator;
/** A message router for Rooms which follows a 1-near step policy.
*
* @author Aldiss
*/
public class MultiGroupMessageRouter extends MessageRouter {
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Our near Rooms (the list does not contain our Room).
*/
protected Room nearRooms[];
/** Our Room.
*/
protected Room thisRoom;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Constructor. Just creates internals.
*/
public MultiGroupMessageRouter() {
super();
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Inititializes this MessageRouter.
*
* @param location location this MessageRouter is linked to.
* @param wManager WorldManager of the application.
*/
public void init( WotlasLocation location, WorldManager wManager ) {
// 1 - We get our Room.
if( !location.isRoom()) {
Debug.signal(Debug.FAILURE, this, "Location is not a Room ! Can't init router !" );
return;
}
thisRoom = wManager.getRoom( location );
if( thisRoom==null ) {
Debug.signal(Debug.FAILURE, this, "Room not found ! Can't init router !" );
return;
}
// 2 - We create a list of the rooms near ours.
if( thisRoom.getRoomLinks()==null || thisRoom.getRoomLinks().length==0 ) {
nearRooms = new Room[0]; // this way no need to test for nearRooms nullity...
return;
}
HashMap tempRoomList = new HashMap(10);
for( int i=0; i<thisRoom.getRoomLinks().length; i++ ) {
Room otherRoom = thisRoom.getRoomLinks()[i].getRoom1();
if( otherRoom==thisRoom ) {
otherRoom = thisRoom.getRoomLinks()[i].getRoom2();
if( otherRoom==thisRoom ) continue;
}
if( otherRoom==null ) continue;
// ok do we have this room in our list ?
if( !tempRoomList.containsKey(""+otherRoom.getRoomID()) )
tempRoomList.put( ""+otherRoom.getRoomID(), otherRoom );
}
nearRooms = new Room[tempRoomList.size()];
Iterator it = tempRoomList.values().iterator();
int counter = 0;
while( it.hasNext() ) {
nearRooms[counter] = (Room) it.next();
counter++;
}
tempRoomList.clear(); // let's help the garbage collector...
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To add a player to this group. We update its location.
*
* Call this method when a player is added to the map. If the player is arriving from
* another room call movePlayer.
*
* @param player player to add
* @return true if the player was added successfully, false if an error occured.
*/
public boolean addPlayer( Player player ) {
// 1 - We add this player to our list & don't care if it's already in there
players.put( player.getPrimaryKey(), player );
player.setLocation( thisRoom.getLocation() ); // update player location
if(!player.isConnectedToGame())
return true; // no need to advertise if the player is not connected
// 2 - We advertise our presence to other players in the LOCAL room
AddPlayerToRoomMessage aMsg = new AddPlayerToRoomMessage( null, player );
synchronized( players ) {
Iterator it = players.values().iterator();
while( it.hasNext() ) {
Player p = (Player) it.next();
if(p!=player) {
aMsg.setOtherPlayer(p); // needed for the LieManager to know who is
p.sendMessage( aMsg ); // asking for the player's name....
}
}
}
// 3 - We advertise our presence to other players of the 1 step-NEAR rooms
for( int i=0; i<nearRooms.length; i++ ) {
Hashtable otherPlayers = nearRooms[i].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....
}
}
}
// 4 - We send DOORS & PLAYERS data to the added player
player.sendMessage( new RoomPlayerDataMessage( thisRoom, player ) );
for( int i=0; i<nearRooms.length; i++ ) {
player.sendMessage( new DoorsStateMessage( nearRooms[i] ) );
player.sendMessage( new RoomPlayerDataMessage( nearRooms[i], player ) );
}
// 5 - We send CHAT DATA to the added player
updateChatInformation( (PlayerImpl) player );
return true;
/*** END OF ADDPLAYER ***/
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To update the local chat information for a player. We update the state of the
* player
*/
protected void updateChatInformation( PlayerImpl player ) {
// 1 - and signal our player to default chat room...
sendMessage( new AddPlayerToChatRoomMessage( player.getPrimaryKey(), ChatRoom.DEFAULT_CHAT ),
player );
// 2 - We send CHAT data to the added player
// ( list of the players of the default chat room )
player.sendMessage( new SetCurrentChatRoomMessage( ChatRoom.DEFAULT_CHAT, players ) );
// 3 - We seek for a valid chatList if any...
synchronized( players ) {
Iterator it = players.values().iterator();
while( it.hasNext() ) {
PlayerImpl p = (PlayerImpl) it.next();
if(p!=player && p.isConnectedToGame() ) {
ChatList chatList = p.getChatList();
if( chatList!=null ) {
player.setChatList( chatList );
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -