Beispiel #1
0
int SOCK_SendPacket(SOCKET socket, Packet& packet, unsigned long protover, bool extended)
{
	if(!extended)
	{
		if(!packet.GetLength()) return 0;
		uint8_t* data = new uint8_t[packet.GetLength()];
		uint32_t size = packet.GetLength();
		std::vector<uint8_t> tbuf = packet.GetBuffer();
		for(uint32_t i = 0; i < size; i++)
			data[i] = tbuf[i];

		int ret_code = send_msg(socket, protover, data, size, 1);
		delete[] data;
		if(ret_code == -2) return SERR_TIMEOUT;
		if(ret_code == -1) return SERR_CONNECTION_LOST;
		return 0;
	}
	else
	{
		uint8_t* data = new uint8_t[packet.GetLength()+8];
		*(uint32_t*)(data) = packet.GetLength();
		*(uint32_t*)(data+4) = 0x80100000;
		std::vector<uint8_t> tbuf = packet.GetBuffer();
		for(uint32_t i = 0; i < packet.GetLength(); i++)
			data[i+8] = tbuf[i];

		int ret_code = send(socket, (const char*)data, packet.GetLength()+8, 0);
		delete[] data;
		if(ret_code == -1) return SERR_CONNECTION_LOST;
		return 0;
	}
}
void MapSvc::OnProcessPacket(Session& session,Packet& packet)
{

DEBUG_PRINTF1( "C_S req pkg-------MsgType[%d] \n", packet.MsgType );
	DEBUG_SHOWHEX( packet.GetBuffer()->GetReadPtr()-PACKET_HEADER_LENGTH, packet.GetBuffer()->GetDataSize()+PACKET_HEADER_LENGTH, 0, __FILE__, __LINE__ );

	switch(packet.MsgType)
	{
		case 201:
			ProcessEnterMap(session,packet);
			break;

		case 202:
			ProcessMapMove(session,packet);
			break;

 		case 203:
			ProcessTeleportByRune(session,packet);
			break;

		default:
			ClientErrorAck(session,packet,ERR_SYSTEM_PARAM);
			LOG(LOG_ERROR,__FILE__,__LINE__,"MsgType[%d] not found",packet.MsgType);
			break;
		}
}
//S_S 请求及应答
int SSClient::SSRequest( Packet &packet)
{
	int iRet = 0;

	//连接
	iRet = Connect();
	if(iRet<0)
		goto EndOfProcess;


DEBUG_PRINTF( "SSRequest: req pkg------- \n" );
	DEBUG_SHOWHEX( packet.GetBuffer()->GetReadPtr(), packet.GetBuffer()->GetDataSize(), 0, __FILE__, __LINE__ );

	//发送消息
	iRet = SendMsg(packet);
	if(iRet<0)
		goto EndOfProcess;

	//缓存清空
	packet.GetBuffer()->Reset();

	//接收应答
	iRet = RecvMsg(packet);
	if(iRet<0)
		goto EndOfProcess;

DEBUG_PRINTF( "SSRequest: ack pkg------- \n" );
	DEBUG_SHOWHEX( packet.GetBuffer()->GetReadPtr()-PACKET_HEADER_LENGTH, packet.GetBuffer()->GetDataSize()+PACKET_HEADER_LENGTH , 0, __FILE__, __LINE__ );
		
EndOfProcess:

	Close();
	return iRet;
}
//@brief	[MsgType:0202]移动
void MapSvc::ProcessMapMove(Session& session,Packet& packet)
{
	UInt32	RetCode = 0;
	DataBuffer	serbuffer(1024);
	int iRet = 0;

	UInt32 RoleID = packet.RoleID;
 	ArchvRoute art;

	//序列化类
	Serializer s(packet.GetBuffer());
	s>>art;
	if( s.GetErrorCode()!= 0 )
	{
		LOG(LOG_ERROR,__FILE__,__LINE__,"serial error" );
		return;
	}

DEBUG_PRINTF1( " ProcessMapMove  roleID[%d]\n", RoleID );
	//业务处理
	iRet = _mainSvc->GetCoreData()->ProcessRoleMove( RoleID, art);
	if(iRet)
	{
		LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessRoleMove error" );
		return;
	}

	return;
}
int SSClient::RecvMsg( Packet & packet)
{
	DataBuffer * buff = packet.GetBuffer();

	//消息头接收
	int iRecv = SockRecv( buff->GetWritePtr(), PACKET_HEADER_LENGTH);
	if( iRecv < 0 || iRecv != PACKET_HEADER_LENGTH )
		return -1;

	//移动 写指针
	buff->MoveWritePtr(iRecv);

	//解包头
	packet.UnpackHeader();

	//包体长度
	int iLeft = packet.Length- PACKET_HEADER_LENGTH;
	if( 0 == iLeft )
		return 0;

	//消息体接收
	iRecv = SockRecv( buff->GetWritePtr(), iLeft );
	if( iRecv != iLeft )
		return -1;

	//移动 写指针 
	buff->MoveWritePtr(iRecv);

	return 0;
}
void PKSvc::OnProcessPacket(Session& session,Packet& packet)
{
	
DEBUG_PRINTF1( "C_S req pkg-------MsgType[%d] \n", packet.MsgType );
	DEBUG_SHOWHEX( packet.GetBuffer()->GetReadPtr()-PACKET_HEADER_LENGTH, packet.GetBuffer()->GetDataSize()+PACKET_HEADER_LENGTH, 0, __FILE__, __LINE__ );

	switch(packet.MsgType)
	{
		case 401:	//请求PK
			ProcessPKReq(session,packet);
			break;

		default:
			ClientErrorAck(session,packet,ERR_SYSTEM_PARAM);
			LOG(LOG_ERROR,__FILE__,__LINE__,"MsgType[%d] not found",packet.MsgType);
			break;
		}
}
int SSClient::SendMsg( Packet & packet )
{

	DataBuffer * buff = packet.GetBuffer();
	int iRet = SockSend( buff->GetReadPtr(), buff->GetDataSize());
	if(iRet < 0)
		return -1;
		
	return 0;
}
//数据包信息
void OffLineUpdateSvc::OnProcessPacket(Session& session,Packet& packet)
{
	DEBUG_PRINTF1( "C_S req pkg-------MsgType[%d] \n", packet.MsgType );
	DEBUG_SHOWHEX( packet.GetBuffer()->GetReadPtr()-PACKET_HEADER_LENGTH, packet.GetBuffer()->GetDataSize()+PACKET_HEADER_LENGTH, 0, __FILE__, __LINE__ );

	switch(packet.MsgType)
	{
		case 1601: //[MsgType:1601]升级数据查询
			ProcessUpdateDataQuery(session, packet);
		break;

    case 1602: //[MsgType:1602]开始修炼升级
			ProcessBeginUpdate(session,  packet);
			break;

		case 1603: //[MsgType:1603]金币加速挂机
	    ProcessSpeedupUpdate(session, packet);
		  break;

		case 1604: //[MsgType:1604]停止修炼升级
	    ProcessStopUpdate(session, packet);
		  break;

		case 1605:  //[MsgType:1605] 剩余挂机的时间
	    ProcessLeftUpdateHour(session, packet);
	    break;

		case 1699: //[MsgType:1699]前台触发扣费
      ProcessLostBilling(session, packet);
      break;

		default:
		ClientErrorAck(session,packet,ERR_SYSTEM_PARAM);
		LOG(LOG_ERROR,__FILE__,__LINE__,"MsgType[%d] not found",packet.MsgType);
		break;
	}
}
    void LoginClientConn::UserLogout(const string &user_id) {
        proto::IMServerUserLogoutRequest logout_req;
        logout_req.set_user_id(user_id);

        Packet packet;
        PacketSetPBMsg(&packet, &logout_req);
        packet.SetVersion(IM_PROTO_VERSION);
        packet.SetServiceId(proto::SID_SERVER);
        packet.SetCommandId(proto::CID_SERVER_REQ_USERLOGOUT);
        // TODO seq num
        packet.SetSeqNum(0);

        // TODO can add to wait for logout ack list
        Send(packet.GetBuffer(), packet.GetLength());
    }
