⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 改工程是一个基于select模型实现的MUD游戏
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		}

	}

	else
	{
		cout<<"看了看四周,感觉没有什么奇怪的。没有什么事情还是走把……"<<endl;
	}

	
}


//抛售东西
void SellObject(SOCKET sock)
{
	//先看看有什么可以卖的
	cout<<"**************也许我可以给你出个好价钱******************"<<endl;
	int index=0;
	for(int i=0;i<OBJECTNUM;i++)
	{
		if(g_player.ownObject[i].ownObjectName!="null")
		{
			cout<<"["<<(index++)<<"]"<<g_player.ownObject[i].ownObjectName<<"   数量:"<<g_player.ownObject[i].num<<endl;
		}
		else
		{ 
			cout<<"["<<(index++)<<"]"<<"  空  "<<endl;
		}
	}
	if(index==0)
	{
		cout<<"你的背包里空空如也!"<<endl;
		return;
	}


	int choice=-1;
	int itemnum=0;
	cout<<"请选择你要出售的物品序号"<<endl;
	cin>>choice;
	if(choice>=index||choice<0)
	{
		cout<<"输入非法,请输入数字0-"<<index-1<<endl;
		return;
	}

	if(g_player.ownObject[choice].ownObjectName=="null")
	{
		cout<<"该项没有物品,请看清楚后选择"<<endl;
		return;
	}

	cout<<"请输入要出售的数量:"<<endl;
	cin>>itemnum;

	Myobject mobj;
	memset(&mobj,0,sizeof(mobj));


	bool bfirstsucess=false;
	bool bsecondsucess=false;

	SCommand scom;
	memset(&scom,0,sizeof(scom));
	updateNewDS useupdata;
	memset(&useupdata,0,sizeof(useupdata));
	updateNewDS effupdata;
	memset(&effupdata,0,sizeof(effupdata));
	if(GetObjectInfo(sock,g_player.ownObject[choice].ownObjectName,mobj))
	{

		if(g_player.ownObject[choice].num<itemnum)
		{
			cout<<"你没有这么多数量的该物品"<<endl;
			return;
		}
		//该物品没有全卖
		if(g_player.ownObject[choice].num-itemnum>0)
		{
			g_player.ownObject[choice].num-=itemnum;

			useupdata.num=g_player.ownObject[choice].num;
			sprintf(useupdata.op,"objectnum%d",choice+1);//数据库从1开始
			useupdata.opType=DBOP_TYPE_NUM;
			strcpy(useupdata.item,"null");				
		}
		else if(g_player.ownObject[choice].num-itemnum==0)//该物品全卖
		{
			g_player.ownObject[choice].num-=itemnum;
			g_player.ownObject[choice].ownObjectName="null";

			useupdata.num=g_player.ownObject[choice].num;
			sprintf(useupdata.op,"objectname%d objectnum%d",choice+1,choice+1);//数据库从1开始
			useupdata.opType=DBOP_TYPE_ALL;
			strcpy(useupdata.item,"null");

		}

		//公共部分
		g_player.money+=(mobj.buy*itemnum)/2;

		effupdata.num=g_player.money;
		strcpy(effupdata.op,"tael");
		effupdata.opType=DBOP_TYPE_NUM;
		strcpy(effupdata.item,"null");

		scom.CommandID=CMD_UPDATE;
		scom.DataSize=sizeof(useupdata);

		int nRet=send(sock,(char*)&scom,sizeof(scom),0);
		if(nRet==SOCKET_ERROR)
		{
			cout<<"SellObject()发送数据出错,请检查网络!"<<endl;
			return;
		}


		nRet=send(sock,(char*)&useupdata,sizeof(useupdata),0);
		if(nRet==SOCKET_ERROR)
		{
			cout<<"SellObject()发送数据出错,请检查网络!"<<endl;
			return;
		}



		char res[20];
		memset(res,0,20);

		nRet=recv(sock,res,sizeof(res),0);
		if(nRet==SOCKET_ERROR)
		{
			cout<<"SellObject()接收数据出错,请检查网络!"<<endl;
			return;
		}
		else
		{
			bfirstsucess=true;
		}



		scom.DataSize=sizeof(effupdata);

		nRet=send(sock,(char*)&scom,sizeof(scom),0);
		if(nRet==SOCKET_ERROR)
		{
			cout<<"SellObject()发送数据出错,请检查网络!"<<endl;
			return;
		}


		nRet=send(sock,(char*)&effupdata,sizeof(effupdata),0);
		if(nRet==SOCKET_ERROR)
		{
			cout<<"SellObject()发送数据出错,请检查网络!"<<endl;
			return;
		}


		memset(res,0,20);

		nRet=recv(sock,res,sizeof(res),0);
		if(nRet==SOCKET_ERROR)
		{
			cout<<"SellObject()接收数据出错,请检查网络!"<<endl;
			return;
		}
		else
		{
			bsecondsucess=true;
		}

		

		if(bfirstsucess&&bsecondsucess)
		{
			cout<<"**************交易成功************"<<endl;
		}

	}

	

}


