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/ServerGiftMoneyHandler.h> 00022 #include <server/ScorchedServer.h> 00023 #include <server/ServerShotHolder.h> 00024 #include <server/ServerState.h> 00025 #include <server/TurnController.h> 00026 #include <tank/TankContainer.h> 00027 #include <tank/TankState.h> 00028 #include <tank/TankScore.h> 00029 #include <engine/GameState.h> 00030 #include <coms/ComsGiftMoneyMessage.h> 00031 #include <coms/ComsMessageSender.h> 00032 #include <common/OptionsTransient.h> 00033 #include <common/Logger.h> 00034 00035 ServerGiftMoneyHandler *ServerGiftMoneyHandler::instance_ = 0; 00036 00037 ServerGiftMoneyHandler *ServerGiftMoneyHandler::instance() 00038 { 00039 if (!instance_) 00040 { 00041 instance_ = new ServerGiftMoneyHandler; 00042 } 00043 return instance_; 00044 } 00045 00046 ServerGiftMoneyHandler::ServerGiftMoneyHandler() 00047 { 00048 ScorchedServer::instance()->getComsMessageHandler().addHandler( 00049 "ComsGiftMoneyMessage", 00050 this); 00051 } 00052 00053 ServerGiftMoneyHandler::~ServerGiftMoneyHandler() 00054 { 00055 } 00056 00057 bool ServerGiftMoneyHandler::processMessage( 00058 NetMessage &netMessage, 00059 const char *messageType, 00060 NetBufferReader &reader) 00061 { 00062 ComsGiftMoneyMessage message; 00063 if (!message.readMessage(reader)) return false; 00064 00065 unsigned int fromPlayerId = message.getFromPlayerId(); 00066 unsigned int toPlayerId = message.getToPlayerId(); 00067 00068 // Check we are at the correct time to buy anything 00069 if (ScorchedServer::instance()->getGameState().getState() != 00070 ServerState::ServerStateBuying) 00071 { 00072 Logger::log( "ERROR: Player attempted to gift money but in incorrect state"); 00073 return true; 00074 } 00075 00076 // Check we are in the correct round no to buy anything 00077 if (ScorchedServer::instance()->getOptionsTransient().getCurrentGameNo() != 0) 00078 { 00079 Logger::log( "ERROR: Player attempted to gift money at incorrect time"); 00080 return true; 00081 } 00082 00083 // Check that is player still exists 00084 Tank *fromTank = ScorchedServer::instance()-> 00085 getTankContainer().getTankById(fromPlayerId); 00086 if (!fromTank) 00087 { 00088 Logger::log( "ERROR: Player gifting does not exist"); 00089 return true; 00090 } 00091 if (fromTank->getDestinationId() != netMessage.getDestinationId()) 00092 { 00093 Logger::log( "ERROR: Player gifting does not exist at this destination"); 00094 return true; 00095 } 00096 if (fromTank->getState().getState() != TankState::sNormal) 00097 { 00098 Logger::log( "ERROR: Player gifting is not alive"); 00099 return true; 00100 } 00101 if (!TurnController::instance()->playerThisTurn(fromPlayerId)) 00102 { 00103 Logger::log( "ERROR: Player gifting should not be buying"); 00104 return true; 00105 } 00106 00107 // Check to player 00108 Tank *toTank = ScorchedServer::instance()-> 00109 getTankContainer().getTankById(toPlayerId); 00110 if (!toTank) 00111 { 00112 Logger::log( "ERROR: Player gifting to does not exist"); 00113 return true; 00114 } 00115 if (toTank->getState().getState() != TankState::sNormal && 00116 toTank->getState().getState() != TankState::sDead) 00117 { 00118 Logger::log( "ERROR: Player gifting to is not alive"); 00119 return true; 00120 } 00121 if (toTank->getTeam() != fromTank->getTeam()) 00122 { 00123 Logger::log( "ERROR: Player gifting is in different teams"); 00124 return true; 00125 } 00126 if (toTank == fromTank) 00127 { 00128 Logger::log( "ERROR: Player gifting to/from is same"); 00129 return true; 00130 } 00131 00132 // Check tank has required amount of money 00133 int money = message.getMoney(); 00134 if (money < 0) return true; 00135 if (fromTank->getScore().getMoney() < money) return true; 00136 00137 fromTank->getScore().setMoney( 00138 fromTank->getScore().getMoney() - money); 00139 toTank->getScore().setMoney( 00140 toTank->getScore().getMoney() + money); 00141 00142 // Forward this message to the intended 00143 ComsMessageSender::sendToAllPlayingClients(message); 00144 00145 return true; 00146 }
1.5.3