home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / shared / comm / gameinfo.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.8 KB  |  217 lines

  1. /*
  2.  * @(#)GameInfo.java
  3.  */
  4. package games.Battle.shared.comm;
  5.  
  6. import java.io.*;
  7.  
  8. /**
  9.  * GameInfo encapsulates all the knowledge about a particular game on the 
  10.  * server. The GameInfo packet knows which players have joined the game,
  11.  * how many are still needed to start the game, and what the gameId of 
  12.  * the game is.
  13.  * <P>
  14.  * The server transmits a series of GameInfo packets to the client so that
  15.  * the user can choose which game they would like to join. Once a game is
  16.  * full of players, the game is automatically started by the server, and
  17.  * the players can only quit by forfeiting the game.
  18.  *
  19.  * @version 1.00
  20.  * @author Alex Nicolaou
  21.  * @author Jay Steele
  22.  */
  23.  
  24. public class GameInfo extends BattlePacket {
  25.     /**
  26.      * The game will start when numPlayersNeeded players have joined.
  27.      */
  28.     byte numPlayersNeeded;
  29.     /**
  30.      * The game keeps a count of how many players have joined so far.
  31.      */
  32.     byte numPlayersIn;
  33.     /**
  34.      * The game has a unique ID number so that the client and server can
  35.      * refer to it unambigously across the network.
  36.      */
  37.     byte gameId;
  38.  
  39.     /**
  40.      * The game ID is always generated by the server, using this static
  41.      * counter to ensure that each new game ID is unique.
  42.      */
  43.     static byte gameIdCounter = 0;
  44.  
  45.     /**
  46.      * The game keeps an array of players that have joined so far.
  47.      */
  48.     public PlayerInfo[] players;
  49.  
  50.     /**
  51.      * Construct an empty GameInfo packet so that it can be read from an
  52.      * input stream.
  53.      */
  54.     public GameInfo() {
  55.         numPlayersNeeded = 0;
  56.         numPlayersIn = 0;
  57.         gameId = 0;
  58.     }
  59.  
  60.     /**
  61.      * Construct a new GameInfo packet, including making a new ID.
  62.      * @param numPlayers the number of players needed to start this game
  63.      */
  64.     public GameInfo(int numPlayers) {
  65.         numPlayersNeeded = (byte)numPlayers;
  66.         numPlayersIn = 0;
  67.         gameId = gameIdCounter;
  68.         gameIdCounter++;
  69.  
  70.         players = new PlayerInfo[numPlayersNeeded];
  71.     }
  72.  
  73.     /**
  74.      * Returns true if the game is full of all the players it needs.
  75.      */
  76.     public boolean isRunning() {
  77.         return numPlayersIn == numPlayersNeeded;
  78.     }
  79.  
  80.     /**
  81.      * Empties all the players out of this game.
  82.      */
  83.     public synchronized void reset() {
  84.         for (int i = 0; i < numPlayersNeeded; i++)
  85.             players[i] = null;
  86.         numPlayersIn = 0;
  87.     }
  88.  
  89.     /**
  90.      * Allows a new player to join this game.
  91.      */
  92.     public synchronized void join(PlayerInfo p) {
  93.         for (int i = 0; i < numPlayersIn; i++)
  94.             if (players[i].sameId(p)) 
  95.                 return; // can't join game twice!
  96.  
  97.         if (!isRunning()) {
  98.             players[numPlayersIn] = p;
  99.             numPlayersIn++;
  100.         }
  101.     }
  102.  
  103.     /**
  104.      * Allows a player to get out of this game.
  105.      */
  106.     public synchronized void quit(PlayerInfo p) {
  107.         if (isRunning())
  108.             return; 
  109.  
  110.         for (int i = 0; i < numPlayersIn; i++)
  111.             if (players[i].sameId(p)) {
  112.                 numPlayersIn--;
  113.                 players[i] = players[numPlayersIn];
  114.                 players[numPlayersIn] = null;
  115.                 return;
  116.             }
  117.     }
  118.  
  119.     /**
  120.      * Returns how many players are needed to fill this game.
  121.      */
  122.     public int numberNeeded() {
  123.         return numPlayersNeeded;
  124.     }
  125.  
  126.     /**
  127.      * Returns how many players have joined this game.
  128.      */
  129.     public int numberJoined() {
  130.         return numPlayersIn;
  131.     }
  132.  
  133.     /**
  134.      * Returns this game's unique identifier
  135.      */
  136.     public int getId() {
  137.         return gameId; 
  138.     }
  139.  
  140.     /**
  141.      * Produce an array of bytes representing a GameInfo
  142.      */
  143.     public byte[] toBytes() {
  144.         byte[] buffer = new byte[3];
  145.         buffer[0] = numPlayersNeeded;
  146.         buffer[1] = numPlayersIn;
  147.         buffer[2] = gameId;
  148.  
  149.         return buffer;
  150.     }
  151.  
  152.     /**
  153.      * Produce a GameInfo from an array of bytes.
  154.      */
  155.     public void fromBytes(byte[] buffer) {
  156.         numPlayersNeeded = buffer[0];
  157.         numPlayersIn = buffer[1];
  158.         gameId = buffer[2];
  159.  
  160.         players = new PlayerInfo[numPlayersNeeded];
  161.     }
  162.  
  163.     /**
  164.      * Write this GameInfo to the OutputStream given. Overrides the 
  165.      * base class so that we can easily write out the embedded PlayerInfo
  166.      * objects.
  167.      * @param os the output stream that the info packet is written to
  168.      */
  169.     public void writeTo(OutputStream os) throws java.io.IOException {
  170.         super.writeTo(os);
  171.         for (int i = 0; i < numPlayersIn; i++) {
  172.             players[i].writeTo(os);
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * Read this GameInfo from the InputStream given. Overrides the 
  178.      * base class so that we can easily read in the embedded PlayerInfo
  179.      * objects.
  180.      * @param is the input stream that the info packet is read from
  181.      */
  182.     public int readFrom(InputStream is) throws java.io.IOException {
  183.         int size = super.readFrom(is);
  184.  
  185.         for (int i = 0; i < numPlayersIn; i++) {
  186.             players[i] = new PlayerInfo();
  187.             size += players[i].readFrom(is);
  188.         }
  189.  
  190.         return size;
  191.     }
  192.  
  193.     /**
  194.      * Returns a string representation of this game.
  195.      */
  196.     public String toString() {
  197.         return "Game needs " + numPlayersNeeded + ", has " + numPlayersIn;
  198.     }
  199.  
  200.     public int hashCode() {
  201.         return gameId;
  202.     }
  203.  
  204.     public boolean equals(Object other) {
  205.         boolean ret = false;
  206.         try {
  207.             GameInfo o = (GameInfo)other;
  208.             if (o.getId() == gameId)
  209.                 ret = true;
  210.         }
  211.         catch (Exception e) {
  212.             ret = false;
  213.         }
  214.         return ret;
  215.     }
  216. }
  217.