Skip to content

Commit

Permalink
add file header and sepators between functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanMenda committed May 3, 2024
1 parent fe96af2 commit 47414fc
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 330 deletions.
7 changes: 7 additions & 0 deletions Libraries/include/GamesPlayedPage.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/******************************************************************************
* File: GamesPlayedPage.hpp
* Authors: see AUTHORS file
* Date: May 3, 2024
* Description: This file contains the required method to render the 2nd Page
*****************************************************************************/

#pragma once

#include "Stuckfish.hpp"
Expand Down
7 changes: 7 additions & 0 deletions Libraries/include/Logic.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/******************************************************************************
* File: Logic.hpp
* Authors: see AUTHORS file
* Date: May 3, 2024
* Description: This file contains the API requests methods
*****************************************************************************/

#pragma once

#include <iostream>
Expand Down
7 changes: 7 additions & 0 deletions Libraries/include/Page.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/******************************************************************************
* File: Page.hpp
* Authors: see AUTHORS file
* Date: May 3, 2024
* Description: Page was meant to be an interface for other pages...
*****************************************************************************/

#pragma once

#include "Logic.hpp"
Expand Down
7 changes: 7 additions & 0 deletions Libraries/include/Stuckfish.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/******************************************************************************
* File: Stuckfish.hpp
* Authors: see AUTHORS file
* Date: May 3, 2024
* Description: This file contains the Core class and setup methods
*****************************************************************************/

#pragma once

#include <string>
Expand Down
7 changes: 7 additions & 0 deletions Libraries/include/UserInfosPage.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/******************************************************************************
* File: UserInfosPage.hpp
* Authors: see AUTHORS file
* Date: May 3, 2024
* Description: This file contains the required methods to render the 1st page
*****************************************************************************/

#include "Page.hpp"
#include "Exceptions.hpp"

Expand Down
7 changes: 7 additions & 0 deletions Libraries/include/fonts.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/******************************************************************************
* File: fonts.hpp
* Authors: see AUTHORS file
* Date: May 3, 2024
* Description: This file contains all the data related to the fonts we are using
*****************************************************************************/

#pragma once

extern unsigned char roboto_regular[168260];
Expand Down
180 changes: 98 additions & 82 deletions Sources/App/Logic.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/******************************************************************************
* File: Logic.cpp
* Authors: see AUTHORS file
*****************************************************************************/

#include "../../Libraries/include/Logic.hpp"

static std::string
Expand Down Expand Up @@ -26,97 +31,108 @@ extractInt(const Value& jsonObject, const std::string& fieldName)

namespace Stuckfish
{
bool
Logic::IsChessDotComUser(const std::string& username)
{
std::string url = "https://api.chess.com/pub/player/" + to_lower(username);
cpr::Response res = cpr::Get(cpr::Url{ url });

if (res.status_code == cpr::status::HTTP_OK)
return true;
return false;
}

void
Logic::GetInfosFromListOfGamesPlayed(const std::string& username, const Document& doc)
{
// Check if parsing succeeded
if (!doc.HasParseError()) {
if (doc.HasMember("games") && doc["games"].IsArray()) {
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";
return;
}
// Iterate over the array and do whatever you need with each game object
for (SizeType i = 0; i < gamesArray.Size(); ++i) {
const Value& game = gamesArray[i];
GamesData gameData;

// get white username and rating
if ((gameData.whiteUsername = extractString(game["white"], "username")) == "")
std::cerr << "Unable to get white username\n";
if ((gameData.whiteRating = extractInt(game["white"], "rating")) == "")
std::cerr << "Unable to get white rating\n";

// get black username and rating
if ((gameData.blackUsername = extractString(game["black"], "username")) == "")
std::cerr << "Unable to get black username\n";
if ((gameData.blackRating = extractInt(game["black"], "rating")) == "")
std::cerr << "Unable to get black rating\n";

// get time_class
if ((gameData.timeClass = extractString(game, "time_class")) == "")
std::cerr << "Unable to get time class\n";

//get pgn
if ((gameData.pgn = extractString(game, "pgn")) == "")
std::cerr << "Unable to get png\n";

// 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";
else
gameData.gameResult = game["black"]["result"] != "win" ? "Lost" : "Won";

// insert this game data in the vector
_gamesData.push_back(gameData);
}
}
else {
std::cerr << "JSON does not contain a valid 'games' array field." << std::endl;
//------------------------------------------------------------------------------
/**
*/
bool
Logic::IsChessDotComUser(const std::string& username)
{
std::string url = "https://api.chess.com/pub/player/" + to_lower(username);
cpr::Response res = cpr::Get(cpr::Url{ url });

if (res.status_code == cpr::status::HTTP_OK)
return true;
return false;
}

//------------------------------------------------------------------------------
/**
*/
void
Logic::GetInfosFromListOfGamesPlayed(const std::string& username, const Document& doc)
{
// Check if parsing succeeded
if (!doc.HasParseError()) {
if (doc.HasMember("games") && doc["games"].IsArray()) {
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";
return;
}
// Iterate over the array and do whatever you need with each game object
for (SizeType i = 0; i < gamesArray.Size(); ++i) {
const Value& game = gamesArray[i];
GamesData gameData;

// get white username and rating
if ((gameData.whiteUsername = extractString(game["white"], "username")) == "")
std::cerr << "Unable to get white username\n";
if ((gameData.whiteRating = extractInt(game["white"], "rating")) == "")
std::cerr << "Unable to get white rating\n";

// get black username and rating
if ((gameData.blackUsername = extractString(game["black"], "username")) == "")
std::cerr << "Unable to get black username\n";
if ((gameData.blackRating = extractInt(game["black"], "rating")) == "")
std::cerr << "Unable to get black rating\n";

// get time_class
if ((gameData.timeClass = extractString(game, "time_class")) == "")
std::cerr << "Unable to get time class\n";

//get pgn
if ((gameData.pgn = extractString(game, "pgn")) == "")
std::cerr << "Unable to get png\n";

// 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";
else
gameData.gameResult = game["black"]["result"] != "win" ? "Lost" : "Won";

// insert this game data in the vector
_gamesData.push_back(gameData);
}
}
else {
std::cerr << "Parsing failed with error code " << GetParseError_En(doc.GetParseError()) << std::endl;
std::cerr << "JSON does not contain a valid 'games' array field." << std::endl;
return;
}
}
else {
std::cerr << "Parsing failed with error code " << GetParseError_En(doc.GetParseError()) << std::endl;
return;
}
}

void
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);
} 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";
}
//------------------------------------------------------------------------------
/**
*/
void
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);
} 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";
}
return;
}

}
Loading

0 comments on commit 47414fc

Please sign in to comment.