/
avplat
/
Tetris-MFC
Обзор
Документация
Войти
/
avplat
/
Tetris-MFC
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
CRatingPage.cpp
153 строки
4 KB
Андрей Платонов
нумеруем игроков в таблице рейтингов
26 окт 2025, 19:51
26 окт 2025, 19:51
1256c81
Код
Авторство
О чём код?
/// @file /// @brief Вкладка с рейтингами игроков /// @copyright Copyright Андрей Платонов, 2025 #include "pch.h" #include <format> #include <string> #include <vector> #include "framework.h" #include "resource.h" #include "My_Tetris.h" #include "CRatingPage.h" #ifdef _DEBUG #define new DEBUG_NEW #endif const unsigned ratingsCount = 50; BEGIN_MESSAGE_MAP( CRatingPage, CDialogEx ) ON_WM_SIZE() END_MESSAGE_MAP() CRatingPage::CRatingPage( CWnd* pParent ) : CDialogEx( IDD_RATING_TAB, pParent ) { } void CRatingPage::UpdateRatings() { ASSERT( ratingList.GetSafeHwnd() != nullptr ); ratingList.DeleteAllItems(); RatingDb& db = theApp.GetRatingDb(); std::vector< RatingData > data; if ( db.GetRatings( ratingsCount, data ) ) { for ( std::size_t i = 0; i < data.size(); ++i ) { const RatingData& d = data[ i ]; CString n = std::format( _T( "{}" ), i + 1 ).c_str(); int res = ratingList.InsertItem( static_cast< int >( i ), n ); ASSERT( res != -1 ); BOOL res1 = ratingList.SetItemText( static_cast< int >( i ), 1, d.Name ); ASSERT( res1 != FALSE ); CString sc = std::format( _T( "{}" ), d.Score ).c_str(); res1 = ratingList.SetItemText( static_cast< int >( i ), 2, sc ); ASSERT( res1 != FALSE ); CString dur = std::format( _T( "{}" ), d.Dur.count() ).c_str(); res1 = ratingList.SetItemText( static_cast< int >( i ), 3, dur ); ASSERT( res1 != FALSE ); // @todo В функции current_zone ошибка, приводящая к утечке памяти. Позже можно можно вернуть этот вариант, когда она будет исправлена. // auto const localTime = std::chrono::current_zone()->to_local( d.Time ); // CString tm = std::format( _T( "{:%d.%m.%Y, %H:%M}" ), localTime ).c_str(); SYSTEMTIME localTime; unixTimeToSystemTime( std::chrono::system_clock::to_time_t( d.Time ), localTime ); CString tm = std::format( _T( "{:02}.{:02}.{:04}, {:02}:{:02}" ), localTime.wDay, localTime.wMonth, localTime.wYear, localTime.wHour, localTime.wMinute ).c_str(); res1 = ratingList.SetItemText( static_cast<int>(i), 4, tm ); ASSERT( res1 != FALSE ); } } } BOOL CRatingPage::OnInitDialog() { CDialogEx::OnInitDialog(); ASSERT( ratingList.GetSafeHwnd() != nullptr ); int res = ratingList.InsertColumn( 0, _T( "#" ), LVCFMT_RIGHT, 40 ); ASSERT( res != -1 ); res = ratingList.InsertColumn( 1, _T( "Имя игрока" ), LVCFMT_LEFT, 200 ); ASSERT( res != -1 ); res = ratingList.InsertColumn( 2, _T( "Счет" ), LVCFMT_RIGHT, 200 ); ASSERT( res != -1 ); res = ratingList.InsertColumn( 3, _T( "Продолжительность" ), LVCFMT_RIGHT, 200 ); ASSERT( res != -1 ); res = ratingList.InsertColumn( 4, _T( "Время" ), LVCFMT_RIGHT, 200 ); ASSERT( res != -1 ); return TRUE; } void CRatingPage::DoDataExchange( CDataExchange* pDX ) { ASSERT( pDX != nullptr ); CDialogEx::DoDataExchange( pDX ); DDX_Control( pDX, IDC_RATING_LIST, ratingList ); } void CRatingPage::PostNcDestroy() { CDialogEx::PostNcDestroy(); delete this; } void CRatingPage::OnSize( UINT nType, int cx, int cy ) { CDialogEx::OnSize( nType, cx, cy ); if ( ratingList.GetSafeHwnd() != nullptr ) { CRect rc1; GetWindowRect( &rc1 ); CRect rc2; ratingList.GetWindowRect( &rc2 ); int border = rc2.left - rc1.left; int borderTop = rc2.top - rc1.top; ASSERT( border >= 0 ); ASSERT( borderTop >= 0 ); ratingList.SetWindowPos( nullptr, border, borderTop, cx - border * 2, cy - border - borderTop, SWP_NOZORDER ); } } void CRatingPage::unixTimeToSystemTime( time_t t, SYSTEMTIME& st ) { FILETIME ft; unixTimeToFileTime( t, ft ); SYSTEMTIME st1; BOOL res = FileTimeToSystemTime( &ft, &st1 ); ASSERT( res != FALSE ); res = SystemTimeToTzSpecificLocalTime( nullptr, &st1, &st ); ASSERT( res != FALSE ); } void CRatingPage::unixTimeToFileTime( time_t t, FILETIME& ft ) { LONGLONG ll; // Convert seconds to 100-nanosecond intervals ll = Int32x32To64( t, 10000000 ); // Add the difference in 100-nanosecond intervals between 1601 and 1970 ll += 116444736000000000; ft.dwLowDateTime = (DWORD)ll; ft.dwHighDateTime = ll >> 32; }