Commit 2c605443 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

working library

parent ca0bd04a
Loading
Loading
Loading
Loading
+8 −11
Original line number Diff line number Diff line
@@ -10,8 +10,9 @@
#include "HttpConnect.h"
#include "ttt.h"

// HELPERS - mostly by ChatGPT
// trim whitespace
// HELPERS

// trim whitespace, based on ChatGPT
std::string trim(const std::string& str) {
  size_t start = str.find_first_not_of(" \t\n\r\f\v");
  size_t end = str.find_last_not_of(" \t\n\r\f\v");
@@ -26,7 +27,7 @@ std::string removeQuotes(const std::string& str) {
  return str;
}

// Basic JSON parser for flat key-value pairs
// Basic JSON parser for flat key-value pairs, based on ChatGPT
map<string, string> parseJson(const std::string& jsonString) {
  map<string, string> result;
  string trimmedJson = trim(jsonString);
@@ -44,8 +45,7 @@ map<string, string> parseJson(const std::string& jsonString) {
  while (getline(ss, item, ',')) {
    size_t colonPos = item.find(':');
    if (colonPos == std::string::npos) {
      // ignore invalide key-value pair
      // std::cerr << "Invalid key-value pair." << std::endl;
      // ignore invalid key-value pair
      continue;
    }

@@ -61,7 +61,7 @@ map<string, string> parseJson(const std::string& jsonString) {
  return result;
}

// parses an HTTP response and returns the body of the response
// parses an HTTP response and returns the body of the response, based on ChatGPT
string parseHttpResponse(const string &httpResponse) {
  std::istringstream responseStream(httpResponse);
  std::string line;
@@ -102,8 +102,8 @@ string parseHttpResponse(const string &httpResponse) {
  return body;
}

// Courtesy of ChatGPT
string url_encode(const std::string &value) {
// URL-encode a given string, based on ChatGPT
string url_encode(const string &value) {
  std::ostringstream escaped;
  escaped.fill('0');
  escaped << std::hex;
@@ -127,10 +127,8 @@ string url_encode(const std::string &value) {
 * @return The Http response.
 */
string requestTTT(string script) {
  // cout << "Trying " +script << endl;
  string answer =
      parseHttpResponse(HttpConnect::makeRequest("agile.bu.edu", "/backend/web/" + script, "8013"));
      //parseHttpResponse(HttpConnect::makeRequest("localhost", "/" + script, "8080"));
  return answer;
}

@@ -144,7 +142,6 @@ int strToInt(string theStr) {


// IMPLEMENTATIONS

PLAYER_CODE newPlayer(string name) {
  return requestTTT("newPlayer.pl");
}