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 java.lang.Math.*;
023
024 import java.util.*;
025 import net.jetrix.*;
026 import net.jetrix.messages.channel.CommandMessage;
027 import net.jetrix.messages.channel.PlineMessage;
028
029 /**
030 * Display a random number.
031 *
032 * @author Emmanuel Bourg
033 * @version $Revision: 803 $, $Date: 2009-02-25 22:22:12 +0100 (Wed, 25 Feb 2009) $
034 */
035 public class RandomCommand extends AbstractCommand
036 {
037 private Random random = new Random();
038
039 public String[] getAliases()
040 {
041 return (new String[] { "random", "roll" });
042 }
043
044 public String getUsage(Locale locale)
045 {
046 return "/random <min> <max>";
047 }
048
049 public void execute(CommandMessage m)
050 {
051 Client client = (Client) m.getSource();
052
053 // get the minimum and maximum values
054 int min = 1;
055 int max = 6;
056
057 if (m.getParameterCount() >= 2)
058 {
059 int a = m.getIntParameter(0, min);
060 int b = m.getIntParameter(1, max);
061
062 min = min(a, b);
063 max = max(a, b);
064 }
065 else if (m.getParameterCount() == 1)
066 {
067 max = Math.max(min, m.getIntParameter(0, max));
068 }
069
070 int result = random.nextInt(abs(max - min) + 1);
071
072 // display the result
073 PlineMessage response = new PlineMessage();
074 response.setKey("command.random.result", client.getUser().getName(), min, max, (result + min));
075 client.getChannel().send(response);
076 }
077 }