//显示消息面板,包括封装消息,发送消息。
//消息长度:512bytes
//消息格式:		|       接收玩家用户名		 |	
//                  |	   信息头		|消息内容|
//消息头格式:		|	发送玩家昵称	|
void ShowDialogMenu(SOCKET sock)
{
	
	char diag[512];	
	char rebuilt[512];	
	GameMessage mess;
	

	while(true)
	{
		int choice;
		memset(rebuilt,0,512);
		memset(diag,0,512);
		memset(&mess,0,sizeof(mess));
		//玩家列表,以供选择。格式化输出
		int i=0;
		ColorTextOut("[当前在线玩家列表]",false);
		cout<<endl;
		for(vector<OnLineList>::iterator iter=g_OnLineRoles.begin();iter!=g_OnLineRoles.end();iter++)
		{
			cout<<"["<<i++<<"] "<<iter->nName<<"    ";
			if(i%4==0)
				cout<<endl;
		}
		cout<<endl;
		//格式化输出结束

		cout<<"请选择你要说话的对象:(序号外任意键退出)"<<endl;
		cin>>choice;

		if(choice>(int)(g_OnLineRoles.size()-1)||choice<0)
			return;

		cout<<"请输入你要说的内容:"<<endl;
		cin>>diag;
		
		//消息封装
		strcat(rebuilt,g_player.nickName);
		strcat(rebuilt," ");
		strcat(rebuilt,diag);

		strcpy(mess.message,rebuilt);
		strcpy(mess.recver,g_OnLineRoles[choice].uName);

		SCommand scom;
		scom.CommandID=CMD_SPEAK;
		scom.DataSize=sizeof(mess);
		int nRet=send(sock,(char*)&scom,sizeof(scom),0);
		if(nRet==SOCKET_ERROR)
		{
			cout<<"发送说话数据出错,请检查网络!"<<endl;
			return;
		}


		nRet=send(sock,(char*)&mess,sizeof(mess),0);
		if(nRet==SOCKET_ERROR)
		{
			cout<<"发送说话数据出错,请检查网络!"<<endl;
			return;
		}
	}


}

