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

📄 client.cpp

📁 网络围棋对战的客户端
💻 CPP
字号:
// Client.cpp : implementation file
//

#include "stdafx.h"
#include "WeiQiClient.h"
#include <ShlWApi.h>
#include "Client.h"
#include "WeiQiSocket.h"
#include "LogInDlg.h"
#include "GameListDlg.h"
#include "Top10Dlg.h"
#include "MainFrm.h"
#include "WeiQiClientDoc.h"
#include "WeiQiClientView.h"
#include "WeiQi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClient

CClient::CClient()
{
	m_pView=NULL;
	m_GameName=_T("");
	m_pSocket=NULL;
	m_pLogInDlg=NULL;
	m_pWeiQi=new CWeiQi;
	m_InGame=FALSE;
	m_GameStart=FALSE;
}

CClient::~CClient()
{
}


BEGIN_MESSAGE_MAP(CClient, CWnd)
	//{{AFX_MSG_MAP(CClient)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CClient message handlers

BOOL CClient::FetchMsg()
{
	TCHAR pMsg[1000];
	char pTempMsg[1000];
	int ByteCount;
	BOOL EndFlag=FALSE;
	StrCpy(pMsg,_T(""));
	do
	{
		strcpy(pTempMsg,"");
		ByteCount=m_pSocket->Receive(pTempMsg,1000);	//Reveive只接收字节流
		if(ByteCount>1000||ByteCount<=0)
			return FALSE;
		else if(ByteCount<1000&&ByteCount>0)
			EndFlag=TRUE;
		pTempMsg[ByteCount]=0;
		StrCat(pMsg,_T(pTempMsg));
	}while(!EndFlag);
	pMsg[10]=0;
	if(StrCmp(pMsg,_T("__LOGINOK_"))==0)		//"__LOGINOK_"
	{
		pMsg[10]=_T(' ');
		LogInMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("_LOGINFAIL"))==0)		//"_LOGINFAIL"
	{
		MessageBox(_T("用户名不存在或密码无效"));
		return TRUE;
	}
	if(StrCmp(pMsg,_T("_ALREADYIN"))==0)		//"_ALREADYIN"
	{
		MessageBox(_T("此用户已在线"));
		return TRUE;
	}
	if(StrCmp(pMsg,_T("__NEWFAIL_"))==0)		//"__NEWFAIL_"
	{
		MessageBox(_T("此用户已存在"));
		return TRUE;
	}
	if(StrCmp(pMsg,_T("_CREATEOK_"))==0)		//"_CREATEOK_"
	{
		pMsg[10]=_T(' ');
		CreateMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("CREATEFAIL"))==0)		//"CREATEFAIL"
	{
		MessageBox(_T("此游戏已存在"));
		return TRUE;
	}
	if(StrCmp(pMsg,_T("JOINGAMEOK"))==0)		//"JOINGAMEOK"
	{
		pMsg[10]=_T(' ');
		JoinGameMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("__ONEJOIN_"))==0)		//"__ONEJOIN_"
	{
		pMsg[10]=_T(' ');
		OneJoinMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("_JOINFAIL_"))==0)		//"_JOINFAIL_"
	{
		MessageBox(_T("此游戏人满或已取消"));
		return TRUE;;
	}
	if(StrCmp(pMsg,_T("RELISTGAME"))==0)		//"RELISTGAME"
	{
		pMsg[10]=_T(' ');
		ReListMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("_RETOPTEN_"))==0)		//"_RETOPTEN_"
	{
		pMsg[10]=_T(' ');
		TopTenMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("_HOSTEXIT_"))==0)		//"_HOSTEXIT_"
	{
		pMsg[10]=_T(' ');
		HostExitMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("CLIENTEXIT"))==0)		//"CLIENTEXIT"
	{
		pMsg[10]=_T(' ');
		ClientExitMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("EXITGAMEOK"))==0)		//"EXITGAMEOK"
	{
		pMsg[10]=_T(' ');
		ExitGameMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("_RECHATMSG"))==0)		//"_RECHATMSG"
	{
		pMsg[10]=_T(' ');
		CharMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("_SHUTDOWN_"))==0)		//"_SHUTDOWN_"
	{
		pMsg[10]=_T(' ');
		ShutDownMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("__REPLACE_"))==0)		//"__REPLACE_"
	{
		pMsg[10]=_T(' ');
		RePlaceMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("__HESURR__"))==0)		//"__HESURR__"
	{
		pMsg[10]=_T(' ');
		HeSurrMsg(pMsg);
		return TRUE;
	}
	if(StrCmp(pMsg,_T("QUITGAMEOK"))==0)
	{
		return TRUE;
	}

	return FALSE;
}

void CClient::SendMsg(CString Msg)
{
	if(m_pSocket!=NULL)
		m_pSocket->Send(Msg,Msg.GetLength());
}

void CClient::LogInMsg(PTSTR pMsg)
{
	m_Player.SetScore((UINT)atoi(pMsg+12));
	m_pLogInDlg->EndDialog(IDOK);
}

void CClient::CreateMsg(PTSTR pMsg)
{
	m_InGame=TRUE;
	m_pView->m_GameNameText=pMsg+12;
	m_pWeiQi->m_Color=BLACK;
	m_pView->UpdateData(FALSE);
}

