au.com.solidsoftware.multimahjong.mms
Class GamesList

java.lang.Object
  |
  +--au.com.solidsoftware.multimahjong.mms.GamesList

public class GamesList
extends java.lang.Object
implements MMSConstants

GamesList stores and manages the list of games currently being played on the MultiMahjongServer. GamesList has two arrays for representing games:

The mini array is returned to the Registrar when the list of games is required. It is a much smaller representation than the main array, and so it is quicker to transfer over TCP/IP. GamesList creates a new RemoteReferee and stores it in the main array every time a new game is requested by the Registrar.

GamesList must also keep track of the MMCs that require updated information whenever a new game or player is added to the list. It keeps a list of GamesObservers in a HashSet and notifies them whenever a change is made. GamesObservers are added whenever an initial request is made for the entire list of games (when a user on an MMC wishes to join a game).

Author:
Michael Hart
See Also:
MiniGame, Registrar, RemoteReferee, GamesObserver, HashSet

Field Summary
private  RemoteReferee[] mainList
          The main array of games on the MMS.
private  MiniGame[] miniList
          The mini array of games on the MMS.
private  int numberOfGames
          The number of games in both the mainList and miniList
private  java.util.HashSet observerList
          The list of GamesObservers waiting for updates
 
Constructor Summary
GamesList()
           
 
Method Summary
 int addPlayer(java.lang.String name, int iconID, int gameID, ObserverIntf gamesObserver)
          Adds a new player to the specified RemoteReferee.
 int createNewGame(java.lang.String name, int limit, int iconID, ObserverIntf playersObserver)
          Creates a new RemoteReferee and adds it to the mainList.
 MiniGame[] getGamesList(ObserverIntf gamesObserver)
          Returns the miniList of games and in the process, adds a new GamesObserver to the observerList.
 int getNumberOfGames()
          Just gets the number of games.
 void removeGame(int gameID)
          Removes the specified game from mainList and miniList.
 Referee startGame(int gameID)
          Calls the startGame method in the specified RemoteReferee.
 Referee waitForStart(int gameID)
          Calls the waitForStart method in the specified RemoteReferee.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

mainList

private RemoteReferee[] mainList
The main array of games on the MMS.

miniList

private MiniGame[] miniList
The mini array of games on the MMS. This is a compact version of the mainList used for transferring over TCP/IP.

observerList

private java.util.HashSet observerList
The list of GamesObservers waiting for updates

numberOfGames

private int numberOfGames
The number of games in both the mainList and miniList
Constructor Detail

GamesList

public GamesList()
Method Detail

getNumberOfGames

public int getNumberOfGames()
Just gets the number of games. Don't know if this is actually needed.

createNewGame

public int createNewGame(java.lang.String name,
                         int limit,
                         int iconID,
                         ObserverIntf playersObserver)
                  throws java.rmi.RemoteException
Creates a new RemoteReferee and adds it to the mainList. Firstly, this method checks to see whether the mainList is full. If it's not, then the method proceeds to create a new RemoteReferee and a corresponding MiniGame and adds them to their respective lists. Finally, the method notifies all GamesObservers that a new game has been created.
Parameters:
name - the name of the player creating the game
limit - the score limit of the new game
iconID - the chosen icon of the player creating the game
playersObserver - the observer to notify when players are added
Returns:
the ID of the created game if successful, or LIST_FULL if no more games can be created
See Also:
mainList, miniList, RemoteReferee, MiniGame, GamesObserver, PlayersObserver

addPlayer

public int addPlayer(java.lang.String name,
                     int iconID,
                     int gameID,
                     ObserverIntf gamesObserver)
              throws java.rmi.RemoteException
Adds a new player to the specified RemoteReferee. Firstly, this method checks to see whether the game has started. If it hasn't, then the method calls the addPlayer method in the ServerGame. It also removes the GamesObserver that this user had from the list, because the user does not need to know about game updates anymore. Finally, the method notifies all remaining GamesObservers that a new player has been added to this game.
Parameters:
name - the name of the player being added
iconID - the chosen icon of the player being added
gameID - the game that the player is being added to
gamesObserver - the observer that this user was using to get updates through
Returns:
the ID of the new player if successful, or LIST_FULL if no more players can be added
See Also:
mainList, RemoteReferee, RemoteReferee#addPlayer(Observer, String, int), GamesObserver

getGamesList

public MiniGame[] getGamesList(ObserverIntf gamesObserver)
Returns the miniList of games and in the process, adds a new GamesObserver to the observerList. This method is called thorugh the Registrar when a user wishing to join a game wants to get the list of games currently needing players.
Parameters:
gamesObserver - the observer that the user will receive updates through
Returns:
the miniList of games
See Also:
miniList, observerList, GamesObserver, Registrar

startGame

public Referee startGame(int gameID)
                  throws java.rmi.RemoteException
Calls the startGame method in the specified RemoteReferee. Firstly, this method notifies all GamesObservers that there are no more spaces needed to be filled in this game. Then it just calls and returns whatever value the startGame method returns in the RemoteReferee.
Parameters:
gameID - the game that is being started
Returns:
whatever RemoteReferee.startGame returns
See Also:
RemoteReferee, RemoteReferee.startGame(), GamesObserver

waitForStart

public Referee waitForStart(int gameID)
                     throws java.rmi.RemoteException
Calls the waitForStart method in the specified RemoteReferee.
Parameters:
gameID - the game that is waiting to be started
Returns:
whatever RemoteReferee.waitForStart returns
See Also:
RemoteReferee, RemoteReferee.waitForStart()

removeGame

public void removeGame(int gameID)
                throws java.rmi.RemoteException
Removes the specified game from mainList and miniList.
Parameters:
gameID - the game to be removed
See Also:
mainList, miniList