www.pudn.com > renju_src.rar > ClientSocket.cpp


// ClientSocket.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Renju.h" 
#include "RenjuDoc.h" 
#include "RenjuView.h" 
#include "ClientSocket.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CClientSocket 
 
CClientSocket::CClientSocket(CRenjuView* pView) 
{ 
	m_pView = pView; 
} 
 
CClientSocket::~CClientSocket() 
{ 
} 
 
 
// Do not edit the following lines, which are needed by ClassWizard. 
#if 0 
BEGIN_MESSAGE_MAP(CClientSocket, CSocket) 
	//{{AFX_MSG_MAP(CClientSocket) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
#endif	// 0 
 
///////////////////////////////////////////////////////////////////////////// 
// CClientSocket member functions 
 
void CClientSocket::OnReceive(int nErrorCode)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	int buf[3]; 
	Receive(buf, 3 * sizeof(int)); 
 
	// get a newgame request 
	if(buf[0] == NEWGAME) 
	{ 
		m_pView->SendMessage(WM_USER_NEWGAME); 
	} 
	// partner refused the newgame request 
	else if(buf[0] == NONEWGAME) 
	{ 
		m_pView->GetParent()->SetWindowText(_T("Renju - Game over")); 
		AfxMessageBox(_T("Your partner doesn't want to play a new game now")); 
	} 
	// partner disconnect the link to you 
	else if(buf[0] == DISCONNECT) 
	{ 
		m_pView->m_bLinked = FALSE; 
		m_pView->m_bInGame = FALSE; 
		m_pView->DestroyClientSocket(); 
		m_pView->ResetCoords(); 
		m_pView->GetParent()->SetWindowText(_T("Renju - Ready")); 
		AfxMessageBox(_T("Your partner disconnected!")); 
	} 
	// partner accept the connection request or the newgame request 
	else if(buf[0] == ACCEPT) 
	{ 
		m_pView->m_bMyTurn = FALSE; 
		m_pView->m_bLinked = TRUE; 
		m_pView->m_bOpponentWin = FALSE; 
		m_pView->m_bInGame = TRUE; 
		m_pView->ResetCoords(); 
		m_pView->m_nColor = -1; 
		m_pView->GetParent()->SetWindowText(_T("Renju - Opponent's turn")); 
	} 
	// partner refused the connection request 
	else if(buf[0] == DECLINE) 
	{ 
		AfxMessageBox(_T("Your partner cannot play with you right now!")); 
		m_pView->GetParent()->SetWindowText(_T("Renju - Ready")); 
		m_pView->DestroyClientSocket(); 
	} 
	// partner surrender to you 
	else if(buf[0] == SURRENDER) 
	{ 
		m_pView->m_bMyTurn = FALSE; 
		m_pView->m_bInGame = FALSE; 
		m_pView->GetParent()->SetWindowText(_T("Renju - Game over")); 
		AfxMessageBox(_T("Your partner surrendered to you.\n" 
						"You won this game!")); 
	} 
	// other messages 
	else 
	{ 
		m_pView->OneStep(buf[1], buf[2]); 
		// even game and game over 
		if(buf[0] == EVEN) 
		{ 
			m_pView->m_bMyTurn = FALSE; 
			m_pView->m_bInGame = FALSE; 
			m_pView->GetParent()->SetWindowText(_T("Renju - Game over")); 
			AfxMessageBox(_T("Even game!")); 
		} 
		// I lost this game 
		else if(buf[0] == ULOSE) 
		{ 
			m_pView->m_bMyTurn = FALSE; 
			m_pView->m_bInGame = FALSE; 
			m_pView->GetParent()->SetWindowText(_T("Renju - Game over")); 
			AfxMessageBox(_T("Sorry!\n" 
							"You lost this game.")); 
		} 
		// I won this game 
		else if(buf[0] == UWIN) 
		{ 
			m_pView->m_bMyTurn = FALSE; 
			m_pView->m_bInGame = FALSE; 
			m_pView->GetParent()->SetWindowText(_T("Renju - Game over")); 
			AfxMessageBox(_T("Congratulations!\n" 
							"You won this game.")); 
		} 
		// opponent won temporarily, waiting for my last step 
		else if(buf[0] == IWIN) 
		{ 
			m_pView->m_bMyTurn = TRUE; 
			m_pView->m_bOpponentWin = TRUE; 
			m_pView->GetParent()->SetWindowText(_T("Renju - Your turn")); 
		} 
		// just an ordinary step 
		else if(buf[0] == COORDS) 
		{ 
			m_pView->m_bMyTurn = TRUE; 
			m_pView->GetParent()->SetWindowText(_T("Renju - Your turn")); 
		} 
	} 
	 
	CSocket::OnReceive(nErrorCode); 
}