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.filter;
021    
022    import net.jetrix.*;
023    import net.jetrix.messages.channel.*;
024    import net.jetrix.messages.channel.specials.*;
025    
026    import java.util.*;
027    
028    import org.apache.commons.lang.StringUtils;
029    
030    /**
031     * Game mod : The first player completing 7 tetris win.
032     *
033     * @author Emmanuel Bourg
034     * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
035     */
036    public class TetrisFilter extends GenericFilter
037    {
038        private int[] tetrisCount = new int[6];
039        private int tetrisLimit = 7;
040        private boolean addToAll = false;
041    
042        public void init()
043        {       
044            tetrisLimit = config.getInt("limit", tetrisLimit);
045            addToAll = config.getBoolean("addToAll", addToAll);
046        }
047    
048        public void onMessage(StartGameMessage m, List<Message> out)
049        {
050            Arrays.fill(tetrisCount, 0);
051    
052            GmsgMessage message = new GmsgMessage();
053            message.setKey("filter.tetris.start_message", tetrisLimit);
054    
055            out.add(m);
056            out.add(message);
057        }
058    
059        public void onMessage(FourLinesAddedMessage m, List<Message> out)
060        {
061            int from = m.getFromSlot() - 1;
062            tetrisCount[from]++;
063    
064            if (addToAll)
065            {
066                out.add(m);
067            }
068    
069            if (tetrisCount[from] >= tetrisLimit)
070            {
071                getChannel().send(new EndGameMessage());
072    
073                User winner = getChannel().getPlayer(m.getFromSlot());
074                PlineMessage announce = new PlineMessage();
075                announce.setKey("channel.player_won", winner.getName());
076                getChannel().send(announce);
077            }
078            else
079            {
080                // get the highest tetris count
081                int max = 0;
082                for (int i = 0; i < 6; i++)
083                {
084                    if (tetrisCount[i] > max)
085                    {
086                        max = tetrisCount[i];
087                    }
088                }
089    
090                if (tetrisCount[from] == max)
091                {
092                    // look for the leaders
093                    List<String> leaders = new ArrayList<String>();
094                    for (int i = 0; i < 6; i++)
095                    {
096                        if (tetrisCount[i] == max)
097                        {
098                            Client client = getChannel().getClient(i + 1);
099                            if (client != null)
100                            {
101                                leaders.add(client.getUser().getName());
102                            }
103                        }
104                    }
105    
106                    // announce the leaders
107                    GmsgMessage announce = new GmsgMessage();
108    
109                    if (leaders.size() == 1)
110                    {
111                        announce.setKey("filter.tetris.lead", leaders.get(0), max);
112                    }
113                    else
114                    {
115                        String leadersList = StringUtils.join(leaders.iterator(), ", ");
116                        announce.setKey("filter.tetris.tied", leadersList, max);
117                    }
118    
119                    out.add(announce);
120                }
121            }
122        }
123    
124        public void onMessage(TwoLinesAddedMessage m, List<Message> out)
125        {
126            if (addToAll)
127            {
128                out.add(m);
129            }
130        }
131    
132        public void onMessage(OneLineAddedMessage m, List<Message> out)
133        {
134            if (addToAll)
135            {
136                out.add(m);
137            }
138        }
139    
140        public String getName()
141        {
142            return "7 Tetris Mod";
143        }
144    
145        public String getDescription()
146        {
147            return "Game mod - The first player completing 7 tetris win.";
148        }
149    
150        public String getVersion()
151        {
152            return "1.0";
153        }
154    
155        public String getAuthor()
156        {
157            return "Emmanuel Bourg";
158        }
159    
160    }