View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2001-2003  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;
21  
22  import java.util.*;
23  
24  /***
25   * A user connected to the server. The user can be a player or a spectator.
26   *
27   * @author Emmanuel Bourg
28   * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
29   */
30  public class User
31  {
32      private String name;
33      private String team;
34      private int accessLevel;
35      private int status;
36      private boolean registered;
37      private boolean playing;
38      private int type;
39      private Locale locale;
40      private Map<String, Object> props;
41      private Set<String> ignoredUsers;
42  
43      public static final int STATUS_OK  = 0;
44      public static final int STATUS_AFK = 1;
45      
46      public static final int USER_PLAYER = 0;
47      public static final int USER_SPECTATOR = 1;
48  
49      public User() { }
50  
51      public User(String name)
52      {
53          this.name = name;
54      }
55  
56      public void setName(String name)
57      {
58          this.name = name;
59      }
60  
61      public String getName()
62      {
63          return name;
64      }
65  
66      public void setTeam(String team)
67      {
68          this.team = team;
69      }
70  
71      public String getTeam()
72      {
73          return team;
74      }
75  
76      public void setAccessLevel(int accessLevel)
77      {
78          this.accessLevel = accessLevel;
79      }
80  
81      public int getAccessLevel()
82      {
83          return accessLevel;
84      }
85  
86      public void setStatus(int status)
87      {
88          this.status = status;
89      }
90  
91      public int getStatus()
92      {
93          return status;
94      }
95  
96      public void setRegistered(boolean registered)
97      {
98          this.registered = registered;
99      }
100 
101     public boolean isRegistered()
102     {
103         return registered;
104     }
105 
106     public void setPlaying(boolean playing)
107     {
108         this.playing = playing;
109     }
110 
111     public boolean isPlaying()
112     {
113         return playing;
114     }
115 
116     public void setSpectator()
117     {
118         this.type = USER_SPECTATOR;
119     }
120 
121     public boolean isSpectator()
122     {
123         return (type == USER_SPECTATOR);
124     }
125 
126     public void setPlayer()
127     {
128         this.type = USER_PLAYER;
129     }
130 
131     public boolean isPlayer()
132     {
133         return (type == USER_PLAYER);
134     }
135 
136     public void setLocale(Locale locale)
137     {
138         this.locale = locale;
139     }
140 
141     public Locale getLocale()
142     {
143         return locale;
144     }
145 
146     /***
147      * Tells if the specified nickname is ignored by this user.
148      *
149      * @param name the name of the user
150      * @return <code></code>
151      * @since 0.2
152      */
153     public boolean ignores(String name)
154     {
155         return ignoredUsers == null ? false : ignoredUsers.contains(name.toLowerCase());
156     }
157 
158     /***
159      * Add the specified name to the list of ignored users.
160      *
161      * @param name
162      * @since 0.2
163      */
164     public void ignore(String name)
165     {
166         if (ignoredUsers == null)
167         {
168             ignoredUsers = new TreeSet<String>();
169         }
170 
171         ignoredUsers.add(name.toLowerCase());
172     }
173 
174     /***
175      * Remove the specified name from the list of ignored users.
176      *
177      * @param name
178      * @since 0.2
179      */
180     public void unignore(String name)
181     {
182         if (ignoredUsers != null)
183         {
184             ignoredUsers.remove(name.toLowerCase());
185         }
186     }
187 
188     /***
189      * Return the list of ignored players.
190      *
191      * @since 0.2
192      */
193     public Set<String> getIgnoredUsers()
194     {
195         return ignoredUsers == null ? new TreeSet<String>() : ignoredUsers;
196     }
197 
198     /***
199      * Set an extended property for this player.
200      */
201     public void setProperty(String key, Object value)
202     {
203         if (props == null)
204         {
205             props = new HashMap<String, Object>();
206         }
207         props.put(key, value);
208     }
209 
210     /***
211      * Return an extended property.
212      */
213     public Object getProperty(String key)
214     {
215         return (props == null) ? null : props.get(key);
216     }
217 
218     public String toString()
219     {
220         return "[User " + name + " <" + team + "> playing=" + playing + "]";
221     }
222 
223 }