001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2004  Gamereplay (game@gamereplay.be)
004     * Copyright (C) 2004  Emmanuel Bourg
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019     */
020    
021    package net.jetrix.commands;
022    
023    import java.util.Locale;
024    
025    import net.jetrix.messages.channel.CommandMessage;
026    import net.jetrix.messages.channel.PlineMessage;
027    import net.jetrix.config.*;
028    import net.jetrix.*;
029    
030    /**
031     * Switch between preconfigured settings.
032     *
033     * @author Gamereplay
034     * @author Emmanuel Bourg
035     * @version $Revision: 846 $, $Date: 2010-05-03 17:29:44 +0200 (lun., 03 mai 2010) $
036     */
037    public class ModeCommand extends AbstractCommand
038    {
039        private static int[][] modes =
040                {
041                    {15, 14, 14, 14, 14, 14, 14, 1, 0},
042                    {10, 15, 15, 15, 15, 15, 15, 1, 1},
043                    {20, 14, 13, 13, 13, 13, 14, 1, 0},
044                    {10, 15, 15, 15, 15, 15, 15, 2, 1},
045                    {25, 12, 13, 13, 12, 12, 13, 1, 0},
046                    {0,  17, 17, 17, 16, 16, 17, 1, 0},
047                    {100, 0,  0,  0,  0,  0,  0, 1, 0},
048                    {0,   0,  0,  0, 50, 50,  0, 1, 1},
049                    {0,   0, 50, 50,  0,  0,  0, 1, 1},
050                    {0,   0,  0,  0,  0,  0, 100,1, 1}
051                };
052    
053        static
054        {
055            Language.getInstance().addResources("command.mode");
056        }
057    
058        public String getAlias()
059        {
060            return "mode";
061        }
062    
063        public String getUsage(Locale locale)
064        {
065            return "/" + getAlias() + " <0-" + (modes.length - 1) + ">";
066        }
067    
068        public void updateSetting(Settings settings, int[] mode)
069        {
070            for (Block block : Block.values())
071            {
072                settings.setOccurancy(block, mode[block.ordinal()]);
073            }
074    
075            settings.setLinesPerSpecial(mode[7]);
076            settings.setSpecialAdded(mode[8]);
077        }
078    
079        public void execute(CommandMessage message)
080        {
081            Client client = (Client) message.getSource();
082            Channel channel = client.getChannel();
083    
084            if (message.getParameterCount() == 0)
085            {
086                Locale locale = client.getUser().getLocale();
087    
088                // display all modes available
089                for (int i = 0; i < modes.length; i++)
090                {
091                    Message tmode = new PlineMessage("<red>/" + getAlias() + " <aqua>" + i + "</aqua> : <darkBlue>" + Language.getText("command.mode.message" + i, locale));
092                    client.send(tmode);
093                }
094            }
095            else
096            {
097                int param = -1;
098                
099                try
100                {
101                    param = Integer.parseInt(message.getParameter(0));
102                }
103                catch (NumberFormatException e) { }
104                
105                if (param >= 0 && param < modes.length)
106                {
107                    updateSetting(channel.getConfig().getSettings(), modes[param]);
108    
109                    PlineMessage enabled = new PlineMessage();
110                    enabled.setKey("command.mode.enabled", "key:command.mode.message" + param);
111                    channel.send(enabled);
112                }
113                else
114                {
115                    PlineMessage error = new PlineMessage();
116                    error.setText("<red>/" + getAlias() + "</red> <blue><0-" + (modes.length - 1) + "></blue>");
117                    client.send(error);
118                }
119            }
120        }
121    }