//从服务器获取信息
void GetGameMessage(SOCKET sock)
{
	char recvMess[512];
	memset(recvMess,0,512);
	char content[512];
	memset(content,0,512);

	SCommand scom;
	scom.CommandID=CMD_GETMESSAGE;
	scom.DataSize=0;

	int nRet=send(sock,(char*)&scom,sizeof(scom),0);
	if(nRet==SOCKET_ERROR)
	{
		cout<<"发送说话数据出错,请检查网络!"<<endl;
		return;
	}


	int messNum=-1;
	nRet=recv(sock,(char*)&messNum,sizeof(messNum),0);
	if(nRet==SOCKET_ERROR)
	{
		cout<<"发送说话数据出错,请检查网络!"<<endl;
		return;
	}


	//有多条信息,轮流读取
	while(messNum)
	{
		nRet=recv(sock,recvMess,sizeof(recvMess),0);
		if(nRet==SOCKET_ERROR)
		{
			cout<<"发送说话数据出错,请检查网络!"<<endl;
			return;
		}

		messNum--;

		char*pbuf=recvMess;
		int index=0;
		char head[20];
		memset(head,0,20);
		while(*pbuf!=' ')//信息头
		{
			head[index++]=*pbuf++;
		}
		head[index]='\0';

		*pbuf++;

		index=0;
		while(*pbuf!='\0')
		{
			content[index++]=*pbuf++;
		}
		content[index]='\0';

		if(strcmp(head,"SYS")!=0)//非系统信息
		{
			ColorTextOut("[系统]",false);
			ColorTextOut(head,false);
			cout<<"对你说:"<<content<<endl;
		}
		else//系统信息
		{
			ColorTextOut("[系统通告]",false);
			cout<<content<<endl;
		}

	}
	

}



//进入下一场景函数
bool NextMapScence(dir d)
{
	bool bGone=true;
	switch(d)
	{
	case EAST:
		if(g_player.pos[1]+1<4)
		{
			g_player.pos[1]+=1;
		}
		else bGone=false;
		break;
	case SOUTH:
		if(g_player.pos[0]+1<4)
		{
			g_player.pos[0]+=1;
		}
		else bGone=false;
		break;
	case WEST:
		if(g_player.pos[1]-1>=0)
		{
			g_player.pos[1]-=1;
		}
		else bGone=false;
		break;
	case NORTH:
		if(g_player.pos[0]-1>=0)
		{
			g_player.pos[0]-=1;
		}
		else bGone=false;
		break;
	}
	return bGone;
}



//根据mapx,mapy值显示地图信息
void ShowMapInfo(int mapx,int mapy)
{
	TiXmlDocument doc(g_szfile);
	if(!doc.LoadFile())
	{
		cout<<"导入地图数据时,出现错误!请核实你的客户端资源!"<<endl;
		return;
	}
	char * curMapName=map[mapx][mapy];

	TiXmlElement* root=doc.RootElement();
	TiXmlNode* node=root->FirstChild()->FirstChild();
	for(;node;node=node->NextSibling())
	{
		if(node->FirstChild()->Type()==TiXmlNode::ELEMENT)
		{
			if(strcmp(node->FirstChild()->ToElement()->GetText(),curMapName)==0)
			{
				ColorTextOut("[系统]",false);
				cout<<node->FirstChild()->NextSibling()->ToElement()->GetText()<<endl;
			}
			else continue;
		}
	}

}

//显示系统信息函数,发布系统相关信息
void ShowSystemInfo()
{
	cout<<"**************************系统信息*************************"<<endl;
	ColorTextOut("[系统]",false);
	cout<<"服务器当前共有"<<(unsigned int)g_OnLineRoles.size()<<"位在线玩家"<<endl;
	cout<<"其中玩家列表如下:"<<endl;
	int i=0;
	for(vector<OnLineList>::iterator iter=g_OnLineRoles.begin();iter!=g_OnLineRoles.end();iter++)
	{
		cout<<"["<<i++<<"] "<<iter->nName<<"    ";
		if(i%4==0)
			cout<<endl;
	}
	cout<<endl;
	cout<<"***************************系统信息*************************"<<endl;
	
}


