From 0cb2465542ee5c92cf8a9fce8848e8b7156a5482 Mon Sep 17 00:00:00 2001 From: Jerry Richards Date: Tue, 25 Jul 2017 16:08:34 -0600 Subject: [PATCH] Stats Test Added (#230) Adding Stats test to the scenario litst. --- Tests/CppCxTests/Social/UWP/MainPage.xaml.cpp | 136 +++++++++++++++--- Tests/CppCxTests/Social/UWP/MainPage.xaml.h | 19 +-- Tests/CppCxTests/Social/UWP/Scenarios.cpp | 69 +++++++-- Tests/CppCxTests/Social/UWP/Scenarios.h | 2 + 4 files changed, 180 insertions(+), 46 deletions(-) diff --git a/Tests/CppCxTests/Social/UWP/MainPage.xaml.cpp b/Tests/CppCxTests/Social/UWP/MainPage.xaml.cpp index 3457fd4a..1aad457b 100644 --- a/Tests/CppCxTests/Social/UWP/MainPage.xaml.cpp +++ b/Tests/CppCxTests/Social/UWP/MainPage.xaml.cpp @@ -24,6 +24,11 @@ using namespace Windows::UI::Xaml::Input; using namespace Windows::UI::Xaml::Media; using namespace Windows::UI::Xaml::Navigation; +using namespace Microsoft::Xbox::Services; +using namespace Microsoft::Xbox::Services::Leaderboard; +using namespace Microsoft::Xbox::Services::Statistics; +using namespace Microsoft::Xbox::Services::Statistics::Manager; + struct ScenarioDescriptionItem { int tag; @@ -34,6 +39,8 @@ enum ScenarioItemTag { Scenario_GetUserProfileAsync = 1, Scenario_GetSocialRelationshipsAsync, + Scenario_WriteStat, + Scenario_ReadStat, Scenario_VerifyStringAsync //Scenario_GetUserProfilesForSocialGroupAsync }; @@ -42,6 +49,8 @@ ScenarioDescriptionItem ScenarioDescriptions[] = { { Scenario_GetUserProfileAsync, L"Get user profile" }, { Scenario_GetSocialRelationshipsAsync, L"Get social list" }, + { Scenario_WriteStat, L"Write Stat" }, + { Scenario_ReadStat, L"Read Stat" }, }; bool MainPage::RunScenario(int selectedTag) @@ -54,9 +63,11 @@ bool MainPage::RunScenario(int selectedTag) switch (selectedTag) { - case Scenario_GetUserProfileAsync: m_scenarios.Scenario_GetUserProfileAsync(this, m_xboxLiveContext); break; - case Scenario_GetSocialRelationshipsAsync: m_scenarios.Scenario_GetSocialRelationshipsAsync(this, m_xboxLiveContext); break; - default: return false; + case Scenario_GetUserProfileAsync: m_scenarios.Scenario_GetUserProfileAsync(this, m_xboxLiveContext); break; + case Scenario_GetSocialRelationshipsAsync: m_scenarios.Scenario_GetSocialRelationshipsAsync(this, m_xboxLiveContext); break; + case Scenario_WriteStat: m_scenarios.Scenario_WriteStat(this, m_xboxLiveContext); break; + case Scenario_ReadStat: m_scenarios.Scenario_ReadStat(this, m_xboxLiveContext); break; + default: return false; } return true; @@ -65,21 +76,22 @@ bool MainPage::RunScenario(int selectedTag) MainPage::MainPage() { InitializeComponent(); + this->CoreDispatcher = Windows::UI::Xaml::Window::Current->CoreWindow->Dispatcher; + m_user = ref new Microsoft::Xbox::Services::System::XboxLiveUser(); m_user->SignOutCompleted += ref new EventHandler ([this](Platform::Object^, Microsoft::Xbox::Services::System::SignOutCompletedEventArgs^ args) + { + CoreDispatcher->RunAsync( + Windows::UI::Core::CoreDispatcherPriority::Normal, + ref new Windows::UI::Core::DispatchedHandler([this, args]() { - CoreDispatcher->RunAsync( - Windows::UI::Core::CoreDispatcherPriority::Normal, - ref new Windows::UI::Core::DispatchedHandler([this, args]() - { - this->UserInfoLabel->Text = L"user signed out"; - Log(L"----------------"); - LogFormat(L"User %s signed out", args->User->Gamertag->Data()); - })); - }); - + this->UserInfoLabel->Text = L"user signed out"; + Log(L"----------------"); + LogFormat(L"User %s signed out", args->User->Gamertag->Data()); + })); + }); for (ScenarioDescriptionItem scenario : ScenarioDescriptions) { @@ -89,14 +101,84 @@ MainPage::MainPage() this->ScenarioListBox->Items->Append(listBoxItem); } + StartTimerAndRegisterHandler(); + this->ScenarioListBox->SelectedIndex = 0; SignInSilently(); } -void MainPage::ScenarioListBox_DoubleTapped( - Platform::Object^ sender, - Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs^ e - ) +void MainPage::StartTimerAndRegisterHandler() +{ + auto timer = ref new Windows::UI::Xaml::DispatcherTimer(); + + TimeSpan ts; + ts.Duration = 500; + timer->Interval = ts; + timer->Start(); + auto registrationToken = timer->Tick += ref new EventHandler(this, &MainPage::OnTick); +} + +void MainPage::OnTick(Object^ sender, Object^ e) +{ + StatisticManager^ mgr = Microsoft::Xbox::Services::Statistics::Manager::StatisticManager::SingletonInstance; + if (mgr != nullptr) + { + auto EventList = mgr->DoWork(); + for (StatisticEvent^ Event : EventList) + { + if (Event->ErrorCode != 0) + { + LogFormat(L"DoWork error: %s", Event->ErrorMessage->Data()); + } + + StatisticEventArgs^ args; + + switch (Event->EventType) + { + case StatisticEventType::LocalUserAdded: + LogFormat(L"DoWork LocalUserAdded: %s", Event->User->Gamertag->Data()); + break; + + case StatisticEventType::LocalUserRemoved: + LogFormat(L"DoWork LocalUserRemoved: %s", Event->User->Gamertag->Data()); + break; + + case StatisticEventType::GetLeaderboardComplete: + { + Log(L"DoWork GetLeaderboardComplete:"); + + LeaderboardResultEventArgs^ LbResultEventArgs = safe_cast(Event->EventArgs); + LeaderboardResult^ LbResult = LbResultEventArgs->Result; + + for (LeaderboardRow^ row : LbResult->Rows) + { + String^ colValues; + for (auto columnValue : row->Values) + { + colValues = colValues + L" "; + colValues = colValues + columnValue; + } + LogFormat(L"%16s %6s %12s %8s\n", row->Gamertag->Data(), row->Rank.ToString()->Data(), row->Percentile.ToString()->Data(), colValues->Data()); + } + } + break; + + case StatisticEventType::StatisticUpdateComplete: + LogFormat(L"DoWork StatisticUpdateComplete: %s", Event->User->Gamertag->ToString()); + + IVectorView^ stats = mgr->GetStatisticNames(Event->User); + for (String^ stat : stats) + { + LogFormat(L"DoWork Stat: %s", stat->Data()); + } + + break; + } + } + } +} + +void MainPage::ScenarioListBox_DoubleTapped(Platform::Object^ sender, Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs^ e) { RunSelectedScenario(); } @@ -138,7 +220,7 @@ void MainPage::RunSelectedScenario() { if (this->ScenarioListBox->SelectedItems->Size == 1) { - ClearLogs(); + //ClearLogs(); ListBoxItem^ selectedItem = safe_cast(this->ScenarioListBox->SelectedItem); int selectedTag = safe_cast(selectedItem->Tag); RunScenario(selectedTag); @@ -147,7 +229,7 @@ void MainPage::RunSelectedScenario() void MainPage::RunAllButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { - ClearLogs(); + //ClearLogs(); int scenarioTag = 1; for (;;) { @@ -173,8 +255,7 @@ void MainPage::SignIn() Log(this->UserInfoLabel->Text); auto asyncOp = m_user->SignInAsync(this->CoreDispatcher); - create_task(asyncOp) - .then([this](task t) + create_task(asyncOp).then([this](task t) { try { @@ -182,6 +263,11 @@ void MainPage::SignIn() if (result->Status == Microsoft::Xbox::Services::System::SignInStatus::Success) { m_xboxLiveContext = ref new Microsoft::Xbox::Services::XboxLiveContext(m_user); + + StatisticManager^ mgr = StatisticManager::SingletonInstance; + if (mgr == nullptr) return t; + mgr->AddLocalUser(m_xboxLiveContext->User); + this->UserInfoLabel->Text = L"Sign in succeeded"; } else if (result->Status == Microsoft::Xbox::Services::System::SignInStatus::UserCancel) @@ -207,8 +293,7 @@ void MainPage::SignInSilently() Log(this->UserInfoLabel->Text); auto asyncOp = m_user->SignInSilentlyAsync(nullptr); - create_task(asyncOp) - .then([this](task t) + create_task(asyncOp).then([this](task t) { try { @@ -216,6 +301,11 @@ void MainPage::SignInSilently() if (result->Status == Microsoft::Xbox::Services::System::SignInStatus::Success) { m_xboxLiveContext = ref new Microsoft::Xbox::Services::XboxLiveContext(m_user); + + StatisticManager^ mgr = StatisticManager::SingletonInstance; + if (mgr == nullptr) return t; + mgr->AddLocalUser(m_xboxLiveContext->User); + this->UserInfoLabel->Text = L"SignIn Silent succeeded"; } else if (result->Status == Microsoft::Xbox::Services::System::SignInStatus::UserInteractionRequired) diff --git a/Tests/CppCxTests/Social/UWP/MainPage.xaml.h b/Tests/CppCxTests/Social/UWP/MainPage.xaml.h index 479333df..cb6ccf74 100644 --- a/Tests/CppCxTests/Social/UWP/MainPage.xaml.h +++ b/Tests/CppCxTests/Social/UWP/MainPage.xaml.h @@ -14,13 +14,14 @@ namespace Social_CppCx_140 { - /// - /// An empty page that can be used on its own or navigated to within a Frame. - /// - public ref class MainPage sealed - { - public: - MainPage(); + /// + /// An empty page that can be used on its own or navigated to within a Frame. + /// + public ref class MainPage sealed + { + public: + MainPage(); + void OnTick(Object^ sender, Object^ e); internal: void ClearLogs(); @@ -29,6 +30,8 @@ namespace Social_CppCx_140 property Windows::UI::Core::CoreDispatcher^ CoreDispatcher; private: + void StartTimerAndRegisterHandler(); + void ScenarioListBox_DoubleTapped(Platform::Object^ sender, Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs^ e); void RunButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void RunAllButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); @@ -44,5 +47,5 @@ namespace Social_CppCx_140 Microsoft::Xbox::Services::System::XboxLiveUser^ m_user; Microsoft::Xbox::Services::XboxLiveContext^ m_xboxLiveContext; Scenarios m_scenarios; - }; + }; } diff --git a/Tests/CppCxTests/Social/UWP/Scenarios.cpp b/Tests/CppCxTests/Social/UWP/Scenarios.cpp index 00472e3c..588ee670 100644 --- a/Tests/CppCxTests/Social/UWP/Scenarios.cpp +++ b/Tests/CppCxTests/Social/UWP/Scenarios.cpp @@ -27,6 +27,10 @@ using namespace Microsoft::Xbox::Services::Social; using namespace Microsoft::Xbox::Services::System; using namespace Microsoft::Xbox::Services::Presence; +using namespace Microsoft::Xbox::Services::Statistics; +using namespace Microsoft::Xbox::Services::Statistics::Manager; +using namespace Microsoft::Xbox::Services::Leaderboard; + std::mutex g_blockOfTextLock; void Scenarios::Active_RealTimeActivity(_In_ MainPage^ mainPage, Microsoft::Xbox::Services::XboxLiveContext^ xboxLiveContext) @@ -37,22 +41,22 @@ void Scenarios::Active_RealTimeActivity(_In_ MainPage^ mainPage, Microsoft::Xbox xboxLiveContext->PresenceService->SubscribeToDevicePresenceChange( xboxLiveContext->User->XboxUserId - ); + ); xboxLiveContext->PresenceService->SubscribeToTitlePresenceChange( - xboxLiveContext->User->XboxUserId, + xboxLiveContext->User->XboxUserId, 0x5D2A2BCA - ); + ); EventHandler^ devicePresenceChangeEvent = ref new EventHandler( - [this, mainPage](Platform::Object^, DevicePresenceChangeEventArgs^ eventArgs) + [this, mainPage](Platform::Object^, DevicePresenceChangeEventArgs^ eventArgs) { OnDevicePresenceChange(mainPage, eventArgs); }); m_devicePresenceChangeEventToken = xboxLiveContext->PresenceService->DevicePresenceChanged += devicePresenceChangeEvent; EventHandler^ titlePresenceChangeEvent = ref new EventHandler( - [this, mainPage](Platform::Object^, TitlePresenceChangeEventArgs^ eventArgs) + [this, mainPage](Platform::Object^, TitlePresenceChangeEventArgs^ eventArgs) { OnTitlePresenceChange(mainPage, eventArgs); }); @@ -70,7 +74,7 @@ void Scenarios::Scenario_GetUserProfileAsync(_In_ MainPage^ ui, Microsoft::Xbox: auto asyncOp = xboxLiveContext->ProfileService->GetUserProfileAsync(xboxLiveContext->User->XboxUserId); create_task(asyncOp) - .then([this, ui](task resultTask) + .then([this, ui](task resultTask) { try { @@ -105,10 +109,10 @@ void Scenarios::Scenario_GetSocialRelationshipsAsync(_In_ MainPage^ ui, Microsof { ui->Log(L"Calling get_social_relationships..."); - PresenceData^ presenceData = ref new PresenceData(L"12200100-88da-4d8b-af88-e38f5d2a2bca", L"rpdemo" ); + PresenceData^ presenceData = ref new PresenceData(L"12200100-88da-4d8b-af88-e38f5d2a2bca", L"rpdemo"); auto pAsyncOp2 = xboxLiveContext->PresenceService->SetPresenceAsync(true, presenceData); create_task(pAsyncOp2) - .then([this,ui](task resultTask) + .then([this, ui](task resultTask) { try { @@ -116,7 +120,7 @@ void Scenarios::Scenario_GetSocialRelationshipsAsync(_In_ MainPage^ ui, Microsof ui->LogFormat(L"SetPresenceAsync succeeded"); } catch (Platform::Exception^ ex) - { + { ui->LogFormat(L"SetPresenceAsync failed: 0x%0.8x", ex->HResult); } }); @@ -131,7 +135,7 @@ void Scenarios::Scenario_GetSocialRelationshipsAsync(_In_ MainPage^ ui, Microsof SocialRelationship::All, startIndex, maxItems - ); + ); create_task(pAsyncOp) .then([this, maxItems, ui](task resultTask) @@ -160,14 +164,50 @@ void Scenarios::Scenario_GetSocialRelationshipsAsync(_In_ MainPage^ ui, Microsof } } catch (Platform::Exception^ ex) - { + { ui->LogFormat(L"get_social_relationships failed: 0x%0.8x", ex->HResult); } }); } -void -Scenarios::OnDevicePresenceChange(_In_ MainPage^ ui, _In_ DevicePresenceChangeEventArgs^ args) +void Scenarios::Scenario_WriteStat(_In_ MainPage^ ui, Microsoft::Xbox::Services::XboxLiveContext^ xboxLiveContext) +{ + StatisticManager^ mgr = StatisticManager::SingletonInstance; + if (mgr == nullptr) return; + + String^ statName = L"HighScore"; + long long statValue = 1001; + + mgr->SetStatisticIntegerData(xboxLiveContext->User, statName, statValue); + mgr->RequestFlushToService(xboxLiveContext->User); + + ui->LogFormat(L"WriteStat: %s : %s", statName->Data(), statValue.ToString()->Data()); +} + +void Scenarios::Scenario_ReadStat(_In_ MainPage^ ui, Microsoft::Xbox::Services::XboxLiveContext^ xboxLiveContext) +{ + static bool once = false; + + StatisticManager^ mgr = StatisticManager::SingletonInstance; + if (mgr == nullptr) return; + + if (!once) + { + once = true; + xboxLiveContext->Settings->ServiceCallRouted += ref new EventHandler([=](Object^, XboxServiceCallRoutedEventArgs^ args) + { + ui->LogFormat(L"[URL]: %s %s", args->HttpMethod->Data(), args->Url->AbsoluteUri->Data()); + ui->LogFormat(L"[Response]: %s %s", args->HttpStatus.ToString()->Data(), args->ResponseBody->Data()); + }); + } + + String^ statName = L"HighScore"; + LeaderboardQuery^ Query = ref new LeaderboardQuery(); + + mgr->GetLeaderboard(xboxLiveContext->User, statName, Query); +} + +void Scenarios::OnDevicePresenceChange(_In_ MainPage^ ui, _In_ DevicePresenceChangeEventArgs^ args) { std::lock_guard lockGuard(g_blockOfTextLock); @@ -179,8 +219,7 @@ Scenarios::OnDevicePresenceChange(_In_ MainPage^ ui, _In_ DevicePresenceChangeEv ui->LogFormat(L"Is user logged in: %S", args->IsUserLoggedOnDevice ? L"True" : L"False"); } -void -Scenarios::OnTitlePresenceChange(_In_ MainPage^ ui, _In_ TitlePresenceChangeEventArgs^ args) +void Scenarios::OnTitlePresenceChange(_In_ MainPage^ ui, _In_ TitlePresenceChangeEventArgs^ args) { std::lock_guard lockGuard(g_blockOfTextLock); diff --git a/Tests/CppCxTests/Social/UWP/Scenarios.h b/Tests/CppCxTests/Social/UWP/Scenarios.h index fd5efc50..53cc8b9e 100644 --- a/Tests/CppCxTests/Social/UWP/Scenarios.h +++ b/Tests/CppCxTests/Social/UWP/Scenarios.h @@ -19,6 +19,8 @@ namespace Social_CppCx_140 void Active_RealTimeActivity(_In_ MainPage^ mainPage, Microsoft::Xbox::Services::XboxLiveContext^ xboxLiveContext); void Scenario_GetUserProfileAsync(_In_ MainPage^ mainPage, Microsoft::Xbox::Services::XboxLiveContext^ xboxLiveContext); void Scenario_GetSocialRelationshipsAsync(_In_ MainPage^ mainPage, Microsoft::Xbox::Services::XboxLiveContext^ xboxLiveContext); + void Scenario_WriteStat(_In_ MainPage^ mainPage, Microsoft::Xbox::Services::XboxLiveContext^ xboxLiveContext); + void Scenario_ReadStat(_In_ MainPage^ mainPage, Microsoft::Xbox::Services::XboxLiveContext^ xboxLiveContext); private: Windows::Foundation::EventRegistrationToken m_devicePresenceChangeEventToken;