1 Star 0 Fork 32

condition/CM-Server

forked from 留天下/CM-Server 
Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
cmserver.cpp 10.70 KB
Copy Edit Raw Blame History
留天下 authored 2018-03-15 16:44 . update
#include "cmserver.h"
#include<iostream>
#include<string.h>
using namespace std;
CMServer::CMServer():
TCPServer(NULL,NULL,6036,EPOLL_MODE,10)
{
}
CMServer::~CMServer()
{
}
void CMServer::OnRecv(int clientfd, char *msg, int flag)
{
switch (flag)
{
case DISCONN_FLAG:
DoPlayerLeaveMsg(clientfd);
break;
case NORMAL_FLAG:
{
M_Type type=*((M_Type*)msg);
switch (type)
{
case M_InitData:
DoInitDataMsg(clientfd,(InitData_Msg*)msg);
break;
case M_WorldTalk:
DoWroldTalkMsg(clientfd,(WorldTalk_Msg*)msg);
break;
case M_PrivateTalk:
DoPrivateTalkMsg(clientfd,(PrivateTalk_Msg*)msg);
break;
case M_InitPos:
DoInitPosMsg(clientfd,(InitPos_Msg*)msg);
break;
case M_PlayerLeave:
DoPlayerLeaveMsg(clientfd);
break;
case M_MoveTo:
DoPlayerMoveToMsg(clientfd,(MoveTo_Msg*)msg);
break;
case M_VerifyPos:
DoPlayerVerifyPosMsg(clientfd,(VerifyPos_Msg*)msg);
break;
case M_UpdateData:
DoUpdateDataMsg(clientfd,(UpdateData_Msg*)msg);
break;
case M_TeamApply:
doTeamApplyMsg(clientfd,(TeamApply_Msg*)msg);
break;
case M_RefuseTeam:
DoRefuseTeamMsg(clientfd,(RefuseTeam_Msg*)msg);
break;
case M_AgreeTeam:
DoAgreeTeamMsg(clientfd,(AgreeTeam_Msg*)msg);
break;
case M_TeamMove:
DoTeamMoveMsg(clientfd,(TeamMove_Msg*)msg);
break;
case M_TeamGotoMap:
DoTeamGotoMapMsg(clientfd,(TeamGotoMap_Msg*)msg);
break;
case M_DissolveTeam:
DoDissolveTeamMsg(clientfd,(TeamManage_Msg*)msg);
break;
case M_TeamFight:
DoTeamFightMsg(clientfd,(TeamFight_Msg*)msg);
break;
case M_PlayerAtk:
DoPlayerAtkMsg(clientfd,(PlayerAtk_Msg*)msg);
break;
case M_MonsterAtk:
DoMonsterAtkMsg(clientfd,(MonsterAtk_Msg*)msg);
break;
case M_PlayerRun:
DoPlayerRunMsg(clientfd,(PlayerRun_Msg*)msg);
break;
case M_UseMedication:
DoUseMedicationMsg(clientfd,(UseMedication_Msg*)msg);
break;
case M_PlayerDie:
DoPlayerDieMsg(clientfd,(PlayerDie_Msg*)msg);
break;
default:
break;
}
}
break;
default:
break;
}
}
void CMServer::DoWroldTalkMsg(const int& fd,WorldTalk_Msg* msg)
{
cout<<m_playermaps[fd].rolename<<" in the world say: "<<msg->msg<<endl;
msg->fd=fd;
for(auto var:m_playermaps)
{
if(var.first!=fd)
{
write(var.first,(char*)msg,sizeof(WorldTalk_Msg));
}
}
}
void CMServer::DoPrivateTalkMsg(const int& fd,PrivateTalk_Msg* msg)
{
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
msg->fd=fd;
cout<<m_playermaps[fd].rolename<<" say "<<msg->msg<<" to "<<m_playermaps[msg->dest].rolename<<endl;
write(msg->dest,(char*)msg,sizeof(PrivateTalk_Msg));
}
}
void CMServer::DoInitDataMsg(const int& fd,InitData_Msg* rlmsg)
{
m_playermaps[fd].playername=rlmsg->playername;
m_playermaps[fd].rolename=rlmsg->rolename;
m_playermaps[fd].playertype=rlmsg->playertype;
m_playermaps[fd].curmap=rlmsg->curmap;
m_playermaps[fd].blood=rlmsg->blood;
m_playermaps[fd].mana=rlmsg->mana;
m_playermaps[fd].attack=rlmsg->attack;
m_playermaps[fd].defense=rlmsg->defense;
m_playermaps[fd].grade=rlmsg->grade;
cout<<rlmsg->playername<<":"<<rlmsg->rolename<<" online!"<<endl;
for(auto var:m_playermaps)
{
if(var.first!=fd)
{
InitData_Msg smsg;
smsg.type=M_InitData;
strcpy(smsg.playername,m_playermaps[var.first].playername.c_str());
strcpy(smsg.rolename,m_playermaps[var.first].rolename.c_str());
strcpy(smsg.playertype,m_playermaps[var.first].playertype.c_str());
smsg.curmap=m_playermaps[var.first].curmap;
smsg.blood=m_playermaps[var.first].blood;
smsg.mana=m_playermaps[var.first].mana;
smsg.attack=m_playermaps[var.first].attack;
smsg.defense=m_playermaps[var.first].defense;
smsg.grade=m_playermaps[var.first].grade;
smsg.fd=var.first;
SendMsg(fd,(char*)&smsg,sizeof(smsg));
rlmsg->fd=fd;
SendMsg(var.first,(char*)rlmsg,sizeof(InitData_Msg));
}
}
}
void CMServer::DoInitPosMsg(const int& fd,InitPos_Msg* msg)
{
m_playermaps[fd].x=msg->x;
m_playermaps[fd].y=msg->y;
m_playermaps[fd].curmap=msg->curmap;
cout<<m_playermaps[fd].rolename<<" initialize position("<<msg->x<<","<<msg->y<<")"<<endl;
for(auto var:m_playermaps)
{
if(var.first!=fd)
{
msg->fd=fd;
SendMsg(var.first,(char*)msg,sizeof(InitPos_Msg));
InitPos_Msg smsg;
smsg.fd=var.first;
smsg.type=M_InitPos;
smsg.x=m_playermaps[var.first].x;
smsg.y=m_playermaps[var.first].y;
smsg.curmap=m_playermaps[var.first].curmap;
SendMsg(fd,(char*)&smsg,sizeof(smsg));
}
}
}
void CMServer::DoPlayerLeaveMsg(const int& fd)
{
cout<<m_playermaps[fd].rolename<<" off line"<<endl;
for(auto var:m_playermaps)
{
if(var.first!=fd)
{
PlayerLeave_Msg msg;
msg.type=M_PlayerLeave;
msg.fd=fd;
SendMsg(var.first,(char*)&msg,sizeof(msg));
}
}
m_playermaps.erase(fd);
}
void CMServer::DoPlayerMoveToMsg(const int& fd,MoveTo_Msg* msg)
{
cout<<m_playermaps[fd].rolename<<" move to position("<<msg->x<<","<<msg->y<<")"<<endl;
for(auto var:m_playermaps)
{
if(var.first!=fd)
{
msg->fd=fd;
SendMsg(var.first,(char*)msg,sizeof(MoveTo_Msg));
}
}
}
void CMServer::DoPlayerVerifyPosMsg(const int& fd,VerifyPos_Msg* msg)
{
cout<<m_playermaps[fd].rolename<<" verify position("<<msg->x<<","<<msg->y<<")"<<endl;
for(auto var:m_playermaps)
{
if(var.first!=fd)
{
msg->fd=fd;
SendMsg(var.first,(char*)msg,sizeof(VerifyPos_Msg));
}
else
{
m_playermaps[var.first].x=msg->x;
m_playermaps[var.first].y=msg->y;
}
}
}
void CMServer::DoUpdateDataMsg(const int& fd,UpdateData_Msg* msg)
{
m_playermaps[fd].attack=msg->attack;
m_playermaps[fd].blood=msg->blood;
m_playermaps[fd].defense=msg->defense;
m_playermaps[fd].grade=msg->grade;
m_playermaps[fd].mana=msg->mana;
msg->fd=fd;
cout<<m_playermaps[fd].rolename<<" update data"<<endl;
for(auto var:m_playermaps)
{
if(var.first!=fd)
{
SendMsg(var.first,(char*)msg,sizeof(UpdateData_Msg));
}
}
}
void CMServer::doTeamApplyMsg(const int& fd,TeamApply_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" apply to make a team with "<<m_playermaps[msg->dest].rolename<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(TeamApply_Msg));
}
}
void CMServer::DoRefuseTeamMsg(const int& fd,RefuseTeam_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" refused "<<m_playermaps[msg->dest].rolename<<" team apply"<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(RefuseTeam_Msg));
}
}
void CMServer::DoAgreeTeamMsg(const int& fd,AgreeTeam_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" agree "<<m_playermaps[msg->dest].rolename<<" team apply"<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(AgreeTeam_Msg));
}
}
void CMServer::DoTeamMoveMsg(const int& fd,TeamMove_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" team header move "<<m_playermaps[msg->dest].rolename<<" to ("<<msg->x<<","<<msg->y<<")"<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(TeamMove_Msg));
}
}
void CMServer::DoTeamGotoMapMsg(const int& fd,TeamGotoMap_Msg* msg)
{
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" team header move "<<m_playermaps[msg->dest].rolename<<" to "<<msg->map<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(TeamGotoMap_Msg));
}
}
void CMServer::DoDissolveTeamMsg(const int& fd,TeamManage_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<"Dissolve Team "<<m_playermaps[fd].rolename<<" and "<<m_playermaps[msg->dest].rolename<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(TeamManage_Msg));
}
}
void CMServer::DoTeamFightMsg(const int& fd,TeamFight_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" leader "<<m_playermaps[msg->dest].rolename<<" entry fight"<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(TeamFight_Msg));
}
}
void CMServer::DoPlayerAtkMsg(const int& fd,PlayerAtk_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" attack monster"<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(PlayerAtk_Msg));
}
}
void CMServer::DoMonsterAtkMsg(const int& fd,MonsterAtk_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<"monster attack "<<m_playermaps[msg->dest].rolename<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(MonsterAtk_Msg));
}
}
void CMServer::DoPlayerRunMsg(const int& fd,PlayerRun_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" run away"<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(PlayerRun_Msg));
}
}
void CMServer::DoUseMedicationMsg(const int& fd,UseMedication_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" use medication!"<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(UseMedication_Msg));
}
}
void CMServer::DoPlayerDieMsg(const int& fd,PlayerDie_Msg* msg)
{
msg->fd=fd;
if(m_playermaps.find(msg->dest)!=m_playermaps.end())
{
cout<<m_playermaps[fd].rolename<<" die!"<<endl;
SendMsg(msg->dest,(char*)msg,sizeof(PlayerDie_Msg));
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/ShareCodeSite/CM-Server.git
git@gitee.com:ShareCodeSite/CM-Server.git
ShareCodeSite
CM-Server
CM-Server
master

Search