/** (C) Game Page Network, Inc., Confidential, All Rights Reserved */
// JoinCommand.java
// --paul@gamepage.net, 25jul97

import java.io.*;
import java.util.*;

public class JoinCommand extends Command {
  JoinCommand() {
    super("join", 1, "TABLE",
          "Join the open TABLE as the next player.\n"+
          "Example: join 4\n"+
          "See also: tables, open, start, reset, observe, resign.");
  }
  public void process(StringTokenizer args, Client c) {
    Member current = c.getMember();  
    OutputStreamWriter out = c.getWriter();
    String name = args.nextToken();
    Table table = Table.findIt(name);
    String error = "! join "+name+": ";
    if (table==null) {
      c.send(error+"No such open table.");
      return;
    }
    if (table==Club.lobby) {
      c.send(error+"Can not play in the Lobby.");
      return;
    }
    Game game = table.getGame();
    if (game==null) {
      c.send(error+"No active game at table.");
      return;
    }
    if (current.isPlayer(table)) {
      c.send(error+"You are already a player.");
      return;
    }
    if (game.hasPlayers()) {
      c.send(error+"Already has players.");
      return;
    }
    table.join(current);
  }
}

