server.framework.models
Class AbstractPlayer

java.lang.Object
  extended by server.framework.models.AbstractPlayer
All Implemented Interfaces:
java.lang.Comparable<AbstractPlayer>
Direct Known Subclasses:
BBPlayer

public abstract class AbstractPlayer
extends java.lang.Object
implements java.lang.Comparable<AbstractPlayer>

An AbstractPlayer object is a representation of a participant in a framework game. The player has different attributes such as a name, id, color, score, and size. In addition, each AbstractPlayer object connected to the server has a uniquely assigned SocketWrapper object, that is used for communication with the assosiated client.

Author:
Eivind Sorteberg, Martin Jarrett

Constructor Summary
AbstractPlayer(SocketWrapper socket, AbstractServer server, TestModule.TestType testtype)
          Constructor for AbstractPlayer.
 
Method Summary
 int compareTo(AbstractPlayer p2)
          Compares two player objects.
 java.net.SocketAddress getAddress()
          Returns the address of the client that is represented by this AbstractPlayer object.
 java.lang.String[] getAttributeValues()
          Returns a String containing the most important player attributes.
 int[] getColor()
           
 int getId()
           
 java.lang.String getName()
           
 int[] getPosition()
           
 java.lang.Object[] getPositionArray()
          Returns an array containing the player's id and position.
 int getScore()
           
 int[] getScreenResolution()
           
 int getSessionId()
           
 int getSize()
           
 SocketWrapper getSocket()
           
 int getSpeed()
           
 int getTeam()
           
 void increaseScore(int inc)
          Increases the player's score with the provided value.
 int lastAliveConfirmation()
           
 void notifyAboutAlive(int aliveConfirmationId)
          Sets the value of the last received alive confirmation when this is received.
 void notifyAboutPlayerDisconnected()
          Called whenever the player has disconnected from the server.
 void runTest()
          Runs the test specified by the player's test type parameter.
 void sendMessage(java.lang.Object message, boolean confirm)
          Sends a message using the player's assigned SocketWrapper.
 void setAttributeValues(java.lang.String[] values)
          Called when a player's attributes has been received.
 void setName(java.lang.String name)
          Sets the name of the player.
 void setPosition(int[] position)
          Sets the player's position.
 void setPosition(java.lang.String[] position)
          Sets the player's position.
 void setReady(boolean ready)
          Sets the player's ready status to the given value.
 void setReady(java.lang.String ready)
          Sets the player's ready status to the given value, converted to a boolean value.
 void setScore(int score)
          Sets the player's score to the specified value.
 void setSessionId(int sessionId)
          Sets the id of the session to which the player is connected.
 void setSize(int size)
          Sets the player's size to the given value.
 void setSpeed(int speed)
          Sets the player's speed to the given value.
 void setTeam(int team)
          Sets the player's team to the specified value.
 java.lang.String toString()
          Returns a string representation of the player, consisting of the player's name and id.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractPlayer

public AbstractPlayer(SocketWrapper socket,
                      AbstractServer server,
                      TestModule.TestType testtype)
Constructor for AbstractPlayer. All implementing subclasses should call this contructor through a super()-call. The constructor creates an AbstractPlayer-instance, and gives default values to all attributes not specified as input parameters.

Parameters:
server - The server to which the player is connected.
socket - The SocketWrapper used for communication.
testtype - The test type, if any.
Method Detail

getId

public int getId()
Returns:
The player's id.

setName

public void setName(java.lang.String name)
Sets the name of the player.

Parameters:
name - The player's name.

getName

public java.lang.String getName()
Returns:
The player's name.

increaseScore

public void increaseScore(int inc)
Increases the player's score with the provided value.

Parameters:
inc - The score increase.

getScore

public int getScore()
Returns:
The player's score.

toString

public java.lang.String toString()
Returns a string representation of the player, consisting of the player's name and id.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of the player.

getAddress

