📄 client.cpp
字号:
system("pause");//暂停
reborth=true;
showMainMenu(sock);
//退出战斗
return;
}
else goto PL;
}
}
}
ColorTextOut("[系统]退出战斗模式",false);
UpdateAfterFight(sock);
}
void UpdateAfterFight(SOCKET sock)
{
//先判断是否达到升级条件
SCommand scom;
scom.CommandID=CMD_GETLEVELINFO;
char lv[3];
itoa((g_player.level+1),lv,10);//取得下一级的数据
scom.DataSize=(int)strlen(lv);
int nRet=send(sock,(char*)&scom,sizeof(scom),0);
if(nRet==SOCKET_ERROR)
{
cout<<"UpdateAfterFight()发送数据出错,请检查网络!"<<endl;
return;
}
nRet=send(sock,lv,(int)strlen(lv),0);
if(nRet==SOCKET_ERROR)
{
cout<<"UpdateAfterFight()发送数据出错,请检查网络!"<<endl;
return;
}
LevelInfo li;
memset(&li,0,sizeof(li));
nRet=recv(sock,(char*)&li,sizeof(li),0);
if(nRet==SOCKET_ERROR)
{
cout<<"UpdateAfterFight()接收数据出错,请检查网络!"<<endl;
return;
}
if(g_player.exp>li.needexp)//升级后数据的更改
{
g_player.level++;
g_player.hp=li.hp;
g_player.mp=li.mp;
g_player.attack=li.attack;
g_player.defendence=li.defendence;
g_player.celerity=li.celerity;
g_player.exp-=li.needexp;
if(strcmp(li.hasskill,"null")!=0)//等级提高,学习新技能
{
int i=0;
for(;i<SKILLNUM;i++)
{
if(g_player.ownSkill[i]!="null")
continue;
else
{
g_player.ownSkill[i]=li.hasskill;
break;
}
}
updateNewDS updata;
memset(&updata,0,sizeof(updata));
sprintf(updata.op,"skill%d",i+1);
strcpy(updata.item,g_player.ownSkill[i].c_str());
updata.opType=DBOP_TYPE_STRING;
SCommand scom;
scom.CommandID=CMD_UPDATE;
scom.DataSize=sizeof(updata);
nRet=send(sock,(char*)&scom,sizeof(scom),0);
if(nRet==SOCKET_ERROR)
{
cout<<"UpdateAfterFight()发送数据出错,请检查网络!"<<endl;
return;
}
nRet=send(sock,(char*)&updata,sizeof(updata),0);
if(nRet==SOCKET_ERROR)
{
cout<<"UpdateAfterFight()发送数据出错,请检查网络!"<<endl;
return;
}
char res[20];
memset(res,0,20);
nRet=recv(sock,res,sizeof(res),0);
if(nRet==SOCKET_ERROR)
{
cout<<"UpdateAfterFight()接收数据出错,请检查网络!"<<endl;
return;
}
if(strcmp(res,"UP OK")==0)
{
cout<<"你学习了新技能——"<<g_player.ownSkill[i]<<endl;
}
}
}
scom.CommandID=CMD_UPDATEROLEINFO;
scom.DataSize=sizeof(g_player);
nRet=send(sock,(char*)&scom,sizeof(scom),0);
if(nRet==SOCKET_ERROR)
{
cout<<"UpdateAfterFight()发送数据出错,请检查网络!"<<endl;
return;
}
nRet=send(sock,(char*)&g_player,sizeof(g_player),0);
if(nRet==SOCKET_ERROR)
{
cout<<"UpdateAfterFight()发送数据出错,请检查网络!"<<endl;
return;
}
char res[20];
memset(res,0,20);
nRet=recv(sock,res,sizeof(res),0);
if(nRet==SOCKET_ERROR)
{
cout<<"UpdateAfterFight()发送数据出错,请检查网络!"<<endl;
return;
}
if(strcmp(res,"UP OK")==0)
{
cout<<"恭喜你,你的修行增加了,法力增强了"<<endl;
}
}
//获取怪物信息
bool GetMonsterInfo(SOCKET sock,monster& m)
{
char* curpos=map[g_player.pos[0]][g_player.pos[1]];
SCommand scom;
scom.CommandID=CMD_GETMONSTERINFO;
scom.DataSize=(int)strlen(curpos);
int nRet=send(sock,(char*)&scom,sizeof(scom),0);
if(nRet==SOCKET_ERROR)
{
cout<<"GetMonsterInfo()发送数据失败,请检查网络!"<<endl;
return false;
}
nRet=send(sock,curpos,(int)strlen(curpos),0);
if(nRet==SOCKET_ERROR)
{
cout<<"GetMonsterInfo()发送数据失败,请检查网络!"<<endl;
return false;
}
nRet=recv(sock,(char*)&m,sizeof(m),0);
if(nRet==SOCKET_ERROR)
{
cout<<"GetMonsterInfo()接收数据失败,请检查网络!"<<endl;
return false;
}
return true;
}
//计算角色的综合值
void CalculateRoleInfo(SOCKET sock,int& attack,int& defence,int& celerity)
{
if(g_player.ownEquip[0]!="null")
defence=g_player.defendence+GetEquipValueByString(sock,g_player.ownEquip[0]);
else defence=g_player.defendence;
if(g_player.ownEquip[1]!="null")
attack=g_player.attack+GetEquipValueByString(sock,g_player.ownEquip[1]);
else attack=g_player.attack;
if(g_player.ownEquip[2]!="null")
celerity=g_player.celerity+GetEquipValueByString(sock,g_player.ownEquip[2]);
else celerity=g_player.celerity;
}
//一般描述事件(包括交易系统,换装系统,)
void DefaultEvent(SOCKET sock)
{
vector<Myobject> SuperMarket;
char* curScence=map[g_player.pos[0]][g_player.pos[1]];
SCommand scom;
scom.CommandID=CMD_CHECKHASNPC;
scom.DataSize=(int)strlen(curScence);
int nRet=send(sock,(char*)&scom,sizeof(scom),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()发送数据出错,请检查网络!"<<endl;
return;
}
nRet=send(sock,curScence,(int)strlen(curScence),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()发送数据出错,请检查网络!"<<endl;
return;
}
bool hasNPC=false;
nRet=recv(sock,(char*)&hasNPC,sizeof(hasNPC),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()发送数据出错,请检查网络!"<<endl;
return;
}
if(hasNPC)//有npc,则取回npc数据
{
scom.CommandID=CMD_GETNPCINFO;
nRet=send(sock,(char*)&scom,sizeof(scom),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()发送数据出错,请检查网络!"<<endl;
return;
}
nRet=send(sock,curScence,(int)strlen(curScence),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()发送数据出错,请检查网络!"<<endl;
return;
}
npc npcdata;
memset(&npcdata,0,sizeof(npcdata));
nRet=recv(sock,(char*)&npcdata,sizeof(npcdata),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()发送数据出错,请检查网络!"<<endl;
return;
}
//以下显示npc的货品,只有两项物品,如果需要更改的话,需要更改相应的地方(包括游戏中其他的地方)。
int objnum=0;
Myobject mobj;
char buf[256];
cout<<"不经意间竟然发现附近有个位商人……"<<endl<<npcdata.npcName<<endl;
cout<<"出售商品:"<<endl;
//第一件商品
cout<<"["<<objnum++<<"] "<<npcdata.item1<<endl;
memset(buf,0,256);
string temp=npcdata.item1;
memset(&mobj,0,sizeof(mobj));
if(GetObjectInfo(sock,temp,mobj))
{
sprintf(buf,"[%s]物品类型:%s,效果值:%d,价格:%d,使用等级:%d",mobj.objectName,GetStringByObjType(mobj.moType),mobj.value,mobj.buy,mobj.needLevel);
ColorTextOut(buf,false);
cout<<endl;
SuperMarket.push_back(mobj);
}
//第二件商品
cout<<"["<<objnum++<<"] "<<npcdata.item2<<endl;
memset(buf,0,256);
temp=npcdata.item2;
memset(&mobj,0,sizeof(mobj));
if(GetObjectInfo(sock,temp,mobj))
{
sprintf(buf,"[%s]物品类型:%s,效果值:%d,价格:%d,使用等级:%d",mobj.objectName,GetStringByObjType(mobj.moType),mobj.value,mobj.buy,mobj.needLevel);
ColorTextOut(buf,false);
cout<<endl;
SuperMarket.push_back(mobj);
}
//第三件商品
cout<<"["<<objnum++<<"] "<<npcdata.item3<<endl;
memset(buf,0,256);
temp=npcdata.item3;
memset(&mobj,0,sizeof(mobj));
if(GetObjectInfo(sock,temp,mobj))
{
sprintf(buf,"[%s]物品类型:%s,效果值:%d,价格:%d,使用等级:%d",mobj.objectName,GetStringByObjType(mobj.moType),mobj.value,mobj.buy,mobj.needLevel);
ColorTextOut(buf,false);
cout<<endl;
SuperMarket.push_back(mobj);
}
//第四件商品
cout<<"["<<objnum++<<"] "<<npcdata.item4<<endl;
memset(buf,0,256);
temp=npcdata.item4;
memset(&mobj,0,sizeof(mobj));
if(GetObjectInfo(sock,temp,mobj))
{
sprintf(buf,"[%s]物品类型:%s,效果值:%d,价格:%d,使用等级:%d",mobj.objectName,GetStringByObjType(mobj.moType),mobj.value,mobj.buy,mobj.needLevel);
ColorTextOut(buf,false);
cout<<endl;
SuperMarket.push_back(mobj);
}
//第五件商品
cout<<"["<<objnum++<<"] "<<npcdata.item5<<endl;
memset(buf,0,256);
temp=npcdata.item5;
memset(&mobj,0,sizeof(mobj));
if(GetObjectInfo(sock,temp,mobj))
{
sprintf(buf,"[%s]物品类型:%s,效果值:%d,价格:%d,使用等级:%d",mobj.objectName,GetStringByObjType(mobj.moType),mobj.value,mobj.buy,mobj.needLevel);
ColorTextOut(buf,false);
cout<<endl;
SuperMarket.push_back(mobj);
}
//买卖系统
bool iswelldone=false;
while(!iswelldone)
{
int choice=0;
int itemchoice=-1;
int itemnum=0;
bool ismoneyupdate=false;
bool isobjectadd=false;
char in='0';
cout<<"****************欢迎光临******************"<<endl;
cout<<"请选择你当前的操作:"<<endl;
cout<<"1>够买"<<endl;
cout<<"2>抛售"<<endl;
cout<<"3>离开"<<endl;
cin>>in;
if(in<'0'||in>'4')
return;
else choice=in-'0';
switch(choice)
{
case 1:
cout<<"请选择你要够买物品的序号:(序号外任意键退出)"<<endl;
cin>>itemchoice;
if(itemchoice>=objnum||itemchoice<0)
break;
cout<<"请输入你要够买的数量:"<<endl;
cin>>itemnum;
if(SuperMarket[itemchoice].buy*itemnum>g_player.money)
cout<<"你的钱不够!"<<endl;
else
{
//更改当前金钱
g_player.money-=SuperMarket[itemchoice].buy*itemnum;
updateNewDS updata;
memset(&updata,0,sizeof(updata));
strcpy(updata.op,"tael");
strcpy(updata.item,"null");
updata.opType=DBOP_TYPE_NUM;
updata.num=g_player.money;
SCommand scom;
scom.CommandID=CMD_UPDATE;
scom.DataSize=sizeof(updata);
int nRet=send(sock,(char*)&scom,sizeof(scom),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()函数,发送更新数据失败,请检查网络!"<<endl;
return;
}
nRet=send(sock,(char*)&updata,sizeof(updata),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()函数,发送更新数据失败,请检查网络!"<<endl;
return;
}
//反馈结果
char res[20];
memset(res,0,20);
nRet=recv(sock,res,sizeof(res),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()函数,接收结果数据失败,请检查网络!"<<endl;
return;
}
if(strcmp(res,"UP OK")==0)
ismoneyupdate=true;
bool isrevolve=false;//判断背包是否满了
memset(&updata,0,sizeof(updata));
//更改背包数据
for(int i=1;i<=OBJECTNUM;i++)
{
if(g_player.ownObject[i-1].ownObjectName!="null")
{
//背包用已有该物品
if(g_player.ownObject[i-1].ownObjectName==SuperMarket[itemchoice].objectName)
{
//本地数据修改
g_player.ownObject[i-1].num+=itemnum;
//同步数据库
sprintf(updata.op,"objectnum%d",i);
updata.num=g_player.ownObject[i-1].num;
updata.opType=DBOP_TYPE_NUM;
strcpy(updata.item,"null");
isrevolve=true;
break;
}
else continue;
}
else//背包中没有该物品,但有空位
{
//修改本地数据
g_player.ownObject[i-1].ownObjectName=SuperMarket[itemchoice].objectName;
g_player.ownObject[i-1].num=itemnum;
//填充数据包
sprintf(updata.op,"objectname%d objectnum%d",i,i);
strcpy(updata.item,SuperMarket[itemchoice].objectName);
updata.num=itemnum;
updata.opType=DBOP_TYPE_ALL;
isrevolve=true;
break;
}
}
if(!isrevolve)
{
cout<<"你的背包已满,请不要这么贪心!"<<endl;
return;
}
scom.DataSize=sizeof(updata);
nRet=send(sock,(char*)&scom,sizeof(scom),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()函数,发送更新数据失败,请检查网络!"<<endl;
return;
}
nRet=send(sock,(char*)&updata,sizeof(updata),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()函数,发送更新数据失败,请检查网络!"<<endl;
return;
}
memset(res,0,20);
nRet=recv(sock,res,sizeof(res),0);
if(nRet==SOCKET_ERROR)
{
cout<<"DefaultEvent()函数,接收结果数据失败,请检查网络!"<<endl;
return;
}
if(strcmp(res,"UP OK")==0)
isobjectadd=true;
//反馈最终结果
if(ismoneyupdate&&isobjectadd)
{
ColorTextOut("[系统]",false);
cout<<"交易成功!"<<endl;
}
else
{
ColorTextOut("[系统]",false);
cout<<"交易失败!"<<endl;
}
}
break;
case 2:
SellObject(sock);
break;
case 3:
iswelldone=true;
cout<<"******************如果有缘,下次再见*****************"<<endl;
break;
default:
cout<<"输入非法,请输入数字1-3"<<endl;
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -