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

📄 dartscoredlg.cpp

📁 ◆◆◆ 《投掷飞镖记分工具》◆◆◆ 操作系统 : Windows Mobile 5.0 说明 : 中秋节和家人赏月
💻 CPP
字号:
// DartScoreDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DartScore.h"
#include "DartScoreDlg.h"
#include "DlgGameParameter.h"
#include "DlgGameFileList.h"
#include "DlgAGameScore.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CDartScoreDlg dialog

CDartScoreDlg::CDartScoreDlg(CWnd* pParent /*=NULL*/)
	: CDialogWM(CDartScoreDlg::IDD, IDR_MENU_MAIN, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CDartScoreDlg::~CDartScoreDlg ()
{
	DeletePtrAry_APlayerData ();
}

void CDartScoreDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogWM::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CDartScoreDlg, CDialogWM)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
ON_WM_PAINT()
ON_COMMAND(ID_GAME_QUIT, &CDartScoreDlg::OnFileQuit)
ON_COMMAND(ID_GAME_NEW, &CDartScoreDlg::OnGameNew)
ON_COMMAND(ID_GAME_OPEN, &CDartScoreDlg::OnGameOpen)
ON_COMMAND(ID_GAME_SAVE, &CDartScoreDlg::OnGameSave)
ON_COMMAND(ID_GAME_SCORE, &CDartScoreDlg::OnGameScore)
ON_WM_DESTROY()
ON_COMMAND(ID_TOOLS_HELP, &CDartScoreDlg::OnToolsHelp)
ON_COMMAND(ID_TOOLS_ABOUT, &CDartScoreDlg::OnToolsAbout)
END_MESSAGE_MAP()


// CDartScoreDlg message handlers

BOOL CDartScoreDlg::OnInitDialog()
{
	if ( !CDialogWM::OnInitDialog() )
		return FALSE;

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	{	//d
	}	//d
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CDartScoreDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	DRA::RelayoutDialog(
		AfxGetInstanceHandle(), 
		this->m_hWnd, 
		DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_DialogMain_WIDE) : 
			MAKEINTRESOURCE(IDD_DialogMain));
}
#endif

void CDartScoreDlg::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	
	CRect rcClient;
	GetClientRect ( &rcClient );
	if ( !m_bmpMainUI.GetSafeHandle() )
	{
		CString csBmpFileName;
		csBmpFileName.Format ( L"%simg\\MainUI.bmp", g_CurDir );
		LoadBitmapFromFile ( m_bmpMainUI, csBmpFileName );
	}
	if ( m_bmpMainUI.GetSafeHandle() )
	{
		DrawBitmap ( m_bmpMainUI, &dc, rcClient, TRUE );
	}
}

void CDartScoreDlg::OnFileQuit()
{
	OnCancel ();
}

void CDartScoreDlg::OnGameNew()
{
	if ( !g_csCurGameDataFile.IsEmpty() )
	{
		if ( AfxMessageBox ( FormatString(_T("Would you want to close current game and create new game ?") ), MB_ICONQUESTION|MB_YESNO ) != IDYES )
			return;
	}

	CDlgGameParameter dlg;
	if ( dlg.DoModal() != IDOK )
		return;
	g_AGameData.nCycleTimes = dlg.m_nCycleTimes;
	g_AGameData.nDartNum = dlg.m_nDartNum;
	g_AGameData.bGameIsOver = FALSE;

	DeletePtrAry_APlayerData ();
	g_csCurGameDataFile.Empty ();

	for ( int i=0; i<dlg.m_StrAry_PlayersName.GetSize(); i++ )
	{
		CAPlayerData *pAPlayerData = new CAPlayerData;
		if ( !pAPlayerData ) continue;

		if ( !pAPlayerData->SetAGameData ( &g_AGameData, dlg.m_StrAry_PlayersName.GetAt(i) ) )
		{
			delete pAPlayerData;
			continue;
		}
		m_PtrAry_APlayerData.Add ( pAPlayerData );
	}

	g_csCurGameDataFile.Format ( _T("%s%s.gam"), g_CurDir, ConvertTimeToStr( GetCurrentTime(), 12 ) );
	SetCurGameDataFile ( g_csCurGameDataFile );

	if ( !SaveCurGameData () )
	{
		AfxMessageBox ( FormatString ( _T("Save game data to [%s] failed"), g_csCurGameDataFile ) );
	}
}

void CDartScoreDlg::DeletePtrAry_APlayerData()
{
	for ( int i=0; i<m_PtrAry_APlayerData.GetSize(); i++ )
	{
		CAPlayerData *pAPlayerData = (CAPlayerData*)m_PtrAry_APlayerData.GetAt(i);
		if ( pAPlayerData ) delete pAPlayerData;
	}
	m_PtrAry_APlayerData.RemoveAll ();
}

