#include "LoginMsgPacket.h"
#include "Player.h"
#include "PlayerManager.h"
void CLoginMsgPacket::readStream(CInputSocketStream& _instream)
{
_instream.readStream((char*)&m_login_msg, sizeof(s_login_msg));
m_login_msg.m_user_name[m_login_msg.m_user_name_length] = '\0';
m_login_msg.m_password[m_login_msg.m_password_length] = '\0';
m_login_msg.output();
}
void CLoginMsgPacket::writeStream(COutputSocketStream& _outstream)
{
}
void CLoginMsgPacket::excute(CPlayer* _player)
{
s_msg_head login_reback_head;
login_reback_head.setMsgType( REBACK );
login_reback_head.setMsgLen(sizeof(s_msg_head) + sizeof(s_login_reback_msg));
s_login_reback_msg login_reback;
//the init msg to server.
s_msg_head init_head;
init_head.setMsgType(INIT_INFO);
init_head.setMsgLen(sizeof(s_msg_head) + sizeof(s_init_msg));
s_init_msg init_msg;
if( isUserAvaliable() )
{
printf("login success\n");
//the login
login_reback.setSuccess();
login_reback.setSessionID(_player->getSessionID());
_player->setUserName( m_login_msg.m_user_name );
_player->sendMessage((char*)&login_reback_head, sizeof(s_msg_head));
_player->sendMessage((char*)&login_reback, sizeof(s_login_reback_msg));
//the init_msg
init_msg = getInitMsg();
//get the username
init_msg.setUserName(m_login_msg.m_user_name,
m_login_msg.m_user_name_length);
init_msg.setSessionID(_player->getSessionID());
_player->sendMessage((char*)&init_head, sizeof(s_msg_head));
_player->sendMessage((char*)&init_msg, sizeof(s_init_msg));
//send the player now to the player
sendThePlayerToPrevious(init_msg);
//send the player to other avaliable player
sendPreviousToThePlayer(_player);
//add the player to the Map
_player->initInfo(init_msg);
_player->onAvaliable();
}
//reback the erro info
else
{
login_reback.setFail();
_player->sendMessage((char*)&login_reback_head, sizeof(s_msg_head));
_player->sendMessage((char*)&login_reback, sizeof(s_login_reback_msg));
}
}
bool CLoginMsgPacket::isUserAvaliable()
{
m_tmpUser.m_username = m_login_msg.m_user_name;
m_tmpUser.m_password = m_login_msg.m_password;
return UserInfoDB->isUserAvaliable(m_tmpUser);
}
s_init_msg CLoginMsgPacket::getInitMsg()
{
s_init_msg tmpInitMsg;
tmpInitMsg.m_rotate_degree = m_tmpUser.m_rotate;
tmpInitMsg.m_scale = m_tmpUser.m_scale;
tmpInitMsg.m_position = m_tmpUser.m_vPos;
tmpInitMsg.m_resourceID = m_tmpUser.m_resourceID;
return tmpInitMsg;
}
void CLoginMsgPacket::sendPreviousToThePlayer(CPlayer* _player)
{
s_msg_head init_msg_head;
init_msg_head.setMsgType( INIT_INFO );
init_msg_head.setMsgLen ( sizeof(s_msg_head) + sizeof( s_init_msg ));
s_init_msg _init_msg;
CPlayer *tmpPlayer = NULL;
for( SockPlayerMapIter map_iter = PLAYERMANGER->getMapBegin();
map_iter != PLAYERMANGER->getMapEnd(); ++ map_iter)
{
tmpPlayer = map_iter->second;
if(tmpPlayer->isAvaliable() )
{
_init_msg = tmpPlayer->getInit_msg();
_player->sendMessage((char*)&init_msg_head, sizeof(init_msg_head));
_player->sendMessage((char*)&_init_msg, sizeof(s_init_msg));
}
}
}
void CLoginMsgPacket::sendThePlayerToPrevious(s_init_msg &_init_msg)
{
s_msg_head init_msg_head;
init_msg_head.setMsgType( INIT_INFO );
init_msg_head.setMsgLen ( sizeof(s_msg_head) + sizeof( s_init_msg ));
CPlayer *tmpPlayer = NULL;
for( SockPlayerMapIter map_iter = PLAYERMANGER->getMapBegin();
map_iter != PLAYERMANGER->getMapEnd(); ++ map_iter)
{
tmpPlayer = map_iter->second;
if(tmpPlayer->isAvaliable() )
{
tmpPlayer->sendMessage((char*)&init_msg_head, sizeof(init_msg_head));
tmpPlayer->sendMessage((char*)&_init_msg, sizeof(s_init_msg));
}
}
}
评论30
最新资源