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.config;
021    
022    import java.util.*;
023    
024    /**
025     * Channel configuration.
026     *
027     * @author Emmanuel Bourg
028     * @version $Revision: 860 $, $Date: 2010-05-06 13:21:05 +0200 (jeu., 06 mai 2010) $
029     */
030    public class ChannelConfig
031    {
032        private Settings settings;
033        private String name = "noname";
034        private String password;
035        private String description;
036        private String topic;
037        private int maxPlayers = PLAYER_CAPACITY;
038        private int maxSpectators = SPECTATOR_CAPACITY;
039        private int accessLevel;
040        private boolean persistent;
041        private String winlistId;
042        private boolean idleAllowed;
043        private boolean visible = true;
044        private Speed speed = Speed.MIXED;
045    
046        /** extended properties */
047        private Properties props;
048    
049        /** channel filter definitions */
050        private List<FilterConfig> filters;
051    
052        /** Default spectator capacity */
053        public static final int SPECTATOR_CAPACITY = 50;
054    
055        /** Default player capacity */
056        public static final int PLAYER_CAPACITY = 6;
057    
058        public ChannelConfig()
059        {
060            filters = new ArrayList<FilterConfig>();
061            settings = new Settings();
062        }
063    
064        /**
065         * Gets game parameters.
066         */
067        public Settings getSettings()
068        {
069            return settings;
070        }
071    
072        /**
073         * Sets game parameters.
074         *
075         * @param settings
076         */
077        public void setSettings(Settings settings)
078        {
079            this.settings = settings;
080        }
081    
082        /**
083         * Gets channel name.
084         */
085        public String getName()
086        {
087            return name;
088        }
089    
090        /**
091         * Sets channel name.
092         *
093         * @param name
094         */
095        public void setName(String name)
096        {
097            this.name = name;
098        }
099    
100        /**
101         * Gets the password.
102         */
103        public String getPassword()
104        {
105            return password;
106        }
107    
108        /**
109         * Sets the password to enter the channel.
110         */
111        public void setPassword(String password)
112        {
113            this.password = password;
114        }
115    
116        /**
117         * Gets channel description.
118         */
119        public String getDescription()
120        {
121            return description;
122        }
123    
124        /**
125         * Sets the description shown on entering the channel.
126         */
127        public void setDescription(String description)
128        {
129            this.description = description;
130        }
131    
132        /**
133         * Return the topic.
134         */
135        public String getTopic() {
136            return topic;
137        }
138    
139        /**
140         * Set the topic.
141         */
142        public void setTopic(String topic) {
143            this.topic = topic;
144        }
145    
146        /**
147         * Gets maximum number of players allowed.
148         */
149        public int getMaxPlayers()
150        {
151            return maxPlayers;
152        }
153    
154        /**
155         * Sets the maximum number of players allowed at the same time in the channel.
156         *
157         * @param maxPlayers
158         */
159        public void setMaxPlayers(int maxPlayers)
160        {
161            this.maxPlayers = maxPlayers;
162        }
163    
164        /**
165         * Gets maximum number of spectators allowed.
166         */
167        public int getMaxSpectators()
168        {
169            return maxSpectators;
170        }
171    
172        /**
173         * Sets the maximum number of spectators allowed at the same time in the channel.
174         *
175         * @param maxSpectators
176         */
177        public void setMaxSpectators(int maxSpectators)
178        {
179            this.maxSpectators = maxSpectators;
180        }
181    
182        /**
183         * Gets the minimum access level required to join the channel.
184         */
185        public int getAccessLevel()
186        {
187            return accessLevel;
188        }
189    
190        /**
191         * Sets the minimum access level required to join the channel.
192         *
193         * @param accessLevel
194         */
195        public void setAccessLevel(int accessLevel)
196        {
197            this.accessLevel = accessLevel;
198        }
199    
200        /**
201         * Tell if the channel will vanish once the last player leave
202         *
203         * @return <tt>true</tt> if the channel is persistent, <tt>false</tt> if not
204         */
205        public boolean isPersistent()
206        {
207            return persistent;
208        }
209    
210        /**
211         * Sets channel persistence
212         *
213         * @param persistent
214         */
215        public void setPersistent(boolean persistent)
216        {
217            this.persistent = persistent;
218        }
219    
220        public String getWinlistId()
221        {
222            return winlistId;
223        }
224    
225        public void setWinlistId(String winlistId)
226        {
227            this.winlistId = winlistId;
228        }
229    
230        public String getProperty(String name)
231        {
232            return (props == null) ? null : props.getProperty(name);
233        }
234    
235        public void setProperty(String name, String value)
236        {
237            if (props == null)
238            {
239                props = new Properties();
240            }
241            props.setProperty(name, value);
242        }
243    
244        /**
245         * Tell if a password is required to enter the channel.
246         */
247        public boolean isPasswordProtected()
248        {
249            return (password != null);
250        }
251    
252        /**
253         * Returns an iterator of registered filters.
254         */
255        public Iterator<FilterConfig> getFilters()
256        {
257            return filters.iterator();
258        }
259    
260        /**
261         * Registers a new filter.
262         */
263        public void addFilter(FilterConfig fconf)
264        {
265            filters.add(fconf);
266        }
267    
268        public boolean isIdleAllowed()
269        {
270            return idleAllowed;
271        }
272    
273        public void setIdleAllowed(boolean idleAllowed)
274        {
275            this.idleAllowed = idleAllowed;
276        }
277    
278        public boolean isVisible()
279        {
280            return visible;
281        }
282    
283        public void setVisible(boolean visible)
284        {
285            this.visible = visible;
286        }
287    
288        public Speed getSpeed()
289        {
290            return speed;
291        }
292    
293        public void setSpeed(Speed speed)
294        {
295            this.speed = speed;
296        }
297    
298        /**
299         * Tells if the specified protocol is compatible with the speed restriction of the channel.
300         * 
301         * @param protocol the name of the protocol
302         * @since 0.3
303         */
304        public boolean isProtocolAccepted(String protocol)
305        {
306            switch (speed)
307            {
308                case NORMAL:
309                    return !"tetrifast".equals(protocol);
310                case FAST:
311                    return !"tetrinet".equals(protocol);
312                default:
313                    return true;
314            }
315        }
316    }