void ShopSvc::ProcessselectShopItem(Session& session,Packet& packet)
{
	//查询背包物品函数
	UInt32	RetCode = 0;
	DataBuffer	serbuffer(1024);
	char szSql[1024];
	Connection con,conSub;
	DBOperate dbo,dboSub;

	Int16 iRet = 0;
	UInt32 roleID = packet.RoleID;
	List<ArchvShopItem> shopItem;
	ArchvShopItem item1;

	//序列化类
	Serializer s(packet.GetBuffer());
	if( s.GetErrorCode()!= 0 )
	{
		RetCode = ERR_SYSTEM_SERERROR;
		LOG(LOG_ERROR,__FILE__,__LINE__,"serial error" );
		goto EndOf_Process;
	}

	//获取DB连接
	con = _cp->GetConnection();
	dbo.SetHandle(con.GetHandle());

	conSub=_cp->GetConnection();
	dboSub.SetHandle(conSub.GetHandle());

	DEBUG_PRINTF( "C_S ProcessShopItem sucess!!!!!!!!\n" );

	//查询全部的
	sprintf( szSql, "select ItemID,Category,NowPrice from ShopItem;");
	iRet=dbo.QuerySQL(szSql);
	if(iRet==0)
	{
		while(dbo.HasRowData())
		{
			item1.ItemID=dbo.GetIntField(0);
			item1.Category=dbo.GetIntField(1);
			item1.NowPrice=dbo.GetIntField(2);
			if(item1.Category==7)
			{
				sprintf( szSql, "select leftNum from specialItem where ItemID=%d;",item1.ItemID);
				iRet=dboSub.QuerySQL(szSql);
				if(iRet==0)
				{
					item1.Num=dboSub.GetIntField(0);
				}
			}

			shopItem.push_back(item1);
			dbo.NextRow();
		}
	}
	else
	{

		RetCode = ERR_SYSTEM_DBNORECORD;
		LOG(LOG_ERROR,__FILE__,__LINE__,"QuerySQL error[%s],szSql[%s] ", mysql_error(con.GetHandle()), szSql);
		goto EndOf_Process;
	}

	//LOG(LOG_ERROR,__FILE__,__LINE__,"QuerySQL data what to get not found,szSql[%d %d] " , PackNum,GetRoleCellNum(roleID));
	EndOf_Process:

	//组应答数据
	Packet p(&serbuffer);
	s.SetDataBuffer(&serbuffer);
	serbuffer.Reset();

	p.CopyHeader(packet);
	p.Direction = DIRECT_C_S_RESP;
	p.PackHeader();


	s<<RetCode;

	if( 0 == RetCode )
	{//RetCode 为0 才会返回包体剩下内容
		s<<shopItem;

	}

	p.UpdatePacketLength();

	//发送应答数据
	if( session.Send(&serbuffer) )
	{
		LOG(LOG_ERROR,__FILE__,__LINE__,"session.Send error ");
	}

	DEBUG_PRINTF( "ack pkg=======, \n" );
	DEBUG_SHOWHEX( serbuffer.GetReadPtr(), serbuffer.GetDataSize(), 0, __FILE__, __LINE__ );

	return;

}
void ShopSvc::ProcessBuyItem(Session& session,Packet& packet)
{


	UInt32	RetCode = 0;
	DataBuffer	serbuffer(1024);
	char szSql[1024];
	Connection con;
	DBOperate dbo;
	Int16 iRet = 0;
	UInt16 num=0;
	UInt16 cellType=0;
	UInt16 itemType=0;
	UInt16 IsStack=0;
	UInt16 Bind=0;
	UInt32 Dur=0;
	UInt32 price=0;
	UInt32 EntityID=0;
	UInt32 CellIndex=0;
	UInt32 leftnum=0;

	List<UInt16> cell;
	List<ItemCell> lic;
	ItemCell lic1;
	List<UInt16>::iterator itor;

	UInt32 roleID = packet.RoleID;
	UInt32 itemID=0;
	UInt16 count=0;
	UInt32 numcell=1;
	UInt16 Rarity=0;
	UInt32 BuyType=0;
	Byte type=0;
	Byte flag=0;

	//序列化类
	Serializer s(packet.GetBuffer());
	s>>type>>itemID>>num;
	LOG(LOG_ERROR,__FILE__,__LINE__,"type[%d]--itemid[%d]--num[%d]",type,itemID,num);
	//得到物品ID和数量
	if( s.GetErrorCode()!= 0 )
	{
		RetCode = ERR_SYSTEM_SERERROR;
		LOG(LOG_ERROR,__FILE__,__LINE__,"serial error" );
		goto EndOf_Process;
	}

	//获取DB连接
	con = _cp->GetConnection();
	dbo.SetHandle(con.GetHandle());
	//验证价格

	sprintf( szSql, "select NowPrice from ShopItem where ItemID=%d and Category=%d;",itemID,type );
	iRet = dbo.QuerySQL(szSql);
	if(iRet==0)
	{
		price=dbo.GetIntField(0);
	}
	else
	{
		RetCode = ERR_SYSTEM_DBNORECORD;
		LOG(LOG_ERROR,__FILE__,__LINE__,"QuerySQL data not found or erro,szSql[%s] " , szSql);
		goto EndOf_Process;
	}
	if(type==6)
	{
		//表示礼券购买
		iRet=DropGift(roleID,price*num);
		if(iRet==1)
		{
			//没有这么多数量的礼券
			RetCode = NO_MUCH_GIFT;
			LOG(LOG_ERROR,__FILE__,__LINE__,"Not so many Gift!! ");
			goto EndOf_Process;
		}
		else if(iRet==-1)
		{
			//错误
			RetCode = ERR_APP_DATA;
			LOG(LOG_ERROR,__FILE__,__LINE__,"there are some erro!! ");
			goto EndOf_Process;
		}

	}
	else if(type==7)
	{
		//表示限量抢购
		sprintf( szSql, "select ItemID,leftNum from specialItem where ItemID=%d;",itemID);

		iRet = dbo.QuerySQL(szSql);
		if(iRet!=0)
		{
			RetCode = ERR_SYSTEM_DBNORECORD;
			LOG(LOG_ERROR,__FILE__,__LINE__,"QuerySQL data not found ,szSql[%s] " , szSql);
			goto EndOf_Process;
		}

		else
		{
			leftnum=dbo.GetIntField(1);
		}

		if(leftnum<num)
		{
			//数量不足
			RetCode = ERR_SYSTEM_DBNORECORD;
			LOG(LOG_ERROR,__FILE__,__LINE__,"Not so many count!!!! ");
			goto EndOf_Process;
		}

	}
if(type!=6)
{
		// type 为5表示vip折扣
		if (type == 5)
		{
			RolePtr pRole = _mainSvc->GetCoreData()->ProcessGetRolePtr(roleID);
			sprintf( szSql, "select Discount from VipDiscountDesc where VipLevel = %d;",pRole->VIP());

			iRet = dbo.QuerySQL(szSql);
			if( iRet != 0 )
			{
				RetCode = ERR_SYSTEM_DBERROR;
				LOG(LOG_ERROR,__FILE__,__LINE__,",szSql[%s] ", szSql);
				goto EndOf_Process;
				}
			int discount = 0;
			while (dbo.HasRowData())
			{
				discount = dbo.GetIntField(0);
				dbo.NextRow();
			}
			if (discount != 0)
			{
				price = ((price * discount /100) > 0) ? (price * discount /100) :1;
			}
		}
		iRet=_mainSvc->GetBagSvc()->DropGold(roleID,price*num);
		if(iRet==-1)
		{
			//不足
			RetCode = NO_MUCH_GOLD;
			LOG(LOG_ERROR,__FILE__,__LINE__,"Not so many Gold!! ");
			goto EndOf_Process;
		}
		else if(iRet==0)
		{
			//错误
			RetCode = ERR_SYSTEM_DBNORECORD;
			LOG(LOG_ERROR,__FILE__,__LINE__,"there are some erro!! ");
			goto EndOf_Process;
		}
}
	//组合SQL语句,查询出完整的物品信息,查询出来的是物品类型和物品单元格类型
	sprintf( szSql, "select Rarity,IsStack,Bind,CostMoneyType,CostBuy, Durability,ItemType,CellType from Item where ItemID = %d;",itemID );

	iRet = dbo.QuerySQL(szSql);
	if( 1 == iRet)
	{
		RetCode = ERR_SYSTEM_DBNORECORD;
		LOG(LOG_ERROR,__FILE__,__LINE__,"QuerySQL data not found ,szSql[%s] " , szSql);
		goto EndOf_Process;
	}
	if( iRet < 0 )
	{
		RetCode = ERR_SYSTEM_DBERROR;
		LOG(LOG_ERROR,__FILE__,__LINE__,",szSql[%s] ", szSql);
		goto EndOf_Process;
 	}
 	//分离出数据做成变量, IsStack 是否堆叠   绑定属性Bind   Durability耐久度
 	if(iRet==0)
	{
		if(dbo.RowNum()==1)
		while(dbo.HasRowData())
		{
			Rarity=dbo.GetIntField(0);

			IsStack=dbo.GetIntField(1);
			Bind=dbo.GetIntField(2);
			BuyType=dbo.GetIntField(3);
			//price=dbo.GetIntField(4);
			Dur=dbo.GetIntField(5);
			itemType= dbo.GetIntField(6);
			cellType=dbo.GetIntField(7);

			//记录集下一条记录
			dbo.NextRow();
		}
	}
	//可知道物品中一些是不具备实体ID的,下面是进行对实体ID表进行插入数据的代码
	//判断是否具有实体ID函数,是返回1,不是返回0,int isEntity(int itemtype);该函数还没有开始编写代码
	if(itemType==2)//具备实体ID的话进行实体表插入, 更改的话对这里的值做个修改
	{
		flag=0;
		if(IsStack==1)
		{
			RetCode = ERR_SYSTEM_DBNORECORD;
			LOG(LOG_ERROR,__FILE__,__LINE__,"ExceSQL data error ,szSql[%s] " , szSql);
			goto EndOf_Process;
		}

		if(_mainSvc->GetBagSvc()->Ifhassomany(roleID,cell,num)!=1)
		{
			//背包不足
			RetCode = ERR_SYSTEM_DBNORECORD;
			LOG(LOG_ERROR,__FILE__,__LINE__," Bag has not so many room !" );
			goto EndOf_Process;
		}
		lic1.bindStatus=Bind;
 		lic1.durability=Dur;
 		lic1.EntityID=EntityID;
 		lic1.ItemID=itemID;


	}
	else
	{
		if(IsStack==0)
		{//不可以堆叠
		flag=1;
				if(_mainSvc->GetBagSvc()->Ifhassomany(roleID,cell,num)!=1)
				{
				//背包没有那么多空间
					RetCode = ERR_SYSTEM_DBERROR;
					LOG(LOG_ERROR,__FILE__,__LINE__,"Bag has no Room to insert,szSql[%s] ", mysql_error(con.GetHandle()), szSql);
					goto EndOf_Process;
				}
		}
		else
		{
				flag=2;
				numcell=num/TOPSTACK_NUM;//一共这么多个单元
				if(num%TOPSTACK_NUM!=0)
				{
					numcell=numcell+1;
				}

				if(_mainSvc->GetBagSvc()->Ifhassomany(roleID,cell,numcell)==0)
				{
					//背包没有那么多空间
					RetCode = ERR_SYSTEM_DBERROR;
					LOG(LOG_ERROR,__FILE__,__LINE__,"Bag has no Room to insert,szSql[%s] ", mysql_error(con.GetHandle()), szSql);
					goto EndOf_Process;
				}

	 	}
		sprintf( szSql, "select CdTime from Item where ItemID= %d;", itemID);
		iRet = dbo.QuerySQL(szSql);
		if(iRet==0)
		{
			lic1.cdTime=dbo.GetIntField(0);
		}
 		lic1.bindStatus=Bind;
		lic1.durability=Dur;
		lic1.EntityID=EntityID;
		lic1.ItemID=itemID;
 	}
	//向数据库里面进行插入数据!
	for( itor = cell.begin(); itor != cell.end(); itor++ )
	{
	if(flag==0)
	{
		sprintf( szSql,"insert into Entity(ItemID,Durability,BindStatus) values(%d,%d,%d);",itemID,Dur,Bind);

				iRet = dbo.ExceSQL(szSql);
				if( iRet < 0 )
				{
					RetCode = ERR_SYSTEM_DBERROR;
					LOG(LOG_ERROR,__FILE__,__LINE__,"ExceSQL error[%s],szSql[%s] ", mysql_error(con.GetHandle()), szSql);
					goto EndOf_Process;
		 		}

				EntityID=dbo.LastInsertID();

				sprintf(szSql, "insert into Bag(RoleID,CellType,CellIndex,ItemType,ItemID,EntityID,Num)values(%d,%d,%d,%d,%d,%d,1);",roleID,cellType,*itor,itemType,itemID,EntityID);
				iRet=dbo.ExceSQL(szSql);

				if( 1 == iRet )
				{
					RetCode = ERR_SYSTEM_DBNORECORD;
					LOG(LOG_ERROR,__FILE__,__LINE__,"ExceSQL data not found ,szSql[%s] " , szSql);
					goto EndOf_Process;
				}
				if( iRet < 0 )
				{
					RetCode = ERR_SYSTEM_DBERROR;
					LOG(LOG_ERROR,__FILE__,__LINE__,"ExceSQL error[%s],szSql[%s] ", mysql_error(con.GetHandle()), szSql);
					goto EndOf_Process;
				}

				lic1.num=1;
				lic1.celIndex=*itor;
				lic1.EntityID=EntityID;
				lic.push_back(lic1);
	}
	if(flag==2)
	{
				if(numcell==1)
				{
					//数量就是为传输的数量
					if(num%TOPSTACK_NUM==0)
					{
						count=TOPSTACK_NUM;
					}
					else
					{
						count=num%TOPSTACK_NUM;
					}
				}
				else
				{
					count=TOPSTACK_NUM;
				}

				sprintf(szSql, "insert into Bag(RoleID,CellType,CellIndex,ItemType,ItemID,EntityID,Num)values(%d,%d,%d,%d,%d,%d,%d);",roleID,cellType,*itor,itemType,itemID,EntityID,count);
				iRet=dbo.ExceSQL(szSql);

				if( 1 == iRet )
				{
					RetCode = ERR_SYSTEM_DBNORECORD;
					LOG(LOG_ERROR,__FILE__,__LINE__,"ExceSQL data not found ,szSql[%s] " , szSql);
					goto EndOf_Process;
				}
				if( iRet < 0 )
				{
					RetCode = ERR_SYSTEM_DBERROR;
					LOG(LOG_ERROR,__FILE__,__LINE__,"ExceSQL error[%s],szSql[%s] ", mysql_error(con.GetHandle()), szSql);
					goto EndOf_Process;
				}

				lic1.num=count;
				lic1.celIndex=*itor;
				lic.push_back(lic1);
		}
		else if(flag==1)
		{
				sprintf(szSql, "insert into Bag(RoleID,CellType,CellIndex,ItemType,ItemID,EntityID,Num)values(%d,%d,%d,%d,%d,%d,1);",roleID,cellType,*itor,itemType,itemID,EntityID);
				iRet=dbo.ExceSQL(szSql);

				if( 1 == iRet )
				{
					RetCode = ERR_SYSTEM_DBNORECORD;
					LOG(LOG_ERROR,__FILE__,__LINE__,"ExceSQL data not found ,szSql[%s] " , szSql);
					goto EndOf_Process;
				}
				if( iRet < 0 )
				{
					RetCode = ERR_SYSTEM_DBERROR;
					LOG(LOG_ERROR,__FILE__,__LINE__,"ExceSQL error[%s],szSql[%s] ", mysql_error(con.GetHandle()), szSql);
					goto EndOf_Process;
				}

				lic1.num=1;
				lic1.celIndex=*itor;
				lic.push_back(lic1);
		}
		numcell--;
	}
	_mainSvc->GetTaskSvc()->OnBagItemAddOrDelete(roleID,itemID);


	EndOf_Process:

	//组应答数据
	Packet p(&serbuffer);
	s.SetDataBuffer(&serbuffer);
	serbuffer.Reset();

	p.CopyHeader(packet);
	p.Direction = DIRECT_C_S_RESP;
	p.PackHeader();


	s<<RetCode;
	if( 0 == RetCode )
	{//RetCode 为0 才会返回包体剩下内容
		//s<<lic;
	}

	p.UpdatePacketLength();

	//发送应答数据
	if( session.Send(&serbuffer) )
	{
		LOG(LOG_ERROR,__FILE__,__LINE__,"session.Send error ");
	}

	DEBUG_PRINTF( "ack pkg=======, \n" );
	DEBUG_SHOWHEX( serbuffer.GetReadPtr(), serbuffer.GetDataSize(), 0, __FILE__, __LINE__ );
	if( 0 == RetCode )
	{

		_mainSvc->GetBagSvc()->NotifyBag(roleID,lic);
		if(type!=6)
		{
			_mainSvc->GetBagSvc()->NotifyMoney(roleID);
		}

		if(type==7)
		{
			UpdateDBspecialItem(itemID,num);
		}
			UpdateDBItemSellCount(itemID,type,num);
 	}
	return;


}
void PKSvc::ProcessPKReq(Session& session,Packet& packet)
{
	UInt32	RetCode = 0;
	DataBuffer	serbuffer(1024);
	char szSql[1024];
	Connection con;
	DBOperate dbo;
	int iRet = 0;
	PKBriefList pkList;
	
	UInt32 roleID = packet.RoleID;
	Byte creatureFlag;
	UInt32 creatureID;

	ArchvRolePKInfo pkInfo;
	List<ArchvRolePKInfo> lrpki;

	UInt32 pkID = 0;
	ArchvCreatureStatus cs;
	List<ArchvCreatureStatus> lcs;
	list<TeamRole> roleIDitor; 
	list<TeamRole>::iterator itor;
	list<UInt32> roleID1,roleID2;
	list<UInt32>::iterator it;
	Team team;
	ArchvPosition posMid;
	ArchvPosition posPKOrigin;
	int iTmp = 0;
	list<SenceMonster> monsterrs; 
	list<SenceMonster>::iterator itor1;
	Byte Status=0;
	RolePtr srcRole = _mainSvc->GetCoreData()->ProcessGetRolePtr(roleID);
	RolePtr desRole;
	Monster desMonster;
	ArchvPosition pos1;
	ArchvPosition pos2;
	int pp=-1;
	int j=1;
 
	//序列化类
	Serializer s(packet.GetBuffer());
	s>>creatureFlag>>creatureID;
	if( s.GetErrorCode()!= 0 )
	{
		RetCode = ERR_SYSTEM_SERERROR;
		LOG(LOG_ERROR,__FILE__,__LINE__,"serial error" );
		goto EndOf_Process;
	}

	//获取DB连接
/*	con = _cp->GetConnection();
	dbo.SetHandle(con.GetHandle());
	*/

LOG(LOG_ERROR,__FILE__,__LINE__,"creatureFlag[%d],creatureID[%d]  ", creatureFlag, creatureID );
	
	//生物标志校验
	if( creatureFlag <=0 || creatureFlag > 4 )
	{
		RetCode = ERR_APP_DATA;
		LOG(LOG_ERROR,__FILE__,__LINE__,"creatureFlag error!! creatureFlag[%d] ", creatureFlag );
		goto EndOf_Process;
	}
	//会发生角色无法PK得状态,可以使用定时放开PK限制
	pkID=_mainSvc->GetCoreData()->IfRoleInPK(roleID);


	if(pkID!=0)
	{
		LOG(LOG_DEBUG,__FILE__,__LINE__,"111111111111 pkID=%d  roleID=%d", pkID, roleID  );
		
		RetCode = 2011;//角色的上场战斗没有结束
		LOG(LOG_ERROR,__FILE__,__LINE__,"role status error!! role is in the pk  RoleId=%d", roleID  );
		goto EndOf_Process;
	}



	if( 0 == srcRole->ID() )
	{
		RetCode = ERR_APP_ROLENOTEXISTS;
		LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessGetRole error,roleID[%d]", roleID  );
		goto EndOf_Process;
	}

	
		if(srcRole->TeamFlag()==0)
		{
			//单独作战,跳过
		}
		else
		{

				if(srcRole->TeamFlag()!=2)
				{
					//失败,不是队长
					RetCode = ERR_APP_ROLENOTEXISTS;
					LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessGetRole error,roleID[%d]", roleID  );
					goto EndOf_Process;
				}
				team=_mainSvc->GetCoreData()->GetTeams(srcRole->TeamID());
				roleIDitor=team.GetMemberRoleID();
				if(roleIDitor.size()>1)
				{
					//队伍的人数
						for(itor=roleIDitor.begin();itor!=roleIDitor.end();itor++)
						{
							//队伍成员
								if(itor->status==1)
								{
									//队员处于归队状态				
										roleID1.push_back(itor->roleId);
								
								}
								
						}//end for

							pkList.player1=roleID1;//队员,不包括自己
				
			 }//end if
		}//end if else

	pos1 = srcRole->Pos();

	//获取被攻击者信息
	if( 1 == creatureFlag )
	{//被攻击者为角色
	
		//获取被攻击角色的信息
		desRole = _mainSvc->GetCoreData()->ProcessGetRolePtr(creatureID);
		if( 0 == desRole->ID() )
		{
			RetCode = ERR_APP_ROLENOTEXISTS;
			LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessGetRole error,creatureID[%d]", creatureID  );
			goto EndOf_Process;
		}

		//判断是否同一地图
		if( srcRole->MapID() != desRole->MapID() )
		{
			RetCode = ERR_APP_DATA;
			LOG(LOG_ERROR,__FILE__,__LINE__,"error MapID,srcRole->MapID()[%d],desRole->MapID()[%d] ", srcRole->MapID(), desRole->MapID()  );
			goto EndOf_Process;
		}
		if(desRole->TeamFlag()==0||desRole->TeamFlag()==1)
		{
			//单独作战,跳过
		}
		else
		{
				roleIDitor.clear();
				team=_mainSvc->GetCoreData()->GetTeams(desRole->TeamID());
				roleIDitor=team.GetMemberRoleID();
				if(roleIDitor.size()>1)
				{
					//队伍的人数
					for(itor=roleIDitor.begin();itor!=roleIDitor.end();itor++)
					{
						//队伍成员
							if(itor->status==1||itor->status==2)
							{
								//队员处于归队状态
								if(itor->roleId!=creatureID)
								{
									roleID2.push_back(itor->roleId);
									
								}
								
							}
							
					}
						pkList.player2=roleID2;//队员,不包括自己
				
			 }
		}
		pos2 = desRole->Pos();

	}
	else if( 2 == creatureFlag )
	{//被攻击者为怪

		//获取怪信息
		pos2=_mainSvc->GetCoreData()->GetSenceMonster(srcRole->MapID(),creatureID,monsterrs);
		if(monsterrs.size()==0)
		{
			RetCode = ERR_APP_ROLENOTEXISTS;
			LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessGetMonster error,creatureID[%d]", creatureID  );
			goto EndOf_Process;
		}
		desMonster = _mainSvc->GetCoreData()->ProcessGetMonster( srcRole->MapID(), creatureID );
		if( 0 == desMonster.ID() )
		{
			RetCode = ERR_APP_ROLENOTEXISTS;
			LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessGetMonster error,creatureID[%d]", creatureID  );
			goto EndOf_Process;
		}
		desMonster.ID(creatureID);
		
	}

	//获取pk 双方间直线中点坐标
  posMid = GetMidpoint( pos1, pos2 );


	//计算相对坐标原点位置
	//posPKOrigin = CalcPKOrigin( posMid );
	
LOG(LOG_DEBUG,__FILE__,__LINE__,"----------->>>>posMid x[%d],y[%d], srcRole->currPos x[%d],y[%d], desRole->currPos x[%d],y[%d], posPKOrigin x[%d],y[%d]  ",
	posMid.X, posMid.Y, pos1.X, pos1.Y, pos2.X, pos2.Y, posPKOrigin.X, posPKOrigin.Y );

	//组建 S_S pk 请求包: pk发起者
	if(pos1.X-posMid.X>0)//在右边,暂时没有获取对方的位置
	{
		pos1.X=PKSCREEN_XLENGTH/2-10;
		pos1.Y=PKSCREEN_YLENGTH/2;
	}
	else
	{
		pos1.X=PKSCREEN_XLENGTH/2+10;
		pos1.Y=PKSCREEN_YLENGTH/2;
	}
	if(roleID1.size()%2==0)
	{
		pos1.Y=pos1.Y-1;
	}
		iRet = MakeSSPkgForRole( pos1, srcRole, lrpki,1);
	
	if(iRet)
	{
		RetCode = ERR_APP_OP;
		LOG(LOG_ERROR,__FILE__,__LINE__,"MakeSSPkgForRole error!! srcRole->ID()[%d] ", srcRole->ID() );
		goto EndOf_Process;
	}
	//队伍队员

	for(it=roleID1.begin();it!=roleID1.end();it++)
	{
		pp=-1*pp;
		srcRole = _mainSvc->GetCoreData()->ProcessGetRolePtr(*it);
		if( 0 == srcRole->ID() )
		{
			RetCode = ERR_APP_ROLENOTEXISTS;
			LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessGetRole error,creatureID[%d]", creatureID  );
			goto EndOf_Process;
		}
		pos1.Y=pos1.Y+2*j*pp;
		j++;
	//	posPKOrigin.X=
		iRet = MakeSSPkgForRole( pos1, srcRole, lrpki,1);
		if(iRet)
		{
			RetCode = ERR_APP_OP;
			LOG(LOG_ERROR,__FILE__,__LINE__,"MakeSSPkgForRole error!! srcRole->ID()[%d] ", srcRole->ID() );
			goto EndOf_Process;
		}
	}

	//组建 S_S pk 请求包: 被攻击者
	if( 1 == creatureFlag )
	{
			if(pos2.X-posMid.X>0)//在右边,暂时没有获取对方的位置
			{
				pos2.X=PKSCREEN_XLENGTH/2-10;
				pos2.Y=PKSCREEN_YLENGTH/2;
			}
			else
			{
				pos2.X=PKSCREEN_XLENGTH/2+10;
				pos2.Y=PKSCREEN_YLENGTH/2;
			}
		iRet = MakeSSPkgForRole(  pos2, desRole, lrpki,2); //被攻击者为角色
			 pp=-1;
			 j=1;
		for(it=roleID2.begin();it!=roleID2.end();it++)
		{
				pp=-1*pp;
				desRole = _mainSvc->GetCoreData()->ProcessGetRolePtr(*it);
				if( 0 == desRole->ID() )
				{
					RetCode = ERR_APP_ROLENOTEXISTS;
					LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessGetRole error,creatureID[%d]", creatureID  );
					goto EndOf_Process;
				}
			//	posPKOrigin.X=
				pos2.Y=pos2.Y+2*j*pp;
				j++;
		
				iRet = MakeSSPkgForRole( pos1, desRole, lrpki,2);
				if(iRet)
				{
					RetCode = ERR_APP_OP;
					LOG(LOG_ERROR,__FILE__,__LINE__,"MakeSSPkgForRole error!! srcRole->ID()[%d] ", srcRole->ID() );
					goto EndOf_Process;
				}
		}
		
	}
	else if( 2 == creatureFlag )
	{
		pp=1;
		if(pos2.X-posMid.X>0)//在右边,暂时没有获取对方的位置
			{
				pos2.X=PKSCREEN_XLENGTH/2-5;
				pos2.Y=PKSCREEN_YLENGTH/2;
				pp=-1;
			}
			else
			{
				pos2.X=PKSCREEN_XLENGTH/2+5;
				pos2.Y=PKSCREEN_YLENGTH/2;
				pp=-1;
			}
    iRet = MakeSSPkgForMonster(  pos2, desMonster, lrpki,1); //怪
		UInt32 i=1;
			 j=1;
		if(monsterrs.size()>1)
		{		
				itor1=monsterrs.begin();
				itor1++;//跳过第一条数据
				for(;itor1!=monsterrs.end();itor1++)
				{		
					pos2.X=pos2.X-j*pp*2;
					 desMonster=_mainSvc->GetCoreData()->ProcessGetMonster( desMonster.MapID(), itor1->MonsterType);
						desMonster.ID(creatureID+i);
						iRet = MakeSSPkgForMonster(  pos2, desMonster, lrpki,itor1->num); //怪
						i=itor1->num+i;
				}
	  }
	}
	if(iRet)
	{
		RetCode = ERR_APP_OP;
		LOG(LOG_ERROR,__FILE__,__LINE__,"MakeSSPkgForRole error!! desRole->ID()[%d] ", desRole->ID() );
		goto EndOf_Process;
	}


	
	//pk 拉人功能,根据组队、坐标范围来拉人进入pk

	pkID=roleID;

	pkList.player1.push_back(roleID);//队员,不包括自己
	pkList.player2.push_back(creatureID);//队员,不包括自己
	pkList.pkID=pkID;
	_mainSvc->GetCoreData()->AddPKBrif(pkID,pkList);

	//ss 消息处理
	
	Status=_mainSvc->GetCoreData()->ProcessGetRolePtr(roleID)->Status();
	if(Status!=1)
	{
		RetCode = ERR_APP_ROLENOTEXISTS;
		LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessGetRole error,roleID[%d]", roleID  );
		goto EndOf_Process;
	}
	else
	{
		srcRole->Status(2);
	}
	iRet = _mainSvc->GetSSClientManager()->ProcessPkReq( srcRole->MapID(), posMid.X, posMid.Y, lrpki, pkID);
	if(iRet)
	{
		RetCode = iRet;
		LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessPkReq error ,RetCode[%d]", RetCode );		
		goto EndOf_Process;
	}

	
EndOf_Process:

	//状态的重置
	if(RetCode)
	{
		srcRole->Status(1);		//重置为非pk状态
		_mainSvc->GetCoreData()->EraseRolePKID(roleID);				//删除 PKID

		LOG(LOG_DEBUG,__FILE__,__LINE__,"222222222 _mainSvc->GetCoreData()->IfRoleInPK(roleID) [%d]", _mainSvc->GetCoreData()->IfRoleInPK(roleID) );
	}

	//组应答数据
	Packet p(&serbuffer);
	s.SetDataBuffer(&serbuffer);
	serbuffer.Reset();
	
	p.CopyHeader(packet);
	p.Direction = DIRECT_C_S_RESP;
	p.PackHeader();

		
	s<<RetCode;
	if( 0 == RetCode )
	{//RetCode 为0 才会返回包体剩下内容
//		s<<11;
	}	

	p.UpdatePacketLength();
	
	//发送应答数据
	if( session.Send(&serbuffer) )
	{
		LOG(LOG_ERROR,__FILE__,__LINE__,"session.Send error ");
	}
	
DEBUG_PRINTF1( "C_S ack pkg ----- MsgType[%d]  \n", packet.MsgType );
	DEBUG_SHOWHEX( serbuffer.GetReadPtr(), serbuffer.GetDataSize(), 0, __FILE__, __LINE__ );


	//--------------------------------------------------------------------------
	//成功处理,则修改生物状态
	if( 0 == RetCode )
	{
		GetCtStatus( lrpki, lcs );
		_mainSvc->GetCoreData()->ChangeCreatureStatus( srcRole->MapID(), lcs);
	}
		
	
}
//@brief	[MsgType:0203] (使用飞行符文)瞬移
void MapSvc::ProcessTeleportByRune(Session& session,Packet& packet)
{
	UInt32	RetCode = 0;
	DataBuffer	serbuffer(1024);
	Connection con;
	DBOperate dbo;
	int iRet = 0;

	UInt32 roleID = packet.RoleID;
	UInt32 mapID = 0;
	UInt16 x;
	UInt16 y;
 	ArchvRoute art;
 	ArchvPosition pos;
 	int isToCurrMap = 0;		//是否本地图内瞬移	0否   1 是

	//序列化类
	Serializer s(packet.GetBuffer());
	s>>mapID>>x>>y;
	if( s.GetErrorCode()!= 0 )
	{
		LOG(LOG_ERROR,__FILE__,__LINE__,"serial error" );
		return;
	}

DEBUG_PRINTF1( " ProcessTeleportByRune  roleID[%d]\n", roleID );
	//获取角色信息
	RolePtr pRole = _mainSvc->GetCoreData()->ProcessGetRolePtr(roleID);
	if(pRole->ID()==0)
	{
		RetCode = ERR_APP_DATA;
		LOG(LOG_ERROR,__FILE__,__LINE__,"data error!! role not found,roleID[%d]", roleID);
		goto EndOf_Process;
	}

	//组队状态,不能瞬移
	if(pRole->TeamFlag())
	{
		RetCode = ERR_APP_DATA;
		LOG(LOG_ERROR,__FILE__,__LINE__,"status error!! role in team, roleID[%d]", roleID);
		goto EndOf_Process;
	}

	//是否在本地图瞬移
	if( pRole->MapID() == mapID )
	{//本地图内瞬移

		isToCurrMap = 1;

		//移动
		pos.X = x;
		pos.Y = y;
		art.listPos.push_back(pos);
		iRet = _mainSvc->GetCoreData()->ProcessRoleMove( roleID, art);
		if(iRet)
		{
			RetCode = ERR_APP_OP;
			LOG(LOG_ERROR,__FILE__,__LINE__,"ProcessRoleMove error" );
			goto EndOf_Process;
		}

	}
	else
	{//跨地图瞬移
		pRole->MapID(mapID);
		isToCurrMap = 0;

		//业务处理
		iRet = _mainSvc->GetCoreData()->ProcessRoleEnterMap( roleID, mapID, x, y);
		if( iRet == -1)
		{
			RetCode = ERR_APP_OP;
			LOG(LOG_ERROR,__FILE__,__LINE__,"GetCoreData()->ProcessRoleEnterMap error");
			goto EndOf_Process;
		}
		if(iRet == 1)
		{
			RetCode = ERR_APP_ALREADYINMAP;
			LOG(LOG_ERROR,__FILE__,__LINE__,"GetCoreData()->ProcessRoleEnterMap error");
			goto EndOf_Process;
		}
	}


EndOf_Process:
	//组应答数据
	Packet p(&serbuffer);
	s.SetDataBuffer(&serbuffer);
	serbuffer.Reset();

	p.CopyHeader(packet);
	p.Direction = DIRECT_C_S_RESP;
	p.PackHeader();

	s<<RetCode;
	if( 0 == RetCode )
	{//RetCode 为0 才会返回包体剩下内容
//		s<<Time;
	}

	p.UpdatePacketLength();

	//发送应答数据
	if( session.Send(&serbuffer) )
	{
		LOG(LOG_ERROR, __FILE__, __LINE__, "session->Send() error,errno[%d],strerror[%s]", errno, strerror(errno) );
	}

DEBUG_PRINTF1( "C_S ack pkg ----- MsgType[%d]  \n", packet.MsgType );
	DEBUG_SHOWHEX( serbuffer.GetReadPtr(), serbuffer.GetDataSize(), 0, __FILE__, __LINE__ );


	if(RetCode==0)
	{
		//跨地图瞬移
		if(0==isToCurrMap)
		{
			//[MsgType:0202](单播)进入场景,场景所有其他角色的列表
			_mainSvc->GetCoreData()->NotifyAllRoleInfo(roleID);
		}
	}
	return;


}
//@brief	[MsgType:0201]进入场景
void MapSvc::ProcessEnterMap(Session& session,Packet& packet)
{
	UInt32	RetCode = 0;
	DataBuffer	serbuffer(1024);
	char szSql[1024];
	Connection con;
	DBOperate dbo;
	int iRet = 0;

	UInt32 roleID = packet.RoleID;
	UInt32 mapID = 0;
	UInt16 x;
	UInt16 y;
	UInt32 Time = 0;

	//序列化类
	Serializer s(packet.GetBuffer());
	s>>mapID>>x>>y;
	if( s.GetErrorCode()!= 0 )
	{
		RetCode = ERR_SYSTEM_SERERROR;
		LOG(LOG_ERROR,__FILE__,__LINE__,"serial error" );
		goto EndOf_Process;
	}

//DEBUG_PRINTF4( " ProcessEnterMap--roleID[%d],MapID[%d],.X[%d],.Y[%d]  \n", roleID, mapID, x, y );
	//业务处理
	iRet = _mainSvc->GetCoreData()->ProcessRoleEnterMap( roleID, mapID, x, y);
	if( iRet == -1)
	{
		RetCode = ERR_APP_OP;
		LOG(LOG_ERROR,__FILE__,__LINE__,"GetCoreData()->ProcessRoleEnterMap error");
		goto EndOf_Process;
	}
	if(iRet == 1)
	{
		RetCode = ERR_APP_ALREADYINMAP;
		LOG(LOG_ERROR,__FILE__,__LINE__,"GetCoreData()->ProcessRoleEnterMap error");
		goto EndOf_Process;

	}

EndOf_Process:
	//应答包时间字段
	Time = time(NULL);

	//组应答数据
	Packet p(&serbuffer);
	s.SetDataBuffer(&serbuffer);
	serbuffer.Reset();

	p.CopyHeader(packet);
	p.Direction = DIRECT_C_S_RESP;
	p.PackHeader();

	s<<RetCode;
	if( 0 == RetCode )
	{//RetCode 为0 才会返回包体剩下内容
		s<<Time;
	}

	p.UpdatePacketLength();

	//发送应答数据
	if( session.Send(&serbuffer) )
	{
		LOG(LOG_ERROR, __FILE__, __LINE__, "session->Send() error,errno[%d],strerror[%s]", errno, strerror(errno) );
	}

//DEBUG_PRINTF1( "C_S ack pkg ----- MsgType[%d]  \n", packet.MsgType );
//	DEBUG_SHOWHEX( serbuffer.GetReadPtr(), serbuffer.GetDataSize(), 0, __FILE__, __LINE__ );
	if(RetCode==0)
	{
		RolePtr pRole=_mainSvc->GetCoreData()->ProcessGetRolePtr(roleID);
		//[MsgType:0202](单播)进入场景,场景所有其他角色的列表
		_mainSvc->GetCoreData()->NotifyAllRoleInfo(roleID);

		//s-c205进入地图
		//NotifyEnterMap(roleID,mapID,x,y);
		if(pRole->TeamFlag()==1)
		{
			NotifyTeamLeader(roleID,pRole->LeaderRoleID());
		}

 LOG(LOG_DEBUG,__FILE__,__LINE__,"ProcessEnterMap OK!!roleID[%d],mapID[%d], EnterMapNum[%d]", pRole->ID(), mapID, pRole->EnterMapNum());

		//角色登陆进地图的未完成的挂机数据
		_mainSvc->GetOffLineUpdateSvc()->OnRoleLoginNotify(roleID);

		if(pRole->EnterMapNum() == 1)
		{
			//角色登录的符文使用数据
		    List<RoleRune> lic;
		    pRole->PopulateRoleRuneList(lic);

			if(lic.size()>0){
				NotifyActRuneListOnLogin(roleID, lic);
			}
		}
	}

	return;
}