001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2010  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.filter;
021    
022    import java.util.ArrayList;
023    import java.util.Iterator;
024    import java.util.List;
025    
026    import net.jetrix.Client;
027    import net.jetrix.Message;
028    import net.jetrix.messages.channel.PlineMessage;
029    import net.jetrix.messages.channel.StartGameMessage;
030    import net.jetrix.messages.channel.GmsgMessage;
031    import net.jetrix.messages.channel.TextMessage;
032    import net.jetrix.protocols.TetrifastProtocol;
033    import org.apache.commons.lang.StringUtils;
034    
035    /**
036     * Filter displaying a warning at the beginning of the game if everyone
037     * isn't playing with the same piece delay.
038     *
039     * @author Emmanuel Bourg
040     * @version $Revision: 860 $, $Date: 2010-05-06 13:21:05 +0200 (jeu., 06 mai 2010) $
041     * @since 0.3
042     */
043    public class UnbalancedSpeedWarningFilter extends GenericFilter
044    {
045        public void onMessage(StartGameMessage m, List<Message> out)
046        {
047            Iterator<Client> players = getChannel().getPlayers();
048            
049            // collect the name of the TetriFast and TetriNET players
050            List<String> fastClients = new ArrayList<String>();
051            List<String> normalClients = new ArrayList<String>();
052            while (players.hasNext())
053            {
054                Client client = players.next();
055                if (client != null && client.getUser().isPlayer())
056                {
057                    if (client.getProtocol() instanceof TetrifastProtocol)
058                    {
059                        fastClients.add(client.getUser().getName());
060                    }
061                    else
062                    {
063                        normalClients.add(client.getUser().getName());
064                    }
065                }
066            }
067            
068            if (!normalClients.isEmpty() && !fastClients.isEmpty())
069            {
070                TextMessage warning = createWarningMessage(normalClients, fastClients);
071                out.add(warning);
072                
073                out.add(m);
074    
075                GmsgMessage gmsg = new GmsgMessage();
076                gmsg.setKey(warning.getKey());
077                gmsg.setParams(warning.getParams());
078                out.add(gmsg);
079            }
080            else
081            {
082                out.add(m);
083            }
084        }
085    
086        private TextMessage createWarningMessage(List<String> normalClients, List<String> fastClients)
087        {
088            String type;
089            List<String> clients;
090            if (normalClients.size() < fastClients.size())
091            {
092                type = "normal";
093                clients = normalClients;
094            }
095            else
096            {
097                type = "fast";
098                clients = fastClients;
099            }
100            
101            PlineMessage message = new PlineMessage();
102            if (clients.size() == 1)
103            {
104                message.setKey("filter.unbalanced_speed." + type + ".one", clients.get(0));
105            }
106            else
107            {
108                List<String> firstClients = clients.subList(0, clients.size() - 1);
109                String lastClient = clients.get(clients.size() - 1); 
110                System.out.println("first: " + firstClients);
111                System.out.println("last: " + lastClient);
112                
113                message.setKey("filter.unbalanced_speed." + type + ".many", StringUtils.join(firstClients.toArray(), ", "), lastClient);
114            }
115            
116            return message;
117        }
118    
119        public String getName()
120        {
121            return "Unbalanced speed warning";
122        }
123    
124        public String getDescription()
125        {
126            return "Displays a warning at the beginning of the game if everyone isn't playing with the same piece delay";
127        }
128    
129        public String getVersion()
130        {
131            return "1.0";
132        }
133    
134        public String getAuthor()
135        {
136            return "Emmanuel Bourg";
137        }
138    }