//显示人物状态,包游戏采用基本属性和装备属性分离保存模式
//数据库中,三位置为:防具,武器,鞋子
void ShowStatus(SOCKET sock)
{
	int amend=0;
	cout<<"****************************人物状态************************"<<endl;
	cout<<"角色名:"<<g_player.nickName<<endl;
	cout<<"当前等级:"<<g_player.level<<endl;
	cout<<"当前血量:"<<g_player.hp<<endl;
	cout<<"当前魔法值:"<<g_player.mp<<endl;
	if(g_player.ownEquip[1]!="null")
	{
		char amendstr[20];
		memset(amendstr,0,20);
		amend=GetEquipValueByString(sock,g_player.ownEquip[1]);
		cout<<"当前攻击值:"<<g_player.attack;
		char numstr[3];
		memset(numstr,0,3);
		itoa(amend,numstr,10);
		strcat(amendstr,"+");
		strcat(amendstr,numstr);
		ColorTextOut(amendstr,true);
		cout<<"装备有[";
		ColorTextOut(g_player.ownEquip[1].c_str(),true);
		cout<<"]"<<endl;
		amend=0;
	}
	else
		cout<<"当前攻击值:"<<g_player.attack<<endl;
	if(g_player.ownEquip[0]!="null")
	{
		char amendstr[20];
		memset(amendstr,0,20);
		amend=GetEquipValueByString(sock,g_player.ownEquip[0]);
		cout<<"当前防御值:"<<g_player.defendence;
		char numstr[3];
		memset(numstr,0,3);
		itoa(amend,numstr,10);
		strcat(amendstr,"+");
		strcat(amendstr,numstr);
		ColorTextOut(amendstr,true);
		cout<<"装备有[";
		ColorTextOut(g_player.ownEquip[0].c_str(),true);
		cout<<"]"<<endl;
		amend=0;
	}
	else
		cout<<"当前防御值:"<<g_player.defendence<<endl;
	if(g_player.ownEquip[2]!="null")
	{
		char amendstr[20];
		memset(amendstr,0,20);
		amend=GetEquipValueByString(sock,g_player.ownEquip[2]);
		cout<<"当前敏捷值:"<<g_player.celerity;
		char numstr[3];
		memset(numstr,0,3);
		itoa(amend,numstr,10);
		strcat(amendstr,"+");
		strcat(amendstr,numstr);
		ColorTextOut(amendstr,true);
		cout<<"装备有[";
		ColorTextOut(g_player.ownEquip[2].c_str(),true);
		cout<<"]"<<endl;
	}
	else
		cout<<"当前敏捷值:"<<g_player.celerity<<endl;
	cout<<"职业:"<<GetStringByOccuType(g_player.ownOccup)<<endl;
	cout<<"性别:"<<(g_player.ownSex==1?"男":"女")<<endl;
	cout<<"当前拥有银两:"<<g_player.money<<endl;
	cout<<"当前经验值:"<<g_player.exp<<endl;
	cout<<"拥有的技能有:"<<endl;
	for(int i=0;i<7;i++)
	{
		if(g_player.ownSkill[i]!="null")
		{
			cout<<"["<<i<<"] "<<g_player.ownSkill[i].c_str();
			if(i%4==0)
				cout<<endl;
		}
	}
	cout<<"***************************人物状态*************************"<<endl;

}


//获取装备固定值
int GetEquipValueByString(SOCKET sock,string& str)
{
	int nRet=0;
	//SCommand scom;
	Myobject mobj;
	memset(&mobj,0,sizeof(Myobject));
	if(GetObjectInfo(sock,str,mobj))
		nRet=mobj.value;
	return nRet;
}



//获取技能参数

void GetSkillParamByString(SOCKET sock,string& str,skill& skparam)
{
	bool bRet=false;

	SCommand scom;
	scom.CommandID=CMD_GETSKILLINFO;
	scom.DataSize=(int)str.length();

	int nRet=send(sock,(char*)&scom,sizeof(scom),0);
	if(nRet==SOCKET_ERROR)
	{
		cout<<"GetSkillParamByString()发送数据失败,请检查网络!"<<endl;
		return;
	}

	nRet=send(sock,str.c_str(),(int)str.length(),0);
	if(nRet==SOCKET_ERROR)
	{
		cout<<"GetSkillParamByString()发送数据失败,请检查网络!"<<endl;
		return;
	}


	nRet=recv(sock,(char*)&skparam,sizeof(skparam),0);
	if(nRet==SOCKET_ERROR)
	{
		cout<<"GetSkillParamByString()接收数据失败,请检查网络!"<<endl;
		return;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -