Shadowrun: Awakened 29 September 2011 - Build 871
CreateCharacterMessage.cpp
Go to the documentation of this file.
00001 #include "CreateCharacterMessage.h"
00002 #include "ClientRegister.h"
00003 #include "WorldServer.h"
00004 #include "CreateCharacterPacket.h"
00005 #include "ReplyPacket.h"
00006 
00007 namespace SraNetwork
00008 {
00009     CreateCharacterMessage::CreateCharacterMessage(void)
00010     {
00011     }
00012 
00013 
00014     CreateCharacterMessage::~CreateCharacterMessage(void)
00015     {
00016     }
00017     
00018     SraPacket* CreateCharacterMessage::createPacketFromData(const unsigned char* data)
00019     {
00020         return (CreateCharacterPacket*)data;
00021     }
00022 
00023     void CreateCharacterMessage::process(MessageData* message)
00024     {
00025         printf("Processing a CreateCharacterMessage\n");
00026         RakNet::RakPeerInterface *clientI = SraNetwork::WorldServer::getClientInterface();
00027         if (clientI == NULL) 
00028         {
00029             printf("Error: A non existing client is used in CreateCharacterMessage::process\n");
00030             return;
00031         }
00032         
00033         
00034         ClientRegister* reg = ClientRegister::getInstance();
00035         const SraClientData* client = reg->getClientFromAddress( message->address );
00036         if (client == NULL)
00037         {
00038             printf("Error: A non existing client is used in CreateCharacterMessage::process\n");
00039             return;
00040         }
00041 
00042         // The client wants to create a new character
00043         CreateCharacterPacket packet;
00044         packet.Deserialize(&message->stream);
00045 
00046         // First check if the data is valid.
00047         RakString resultMessage = "";
00048         BitStream stream;
00049         if (!reg->checkForValidCharacterInfo(&packet.characterInfo, &resultMessage))
00050         {
00051             printf("Failed to crate a character because:  %s\n", resultMessage.C_String());
00052             ReplyPacket rp;
00053             rp.replyCode = ReplyPacket::FAIL;
00054             rp.messageOpCode = ID_CREATE_CHAR;
00055             rp.replyMessage = resultMessage;
00056             rp.Serialize(&stream);
00057             clientI->Send( &stream, MEDIUM_PRIORITY, 
00058                 RELIABLE_ORDERED, 0, message->address, false);
00059             return;
00060         }
00061         
00062         // Now try to create the character.
00063         int cId = reg->createCharacterForPlayer( packet.characterInfo.raceId, 
00064             message->address, client->clientID);
00065 
00066         if (cId == -1)
00067         {
00068             //TODO: We could use a more detailed message here ;)
00069             printf("Failed to create a character without a reason\n");
00070             ReplyPacket rp;
00071             rp.replyCode = ReplyPacket::FAIL;
00072             rp.messageOpCode = ID_CREATE_CHAR;
00073             rp.replyMessage = "Failed to create the character\n";
00074             rp.Serialize(&stream);
00075             clientI->Send( &stream, MEDIUM_PRIORITY, 
00076                 RELIABLE_ORDERED, 0, message->address, false);
00077             return;
00078         } 
00079         else
00080         {
00081             printf("Successfully created a character\n");
00082             // We don't need any message when returning OK
00083             ReplyPacket rp;
00084             rp.replyCode = ReplyPacket::OK;
00085             rp.messageOpCode = ID_CREATE_CHAR;
00086             rp.replyMessage = "";
00087             rp.Serialize(&stream);
00088             clientI->Send( &stream, MEDIUM_PRIORITY, 
00089                 RELIABLE_ORDERED, 0, message->address, false);
00090             return;
00091         }
00092     }
00093 }

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net