ServerCommon.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/ServerCommon.h>
00022 #include <server/ScorchedServer.h>
00023 #include <server/ScorchedServerUtil.h>
00024 #include <server/ServerMessageHandler.h>
00025 #include <server/ServerChannelManager.h>
00026 #include <tank/TankContainer.h>
00027 #include <tank/TankState.h>
00028 #include <tank/TankScore.h>
00029 #include <target/TargetLife.h>
00030 #include <common/OptionsScorched.h>
00031 #include <common/OptionsTransient.h>
00032 #include <common/Logger.h>
00033 #include <common/FileLogger.h>
00034 #include <common/Defines.h>
00035 #include <coms/ComsMessageSender.h>
00036 #include <coms/ComsConnectRejectMessage.h>
00037 #include <net/NetInterface.h>
00038 
00039 static FileLogger *serverFileLogger = 0;
00040 
00041 void ServerCommon::startFileLogger()
00042 {
00043         if (!serverFileLogger) 
00044         {
00045                 char buffer[256];
00046                 snprintf(buffer, 256, "ServerLog-%i-", 
00047                         ScorchedServer::instance()->getOptionsGame().getPortNo());
00048 
00049                 serverFileLogger = new FileLogger(buffer);
00050                 if (0 != strcmp(ScorchedServer::instance()->getOptionsGame().
00051                         getServerFileLogger(), "none"))
00052                 {
00053                         Logger::addLogger(serverFileLogger);
00054                         Logger::log( "Created file logger.");
00055                 }
00056                 else
00057                 {
00058                         Logger::log( "Not created file logger.");
00059                 }
00060         }       
00061 }
00062 
00063 void ServerCommon::kickDestination(unsigned int destinationId, 
00064         const std::string &message)
00065 {
00066         Logger::log(S3D::formatStringBuffer("Kicking destination \"%i\"", 
00067                 destinationId));
00068 
00069         if (message[0])
00070         {
00071                 ComsConnectRejectMessage rejectMessage(message.c_str());
00072                 ComsMessageSender::sendToSingleClient(rejectMessage, destinationId);
00073                 ScorchedServer::instance()->getNetInterface().processMessages();
00074         }
00075 
00076         bool kickedPlayers = false;
00077         std::map<unsigned int, Tank *>::iterator itor;
00078         std::map<unsigned int, Tank *> tanks = 
00079                 ScorchedServer::instance()->getTankContainer().getPlayingTanks();
00080         for (itor = tanks.begin();
00081                 itor != tanks.end();
00082                 itor++)
00083         {
00084                 Tank *tank = (*itor).second;
00085                 if (tank->getDestinationId() == destinationId)
00086                 {
00087                         kickedPlayers = true;
00088                         kickPlayer(tank->getPlayerId());
00089                 }
00090         }
00091         
00092         // Make sure we disconnect even if a player has not been created yet
00093         if (!kickedPlayers)
00094         {
00095                 ScorchedServer::instance()->getNetInterface().
00096                         disconnectClient(destinationId);
00097         }
00098 }
00099 
00100 void ServerCommon::kickPlayer(unsigned int playerId)
00101 {
00102         Logger::log(S3D::formatStringBuffer("Kicking player \"%i\"", playerId));
00103 
00104         Tank *tank = ScorchedServer::instance()->
00105                 getTankContainer().getTankById(playerId);
00106         if (tank)
00107         {
00108                 ServerChannelManager::instance()->sendText(ChannelText("info", 
00109                         "ADMIN_PLAYER_KICKED", 
00110                         "Player \"{0}\" has been kicked from the server",
00111                         tank->getTargetName()), true);
00112                 Logger::log(S3D::formatStringBuffer("Kicking client \"%s\" \"%i\"", 
00113                         tank->getCStrName().c_str(), tank->getPlayerId()));
00114 
00115                 if (tank->getDestinationId() == 0)
00116                 {
00117                         ServerMessageHandler::instance()->
00118                                 destroyPlayer(tank->getPlayerId(), "Kicked");
00119                 }
00120                 else
00121                 {
00122                         // Cannot delay kick a player as the rest of the code
00123                         // may expect him to have gone (when he is still there delayed)
00124                         // and try to send messages to him
00125                         ScorchedServer::instance()->getNetInterface().
00126                                 disconnectClient(tank->getDestinationId());
00127                         ScorchedServer::instance()->getNetInterface().processMessages();
00128                 }
00129         }
00130 }
00131 
00132 void ServerCommon::killAll()
00133 {
00134         Logger::log("Killing all players");
00135 
00136         std::map<unsigned int, Tank *>::iterator itor;
00137         std::map<unsigned int, Tank *> &tanks = 
00138                 ScorchedServer::instance()->getTankContainer().getAllTanks();
00139         for (itor = tanks.begin();
00140                  itor != tanks.end();
00141                  itor++)
00142         {
00143                 Tank *current = (*itor).second;
00144                 if (current->getState().getState() == TankState::sNormal)
00145                 {
00146                         current->getState().setState(TankState::sDead);
00147                         current->getState().setLives(0);
00148                 }
00149         }
00150 }
00151 
00152 void ServerCommon::startNewGame()
00153 {
00154         killAll();
00155 
00156         Logger::log("Starting a new game");
00157         ScorchedServer::instance()->getOptionsTransient().startNewGame();
00158 }
00159 
00160 bool &ServerCommon::getExitEmpty()
00161 {
00162         static bool exitEmpty = false;
00163         return exitEmpty;
00164 }
00165 
00166 void ServerCommon::serverLog(const std::string &text)
00167 {
00168 #ifdef S3D_SERVER
00169         {
00170                 Logger::log(text);
00171         }
00172 #endif
00173 }

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