//
// 将当前游戏数据保存到文件中
//
BOOL CDartScoreDlg::SaveCurGameData ()
{
	if ( g_csCurGameDataFile.IsEmpty() ) return TRUE;
	CString csSaveDataString;
	csSaveDataString += ::GenerateSaveDataString_AGameData ( &g_AGameData );

	for ( int i=0; i<m_PtrAry_APlayerData.GetSize(); i++ )
	{
		CAPlayerData *pAPlayerData = (CAPlayerData*)m_PtrAry_APlayerData.GetAt(i);
		if ( pAPlayerData )
		{
			csSaveDataString += pAPlayerData->GenerateSaveDataString ();
		}
	}
	if ( g_csCurGameDataFile.IsEmpty() ) return FALSE;

	int nDataSize = csSaveDataString.GetLength()*sizeof(TCHAR);
	return ( ::WriteDataToFile ( g_csCurGameDataFile, csSaveDataString.GetBuffer(), nDataSize, _T("wb") ) == nDataSize );
}

//
// 从文件中读取游戏数据
//
BOOL CDartScoreDlg::ReadGameDataFromFile ()
{
	if ( g_csCurGameDataFile.IsEmpty() ) return FALSE;
	int nFileSize = hwGetFileAttr ( g_csCurGameDataFile );
	nFileSize = MIN(100*1024, nFileSize);	// 不能超过100K
	if ( nFileSize < 2 ) return FALSE;
	BYTE *Buffer = new BYTE[nFileSize+sizeof(TCHAR)];
	if ( !Buffer ) return FALSE;
	memset ( Buffer, 0, nFileSize+sizeof(TCHAR) );
	if ( ReadDataFromFile ( g_csCurGameDataFile, Buffer, nFileSize ) != nFileSize )
	{
		delete[] Buffer;
		return FALSE;
	}

	BOOL bOrgIsUncode = ( Buffer[1] == 0 ); // 是UNICODE编码
	CString csFileData = GetCompatibleString ( (LPCTSTR)Buffer, bOrgIsUncode );
	delete[] Buffer;
	CString csOneLine, csWord1, csWord2;

	// 获取 g_AGameData 数据
	while ( TRUE )
	{
		csOneLine = GetOneLine ( csFileData, _T("\r\n"), TRUE );
		if ( csOneLine.IsEmpty() ) break;
		if ( csOneLine.Find ( _T("// "), 0 ) >= 0 )
			continue;
		if ( !PartTwoWord ( csOneLine, csWord1, csWord2, _T('=') ) )
			return FALSE;
		csWord1.MakeLower ();
		if ( csWord1.CompareNoCase(_T("nCycleTimes"))==0 ) g_AGameData.nCycleTimes = atoi(GetMultiByteChar(csWord2));
		else if ( csWord1.CompareNoCase(_T("nDartNum"))==0 ) g_AGameData.nDartNum = atoi(GetMultiByteChar(csWord2));
		else if ( csWord1.CompareNoCase(_T("bGameIsOver"))==0 ) g_AGameData.bGameIsOver = (BOOL)atoi(GetMultiByteChar(csWord2));
	}

	// 获取玩家数据
	DeletePtrAry_APlayerData ();
	CAPlayerData *pAPlayerData = NULL;
	while ( !csFileData.IsEmpty() )
	{
		csOneLine = GetOneLine ( csFileData, _T("\r\n"), TRUE );
		if ( csOneLine.Find ( _T("// "), 0 ) >= 0 )
		{
			int nRightPos = csOneLine.Find ( _T(" ===================="), 0 );
			CString csPlayerName = csOneLine.Mid ( 3, nRightPos-3 );
			pAPlayerData = new CAPlayerData;
			if ( !pAPlayerData ) return FALSE;
			if ( !pAPlayerData->SetAGameData ( &g_AGameData, csPlayerName ) )
			{
				delete pAPlayerData;
				return FALSE;
			}
			m_PtrAry_APlayerData.Add ( pAPlayerData );
			continue;
		}
		if ( csOneLine.IsEmpty () ) continue;
		if ( !PartTwoWord ( csOneLine, csWord1, csWord2, _T('=') ) )
			return FALSE;
		int nIndex = atoi(GetMultiByteChar(csWord1));
		if ( nIndex < 0 || nIndex >= g_AGameData.nCycleTimes )
			return FALSE;
		CStringArray StrAry;
		PartStringAndAddToStrAry ( csWord2, StrAry, _T(',') );
		if ( StrAry.GetSize() != g_AGameData.nDartNum )
			return FALSE;
		for ( int i=0; i<g_AGameData.nDartNum; i++ )
		{
			pAPlayerData->m_pUIntAry_Scores[i].Add ( atoi(GetMultiByteChar(StrAry.GetAt(i))) );
		}
	}

	return TRUE;
}

