<commit_before>#include "Players/Outside_Communicator.h"

#include <iostream>
#include <fstream>
#include <memory>
#include <string>
#include <chrono>

#include "Players/CECP_Mediator.h"
#include "Exceptions/Game_Ended.h"

#include "Utility/String.h"

std::string Outside_Communicator::indent = "\t";

//! Initialize communication with an outside program.
//
//! The function returns an appropriate derived class based on the
//! communiation protocol by the other program. CECP is the only
//! protocol implemented so far. UCI may be implemented in the future.
//! \param player The local player so that its information may be sent
//!        to the outside interface.
//! \returns An appropriate derived class instance with the proper protocol.
std::unique_ptr<Outside_Communicator> connect_to_outside(const Player& player)
{
    Outside_Communicator::log("==================");
    auto protocol_type = Outside_Communicator::receive_command();
    if(protocol_type == "xboard")
    {
        return std::make_unique<CECP_Mediator>(player);
    }
    else
    {
        throw std::runtime_error("Unrecognized protocol: " + protocol_type);
    }
}

Outside_Communicator::Outside_Communicator()
{
}

Outside_Communicator::~Outside_Communicator()
{
    log("Shutting down.");
}

//! Wait for a command from the outside interface and pass it on to derived class instances.
//
//! \returns The command from the outside interface if not "quit".
//! \throws Game_Ended If the command "quit" is received, the game will end and the program will exit.
std::string Outside_Communicator::receive_command()
{
    std::string result;
    std::getline(std::cin, result);
    log("RECEIVING: " + result);
    if(String::remove_extra_whitespace(result).empty())
    {
        log("Got empty input from GUI. Shutting down.");
        result = "quit";
    }

    if(result == "quit")
    {
        throw Game_Ended();
    }

    return result;
}

//! Log data to a local text file.
//
//! \param data A text string to write.
void Outside_Communicator::log(const std::string& data)
{
    static const auto log_file_name = "chess_comm_log.txt";<commit_msg>Regularize GUI input<commit_after>#include "Players/Outside_Communicator.h"

#include <iostream>
#include <fstream>
#include <memory>
#include <string>
#include <chrono>

#include "Players/CECP_Mediator.h"
#include "Exceptions/Game_Ended.h"

#include "Utility/String.h"

std::string Outside_Communicator::indent = "\t";

//! Initialize communication with an outside program.
//
//! The function returns an appropriate derived class based on the
//! communiation protocol by the other program. CECP is the only
//! protocol implemented so far. UCI may be implemented in the future.
//! \param player The local player so that its information may be sent
//!        to the outside interface.
//! \returns An appropriate derived class instance with the proper protocol.
std::unique_ptr<Outside_Communicator> connect_to_outside(const Player& player)
{
    Outside_Communicator::log("==================");
    auto protocol_type = Outside_Communicator::receive_command();
    if(protocol_type == "xboard")
    {
        return std::make_unique<CECP_Mediator>(player);
    }
    else
    {
        throw std::runtime_error("Unrecognized protocol: " + protocol_type);
    }
}

Outside_Communicator::Outside_Communicator()
{
}

Outside_Communicator::~Outside_Communicator()
{
    log("Shutting down.");
}

//! Wait for a command from the outside interface and pass it on to derived class instances.
//
//! \returns The command from the outside interface if not "quit".
//! \throws Game_Ended If the command "quit" is received, the game will end and the program will exit.
std::string Outside_Communicator::receive_command()
{
    std::string result;
    std::getline(std::cin, result);
    log("RECEIVING: " + result);
    result = String::remove_extra_whitespace(result);

    if(result == "quit")
    {
        throw Game_Ended();
    }

    return result;
}

//! Log data to a local text file.
//
//! \param data A text string to write.
void Outside_Communicator::log(const std::string& data)
{
    static const auto log_file_name = "chess_comm_log.txt";
{'fraction_non_alphanumeric': 0.12425328554360812, 'fraction_numerical': 0.0, 'mean_word_length': 4.019184652278177, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4024504', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1196, 'n_words': 383}