home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 4.8 KB | 217 lines |
- /*
- * @(#)GameInfo.java
- */
- package games.Battle.shared.comm;
-
- import java.io.*;
-
- /**
- * GameInfo encapsulates all the knowledge about a particular game on the
- * server. The GameInfo packet knows which players have joined the game,
- * how many are still needed to start the game, and what the gameId of
- * the game is.
- * <P>
- * The server transmits a series of GameInfo packets to the client so that
- * the user can choose which game they would like to join. Once a game is
- * full of players, the game is automatically started by the server, and
- * the players can only quit by forfeiting the game.
- *
- * @version 1.00
- * @author Alex Nicolaou
- * @author Jay Steele
- */
-
- public class GameInfo extends BattlePacket {
- /**
- * The game will start when numPlayersNeeded players have joined.
- */
- byte numPlayersNeeded;
- /**
- * The game keeps a count of how many players have joined so far.
- */
- byte numPlayersIn;
- /**
- * The game has a unique ID number so that the client and server can
- * refer to it unambigously across the network.
- */
- byte gameId;
-
- /**
- * The game ID is always generated by the server, using this static
- * counter to ensure that each new game ID is unique.
- */
- static byte gameIdCounter = 0;
-
- /**
- * The game keeps an array of players that have joined so far.
- */
- public PlayerInfo[] players;
-
- /**
- * Construct an empty GameInfo packet so that it can be read from an
- * input stream.
- */
- public GameInfo() {
- numPlayersNeeded = 0;
- numPlayersIn = 0;
- gameId = 0;
- }
-
- /**
- * Construct a new GameInfo packet, including making a new ID.
- * @param numPlayers the number of players needed to start this game
- */
- public GameInfo(int numPlayers) {
- numPlayersNeeded = (byte)numPlayers;
- numPlayersIn = 0;
- gameId = gameIdCounter;
- gameIdCounter++;
-
- players = new PlayerInfo[numPlayersNeeded];
- }
-
- /**
- * Returns true if the game is full of all the players it needs.
- */
- public boolean isRunning() {
- return numPlayersIn == numPlayersNeeded;
- }
-
- /**
- * Empties all the players out of this game.
- */
- public synchronized void reset() {
- for (int i = 0; i < numPlayersNeeded; i++)
- players[i] = null;
- numPlayersIn = 0;
- }
-
- /**
- * Allows a new player to join this game.
- */
- public synchronized void join(PlayerInfo p) {
- for (int i = 0; i < numPlayersIn; i++)
- if (players[i].sameId(p))
- return; // can't join game twice!
-
- if (!isRunning()) {
- players[numPlayersIn] = p;
- numPlayersIn++;
- }
- }
-
- /**
- * Allows a player to get out of this game.
- */
- public synchronized void quit(PlayerInfo p) {
- if (isRunning())
- return;
-
- for (int i = 0; i < numPlayersIn; i++)
- if (players[i].sameId(p)) {
- numPlayersIn--;
- players[i] = players[numPlayersIn];
- players[numPlayersIn] = null;
- return;
- }
- }
-
- /**
- * Returns how many players are needed to fill this game.
- */
- public int numberNeeded() {
- return numPlayersNeeded;
- }
-
- /**
- * Returns how many players have joined this game.
- */
- public int numberJoined() {
- return numPlayersIn;
- }
-
- /**
- * Returns this game's unique identifier
- */
- public int getId() {
- return gameId;
- }
-
- /**
- * Produce an array of bytes representing a GameInfo
- */
- public byte[] toBytes() {
- byte[] buffer = new byte[3];
- buffer[0] = numPlayersNeeded;
- buffer[1] = numPlayersIn;
- buffer[2] = gameId;
-
- return buffer;
- }
-
- /**
- * Produce a GameInfo from an array of bytes.
- */
- public void fromBytes(byte[] buffer) {
- numPlayersNeeded = buffer[0];
- numPlayersIn = buffer[1];
- gameId = buffer[2];
-
- players = new PlayerInfo[numPlayersNeeded];
- }
-
- /**
- * Write this GameInfo to the OutputStream given. Overrides the
- * base class so that we can easily write out the embedded PlayerInfo
- * objects.
- * @param os the output stream that the info packet is written to
- */
- public void writeTo(OutputStream os) throws java.io.IOException {
- super.writeTo(os);
- for (int i = 0; i < numPlayersIn; i++) {
- players[i].writeTo(os);
- }
- }
-
- /**
- * Read this GameInfo from the InputStream given. Overrides the
- * base class so that we can easily read in the embedded PlayerInfo
- * objects.
- * @param is the input stream that the info packet is read from
- */
- public int readFrom(InputStream is) throws java.io.IOException {
- int size = super.readFrom(is);
-
- for (int i = 0; i < numPlayersIn; i++) {
- players[i] = new PlayerInfo();
- size += players[i].readFrom(is);
- }
-
- return size;
- }
-
- /**
- * Returns a string representation of this game.
- */
- public String toString() {
- return "Game needs " + numPlayersNeeded + ", has " + numPlayersIn;
- }
-
- public int hashCode() {
- return gameId;
- }
-
- public boolean equals(Object other) {
- boolean ret = false;
- try {
- GameInfo o = (GameInfo)other;
- if (o.getId() == gameId)
- ret = true;
- }
- catch (Exception e) {
- ret = false;
- }
- return ret;
- }
- }
-