void CClient::JoinGameMsg(PTSTR pMsg)
{
	int i=12,j,k;
	while(pMsg[i]!=_T(' ')) i++;
	pMsg[i]=0;
	j=i+1;
	while(pMsg[j]!=_T(' ')) j++;
	pMsg[j]=0;
	k=j+1;
	while(pMsg[k]!=_T(' ')) k++;
	pMsg[k]=0;
	m_GameName=pMsg+12;
	m_InGame=TRUE;
	m_GameStart=TRUE;
	m_pView->m_GameNameText=m_GameName;
	m_pView->m_Player2Info=pMsg+i+1;
	m_pView->m_Player2Info+=_T("\r\n等级:");
	m_pView->m_Player2Info+=pMsg+j+1;
	m_pView->m_Player2Info+="\r\n积分:";
	m_pView->m_Player2Info+=pMsg+k+1;
	m_pWeiQi->m_Color=WHITE;
	m_pView->m_Color=BLACK;
	m_pView->UpdateData(FALSE);
}

void CClient::OneJoinMsg(PTSTR pMsg)
{
	MessageBox(_T("有人加入你的游戏"));
	int i=12,j;
	while(pMsg[i]!=_T(' ')) i++;
	pMsg[i]=0;
	j=i+1;
	while(pMsg[j]!=_T(' ')) j++;
	pMsg[j]=0;
	m_InGame=TRUE;
	m_GameStart=TRUE;
	m_pView->m_Player2Info=pMsg+12;
	m_pView->m_Player2Info+="\r\n等级:";
	m_pView->m_Player2Info+=pMsg+i+1;
	m_pView->m_Player2Info+="\r\n积分:";
	m_pView->m_Player2Info+=pMsg+j+1;
	m_pWeiQi->m_Color=BLACK;
	m_pView->m_Color=BLACK;
	m_pView->UpdateData(FALSE);
}

void CClient::ReListMsg(PTSTR pMsg)
{
	m_pView->m_pGameListDlg->SetListBox(pMsg+12);
}

void CClient::TopTenMsg(PTSTR pMsg)
{
	m_pView->m_pTop10Dlg->SetListBox(pMsg+12);
}

void CClient::HostExitMsg(PTSTR pMsg)
{
	m_InGame=FALSE;
	m_GameStart=FALSE;
	MessageBox(_T("主机已退出"));
	m_pView->m_GameNameText=_T("");
	m_pView->m_Player2Info=_T("");
	m_pView->UpdateData(FALSE);
}

void CClient::ClientExitMsg(PTSTR pMsg)
{
	m_GameStart=FALSE;
	MessageBox(_T("对方已退出"));
	m_pView->m_Player2Info=_T("");
	m_pView->UpdateData(FALSE);
}

void CClient::ExitGameMsg(PTSTR pMsg)
{
	m_InGame=FALSE;
	m_GameStart=FALSE;
	if(m_pWeiQi!=NULL)
	{
		delete m_pWeiQi;
		m_pWeiQi=new CWeiQi;
	}
	m_pView->m_GameNameText=_T("");
	m_pView->m_Player2Info=_T("");
	m_pView->UpdateData(FALSE);
	m_pView->m_DrawPoint=FALSE;
	m_pView->m_PointX=0;
	m_pView->m_PointY=0;
	m_pView->Invalidate();
}

void CClient::CharMsg(PTSTR pMsg)
{
	m_pView->m_ReceiveText+=pMsg+12;
	m_pView->m_ReceiveText+=_T("\r\n");
	m_pView->UpdateData(FALSE);
	int n=m_pView->m_ReceiveEdit.GetLineCount();
	m_pView->m_ReceiveEdit.LineScroll(n);
}

void CClient::ShutDownMsg(PTSTR pMsg)
{
	MessageBox(_T("服务器已关机"));
	delete m_pSocket;
	m_pSocket=NULL;
	AfxGetMainWnd()->DestroyWindow();
}

void CClient::RePlaceMsg(PTSTR pMsg)
{
	pMsg[14]=0;
	pMsg[17]=0;
	int x=atoi(pMsg+12);
	int y=atoi(pMsg+15);
	m_pWeiQi->Place(x,y,-m_pWeiQi->m_Color);
	m_pView->m_Color*=-1;
	m_pView->m_DrawPoint=TRUE;
	m_pView->m_PointX=x;
	m_pView->m_PointY=y;
	m_pWeiQi->m_LastX=x;
	m_pWeiQi->m_LastY=y;
	CRect Rect(m_pWeiQi->m_MinX*17+31-8,
		m_pWeiQi->m_MinY*17+32-8,
		m_pWeiQi->m_MaxX*17+31+8,
		m_pWeiQi->m_MaxY*17+32+8);
	m_pView->InvalidateRect(&Rect);
}

void CClient::HeSurrMsg(PTSTR pMsg)
{
	m_InGame=FALSE;
	m_GameStart=FALSE;
	MessageBox(_T("对方认输了,你赢了"));
	if(m_pWeiQi!=NULL)
	{
		delete m_pWeiQi;
		m_pWeiQi=new CWeiQi;
	}
	m_pView->m_GameNameText=_T("");
	m_pView->m_Player2Info=_T("");
	m_pView->UpdateData(FALSE);
	m_pView->m_DrawPoint=0;
	m_pView->m_PointX=0;
	m_pView->m_PointY=0;
	m_pView->Invalidate();
}

⌨️ 快捷键说明

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