📄 playerimpl.java
字号:
/** are we a member of this chat ? or just eavesdropping ?
* @return true if we are a real member of the current chat
*/
public boolean isChatMember() {
return isChatMember;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** are we a member of this chat ? or just eavesdropping ?
* @param isChatMember if we are a real member of the current chat set it to true.
*/
public void setIsChatMember( boolean isChatMember ) {
this.isChatMember = isChatMember;
}
/*------------------------------------------------------------------------------------*/
/** Is this player connected to the game ? ( not synchronized )
* @return true if the player is in the game, false if the client is not connected.
*/
public boolean isConnectedToGame() {
if(connection==null)
return false;
return true;
}
/** To get the player's state (disconnected/connected/away)
*
* @return player state
*/
public PlayerState getPlayerState() {
return playerState;
}
/** To set the player's state (disconnected/connected/away)
*
* @param playerState player state
*/
public void setPlayerState(PlayerState playerState) {
this.playerState = playerState;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To set if this player is connected to the game. (not used on this server side )
* @param isConnected true if the player is in the game, false if the client is not connected.
*/
public void setIsConnectedToGame( boolean isConnected ) {
// no external update possible on the server side !
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** This method is called when a new network connection is created on this player.
*
* @param connection the NetConnection object associated to this connection.
*/
public void connectionCreated( NetConnection connection ) {
synchronized( connectionLock ) {
this.connection = connection;
}
// We forget a little about players we met if we last connected 5 days ago
if (lastDisconnectedTime-System.currentTimeMillis()>(5*86400000))
lieManager.removeMeet(LieManager.FORGET_RECONNECT_LONG);
else
lieManager.removeMeet(LieManager.FORGET_RECONNECT);
// We update our state
playerState.value = PlayerState.CONNECTED;
// We signal our connection to players in the game
// ... and players in the rooms near us
if(location.isRoom()) {
if(myRoom==null) {
Debug.signal( Debug.ERROR, this, "Player "+primaryKey+" has an incoherent location state");
return;
}
// are we present in this room already ?
if( myRoom.getMessageRouter().getPlayer(primaryKey)!=null ) {
// We send an update to players near us...
PlayerConnectedToGameMessage pMsg = new PlayerConnectedToGameMessage(primaryKey,true);
myRoom.getMessageRouter().sendMessage( pMsg, this, MessageRouter.EXTENDED_GROUP );
}
}
// We signal our connection to players in the game
if(location.isTileMap()) {
if(myTileMap==null) {
Debug.signal( Debug.ERROR, this, "Player "+primaryKey+" has an incoherent location state");
return;
}
// are we present in this TileMap already ?
if( myTileMap.getMessageRouter().getPlayer(primaryKey)!=null ) {
// We send an update to players near us...
PlayerConnectedToGameMessage pMsg = new PlayerConnectedToGameMessage(primaryKey,true);
myTileMap.getMessageRouter().sendMessage( pMsg, this, MessageRouter.EXTENDED_GROUP );
}
}
Debug.signal(Debug.NOTICE,null,"Connection opened for player "+playerName+" at "+Tools.getLexicalTime());
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** This method is called when the network connection of the client is no longer
* of this world.
*
* @param connection the NetConnection object associated to this connection.
*/
public void connectionClosed( NetConnection connection ) {
// 0 - no more messages will be sent...
synchronized( connectionLock ) {
this.connection = null;
}
lastDisconnectedTime = System.currentTimeMillis();
// 0.1 - We update our state
playerState.value = PlayerState.DISCONNECTED;
Debug.signal(Debug.NOTICE, null, "Connection closed on player: "+playerName+" at "+Tools.getLexicalTime());
// 1 - Leave any current chat...
if( !currentChatPrimaryKey.equals( ChatRoom.DEFAULT_CHAT ) ) {
RemPlayerFromChatRoomMsgBehaviour remPlayerFromChat
= new RemPlayerFromChatRoomMsgBehaviour( primaryKey, currentChatPrimaryKey );
try{
remPlayerFromChat.doBehaviour( this );
}catch( Exception e ) {
Debug.signal( Debug.ERROR, this, e );
currentChatPrimaryKey = ChatRoom.DEFAULT_CHAT;
}
}
synchronized( chatListLock ) {
chatList = null;
}
// 2 - Stop any current movement
if(location.isRoom()) {
// no movement saved on rooms...
movementComposer.resetMovement();
// We send an update to players near us...
// ... and players in other rooms
NetMessage msg[] = new NetMessage[2];
msg[0] = (NetMessage) movementComposer.getUpdate();
msg[1] = (NetMessage) new PlayerConnectedToGameMessage(primaryKey,false);
if(myRoom==null) {
Debug.signal( Debug.ERROR, this, "Player "+primaryKey+" has an incoherent location state");
return;
}
myRoom.getMessageRouter().sendMessages( msg, this, MessageRouter.EXTENDED_GROUP );
return;
}
else if(location.isTown()) {
// no movement saved on towns...
movementComposer.resetMovement();
return;
}
else if(location.isTileMap()) {
// no movement saved on tileMap...
movementComposer.resetMovement();
// We send an update to players near us...
NetMessage msg[] = new NetMessage[2];
msg[0] = (NetMessage) movementComposer.getUpdate();
msg[1] = (NetMessage) new PlayerConnectedToGameMessage(primaryKey,false);
if(myTileMap==null) {
Debug.signal( Debug.ERROR, this, "Player "+primaryKey+" has an incoherent location state");
return;
}
myTileMap.getMessageRouter().sendMessages( msg, this, MessageRouter.EXTENDED_GROUP );
return;
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Use this method to send a NetMessage to this player. You can use it directly :
* it does not lock, does not wait for the message to be sent before returning
* AND checks that the player is connected.
*
* @param message message to send to the player.
*/
public void sendMessage( NetMessage message ) {
synchronized( connectionLock ) {
if( connection!=null ) {
if( ServerDirector.SHOW_DEBUG )
System.out.println("Player "+primaryKey+" sending msg: "+message);
connection.queueMessage( message );
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Use this method to send a SendTextMessage to this player. You can use it directly :
* it does not lock, does not wait for the message to be sent before returning
* AND checks that the player is connected.
*
* @param message message to send to the player.
* @param otherPlayerKey key of player who sent the message
*/
public void sendChatMessage( SendTextMessage message, PlayerImpl otherPlayer) {
synchronized( connectionLock ) {
if( connection!=null ) {
if( ServerDirector.SHOW_DEBUG )
System.out.println("Player "+primaryKey+" sending to:"+otherPlayer.getPrimaryKey()+" msg: "+message);
connection.queueMessage( message );
if( !primaryKey.equals(otherPlayer.getPrimaryKey()) )
lieManager.addMeet(otherPlayer, LieManager.MEET_CHATMESSAGE);
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To close the network connection if any.
*/
public void closeConnection() {
synchronized( connectionLock ) {
if( connection!=null )
connection.close();
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To ask the grant to send a focus sound... see FOCUS_SOUND_PERIOD definition for
* more information.
*
* @return true if you can send the sound, false otherwise.
*/
public boolean askGrantAccessToSendFocusSound() {
long now = System.currentTimeMillis();
if( focusSoundTimeStamp+FOCUS_SOUND_PERIOD < now ) {
focusSoundTimeStamp=now;
return true; // grant accepted
}
return false; // grant rejected
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Redirects the network listener. USE WITH CARE !! should be only used by the
* BotFactory.
*/
public void removeConnectionListener() {
synchronized( connectionLock ) {
if( connection!=null )
connection.removeConnectionListener(this);
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** write object data with serialize.
*/
public void writeExternal(java.io.ObjectOutput objectOutput)
throws java.io.IOException {
objectOutput.writeInt( ExternalizeGetVersion() );
objectOutput.writeUTF(primaryKey);
objectOutput.writeObject(location);
objectOutput.writeUTF(playerName);
objectOutput.writeUTF(playerPast);
objectOutput.writeUTF(playerAwayMessage);
objectOutput.writeObject(wotCharacter);
objectOutput.writeLong(lastDisconnectedTime);
objectOutput.writeObject(lieManager);
objectOutput.writeObject(playerState);
objectOutput.writeObject(movementComposer);
/*
PlayerState playerState = new PlayerState());
MovementComposer movementComposer = (MovementComposer) new PathFollower());
LieManager lieManager = new LieManager());
*/
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** read object data with serialize.
*/
public void readExternal(java.io.ObjectInput objectInput)
throws java.io.IOException, java.lang.ClassNotFoundException {
int IdTmp = objectInput.readInt();
if( IdTmp == ExternalizeGetVersion() ){
primaryKey = objectInput.readUTF();
location = ( WotlasLocation ) objectInput.readObject();
playerName = objectInput.readUTF();
playerPast = objectInput.readUTF();
playerAwayMessage = objectInput.readUTF();
wotCharacter = ( BasicChar ) objectInput.readObject();
lastDisconnectedTime = objectInput.readLong();;
lieManager = ( LieManager ) objectInput.readObject();
playerState = ( PlayerState ) objectInput.readObject();
movementComposer = ( MovementComposer ) objectInput.readObject();
/*
PlayerState playerState = new PlayerState();
MovementComposer movementComposer = (MovementComposer) new PathFollower();
LieManager lieManager = new LieManager();
*/
} else {
// to do.... when new version
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** id version of data, used in serialized persistance.
*/
public int ExternalizeGetVersion(){
return 1;
}
public PlayerOnTheScreen getScreenObject() {
return new PlayerOnTheScreen( this );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -