001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2001-2004 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix.commands;
021
022 import static net.jetrix.GameState.*;
023
024 import java.util.*;
025
026 import net.jetrix.*;
027 import net.jetrix.clients.TetrinetClient;
028 import net.jetrix.config.*;
029 import net.jetrix.messages.channel.CommandMessage;
030 import net.jetrix.messages.channel.PlineMessage;
031
032 /**
033 * List available channels.
034 *
035 * @author Emmanuel Bourg
036 * @version $Revision: 860 $, $Date: 2010-05-06 13:21:05 +0200 (jeu., 06 mai 2010) $
037 */
038 public class ListCommand extends AbstractCommand
039 {
040 public String[] getAliases()
041 {
042 return (new String[]{"list", "l"});
043 }
044
045 public void execute(CommandMessage m)
046 {
047 Client client = (Client) m.getSource();
048 ChannelManager channelManager = ChannelManager.getInstance();
049 Locale locale = client.getUser().getLocale();
050
051 // get the name of the channel of this player to highlight it
052 String playerChannel = "";
053 if (client.getChannel() != null) playerChannel = client.getChannel().getConfig().getName();
054
055 PlineMessage response = new PlineMessage();
056 response.setKey("command.list.header");
057 client.send(response);
058
059 int i = 0;
060 for (Channel channel : channelManager.channels())
061 {
062 i++;
063
064 ChannelConfig conf = channel.getConfig();
065
066 // skip invisible channels
067 if (!conf.isVisible())
068 {
069 continue;
070 }
071
072 // skip channels with an incompatible speed constraint
073 if (client.getUser().isPlayer() && !conf.isProtocolAccepted(client.getProtocol().getName()))
074 {
075 continue;
076 }
077
078 StringBuilder message = new StringBuilder();
079 message.append("<darkBlue>(" + (playerChannel.equals(conf.getName()) ? "<red>" + i + "</red>" : "<purple>" + i + "</purple>") + ") ");
080 message.append("<purple>" + rightPad(conf.getName(), 6) + "</purple>\t");
081 if (channel.isFull())
082 {
083 message.append("[<red>" + Language.getText("command.list.status.full", locale) + "</red>]");
084 for (int j = 0; j < 11 - Language.getText("command.list.status.full", locale).length(); j++)
085 {
086 message.append(" ");
087 }
088 }
089 else
090 {
091 message.append("[<aqua>" + Language.getText("command.list.status.open", locale) + "</aqua><blue>-" + channel.getPlayerCount() + "/" + conf.getMaxPlayers() + "</blue>]");
092 }
093 if (channel.getGameState() != STOPPED)
094 {
095 message.append(" <gray>{" + Language.getText("command.list.status.ingame", locale) + "}</gray> ");
096 }
097 else
098 {
099 message.append(" ");
100 }
101 message.append("<black>" + conf.getDescription());
102
103 client.send(new PlineMessage(message.toString()));
104 }
105 }
106
107 /**
108 * Add spaces at the right of the string if it's shorter than the specified length.
109 */
110 private String rightPad(String s, int length)
111 {
112 while (s.length() < length)
113 {
114 s += " ";
115 }
116
117 return s;
118 }
119 }