CGameHallFrameView.cpp
上传用户:bsw_2008
上传日期:2013-07-09
资源大小:2446k
文件大小:9k
- // CGameHallFrameView.cpp : implementation of the CCGameHallFrameView class
- //
- #include "stdafx.h"
- #include "CGameHallFrame.h"
- #include "winsock.h"
- #include "CGameHallFrameDoc.h"
- #include "CGameHallFrameView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CCGameHallFrameView
- IMPLEMENT_DYNCREATE(CCGameHallFrameView, CFormView)
- BEGIN_MESSAGE_MAP(CCGameHallFrameView, CFormView)
- //{{AFX_MSG_MAP(CCGameHallFrameView)
- ON_NOTIFY(NM_DBLCLK, IDC_SERVERTREE, OnDblclkServertree)
- ON_WM_SIZE()
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
- ON_MESSAGE(CLI_MESSAGE,OnClientMessage)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCGameHallFrameView construction/destruction
- CCGameHallFrameView::CCGameHallFrameView()
- : CFormView(CCGameHallFrameView::IDD)
- {
- //{{AFX_DATA_INIT(CCGameHallFrameView)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // TODO: add construction code here
- }
- CCGameHallFrameView::~CCGameHallFrameView()
- {
- }
- void CCGameHallFrameView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CCGameHallFrameView)
- DDX_Control(pDX, IDC_SERVERTREE, m_wndTree);
- //}}AFX_DATA_MAP
- }
- BOOL CCGameHallFrameView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CFormView::PreCreateWindow(cs);
- }
- void CCGameHallFrameView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
- GetParentFrame()->RecalcLayout();
- ResizeParentToFit();
- m_imglDrives.Create(IDB_GAMEHALL, 16, 1, RGB (255, 0, 255));
- m_wndTree.SetImageList(&m_imglDrives,TVSIL_NORMAL);
- HTREEITEM hItem=m_wndTree.InsertItem("在线游戏系列",0,0);
- //m_wndTree.SetImageList(&m_img,TVSIL_NORMAL);
- m_wndTree.InsertItem("四国军棋",1,1,hItem);
- //network
- WSADATA wsaData;
- WORD version = MAKEWORD(2, 0);
- int ret = WSAStartup(version, &wsaData);
- if(ret != 0)
- {
- TRACE("Initilize Error!n"); //初始化失败
- AfxMessageBox("Failed in initial socket");
- }
- m_Init=FALSE;r=0;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CCGameHallFrameView printing
- BOOL CCGameHallFrameView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CCGameHallFrameView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CCGameHallFrameView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- void CCGameHallFrameView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
- {
- // TODO: add customized printing code here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CCGameHallFrameView diagnostics
- #ifdef _DEBUG
- void CCGameHallFrameView::AssertValid() const
- {
- CFormView::AssertValid();
- }
- void CCGameHallFrameView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
- CCGameHallFrameDoc* CCGameHallFrameView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCGameHallFrameDoc)));
- return (CCGameHallFrameDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CCGameHallFrameView message handlers
- void CCGameHallFrameView::OnDblclkServertree(NMHDR* pNMHDR, LRESULT* pResult)
- {
- // TODO: Add your control notification handler code here
- HTREEITEM item;
- item=m_wndTree.GetSelectedItem();
- CString s;
- s=m_wndTree.GetItemText(item);
- if(s=="四国军棋")
- {
- if(m_Init==FALSE)
- {
- login=new CLogin();
- if(login->DoModal()==IDOK)
- {
- name=login->m_name;
- code=login->m_code;
- ServerIP=login->m_ServerIP;
- if(name==""||code==""||ServerIP=="")
- MessageBox("每项内容都不能为空");
- else
- r=1;
- delete login;
- }else
- {
- register1=new CRegister1();
- if(register1->DoModal())
- {
- if(register1->m_code1==register1->m_code2)
- {
- code=register1->m_code1;
- sex=register1->m_sex;
- ServerIP=register1->m_IP;
- if(code==""||sex==""||ServerIP=="")
- MessageBox("每项内容都不能为空");
- else
- r=2;
- delete register1;
- }
- }
- }
- if(r==1||r==2)
- {
- if(ClientConnect()==TRUE)
- {
- //m_Init=TRUE;
- //pTable->flag=1;
- //pTable->DrawTable();
- pTable->pClient=&m_client;
- if(r==1)
- {
- ClientMessage('N');
- }else if(r==2)
- {
- ClientMessage('L');
- }
- }
- }
- }
- }
- *pResult = 0;
- }
- void CCGameHallFrameView::OnSize(UINT nType, int cx, int cy)
- {
- CFormView::OnSize(nType, cx, cy);
-
- // TODO: Add your message handler code here
- if( m_wndTree.m_hWnd )
- {
- // Fill the whole Client Area of the View with the Control
- m_wndTree.MoveWindow( 0, 0, cx, cy );
- }
- }
- LRESULT CCGameHallFrameView::OnClientMessage(WPARAM wParam, LPARAM lParam)
- {
- char s[1024],k;
- int len;CString str;
- switch(lParam)
- {
- case FD_CONNECT:
- len=GetLastError();
- if(len!=0)
- {
- AfxMessageBox("Error in Connecting");
- }
- else
- {
- m_Init=TRUE;
- }
- return 0;
- case FD_READ:
- len=recv(m_client.m_hSocket,s,1024,0);
- s[len]=NULL;str=s;
- k=protocol.Anaysys(str);
- dealMessage(k);
- return 0;
- case FD_WRITE:
- return 0;
- case FD_CLOSE:
- return 0;
- default:
- MessageBox("服务器已关闭!");
- return 0;
- }
- }
- BOOL CCGameHallFrameView::ClientConnect()
- {
- if(m_client.InitAndConnect(m_hWnd,PORT,ServerIP)==FALSE)
- {
- return FALSE;
- }
- return TRUE;
- }
- char CH=char(20);
- void CCGameHallFrameView::ClientMessage(char k)
- {
- CString str;
- str=CH;
- str+=k;
- switch(k)
- {
- case 'L':
- str+=code;
- if(sex=="女")
- str+=" 2";
- else
- str+=" 1";
- str+=CH;
- //MessageBox(str);
- Sleep(100);
- m_client.SendString(str);
- break;
- case 'N':
- str+=name;str+=' ';str+=code;
- str+=CH;
- Sleep(100);
- m_client.SendString(str);
- break;
- }
- }
- void CCGameHallFrameView::dealMessage(char k)
- {
- CString s;int i;
- switch(k)
- {
- case 'M':
- name=protocol.name;
- s.Format("你的用户名是:%s",name);
- MessageBox(s);
- pTable->flag=1;
- pTable->DrawTable();
- break;
- case 'O':
- if(protocol.right==1)
- {
- for(int i=0;i<4;i++)
- for(int j=0;j<4;j++)
- {
- pTable->table[i][j].east=protocol.table[i][j].d[0];
- pTable->table[i][j].south=protocol.table[i][j].d[1];
- pTable->table[i][j].west=protocol.table[i][j].d[2];
- pTable->table[i][j].north=protocol.table[i][j].d[3];
- }
- pTable->flag=1;
- pTable->DrawTable();
- }
- else
- {
- MessageBox("用户名或密码错误!");
- }
- break;
- case 'B':
- switch(protocol.direct)
- {
- case 0:
- pTable->table[protocol.desk/4][protocol.desk%4].east=1;
- break;
- case 1:
- pTable->table[protocol.desk/4][protocol.desk%4].south=1;
- break;
- case 2:
- pTable->table[protocol.desk/4][protocol.desk%4].west=1;
- break;
- case 3:
- pTable->table[protocol.desk/4][protocol.desk%4].north=1;
- break;
- default:
- break;
- }
- pTable->DrawTable();
- break;
- case 'P':
- switch(protocol.direct)
- {
- case 0:
- pTable->table[protocol.desk/4][protocol.desk%4].east=0;
- break;
- case 1:
- pTable->table[protocol.desk/4][protocol.desk%4].south=0;
- break;
- case 2:
- pTable->table[protocol.desk/4][protocol.desk%4].west=0;
- break;
- case 3:
- pTable->table[protocol.desk/4][protocol.desk%4].north=0;
- break;
- default:
- break;
- }
- pTable->dlg->user.ID[protocol.direct]=0;
- pTable->dlg->GameEnd(protocol.direct);
- pTable->DrawTable();
- break;
- case 'Q':
- for(i=0;i<4;i++)
- {
- pTable->dlg->user.ID[i]=protocol.user.ID[i];
- pTable->dlg->user.name[i]=protocol.user.name[i];
- pTable->dlg->user.score[i]=protocol.user.score[i];
- pTable->dlg->user.sex[i]=protocol.user.sex[i];
- }
- pTable->dlg->userMessage();
- break;
- case 'A':
- pTable->dlg->chatMessage(protocol.chat);
- break;
- case 'D':
- //pTable->table[pTable->dlg->desk/4][pTable->dlg->desk%4].begin=TRUE;
- pTable->dlg->game.war_kind=protocol.war_kind;
- pTable->dlg->right=protocol.r;
- pTable->dlg->GameBegin();
- break;
- case 'F'://吃
- pTable->dlg->right=protocol.r;
- pTable->dlg->PlayResult('F',protocol.from,protocol.to);
- break;
- case 'G'://被吃
- pTable->dlg->right=protocol.r;
- pTable->dlg->PlayResult('G',protocol.from,protocol.to);
- break;
- case 'R'://碰
- pTable->dlg->right=protocol.r;
- pTable->dlg->PlayResult('R',protocol.from,protocol.to);
- break;
- case 'H'://炸
- pTable->dlg->right=protocol.r;
- pTable->dlg->PlayResult('H',protocol.from,protocol.to);
- break;
- case 'I':
- pTable->dlg->GameEnd(protocol.direct);
- break;
- }
- }