KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdHitDice


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Created on 28.09.2003
19  */

20
21 package freecs.commands;
22 import freecs.interfaces.ICommand;
23 import freecs.interfaces.IGroupState;
24 import freecs.interfaces.IUserStates;
25 import freecs.content.MessageState;
26 import freecs.Server;
27
28 /**
29  * @author Manfred Andres
30  *
31  * freecs.commands
32  */

33 public class CmdHitDice extends AbstractCommand {
34     public final String JavaDoc cmd= "/td";
35     private static final ICommand selve= new CmdHitDice();
36
37     private CmdHitDice () { }
38     
39     public static ICommand getInstance () {
40         return selve;
41     }
42     
43     public boolean execute (MessageState msgState, String JavaDoc param) {
44         if (isPunished (msgState))
45             return false;
46         msgState.targetGroup = msgState.sender.getGroup ();
47         if (msgState.targetGroup.hasState(IGroupState.MODERATED) || msgState.targetGroup.hasState(IGroupState.DEACTIVATE_HITDICE)) {
48             msgState.msgTemplate = "error.noRight.deactivated";
49             msgState.sender.sendMessage(msgState.mp);
50             return false;
51         } else if (msgState.targetGroup.hasState(IGroupState.ENTRANCE)
52                     && !msgState.sender.hasRight (IUserStates.ROLE_GOD)) {
53             msgState.msgTemplate = "error.noRight.noAdmin";
54             msgState.sender.sendMessage(msgState.mp);
55             return false;
56         } else if (param.length () < 1) {
57             msgState.msgTemplate = "error.td.noArg";
58             msgState.sender.sendMessage (msgState.mp);
59             return false;
60         }
61         msgState.reason = param;
62         param = param.toLowerCase ();
63         int posd = param.indexOf ("d");
64         int poss = param.indexOf (" ");
65         int dies,eyes = 6;
66         if (posd > -1 || poss > -1) {
67            String JavaDoc values[];
68            if (posd > -1) values = param.split ("d");
69            else values = param.split (" ");
70            if (values.length > 2 || values.length < 1) {
71               msgState.msgTemplate = "error.td.wrong";
72               msgState.sender.sendMessage (msgState.mp);
73               return false;
74            }
75            try {
76               dies = Integer.parseInt (values[0]);
77               eyes = Integer.parseInt (values[1]);
78            } catch (NumberFormatException JavaDoc nfe) {
79               msgState.msgTemplate = "error.td.wrong";
80               msgState.sender.sendMessage (msgState.mp);
81               return false;
82            }
83         } else {
84            try {
85               dies = Integer.parseInt (param);
86            } catch (NumberFormatException JavaDoc nfe) {
87               msgState.msgTemplate = "error.td.wrong";
88               msgState.sender.sendMessage (msgState.mp);
89               return false;
90            }
91         }
92         if (dies < 1 || eyes < 2) {
93            msgState.msgTemplate = "error.td.wrong";
94            msgState.sender.sendMessage (msgState.mp);
95            return false;
96         } else if (dies > Server.srv.MAX_DIE_NUMBER || eyes > Server.srv.MAX_DIE_EYES) {
97            msgState.msgTemplate = "error.td.toomany";
98            msgState.sender.sendMessage (msgState.mp);
99            return false;
100         }
101         if (dies > 1) {
102            StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
103            int total = 0;
104            for (int i = 0; i < dies; i++) {
105               sb.append (i+1);
106               sb.append (".: <b>");
107               int rnd = (int) Math.round (Math.random () * (eyes -1)) + 1;
108               sb.append (rnd);
109               total += rnd;
110               if (i < dies-1)
111                   sb.append ("</b> / ");
112               else
113                   sb.append ("</b>");
114            }
115            msgState.param = String.valueOf (total);
116            msgState.message = sb.toString ();
117         } else {
118            StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
119            int points = (int) Math.round (Math.random () * (eyes -1)) + 1;
120            msgState.param = String.valueOf (points);
121            msgState.message = String.valueOf (points);
122         }
123         msgState.msgTemplate = "message.td";
124         msgState.sender.getGroup ().sendMessage (msgState.mp);
125         return true;
126     }
127 }
128
Popular Tags