From 73c74cfede0c0a11efb0bffcfd28ca89a2484a63 Mon Sep 17 00:00:00 2001 From: Aman Menda Date: Sun, 5 May 2024 23:13:55 +0100 Subject: [PATCH] feat: display games in a scrollable region --- include/App/Logic.hpp | 14 ++++++++-- include/UI/GamesPlayedPage.hpp | 4 ++- src/App/Logic.cpp | 26 ++++++++--------- src/UI/GamesPlayedPage.cpp | 51 +++++++++++++++------------------- 4 files changed, 48 insertions(+), 47 deletions(-) diff --git a/include/App/Logic.hpp b/include/App/Logic.hpp index d07af11..8e436e2 100644 --- a/include/App/Logic.hpp +++ b/include/App/Logic.hpp @@ -22,25 +22,33 @@ namespace Stuckfish { std::string pgn; std::string timeClass; - std::string gameResult; + std::string result; std::string whiteRating; std::string blackRating; std::string whiteUsername; std::string blackUsername; }; + struct Response + { + int errorCode; + std::string errorMessage; + std::vector gamesData; + }; + class Logic { public: - Logic() : _gamesData() + Logic() : _gamesData(), _res() {}; bool IsChessDotComUser(const std::string& username); - void GamesPlayedWithinPeriod(const std::string& username, const std::string& year, const std::string& month); + const Response& GamesPlayedWithinPeriod(const std::string& username, const std::string& year, const std::string& month); void GetInfosFromListOfGamesPlayed(const std::string& username, const Document& doc); private: std::vector _gamesData; + Response _res; }; } diff --git a/include/UI/GamesPlayedPage.hpp b/include/UI/GamesPlayedPage.hpp index 2ce4506..5c76d92 100644 --- a/include/UI/GamesPlayedPage.hpp +++ b/include/UI/GamesPlayedPage.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - * File: GamesPlayedPage.hpp + * File: GamesPlayedPage.hpp * Authors: see AUTHORS file * Date: May 3, 2024 * Description: This file contains the required method to render the 2nd Page @@ -26,7 +26,9 @@ namespace Stuckfish Core& _app; Logic& _logic; UserData& _userdata; + Response _res; bool _hasRetrievedGames = false; + bool _isFirstElement = true; }; } diff --git a/src/App/Logic.cpp b/src/App/Logic.cpp index c249ff4..3384ac7 100644 --- a/src/App/Logic.cpp +++ b/src/App/Logic.cpp @@ -58,9 +58,8 @@ Logic::GetInfosFromListOfGamesPlayed(const std::string& username, const Document const Value& gamesArray = doc["games"]; if (gamesArray.Empty()) { - // display on the window "No games found" - // Log in the local console the same thing - std::cout << "No games found !\n"; + // No popup, just display a a message on the screen + std::cerr << "No games found !\n"; return; } // Iterate over the array and do whatever you need with each game object @@ -90,9 +89,9 @@ Logic::GetInfosFromListOfGamesPlayed(const std::string& username, const Document // find if player has played as white or black and display if he won or lost the game. if (gameData.whiteUsername == username) - gameData.gameResult = game["white"]["result"] != "win" ? "Lost" : "Won"; + gameData.result = game["white"]["result"] != "win" ? "Lost" : "Won"; else - gameData.gameResult = game["black"]["result"] != "win" ? "Lost" : "Won"; + gameData.result = game["black"]["result"] != "win" ? "Lost" : "Won"; // insert this game data in the vector _gamesData.push_back(gameData); @@ -112,7 +111,7 @@ Logic::GetInfosFromListOfGamesPlayed(const std::string& username, const Document //------------------------------------------------------------------------------ /** */ -void +const Response& Logic::GamesPlayedWithinPeriod(const std::string& username, const std::string& year, const std::string& month) { std::string url = "https://api.chess.com/pub/player/" + to_lower(username) + "/games/" + year + '/' + month; @@ -120,19 +119,16 @@ Logic::GamesPlayedWithinPeriod(const std::string& username, const std::string& y if (res.status_code == cpr::status::HTTP_OK) { Document doc; + doc.Parse(res.text.c_str()); GetInfosFromListOfGamesPlayed(username, doc); + _res.errorCode = EXIT_SUCCESS; + _res.gamesData = _gamesData; } else { - std::cerr << "Request failed with status code: " << res.status_code << std::endl; - return; - } - - for (auto g : _gamesData) { - std::cout << "White username: " << g.whiteUsername << '(' << g.whiteRating << ")\n"; - std::cout << "Black username: " << g.blackUsername << '(' << g.blackRating << ")\n"; - std::cout << "Game Result: " << g.gameResult << "\n\n"; + _res.errorCode = res.status_code; + _res.errorMessage = res.error.message; } - return; + return _res; } } diff --git a/src/UI/GamesPlayedPage.cpp b/src/UI/GamesPlayedPage.cpp index b406a8e..6447650 100644 --- a/src/UI/GamesPlayedPage.cpp +++ b/src/UI/GamesPlayedPage.cpp @@ -29,43 +29,38 @@ void GamesPlayedPage::OnUIRender() ImGui::Text("Games played: April 2024"); // TODO: get the current month using a C++ library ImGui::PopFont(); - ImVec2 buttonSize(confirmButtonSizeX*5, confirmButtonSizeY); - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 300)); - ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.0f, 0.5f)); - // list of all games played in the specified month from which we can extract: // the player and his ELO and same for the opponent, the game status for the player (win/lose/draw) // the game time control (rapid, blitz, bullet) + ImVec2 buttonSize(confirmButtonSizeX*5, confirmButtonSizeY); if (!_hasRetrievedGames) { - // Check if the retrieval has been ended with success or with an error code - // and handle the error appropriately if it occurs. When there are no errors, - // find a way to get the Games data list. - _logic.GamesPlayedWithinPeriod(_userdata.username, "2024", "05"); - _hasRetrievedGames = true; - } + _res = _logic.GamesPlayedWithinPeriod(_userdata.username, "2024", "05"); - // games list - if (ImGui::Button("Button 1", buttonSize)) - { - std::cout << "1"; - } - - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 260)); - if (ImGui::Button("Button 2", buttonSize)) - { - // Handle button click - std::cout << "2"; + // handle every errors here + if (_res.errorCode == EXIT_FAILURE) + { + _errorOccured = true; + _errorMessage = _res.errorMessage; + } + _hasRetrievedGames = true; } + + // Begin a scrollable region + ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar); + for (auto& game : _res.gamesData) { + std::string buttonLabel = game.whiteUsername + "(" + game.whiteRating + ") vs " + + game.blackUsername + "(" + game.blackRating + ") - " + + game.result + " - " + game.timeClass; - ImGui::SetCursorPos(ImVec2(windowMiddlePos.x - (buttonSize.x / 2.0f), windowMiddlePos.y - 220)); - if (ImGui::Button("Button 3", buttonSize)) - { - // Handle button click - std::cout << "3"; + ImVec2 buttonPos = ImVec2((ImGui::GetWindowSize().x - buttonSize.x) / 2.0f, ImGui::GetCursorPosY()); + ImGui::SetCursorPos(buttonPos); + if (ImGui::Button(buttonLabel.c_str(), buttonSize)) { + // link to the next page maybe ? + std::cout << game.pgn; + } } - ImGui::PopStyleVar(); - + ImGui::EndChild(); ImGui::End(); }