ServerMgmtDlg.cpp
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:10k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   ServerMgmtDlg.cpp                                                            
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Implements the server management dialog.
  11. *                       Displays the host game instance with details of 
  12. *                       players connected to session, and enables the host to
  13. *                       disconnect the players.
  14. *                       
  15. *                                                                             
  16. *   Authors: Eran Yariv - 28484475                                           
  17. *            Moshe Zur  - 24070856                                           
  18. *                                                                            
  19. *                                                                            
  20. *   Date: 23/09/98                                                           
  21. *                                                                            
  22. ******************************************************************************/
  23. // servermgmtdlg.cpp : implementation file
  24. //
  25. #include "stdafx.h"
  26. #include <Tanks.h>
  27. #include <ServerMgmtDlg.h>
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33. #define MAKE_PLAYER_DATA(id,judge)          (DWORD((id) + ((judge) << 31)))
  34. #define GET_PLAYER_ID(data)                 ((data) & (0x7FFFFFFF))
  35. #define IS_JUDGE(data)                      (BOOL((data) >> 31))
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CServerMgmtDlg dialog
  38. CServerMgmtDlg::CServerMgmtDlg(CWnd* pParent /*=NULL*/)
  39. : CDialog(CServerMgmtDlg::IDD, pParent),
  40.       m_gCommManager (TANKS_APP->m_gCommManager)
  41. {
  42. //{{AFX_DATA_INIT(CServerMgmtDlg)
  43. // NOTE: the ClassWizard will add member initialization here
  44. //}}AFX_DATA_INIT
  45. }
  46. void CServerMgmtDlg::DoDataExchange(CDataExchange* pDX)
  47. {
  48. CDialog::DoDataExchange(pDX);
  49. //{{AFX_DATA_MAP(CServerMgmtDlg)
  50. DDX_Control(pDX, IDC_PLAYERS_LIST, m_PlayersList);
  51. //}}AFX_DATA_MAP
  52. }
  53. BEGIN_MESSAGE_MAP(CServerMgmtDlg, CDialog)
  54. //{{AFX_MSG_MAP(CServerMgmtDlg)
  55. ON_BN_CLICKED(IDC_DICONNECT, OnDisconnectPlayer)
  56. ON_WM_CLOSE()
  57. ON_WM_TIMER()
  58. //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60. CServerMgmtDlg * CServerMgmtDlg::m_pDlg = NULL;
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CServerMgmtDlg message handlers
  63. #define TIMER_ID            55
  64. #define REFRESH_RATE      1000
  65. void CServerMgmtDlg::OnClose() 
  66. {
  67.     KillTimer (TIMER_ID);
  68. CDialog::OnClose();
  69. }
  70. void 
  71. CServerMgmtDlg::PostNcDestroy( )
  72. {
  73.     Close ();
  74. }
  75. void 
  76. CServerMgmtDlg::OnCancel ()
  77. {
  78.     DestroyWindow ();
  79. }
  80. int CServerMgmtDlg::GetCurSelection()
  81. {
  82.     return m_PlayersList.GetNextItem (-1, LVNI_SELECTED);
  83. }
  84. BOOL CServerMgmtDlg::OnInitDialog() 
  85. {
  86. CDialog::OnInitDialog();
  87.     m_ImgList.Create (16,16,FALSE,0,100);
  88.     m_PlayersList.SetImageList (&m_ImgList, LVSIL_SMALL);
  89.     HINSTANCE hinst = AfxGetInstanceHandle();
  90.     for (int i=0; i<MAX_TANKS; i++)
  91.     {
  92.         HICON hIcon= LoadIcon (hinst, MAKEINTRESOURCE(IDI_TANK1 + i));
  93.         m_ImgIndex[i] = m_ImgList.Add (hIcon);
  94.     }
  95.     m_PlayersList.InsertColumn (0,"Player name", LVCFMT_LEFT, 140);
  96.     m_PlayersList.InsertColumn (1,"Duration", LVCFMT_LEFT, 55);
  97.     m_PlayersList.InsertColumn (2,"Round trip time", LVCFMT_LEFT, 90);
  98.     m_PlayersList.SetBkColor (RGB(0,0,0));
  99.     m_PlayersList.SetTextColor (RGB(255,255,255));
  100.     m_PlayersList.SetTextBkColor (RGB(0,0,0));
  101.     m_PlayersList.SendMessage (LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
  102. SetTimer  (TIMER_ID, REFRESH_RATE, NULL);
  103.     RefreshList (); // Do 1st refresh
  104. return TRUE;  // return TRUE unless you set the focus to a control
  105.               // EXCEPTION: OCX Property Pages should return FALSE
  106. }
  107. void CServerMgmtDlg::OnTimer(UINT nIDEvent) 
  108. {
  109.     RefreshList ();
  110. CDialog::OnTimer(nIDEvent);
  111. }
  112. void 
  113. CServerMgmtDlg::Open (CWnd *pParent)
  114. {   // This function is static !!!!
  115.     if (NULL != m_pDlg)
  116.         return; // Already open
  117.     m_pDlg = new CServerMgmtDlg (pParent);
  118.     ASSERT (m_pDlg);
  119.     m_pDlg->Create (IDD_SERVER_MGMT, pParent);
  120.     m_pDlg->ShowWindow (SW_SHOW);
  121.     m_pDlg->SetFocus ();
  122. }
  123. void
  124. CServerMgmtDlg::Close ()
  125. {   // This function is static !!!!
  126.     if (NULL == m_pDlg)
  127.         return; // Already closed
  128.     delete m_pDlg; // Tricky - delete only after call to PostNcDestroy !!!
  129.     m_pDlg = NULL;
  130. }
  131. void 
  132. CServerMgmtDlg::UpdateOrAddPlayersData (PBOOL pbPlayers)
  133. {
  134.     CString cstrDuration,
  135.             cstrLatency,
  136.             cstrPlayerName;
  137.     int index;
  138.     for (int i=0; i<MAX_TANKS; i++)
  139.     {
  140.         DWORD   dwDuration;
  141.         DWORD   dwLatency;
  142.         BOOL    bIsJudge;
  143.         
  144.         if (m_gCommManager.GetPlayerInfo (i,
  145.                                           bIsJudge,
  146.                                           cstrPlayerName,
  147.                                           dwDuration,   // In millisecs
  148.                                           dwLatency))
  149.         { // Player i exists
  150.             pbPlayers[i] = TRUE;
  151.             dwDuration /= 1000; // Convert to secs
  152.             cstrDuration.Format ("%02d:%02d", dwDuration / 60, dwDuration % 60);
  153.             cstrLatency.Format ("%4d millisecs", dwLatency);
  154.             index = FindPlayerIndex (i);    // Find player's i index in the list
  155.             DWORD dwItemData = MAKE_PLAYER_DATA(i, bIsJudge);
  156.             if (-1 == index)
  157.             {   // Player is new to the list
  158.                 index = m_PlayersList.InsertItem (LVIF_TEXT | LVIF_IMAGE, 
  159.                                                   i, 
  160.                                                   cstrPlayerName,
  161.                                                   0,
  162.                                                   0,
  163.                                                   m_ImgIndex[i],
  164.                                                   dwItemData);
  165.                 m_PlayersList.SetItemData (index, dwItemData);
  166.             }
  167.             m_PlayersList.SetItem (index, 
  168.                                    1, 
  169.                                    LVIF_TEXT, 
  170.                                    cstrDuration,
  171.                                    0,
  172.                                    0,
  173.                                    0,
  174.                                    0);
  175.             m_PlayersList.SetItem (index, 
  176.                                    2, 
  177.                                    LVIF_TEXT, 
  178.                                    cstrLatency,
  179.                                    0,
  180.                                    0,
  181.                                    0,
  182.                                    0);
  183.         }
  184.     }
  185. }
  186. void 
  187. CServerMgmtDlg::RemoveNonExistantPlayers (PBOOL pbPlayers)
  188. {
  189.     int index = -1;
  190.     BOOL bStopScan = FALSE;
  191.     while (!bStopScan)
  192.     {
  193.         index = m_PlayersList.GetNextItem(index, 0);
  194.         if (-1 != index)
  195.         {
  196.             DWORD dwPlayerData = m_PlayersList.GetItemData(index);
  197.             DWORD dwPlayerID = GET_PLAYER_ID(dwPlayerData);
  198.             ASSERT (dwPlayerID < MAX_TANKS);
  199.             if (!pbPlayers[dwPlayerID])
  200.             {   // Found non-existing player in the list ,remove it
  201.                 m_PlayersList.DeleteItem (index);
  202.                 index = -1; // Start scanning from list head
  203.             }
  204.         }
  205.         else
  206.         {   // End of list
  207.             bStopScan = TRUE;
  208.         }
  209.     }
  210. }
  211. void 
  212. CServerMgmtDlg::RefreshList()
  213. {
  214.     m_PlayersList.SetRedraw (FALSE);
  215.     BOOL abPlayerExists[MAX_TANKS];
  216.     memset (abPlayerExists, 0, sizeof (abPlayerExists));
  217.     UpdateOrAddPlayersData (abPlayerExists);
  218.     RemoveNonExistantPlayers (abPlayerExists);
  219.     m_PlayersList.SetRedraw (TRUE);
  220. }
  221. void CServerMgmtDlg::OnDisconnectPlayer() 
  222. {
  223.     int i = GetCurSelection ();
  224.     if (-1 == i)
  225.     {
  226.         AfxMessageBox (IDS_MUST_SELECT_PLAYER_TO_REMOVE, MB_OK | MB_ICONINFORMATION);
  227.         return; // No item selected
  228.     }
  229.     DWORD dwPlayerData = m_PlayersList.GetItemData (i);
  230.     BOOL bIsJudge = IS_JUDGE (dwPlayerData);
  231.     if (bIsJudge)
  232.     {   // User tried to kill the judge - Never allow this
  233.         AfxMessageBox (IDS_CANT_KILL_JUDGE, MB_OK | MB_ICONINFORMATION);
  234.         return; // No item selected
  235.     }
  236.     DWORD dwPlayerID = GET_PLAYER_ID (dwPlayerData);
  237.     ASSERT (dwPlayerID < MAX_TANKS);
  238.     // Now, ask the host to kill the tank (dwPlayerID)
  239. if (!m_gCommManager.KillPlayer (dwPlayerID))
  240.     {   // Can't kill - either already dead or bad ID
  241.         AfxMessageBox (IDS_CANT_KILL_PLAYER, MB_OK | MB_ICONINFORMATION);
  242.         return; // No item selected
  243.     }
  244. }
  245. int CServerMgmtDlg::FindPlayerIndex (UINT uPlayerID)
  246. {
  247.     for (int index=-1; ;)
  248.     {
  249.         index = m_PlayersList.GetNextItem(index, 0);
  250.         if (-1 == index)
  251.             // end of list - return not found 
  252.             return -1;
  253.         DWORD dwPlayerData = m_PlayersList.GetItemData(index);
  254.         DWORD dwPlayerID = GET_PLAYER_ID(dwPlayerData);
  255.         if (uPlayerID == dwPlayerID)
  256.             return index;
  257.     }
  258. }
  259.