CGameHallFrameView.cpp
上传用户:bsw_2008
上传日期:2013-07-09
资源大小:2446k
文件大小:9k
源码类别:

棋牌游戏

开发平台:

Visual C++

  1. // CGameHallFrameView.cpp : implementation of the CCGameHallFrameView class
  2. //
  3. #include "stdafx.h"
  4. #include "CGameHallFrame.h"
  5. #include "winsock.h"
  6. #include "CGameHallFrameDoc.h"
  7. #include "CGameHallFrameView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CCGameHallFrameView
  15. IMPLEMENT_DYNCREATE(CCGameHallFrameView, CFormView)
  16. BEGIN_MESSAGE_MAP(CCGameHallFrameView, CFormView)
  17. //{{AFX_MSG_MAP(CCGameHallFrameView)
  18. ON_NOTIFY(NM_DBLCLK, IDC_SERVERTREE, OnDblclkServertree)
  19. ON_WM_SIZE()
  20. //}}AFX_MSG_MAP
  21. // Standard printing commands
  22. ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
  25. ON_MESSAGE(CLI_MESSAGE,OnClientMessage)
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CCGameHallFrameView construction/destruction
  29. CCGameHallFrameView::CCGameHallFrameView()
  30. : CFormView(CCGameHallFrameView::IDD)
  31. {
  32. //{{AFX_DATA_INIT(CCGameHallFrameView)
  33. // NOTE: the ClassWizard will add member initialization here
  34. //}}AFX_DATA_INIT
  35. // TODO: add construction code here
  36. }
  37. CCGameHallFrameView::~CCGameHallFrameView()
  38. {
  39. }
  40. void CCGameHallFrameView::DoDataExchange(CDataExchange* pDX)
  41. {
  42. CFormView::DoDataExchange(pDX);
  43. //{{AFX_DATA_MAP(CCGameHallFrameView)
  44. DDX_Control(pDX, IDC_SERVERTREE, m_wndTree);
  45. //}}AFX_DATA_MAP
  46. }
  47. BOOL CCGameHallFrameView::PreCreateWindow(CREATESTRUCT& cs)
  48. {
  49. // TODO: Modify the Window class or styles here by modifying
  50. //  the CREATESTRUCT cs
  51. return CFormView::PreCreateWindow(cs);
  52. }
  53. void CCGameHallFrameView::OnInitialUpdate()
  54. {
  55. CFormView::OnInitialUpdate();
  56. GetParentFrame()->RecalcLayout();
  57. ResizeParentToFit();
  58. m_imglDrives.Create(IDB_GAMEHALL, 16, 1, RGB (255, 0, 255));
  59. m_wndTree.SetImageList(&m_imglDrives,TVSIL_NORMAL);
  60. HTREEITEM hItem=m_wndTree.InsertItem("在线游戏系列",0,0);
  61. //m_wndTree.SetImageList(&m_img,TVSIL_NORMAL);
  62. m_wndTree.InsertItem("四国军棋",1,1,hItem);
  63. //network
  64. WSADATA wsaData;
  65. WORD version = MAKEWORD(2, 0);
  66. int ret = WSAStartup(version, &wsaData);
  67. if(ret != 0)
  68. {
  69. TRACE("Initilize Error!n");  //初始化失败
  70. AfxMessageBox("Failed in initial socket");
  71. }
  72. m_Init=FALSE;r=0;
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CCGameHallFrameView printing
  76. BOOL CCGameHallFrameView::OnPreparePrinting(CPrintInfo* pInfo)
  77. {
  78. // default preparation
  79. return DoPreparePrinting(pInfo);
  80. }
  81. void CCGameHallFrameView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  82. {
  83. // TODO: add extra initialization before printing
  84. }
  85. void CCGameHallFrameView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  86. {
  87. // TODO: add cleanup after printing
  88. }
  89. void CCGameHallFrameView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
  90. {
  91. // TODO: add customized printing code here
  92. }
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CCGameHallFrameView diagnostics
  95. #ifdef _DEBUG
  96. void CCGameHallFrameView::AssertValid() const
  97. {
  98. CFormView::AssertValid();
  99. }
  100. void CCGameHallFrameView::Dump(CDumpContext& dc) const
  101. {
  102. CFormView::Dump(dc);
  103. }
  104. CCGameHallFrameDoc* CCGameHallFrameView::GetDocument() // non-debug version is inline
  105. {
  106. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCGameHallFrameDoc)));
  107. return (CCGameHallFrameDoc*)m_pDocument;
  108. }
  109. #endif //_DEBUG
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CCGameHallFrameView message handlers
  112. void CCGameHallFrameView::OnDblclkServertree(NMHDR* pNMHDR, LRESULT* pResult) 
  113. {
  114. // TODO: Add your control notification handler code here
  115. HTREEITEM item;
  116. item=m_wndTree.GetSelectedItem();
  117. CString s;
  118. s=m_wndTree.GetItemText(item);
  119. if(s=="四国军棋")
  120. {
  121. if(m_Init==FALSE)
  122. {
  123. login=new CLogin();
  124. if(login->DoModal()==IDOK)
  125. {
  126. name=login->m_name;
  127. code=login->m_code;
  128. ServerIP=login->m_ServerIP;
  129. if(name==""||code==""||ServerIP=="")
  130. MessageBox("每项内容都不能为空");
  131. else
  132. r=1;
  133. delete login;
  134. }else
  135. {
  136. register1=new CRegister1();
  137. if(register1->DoModal())
  138. {
  139. if(register1->m_code1==register1->m_code2)
  140. {
  141. code=register1->m_code1;
  142. sex=register1->m_sex;
  143. ServerIP=register1->m_IP;
  144. if(code==""||sex==""||ServerIP=="")
  145. MessageBox("每项内容都不能为空");
  146. else
  147. r=2;
  148. delete register1;
  149. }
  150. }
  151. }
  152. if(r==1||r==2)
  153. {
  154. if(ClientConnect()==TRUE)
  155. {
  156. //m_Init=TRUE;
  157. //pTable->flag=1;
  158. //pTable->DrawTable();
  159. pTable->pClient=&m_client;
  160. if(r==1)
  161. {
  162. ClientMessage('N');
  163. }else if(r==2)
  164. {
  165. ClientMessage('L');
  166. }
  167. }
  168. }
  169. }
  170. }
  171. *pResult = 0;
  172. }
  173. void CCGameHallFrameView::OnSize(UINT nType, int cx, int cy) 
  174. {
  175. CFormView::OnSize(nType, cx, cy);
  176. // TODO: Add your message handler code here
  177. if( m_wndTree.m_hWnd )
  178.     {
  179.         // Fill the whole Client Area of the View with the Control
  180.         m_wndTree.MoveWindow( 0, 0, cx, cy );
  181.     }
  182. }
  183. LRESULT CCGameHallFrameView::OnClientMessage(WPARAM wParam, LPARAM lParam)
  184. {
  185. char s[1024],k;
  186. int len;CString str;
  187. switch(lParam)
  188. {
  189. case FD_CONNECT:
  190. len=GetLastError();
  191. if(len!=0)
  192. {
  193. AfxMessageBox("Error in Connecting");
  194. }
  195. else
  196. {
  197. m_Init=TRUE;
  198. }
  199. return 0;
  200. case FD_READ:
  201. len=recv(m_client.m_hSocket,s,1024,0);
  202. s[len]=NULL;str=s;
  203. k=protocol.Anaysys(str);
  204. dealMessage(k);
  205. return 0;
  206. case FD_WRITE:
  207. return 0;
  208. case FD_CLOSE:
  209. return 0;
  210. default:
  211. MessageBox("服务器已关闭!");
  212. return 0;
  213. }
  214. }
  215. BOOL CCGameHallFrameView::ClientConnect()
  216. {
  217. if(m_client.InitAndConnect(m_hWnd,PORT,ServerIP)==FALSE)
  218. {
  219. return FALSE;
  220. }
  221. return TRUE;
  222. }
  223. char CH=char(20);
  224. void CCGameHallFrameView::ClientMessage(char k)
  225. {
  226. CString str;
  227. str=CH;
  228. str+=k;
  229. switch(k)
  230. {
  231. case 'L':
  232. str+=code;
  233. if(sex=="女")
  234. str+=" 2";
  235. else
  236. str+=" 1";
  237. str+=CH;
  238. //MessageBox(str);
  239. Sleep(100);
  240. m_client.SendString(str);
  241. break;
  242. case 'N':
  243. str+=name;str+=' ';str+=code;
  244. str+=CH;
  245. Sleep(100);
  246. m_client.SendString(str);
  247. break;
  248. }
  249. }
  250. void CCGameHallFrameView::dealMessage(char k)
  251. {
  252. CString s;int i;
  253. switch(k)
  254. {
  255. case 'M':
  256. name=protocol.name;
  257. s.Format("你的用户名是:%s",name);
  258. MessageBox(s);
  259. pTable->flag=1;
  260. pTable->DrawTable();
  261. break;
  262. case 'O':
  263. if(protocol.right==1)
  264. {
  265. for(int i=0;i<4;i++)
  266. for(int j=0;j<4;j++)
  267. {
  268. pTable->table[i][j].east=protocol.table[i][j].d[0];
  269. pTable->table[i][j].south=protocol.table[i][j].d[1];
  270. pTable->table[i][j].west=protocol.table[i][j].d[2];
  271. pTable->table[i][j].north=protocol.table[i][j].d[3];
  272. }
  273. pTable->flag=1;
  274.     pTable->DrawTable();
  275. }
  276. else
  277. {
  278. MessageBox("用户名或密码错误!");
  279. }
  280. break;
  281. case 'B':
  282. switch(protocol.direct)
  283. {
  284. case 0:
  285. pTable->table[protocol.desk/4][protocol.desk%4].east=1;
  286. break;
  287. case 1:
  288. pTable->table[protocol.desk/4][protocol.desk%4].south=1;
  289. break;
  290. case 2:
  291. pTable->table[protocol.desk/4][protocol.desk%4].west=1;
  292. break;
  293. case 3:
  294. pTable->table[protocol.desk/4][protocol.desk%4].north=1;
  295. break;
  296. default:
  297. break;
  298. }
  299. pTable->DrawTable();
  300. break;
  301. case 'P':
  302. switch(protocol.direct)
  303. {
  304. case 0:
  305. pTable->table[protocol.desk/4][protocol.desk%4].east=0;
  306. break;
  307. case 1:
  308. pTable->table[protocol.desk/4][protocol.desk%4].south=0;
  309. break;
  310. case 2:
  311. pTable->table[protocol.desk/4][protocol.desk%4].west=0;
  312. break;
  313. case 3:
  314. pTable->table[protocol.desk/4][protocol.desk%4].north=0;
  315. break;
  316. default:
  317. break;
  318. }
  319. pTable->dlg->user.ID[protocol.direct]=0;
  320. pTable->dlg->GameEnd(protocol.direct);
  321. pTable->DrawTable();
  322. break;
  323. case 'Q':
  324. for(i=0;i<4;i++)
  325. {
  326. pTable->dlg->user.ID[i]=protocol.user.ID[i];
  327. pTable->dlg->user.name[i]=protocol.user.name[i];
  328. pTable->dlg->user.score[i]=protocol.user.score[i];
  329. pTable->dlg->user.sex[i]=protocol.user.sex[i];
  330. }
  331. pTable->dlg->userMessage();
  332. break;
  333. case 'A':
  334. pTable->dlg->chatMessage(protocol.chat);
  335. break;
  336. case 'D':
  337. //pTable->table[pTable->dlg->desk/4][pTable->dlg->desk%4].begin=TRUE;
  338. pTable->dlg->game.war_kind=protocol.war_kind;
  339. pTable->dlg->right=protocol.r;
  340. pTable->dlg->GameBegin();
  341. break;
  342. case 'F'://吃
  343. pTable->dlg->right=protocol.r;
  344. pTable->dlg->PlayResult('F',protocol.from,protocol.to);
  345. break;
  346. case 'G'://被吃
  347. pTable->dlg->right=protocol.r;
  348. pTable->dlg->PlayResult('G',protocol.from,protocol.to);
  349. break;
  350. case 'R'://碰
  351. pTable->dlg->right=protocol.r;
  352. pTable->dlg->PlayResult('R',protocol.from,protocol.to);
  353. break;
  354. case 'H'://炸
  355. pTable->dlg->right=protocol.r;
  356. pTable->dlg->PlayResult('H',protocol.from,protocol.to);
  357. break;
  358. case 'I':
  359. pTable->dlg->GameEnd(protocol.direct);
  360. break;
  361. }
  362. }