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 00022 #include <server/ServerPlayerReadyHandler.h> 00023 #include <server/ServerState.h> 00024 #include <server/ScorchedServer.h> 00025 #include <common/Logger.h> 00026 #include <tank/TankContainer.h> 00027 #include <tank/TankState.h> 00028 #include <coms/ComsPlayerReadyMessage.h> 00029 #include <coms/ComsMessageSender.h> 00030 00031 ServerPlayerReadyHandler *ServerPlayerReadyHandler::instance_ = 0; 00032 00033 ServerPlayerReadyHandler *ServerPlayerReadyHandler::instance() 00034 { 00035 if (!instance_) 00036 { 00037 instance_ = new ServerPlayerReadyHandler; 00038 } 00039 return instance_; 00040 } 00041 00042 ServerPlayerReadyHandler::ServerPlayerReadyHandler() 00043 { 00044 ScorchedServer::instance()->getComsMessageHandler().addHandler( 00045 "ComsPlayerReadyMessage", 00046 this); 00047 } 00048 00049 ServerPlayerReadyHandler::~ServerPlayerReadyHandler() 00050 { 00051 } 00052 00053 bool ServerPlayerReadyHandler::processMessage(NetMessage &netMessage, 00054 const char *messageType, NetBufferReader &reader) 00055 { 00056 // Decode the connect message 00057 ComsPlayerReadyMessage message; 00058 if (!message.readMessage(reader)) return false; 00059 00060 // Check this client has not tried to add a tank before 00061 unsigned int tankId = message.getPlayerId(); 00062 Tank *tank = ScorchedServer::instance()->getTankContainer().getTankById(tankId); 00063 if (!tank) 00064 { 00065 Logger::log(S3D::formatStringBuffer("ERROR: Message from unknown tank \"%i\"", tankId)); 00066 return false; 00067 } 00068 00069 if (tank->getDestinationId() != netMessage.getDestinationId()) 00070 { 00071 Logger::log(S3D::formatStringBuffer("ERROR: ServerPlayerReadyHandler - " 00072 "Message from tank at wrong destination \"%i != %i\"", 00073 tank->getDestinationId(), netMessage.getDestinationId())); 00074 return false; 00075 } 00076 00077 // Check the message is sent in the correct state 00078 if (ScorchedServer::instance()->getGameState().getState() != ServerState::ServerStateNewGameReady && 00079 ScorchedServer::instance()->getGameState().getState() != ServerState::ServerStateShotReady && 00080 ScorchedServer::instance()->getGameState().getState() != ServerState::ServerStateShot) 00081 { 00082 return true; 00083 } 00084 00085 // Set this tank as ready to proceed 00086 // This is used by stimuli to check if all tanks are syncronised 00087 tank->getState().setReady(); 00088 00089 return true; 00090 }
1.5.3