ServerReadyState.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <server/ServerReadyState.h>
00022 #include <server/ServerNewGameState.h>
00023 #include <server/ServerState.h>
00024 #include <server/ServerShotHolder.h>
00025 #include <server/ScorchedServer.h>
00026 #include <server/ServerChannelManager.h>
00027 #include <server/ServerCommon.h>
00028 #include <common/OptionsScorched.h>
00029 #include <common/Logger.h>
00030 #include <common/OptionsTransient.h>
00031 #include <coms/ComsPlayerStatusMessage.h>
00032 #include <coms/ComsMessageSender.h>
00033 #include <tank/TankContainer.h>
00034 #include <tank/TankState.h>
00035 
00036 ServerReadyState::ServerReadyState() : 
00037         GameStateI("ServerReadyState"),
00038         time_(0.0f)
00039 {
00040 }
00041 
00042 ServerReadyState::~ServerReadyState()
00043 {
00044 }
00045 
00046 void ServerReadyState::enterState(const unsigned state)
00047 {
00048         // Make sure that there are no shots from the last turns
00049         ServerShotHolder::instance()->clearShots();
00050 
00051         // Add any pending tanks into the game
00052         int count = ServerNewGameState::addTanksToGame(state);
00053 
00054         // Set the wait timer to the current time
00055         time_ = 0.0f;
00056 
00057         if (state == ServerState::ServerStateNewGameReady)
00058         {
00059                 idleTime_ = (float) ScorchedServer::instance()->
00060                         getOptionsGame().getIdleKickTime();
00061         }
00062         else
00063         {
00064                 if (count == 0)
00065                 {
00066                         idleTime_ = (float) ScorchedServer::instance()->
00067                                 getOptionsGame().getIdleShotKickTime();
00068                 }
00069                 else
00070                 {
00071                         idleTime_ = (float) ScorchedServer::instance()->
00072                                 getOptionsGame().getIdleKickTime();
00073                 }
00074         }
00075 #ifndef S3D_SERVER
00076         idleTime_ = 0;
00077 #endif
00078 
00079         // Make all computer players ready
00080         // And send out the first status messages
00081         ComsPlayerStatusMessage statusMessage;
00082         std::map<unsigned int, Tank *> &tanks = 
00083                 ScorchedServer::instance()->getTankContainer().getAllTanks();
00084         std::map<unsigned int, Tank *>::iterator itor;
00085         for (itor = tanks.begin();
00086                         itor != tanks.end();
00087                         itor++)
00088         {
00089                 Tank *tank = (*itor).second;
00090                 if (tank->getDestinationId() == 0)
00091                 {
00092                         // Set computer player ready
00093                         tank->getState().setReady();
00094                 }
00095                 else
00096                 {
00097                         // Set all other players not-ready
00098                         statusMessage.getWaitingPlayers().push_back(tank->getPlayerId());
00099                 }
00100         }
00101 
00102         // Tell clients who we are waiting on
00103         ComsMessageSender::sendToAllPlayingClients(statusMessage, NetInterfaceFlags::fAsync);   
00104 }
00105 
00106 bool ServerReadyState::acceptStateChange(const unsigned state, 
00107                 const unsigned nextState,
00108                 float frameTime)
00109 {
00110         // Send status messages every 5 seconds
00111                 if (((int) time_) / 5 != ((int) (time_ + frameTime)) / 5)
00112                 {
00113                         // Say who we are waiting on
00114                         ComsPlayerStatusMessage statusMessage;
00115                         std::map<unsigned int, Tank *> &tanks = 
00116                                 ScorchedServer::instance()->getTankContainer().getAllTanks();
00117                         std::map<unsigned int, Tank *>::iterator itor;
00118                         for (itor = tanks.begin();
00119                                  itor != tanks.end();
00120                                  itor++)
00121                         {
00122                                 Tank *tank = (*itor).second;
00123                                 if (tank->getState().getReadyState() == TankState::SNotReady)
00124                                 {
00125                                         statusMessage.getWaitingPlayers().push_back(tank->getPlayerId());
00126                                 }
00127                         }
00128                         ComsMessageSender::sendToAllPlayingClients(statusMessage, NetInterfaceFlags::fAsync);                   
00129                 }
00130 
00131         time_ += frameTime;
00132 
00133         // Check all players returned ready
00134         if(ScorchedServer::instance()->getTankContainer().allReady())
00135         {
00136                 //Logger::log( "All ready after %.2f seconds", time_);
00137                 finished();
00138                 return true;
00139         }
00140 
00141         // Check if any players have timed out
00142         if ((idleTime_ > 0) && (time_ > idleTime_))
00143         {
00144                 // Kick all not returned players
00145                 std::map<unsigned int, Tank *> &tanks = 
00146                         ScorchedServer::instance()->getTankContainer().getAllTanks();
00147                 std::map<unsigned int, Tank *>::iterator itor;
00148                 for (itor = tanks.begin();
00149                         itor != tanks.end();
00150                         itor++)
00151                 {
00152                         Tank *tank = (*itor).second;
00153                         if (tank->getState().getReadyState() == TankState::SNotReady)
00154                         {
00155                                 ServerChannelManager::instance()->sendText(
00156                                         ChannelText("info",
00157                                                 "KICK_RESPONSE_TIMEOUTE",
00158                                                 "{0} kicked for not responding for {1} seconds", 
00159                                                 tank->getTargetName(), idleTime_),
00160                                         true);
00161                                 ServerCommon::kickDestination(tank->getDestinationId());
00162                         }
00163                 }
00164 
00165                 finished();
00166 
00167                 // Stimulate into the next state
00168                 return true;
00169         }
00170 
00171         return false;
00172 }
00173 
00174 void ServerReadyState::finished()
00175 {
00176         // Set all the tanks to not ready
00177         ScorchedServer::instance()->getTankContainer().setAllNotReady();
00178 
00179         // Say we are waiting on no one
00180         ComsPlayerStatusMessage statusMessage;
00181         ComsMessageSender::sendToAllPlayingClients(statusMessage);      
00182 }

Generated on Mon Feb 16 15:14:53 2009 for Scorched3D by  doxygen 1.5.3