/** (C) Game Page Network, Inc., Confidential, All Rights Reserved */
// TimerCommand.java
// --paul@gamepage.net, 30oct97

import java.util.*;

public class TimerCommand extends Command {
  TimerCommand() {
    super("timer", -1, "TABLE [GAMETIMER MOVETIMER]",
	  "Report status of the game timers for TABLE.\n"+
	  "At start of game, timers can be set by opening player.");
  }
  public void process(StringTokenizer args, Client c) {
    Member current = c.getMember();
    int argc = args.countTokens();
    if (argc!=1 && argc!=3) {
      c.send("? timer");
      return;
    }
    String name = args.nextToken();
    String error = "! timer @"+name+": ";
    Table table = Table.findIt(name);
    if (table==null) {
      c.send(error+"No such table.");
      return;
    }
    name = table.tag();
    Game game = table.getGame();
    if (game==null) {
      c.send(error+"No game.");
      return;
    }
    
    if (argc==1) { // just report current timer values
      // GameMinutes MoveMinutes P1seconds P2seconds
      c.send("timer "+name+" "+game.getTimers());
      return;
    }

    int gameMinutes = Club.atoi(args.nextToken());
    int moveMinutes = Club.atoi(args.nextToken());
    error = "! timer "+name+" "+gameMinutes+" "+moveMinutes+": ";
    if (gameMinutes < 5 || gameMinutes > 120 ||
	moveMinutes < 1 || moveMinutes > 30 ) {
      c.send(error+"Out of range");
    } else if (game.isActive()) {
      c.send(error+"Game already active.");
    } if (!game.hasPlayers()) {
      c.send(error+"Not yet enough players.");
    } else if (!game.isLead(current)) {
      c.send(error+"You are not the lead player.");
    } else {
      game.setTimer(gameMinutes,moveMinutes);
      // Send broadcast to setup the client timers:
      table.tell(current, "timer "+table+" "
		 +gameMinutes+" "+moveMinutes,Table.P1);
    }
  }
}

