1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include <server/ServerGiftMoneyHandler.h>
22 #include <server/ScorchedServer.h>
23 #include <server/ServerSimulator.h>
24 #include <server/ServerState.h>
25 #include <simactions/TankGiftSimAction.h>
26 #include <target/TargetContainer.h>
27 #include <tank/Tank.h>
28 #include <tank/TankState.h>
29 #include <common/Logger.h>
30 
ServerGiftMoneyHandler(ComsMessageHandler & comsMessageHandler)31 ServerGiftMoneyHandler::ServerGiftMoneyHandler(ComsMessageHandler &comsMessageHandler)
32 {
33 	comsMessageHandler.addHandler(
34 		ComsGiftMoneyMessage::ComsGiftMoneyMessageType,
35 		this);
36 }
37 
~ServerGiftMoneyHandler()38 ServerGiftMoneyHandler::~ServerGiftMoneyHandler()
39 {
40 }
41 
processMessage(NetMessage & netMessage,const char * messageType,NetBufferReader & reader)42 bool ServerGiftMoneyHandler::processMessage(
43 	NetMessage &netMessage,
44 	const char *messageType,
45 	NetBufferReader &reader)
46 {
47 	ComsGiftMoneyMessage message;
48 	if (!message.readMessage(reader)) return false;
49 
50 	unsigned int fromPlayerId = message.getFromPlayerId();
51 
52 	// Check we are at the correct time to buy anything
53 	if ((ScorchedServer::instance()->getServerState().getState() !=
54 		ServerState::ServerBuyingState) &&
55 		(ScorchedServer::instance()->getServerState().getState() !=
56 		ServerState::ServerTankNewGameState))
57 	{
58 		Logger::log( "ERROR: Player attempted to gift money but in incorrect state");
59 		return true;
60 	}
61 
62 	// Check that is player still exists
63 	Tank *fromTank = ScorchedServer::instance()->
64 		getTargetContainer().getTankById(fromPlayerId);
65 	if (!fromTank)
66 	{
67 		Logger::log( "ERROR: Player gifting does not exist");
68 		return true;
69 	}
70 	if (fromTank->getDestinationId() != netMessage.getDestinationId())
71 	{
72 		Logger::log( "ERROR: Player gifting does not exist at this destination");
73 		return true;
74 	}
75 	if (fromTank->getState().getState() != TankState::sBuying)
76 	{
77 		Logger::log( "ERROR: Player gifting when not in buying state");
78 		return true;
79 	}
80 
81 	// Send message
82 	TankGiftSimAction *action = new TankGiftSimAction(message);
83 	ScorchedServer::instance()->getServerSimulator().addSimulatorAction(action);
84 
85 	return true;
86 }
87