ServerBuyAccessoryHandler.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/ServerBuyAccessoryHandler.h>
00022 #include <server/ServerShotHolder.h>
00023 #include <server/ServerState.h>
00024 #include <server/ScorchedServer.h>
00025 #include <server/TurnController.h>
00026 #include <tank/TankContainer.h>
00027 #include <tank/TankState.h>
00028 #include <tank/TankScore.h>
00029 #include <tank/TankAccessories.h>
00030 #include <coms/ComsBuyAccessoryMessage.h>
00031 #include <coms/ComsMessageSender.h>
00032 #include <common/OptionsTransient.h>
00033 #include <common/OptionsScorched.h>
00034 #include <common/Logger.h>
00035 #include <weapons/AccessoryStore.h>
00036 #include <weapons/EconomyStore.h>
00037 
00038 ServerBuyAccessoryHandler *ServerBuyAccessoryHandler::instance_ = 0;
00039 
00040 ServerBuyAccessoryHandler *ServerBuyAccessoryHandler::instance()
00041 {
00042         if (!instance_)
00043         {
00044                 instance_ = new ServerBuyAccessoryHandler;
00045         }
00046         return instance_;
00047 }
00048 
00049 ServerBuyAccessoryHandler::ServerBuyAccessoryHandler()
00050 {
00051         ScorchedServer::instance()->getComsMessageHandler().addHandler(
00052                 "ComsBuyAccessoryMessage",
00053                 this);
00054 }
00055 
00056 ServerBuyAccessoryHandler::~ServerBuyAccessoryHandler()
00057 {
00058 }
00059 
00060 bool ServerBuyAccessoryHandler::processMessage(
00061         NetMessage &netMessage,
00062         const char *messageType,
00063         NetBufferReader &reader)
00064 {
00065         ComsBuyAccessoryMessage message;
00066         if (!message.readMessage(reader)) return false;
00067         unsigned int playerId = message.getPlayerId();
00068 
00069         // Check we are at the correct time to buy anything
00070         if (ScorchedServer::instance()->getGameState().getState() != 
00071                 ServerState::ServerStateBuying)
00072         {
00073                 Logger::log( "ERROR: Player attempted to buy accessory but in incorrect state");
00074                 return true;
00075         }
00076 
00077         // Check we are in the correct round no to buy anything
00078         if (ScorchedServer::instance()->getOptionsTransient().getCurrentGameNo() != 0)
00079         {
00080                 Logger::log( "ERROR: Player attempted to buy at incorrect time");
00081                 return true;
00082         }
00083 
00084         // Check that is player still exists
00085         Tank *tank = ScorchedServer::instance()->getTankContainer().getTankById(playerId);
00086         if (!tank)
00087         {
00088                 Logger::log( "ERROR: Player buying does not exist");
00089                 return true;
00090         }
00091 
00092         if (tank->getDestinationId() != netMessage.getDestinationId())
00093         {
00094                 Logger::log( "ERROR: Player buying does not exist at this destination");
00095                 return true;
00096         }
00097 
00098         // Check this player is alive
00099         if (tank->getState().getState() != TankState::sNormal)
00100         {
00101                 Logger::log( "ERROR: Player buying is not alive");
00102                 return true;
00103         }
00104 
00105         // Check this player has not already given a move
00106         if (ServerShotHolder::instance()->haveShot(playerId))
00107         {
00108                 Logger::log( "ERROR: Player buying has made move");
00109                 return true;
00110         }
00111 
00112         if (!TurnController::instance()->playerThisTurn(playerId))
00113         {
00114                 Logger::log( "ERROR: Player buying should not be buying");
00115                 return true;            
00116         }
00117 
00118         // Check that the accessory is valid
00119         Accessory *accessory = 
00120                 ScorchedServer::instance()->getAccessoryStore().
00121                 findByAccessoryId(message.getAccessoryId());
00122         if (!accessory)
00123         {
00124                 Logger::log(S3D::formatStringBuffer("ERROR: Player buying not-existant weapon \"%i\"", 
00125                         message.getAccessoryId()));
00126                 return true;
00127         }
00128 
00129         // The game state and everything is correct
00130         // Perform the actual add or remove of accessory
00131         if (message.getBuy())
00132         {
00133                 if (!tank->getAccessories().accessoryAllowed(accessory, accessory->getBundle())) return true;
00134                 if (accessory->getNoBuy()) return true;
00135                 if (tank->getScore().getMoney() < accessory->getPrice()) return true;
00136 
00137                 EconomyStore::instance()->getEconomy()->accessoryBought(
00138                         tank, accessory->getName());
00139 
00140                 // Add the accessory
00141                 tank->getAccessories().add(accessory, accessory->getBundle());
00142                 tank->getScore().setMoney(
00143                         tank->getScore().getMoney() - accessory->getPrice());
00144         }
00145         else
00146         {
00147                 if (tank->getAccessories().getAccessoryCount(accessory) <= 0) return true;
00148 
00149                 EconomyStore::instance()->getEconomy()->accessorySold(
00150                         tank, accessory->getName());
00151 
00152                 // Remove the accessory
00153                 tank->getAccessories().rm(accessory, 1);
00154                 tank->getScore().setMoney(
00155                         tank->getScore().getMoney() + accessory->getSellPrice());
00156         }
00157         return true;
00158 }

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