Class GameRoom

java.lang.Object
  extended by GameRoom
All Implemented Interfaces:
Room

public class GameRoom
extends java.lang.Object
implements Room

This is the GameRoom state that keeps track of a game's information and any messages that need to be relayed between clients in a game.


Field Summary
 
Fields inherited from interface Room
ADD_USER, CHAT, COLOR_CHANGE, CREATE, DRAW, END_SESSION, ERASE, ERROR, GAME_INFO, JOIN, LEAVE_GAME, LOGIN, LOGOUT, NEW_ILLUSTRATOR, REMOVE_USER, SIZE_CHANGE, START, STOP, TOPIC_REQUEST
 
Constructor Summary
GameRoom(Client firstPlayer, MainRoom main)
          Construct the GameRoom and specify a Client as illustrator.
GameRoom(MainRoom main)
          Construct the GameRoom
 
Method Summary
 void addNewClient(Client newClient)
          Adds a new client to the set of current clients If the client is the first to be added to the room that client is set as the first illustrator Sends a message to each client in the game room giving the name of the new client format of message: 101_userName_
 boolean canJoin()
          Determines whether a client can join the game room.
 boolean containsUser(java.lang.String userName)
          Checks to see if a given user is in the room.
 int findUser(java.lang.String userName)
          Looks through the array for a user.
 int getGameMode()
          Returns the current game mode.
 Client getIllustrator()
          Returns the currrent illustrator.
 int getNumPlayers()
          Gets the number of players in the game room
 void handleMsg(java.lang.String msg, Client sender)
          Handles Messages from the client
 Client removeClient(java.lang.String userName)
          Remove a client from the room.
 boolean sendTo(java.lang.String userName, java.lang.String msg)
          Sends a given message to a given user
 void sendToAllClients(java.lang.String message)
          Sends a message to all clients.
 void sendToAllOthers(Client sender, java.lang.String msg)
          Sends a given message to all clients in the room except for the sender
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GameRoom

public GameRoom(MainRoom main)
Construct the GameRoom

Parameters:
main - The main room that created this game

GameRoom

public GameRoom(Client firstPlayer,
                MainRoom main)
Construct the GameRoom and specify a Client as illustrator.

Parameters:
firstPlayer - The client creating the room (illustrator)
main - The main room that created this game
Method Detail

getNumPlayers

public int getNumPlayers()
Gets the number of players in the game room

Returns:
The number of players in the game room

addNewClient

public void addNewClient(Client newClient)
Adds a new client to the set of current clients If the client is the first to be added to the room that client is set as the first illustrator Sends a message to each client in the game room giving the name of the new client format of message: 101_userName_

Specified by:
addNewClient in interface Room
Parameters:
newClient - The new client that is joining the server

canJoin

public boolean canJoin()
Determines whether a client can join the game room. checks that the room is not full and game is not started

Returns:
true if the user can join, otherwise false

sendTo

public boolean sendTo(java.lang.String userName,
                      java.lang.String msg)
Sends a given message to a given user

Parameters:
userName - The user name
msg - The message being sent
Returns:
true if the message succeeded, otherwise false

sendToAllOthers

public void sendToAllOthers(Client sender,
                            java.lang.String msg)
Sends a given message to all clients in the room except for the sender

Parameters:
sender - The client who sent the message
msg - The message to be sent

sendToAllClients

public void sendToAllClients(java.lang.String message)
Sends a message to all clients.

Parameters:
message - The message that is sent to every client

removeClient

public Client removeClient(java.lang.String userName)
Remove a client from the room. Note: Currently very hackish. Need to resolve the issue of empty slots by going to hashmaps again, or making a repack() method. Will be running tests to see which is faster - Calvin.

Specified by:
removeClient in interface Room
Parameters:
userName - The name of the client that is being removed
Returns:
The client that has just been removed

handleMsg

public void handleMsg(java.lang.String msg,
                      Client sender)
Handles Messages from the client

Specified by:
handleMsg in interface Room
Parameters:
msg - the message or command that is being sent
sender - the client who sent the message

containsUser

public boolean containsUser(java.lang.String userName)
Checks to see if a given user is in the room.

Specified by:
containsUser in interface Room
Parameters:
userName - the user name to search for
Returns:
true if the room contains the given user name, otherwise false

findUser

public int findUser(java.lang.String userName)
Looks through the array for a user. Returns -1 if no user found.

Parameters:
userName - User name to search for.

getGameMode

public int getGameMode()
Returns the current game mode.


getIllustrator

public Client getIllustrator()
Returns the currrent illustrator.