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 static net.jetrix.GameState.*;
023
024 import java.util.*;
025
026 import net.jetrix.*;
027 import net.jetrix.commands.StartCommand;
028 import net.jetrix.messages.channel.*;
029
030 /**
031 * Starts the game automatically once everyone said "go".
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 StartFilter extends GenericFilter
037 {
038 private long timestamp[];
039 private int delay = 10000;
040 private int countdown = 0;
041
042 public void init()
043 {
044 // reading parameters
045 delay = config.getInt("delay", delay);
046 countdown = config.getInt("countdown", countdown);
047
048 timestamp = new long[6];
049 }
050
051 public void onMessage(PlineMessage m, List<Message> out)
052 {
053 int slot = m.getSlot();
054 String text = m.getText();
055
056 // forward the message
057 out.add(m);
058
059 // do not check server messages
060 if (slot == 0)
061 {
062 return;
063 }
064
065 // can't start if the game isn't stopped
066 if (getChannel().getGameState() != STOPPED)
067 {
068 return;
069 }
070
071 // check if the text starts with "go"
072 if (text != null && text.toLowerCase().trim().startsWith("go"))
073 {
074 // update the timestamp
075 long now = System.currentTimeMillis();
076 timestamp[slot - 1] = now;
077
078 // check if everyone said "go" in the given delay
079 // AFK players shouldn't be checked
080 boolean doStart = true;
081 int i = 0;
082 while (i < 6 && doStart)
083 {
084 Client player = getChannel().getClient(i + 1);
085 doStart = (player == null) || (player != null && (now - timestamp[i]) <= delay);
086 i = i + 1;
087 }
088
089 if (doStart)
090 {
091 Arrays.fill(timestamp, 0);
092
093 if (countdown == 0)
094 {
095 StartGameMessage startMessage = new StartGameMessage();
096 out.add(startMessage);
097 }
098 else
099 {
100 (new StartCommand.CountDown(getChannel(), countdown)).start();
101 }
102 }
103 }
104 }
105
106 public String getName()
107 {
108 return "Start Filter";
109 }
110
111 public String getDescription()
112 {
113 return "Starts the game automatically once everyone said 'go'.";
114 }
115
116 public String getVersion()
117 {
118 return "1.1";
119 }
120
121 public String getAuthor()
122 {
123 return "Emmanuel Bourg";
124 }
125
126 }