1 /***
2 * Jetrix TetriNET Server
3 * Copyright (C) 2001-2004 Emmanuel Bourg
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package net.jetrix.commands;
21
22 import static net.jetrix.GameState.*;
23
24 import java.util.*;
25
26 import net.jetrix.*;
27 import net.jetrix.clients.TetrinetClient;
28 import net.jetrix.config.*;
29 import net.jetrix.messages.channel.CommandMessage;
30 import net.jetrix.messages.channel.PlineMessage;
31
32 /***
33 * List available channels.
34 *
35 * @author Emmanuel Bourg
36 * @version $Revision: 860 $, $Date: 2010-05-06 13:21:05 +0200 (jeu., 06 mai 2010) $
37 */
38 public class ListCommand extends AbstractCommand
39 {
40 public String[] getAliases()
41 {
42 return (new String[]{"list", "l"});
43 }
44
45 public void execute(CommandMessage m)
46 {
47 Client client = (Client) m.getSource();
48 ChannelManager channelManager = ChannelManager.getInstance();
49 Locale locale = client.getUser().getLocale();
50
51
52 String playerChannel = "";
53 if (client.getChannel() != null) playerChannel = client.getChannel().getConfig().getName();
54
55 PlineMessage response = new PlineMessage();
56 response.setKey("command.list.header");
57 client.send(response);
58
59 int i = 0;
60 for (Channel channel : channelManager.channels())
61 {
62 i++;
63
64 ChannelConfig conf = channel.getConfig();
65
66
67 if (!conf.isVisible())
68 {
69 continue;
70 }
71
72
73 if (client.getUser().isPlayer() && !conf.isProtocolAccepted(client.getProtocol().getName()))
74 {
75 continue;
76 }
77
78 StringBuilder message = new StringBuilder();
79 message.append("<darkBlue>(" + (playerChannel.equals(conf.getName()) ? "<red>" + i + "</red>" : "<purple>" + i + "</purple>") + ") ");
80 message.append("<purple>" + rightPad(conf.getName(), 6) + "</purple>\t");
81 if (channel.isFull())
82 {
83 message.append("[<red>" + Language.getText("command.list.status.full", locale) + "</red>]");
84 for (int j = 0; j < 11 - Language.getText("command.list.status.full", locale).length(); j++)
85 {
86 message.append(" ");
87 }
88 }
89 else
90 {
91 message.append("[<aqua>" + Language.getText("command.list.status.open", locale) + "</aqua><blue>-" + channel.getPlayerCount() + "/" + conf.getMaxPlayers() + "</blue>]");
92 }
93 if (channel.getGameState() != STOPPED)
94 {
95 message.append(" <gray>{" + Language.getText("command.list.status.ingame", locale) + "}</gray> ");
96 }
97 else
98 {
99 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 }