public java.net.SocketAddress getAddress()
Returns the address of the client that is represented by this AbstractPlayer object.

Returns:
The address of the player's associated client.

setAttributeValues

public void setAttributeValues(java.lang.String[] values)
Called when a player's attributes has been received. Parses a String containing attribute values and applies the correct values to the different settings. The attribute String is always created by the getAttributeValues() method, and so the rules from that method are used for parsing.

Parameters:
values - The player's attributes
See Also:
getAttributeValues()

getAttributeValues

public java.lang.String[] getAttributeValues()
Returns a String containing the most important player attributes. From this array, a String is created and transmitted to the server when joining connecting to a server, and the player's representation on the other mobile phones is created from this attribute String.

Returns:
A String array containing the player's attributes.

sendMessage

public void sendMessage(java.lang.Object message,
                        boolean confirm)
Sends a message using the player's assigned SocketWrapper.

Parameters:
message - The message to send.
confirm - Whether the receiver should confirm that the message has been received.

getSocket

public SocketWrapper getSocket()
Returns:
The player's associated SocketWrapper object.

notifyAboutPlayerDisconnected

public void notifyAboutPlayerDisconnected()
Called whenever the player has disconnected from the server. Makes sure the connection is closed, and frees all resources occupied by the player object.


setReady

public void setReady(boolean ready)
Sets the player's ready status to the given value.

Parameters:
ready - Whether the player is ready or not.

setReady

public void setReady(java.lang.String ready)
Sets the player's ready status to the given value, converted to a boolean value.

Parameters:
ready - Whether the player is ready or not.

setSize

public void setSize(int size)
Sets the player's size to the given value.

Parameters:
size - The player's size.

getSize

public int getSize()
Returns:
The player's size.

setSpeed

public void setSpeed(int speed)
Sets the player's speed to the given value.

Parameters:
speed - The player's speed.

getSpeed

public int getSpeed()
Returns:
The player's speed.

getScreenResolution

public int[] getScreenResolution()
Returns:
The player's associated client's screen resolution.

getColor

public int[] getColor()
Returns:
The player's color.

runTest

public void runTest()
Runs the test specified by the player's test type parameter.


getSessionId

public int getSessionId()
Returns:
The id of the session to which the player is connected.

setSessionId

public void setSessionId(int sessionId)
Sets the id of the session to which the player is connected.

Parameters:
sessionId - The id of the session to which the player is connected.

setPosition

public void setPosition(java.lang.String[] position)
Sets the player's position.

Parameters:
position - The player's position represented as a String array.

setPosition

public void setPosition(int[] position)
Sets the player's position.

Parameters:
position - The player's position.

getPositionArray

public java.lang.Object[] getPositionArray()
Returns an array containing the player's id and position. This method is used by the GameThread to create the bundled position messages.

Returns:
An array containing the player's id and position.

getPosition

public int[] getPosition()
Returns:
The player's position.

setScore

public void setScore(int score)
Sets the player's score to the specified value.

Parameters:
score - The player's score.

setTeam

public void setTeam(int team)
Sets the player's team to the specified value.

Parameters:
team - The player's team.

getTeam

public int getTeam()
Returns:
The player's team.

compareTo

public int compareTo(AbstractPlayer p2)
Compares two player objects. If this player's score is higher than the other's a positive value is returned. If this player's score is lower than the other player's, a negative value is returned. Finally, if the players have equal score, 0 is returned.

Specified by:
compareTo in interface java.lang.Comparable<AbstractPlayer>
Parameters:
p2 - The player with which to compare the score.
Returns:
The difference in player scores.

notifyAboutAlive

public void notifyAboutAlive(int aliveConfirmationId)
Sets the value of the last received alive confirmation when this is received. Only higher requests ids are stored.

Parameters:
aliveConfirmationId - The received alive confirmation.

lastAliveConfirmation

public int lastAliveConfirmation()
Returns:
The last received alive confirmation.