Skip to content

Commit

Permalink
feat: display games in a scrollable region
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanMenda committed May 5, 2024
1 parent cda9ef7 commit 73c74cf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
14 changes: 11 additions & 3 deletions include/App/Logic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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> _gamesData;
Response _res;
};
}

4 changes: 3 additions & 1 deletion include/UI/GamesPlayedPage.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,7 +26,9 @@ namespace Stuckfish
Core& _app;
Logic& _logic;
UserData& _userdata;
Response _res;

bool _hasRetrievedGames = false;
bool _isFirstElement = true;
};
}
26 changes: 11 additions & 15 deletions src/App/Logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -112,27 +111,24 @@ 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;
cpr::Response res = cpr::Get(cpr::Url{ url });

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;
}

}
51 changes: 23 additions & 28 deletions src/UI/GamesPlayedPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 73c74cf

Please sign in to comment.