001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2001-2003  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;
021    
022    import java.util.*;
023    
024    /**
025     * A user connected to the server. The user can be a player or a spectator.
026     *
027     * @author Emmanuel Bourg
028     * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
029     */
030    public class User
031    {
032        private String name;
033        private String team;
034        private int accessLevel;
035        private int status;
036        private boolean registered;
037        private boolean playing;
038        private int type;
039        private Locale locale;
040        private Map<String, Object> props;
041        private Set<String> ignoredUsers;
042    
043        public static final int STATUS_OK  = 0;
044        public static final int STATUS_AFK = 1;
045        
046        public static final int USER_PLAYER = 0;
047        public static final int USER_SPECTATOR = 1;
048    
049        public User() { }
050    
051        public User(String name)
052        {
053            this.name = name;
054        }
055    
056        public void setName(String name)
057        {
058            this.name = name;
059        }
060    
061        public String getName()
062        {
063            return name;
064        }
065    
066        public void setTeam(String team)
067        {
068            this.team = team;
069        }
070    
071        public String getTeam()
072        {
073            return team;
074        }
075    
076        public void setAccessLevel(int accessLevel)
077        {
078            this.accessLevel = accessLevel;
079        }
080    
081        public int getAccessLevel()
082        {
083            return accessLevel;
084        }
085    
086        public void setStatus(int status)
087        {
088            this.status = status;
089        }
090    
091        public int getStatus()
092        {
093            return status;
094        }
095    
096        public void setRegistered(boolean registered)
097        {
098            this.registered = registered;
099        }
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    }