001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2004-2005 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.*;
023
024 import net.jetrix.*;
025 import net.jetrix.config.*;
026 import net.jetrix.messages.channel.*;
027
028 /**
029 * Set the game field and settings when the game starts. A new puzzle is
030 * displayed every time. The sequence of puzzles is generated by a
031 * {@link PuzzleGenerator}, by default the {@link DownstackPuzzleGenerator}
032 * is used. An alternative generator can be used by specifying the <tt>generator</tt>
033 * parameter in the filter configuration. The filter configuration is passed
034 * to the generator for its initialization.
035 *
036 * @since 0.3
037 *
038 * @author Emmanuel Bourg
039 * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
040 */
041 public class PuzzleFilter extends GenericFilter
042 {
043 private static final String DEFAULT_GENERATOR = DownstackPuzzleGenerator.class.getName();
044
045 private PuzzleGenerator generator;
046
047 public void init()
048 {
049 try
050 {
051 generator = (PuzzleGenerator) Class.forName(getConfig().getString("generator", DEFAULT_GENERATOR)).newInstance();
052 }
053 catch (Exception e)
054 {
055 generator = new DownstackPuzzleGenerator();
056 }
057
058 // initialize the generator
059 generator.init(getConfig());
060 }
061
062 public void onMessage(NewGameMessage m, List<Message> out)
063 {
064 // get the next puzzle
065 Puzzle puzzle = generator.getNextPuzzle();
066
067 // send the puzzle description
068 PlineMessage description = new PlineMessage();
069 description.setKey("filter.puzzle.announce", puzzle.getKey(), puzzle.getName(), puzzle.getAuthor());
070 out.add(description);
071
072 // update the game settings and the channel settings
073 if (puzzle.getSettings() != null)
074 {
075 m.setSettings(puzzle.getSettings());
076 getChannel().getConfig().setSettings(puzzle.getSettings());
077 }
078
079 // forward the new game message
080 out.add(m);
081
082 // update the field of the first slot
083 FieldMessage fieldMessage = new FieldMessage();
084 fieldMessage.setSlot(1);
085 fieldMessage.setField(puzzle.getField().getFieldString());
086
087 out.add(fieldMessage);
088 }
089
090 public void onMessage(FieldMessage m, List<Message> out)
091 {
092 // check the height of the new field
093 Field field = getChannel().getField(0);
094
095 if (field.getHighest() <= 2)
096 {
097 // stop the game
098 getChannel().send(new EndGameMessage());
099
100 // send the congratulation message
101 PlineMessage grats = new PlineMessage();
102 grats.setKey("filter.puzzle.cleared");
103 getChannel().send(grats);
104 }
105
106 // forward the field update
107 out.add(m);
108 }
109 }