void CDartScoreDlg::OnGameOpen()
{
	CDlgGameFileList dlg;
	if ( dlg.DoModal () != IDOK ) return;
	if ( dlg.m_csFileName.IsEmpty () ) return;
	if ( !g_csCurGameDataFile.IsEmpty() )
	{
		if ( AfxMessageBox ( FormatString(_T("Would you want to close current game and open [%s] game ?"), dlg.m_csFileName), MB_ICONQUESTION|MB_YESNO ) != IDYES )
			return;
	}
	SaveCurGameData ();
	g_csCurGameDataFile.Format ( _T("%s%s"), g_CurDir, dlg.m_csFileName );
	SetCurGameDataFile ( g_csCurGameDataFile );
	if ( ReadGameDataFromFile () )
	{
		AfxMessageBox ( _T("Open game data successfully"), MB_ICONINFORMATION );
	}
	else
	{
		AfxMessageBox ( _T("Open game data failed") );
	}
}

void CDartScoreDlg::OnGameSave()
{
	if ( SaveCurGameData () )
	{
		AfxMessageBox ( FormatString ( _T("Save game data to [%s] successfully"), g_csCurGameDataFile ), MB_ICONINFORMATION );
	}
	else
	{
		AfxMessageBox ( FormatString ( _T("Save game data to [%s] failed"), g_csCurGameDataFile ) );
	}
}

void CDartScoreDlg::OnGameScore()
{
	if ( g_csCurGameDataFile.IsEmpty() || g_AGameData.bGameIsOver || g_AGameData.nCycleTimes < 1 || g_AGameData.nDartNum < 1 )
	{
		PostMessage ( WM_COMMAND, ID_GAME_NEW );
		return;
	}

	CDlgAGameScore dlg;
	dlg.m_pPtrAry_APlayerData = &m_PtrAry_APlayerData;
	dlg.DoModal ();
}

void CDartScoreDlg::SetCurGameDataFile ( LPCTSTR lpszCurGameDataFile )
{
	g_csCurGameDataFile = GET_SAFE_STRING ( lpszCurGameDataFile );
	TCHAR szOnlyFileName[MAX_PATH] = {0};
	PartFileAndPathByFullPath ( g_csCurGameDataFile, szOnlyFileName, sizeof(szOnlyFileName) );
	SetWindowText ( szOnlyFileName );
}

void CDartScoreDlg::CalcTotalScore ( CUIntArray &UIntAry )
{
	for ( int nPlayer=0; nPlayer<m_PtrAry_APlayerData.GetSize(); nPlayer++ )
	{
		UIntAry.Add ( 0 );
	}

	BOOL bExitLoop = FALSE;
	for ( int nCycle=0; ; nCycle++ )
	{
		for ( int nPlayer=0; nPlayer<m_PtrAry_APlayerData.GetSize(); nPlayer++ )
		{
			CAPlayerData *pAPlayerData = (CAPlayerData*)m_PtrAry_APlayerData.GetAt(nPlayer);
			if ( !pAPlayerData ) continue;
			UINT nDartScoreTotal = 0;
			for ( int nDart=0; nDart<g_AGameData.nDartNum; nDart++ )
			{
				if ( nCycle >= pAPlayerData->m_pUIntAry_Scores[nDart].GetSize() )
				{
					bExitLoop = TRUE;
					break;
				}
				nDartScoreTotal += pAPlayerData->m_pUIntAry_Scores[nDart].GetAt(nCycle);
			}
			if ( bExitLoop ) break;
			
			UIntAry.SetAt ( nPlayer, UIntAry.GetAt(nPlayer) + nDartScoreTotal );
		}
		if ( bExitLoop ) break;
	}
}
void CDartScoreDlg::OnDestroy()
{
	CDialogWM::OnDestroy();

	if ( !SaveCurGameData () )
	{
		AfxMessageBox ( FormatString ( _T("Save game data to [%s] failed"), g_csCurGameDataFile ) );
	}
	AfxMessageBox ( NOTE_DartScore, MB_ICONINFORMATION );
}

void CDartScoreDlg::OnToolsHelp()
{
	CString csAbout = _T("这是一个投掷飞镖纪录成绩的小工具,在游戏开始前,先[新建]一个游戏纪录,然后选择[记分]菜单对每位玩家的飞镖成绩进行纪录,该软件将自动计算成绩并实时显示排名情况,\
所有数据自动保存到本地,可通过[打开]菜单来查看以往的分数信息。");
	AfxMessageBox ( csAbout, MB_ICONINFORMATION );
}

void CDartScoreDlg::OnToolsAbout()
{
	AfxMessageBox ( NOTE_DartScore, MB_ICONINFORMATION );
	CString csAbout = _T("\r\n\r\n\tDart score tool\r\n\tAuthor:Chrys\r\n\tVer:1.0.0");
	AfxMessageBox ( csAbout, MB_ICONINFORMATION );
}

⌨️ 快捷键说明

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