<commit_before>/*************************************************************************
> File Name: GameAgent.cpp
> Project Name: Hearthstone++
> Author: <NAME>
> Purpose: Hearthstone Game Agent
> Created Time: 2017/09/26
> Copyright (c) 2017, <NAME>
*************************************************************************/
#include <Managers/GameAgent.h>
#include <Tasks/BasicTasks/Brief.h>
#include <Tasks/BasicTasks/Combat.h>
#include <Tasks/BasicTasks/Draw.h>
#include <Tasks/BasicTasks/GameEnd.h>
#include <Tasks/BasicTasks/ModifyMana.h>
#include <Tasks/BasicTasks/Mulligan.h>
#include <Tasks/BasicTasks/PlayCard.h>
#include <Tasks/BasicTasks/PlayerSetting.h>
#include <Tasks/BasicTasks/Requirement.h>
#include <Tasks/BasicTasks/Shuffle.h>
#include <Tasks/BasicTasks/SwapPlayer.h>
#include <Tasks/BasicTasks/Wrapper.h>
#include <Tasks/MetaData.h>

#include <random>

namespace Hearthstonepp
{
std::thread GameAgent::StartAgent()
{
    auto flow = [this]() {
        BeginPhase();
        while (true)
        {
            bool isGameEnd = MainPhase();
            if (isGameEnd)
            {
                break;
            }
        }

        FinalPhase();
    };

    return std::thread(std::move(flow));
}

void GameAgent::GetTaskMeta(TaskMeta& meta)
{
    m_taskAgent.Read(meta);
}

void GameAgent::WriteSyncBuffer(TaskMeta&& data, bool sideChannel)
{
    m_taskAgent.Notify(std::move(data), sideChannel);
}

Player& GameAgent::GetPlayer1()
{
    return m_player1;
}

Player& GameAgent::GetPlayer2()
{
    return m_player2;
}

MetaData GameAgent::RunTask(const ITask& task, Player& player1, Player& player2)
{
    return task.Run(player1, player2);
}

TaskAgent& GameAgent::GetTaskAgent()
{
    return m_taskAgent;
}

void GameAgent::BeginPhase()
{
    std::random_device rd;
    std::default_random_engine gen(rd());

    // get random number, zero or one.
    std::uniform_int_distribution<int> bin(0, 1);
    // swap user with 50% probability
    if (bin(gen) == 1)
    {
        BasicTasks::SwapPlayerTask().Run(m_player1, m_player2);
    }

    auto success = [](const TaskMeta& meta) {
        return meta.status == MetaData::MULLIGAN_SUCCESS;
    };

    TaskMeta meta;
    // BeginPhase Task List
    m_taskAgent.Run(
        meta, m_player1, m_player2, BasicTasks::PlayerSettingTask(m_taskAgent),
        BasicTasks::DoBothPlayer(BasicTasks::ShuffleTask()),
        BasicTasks::DoBothPlayer(BasicTasks::DrawTask(NUM_BEGIN_DRAW, m_taskAgent)),
        BasicTasks::BriefTask(),
        BasicTasks::DoUntil(BasicTasks::MulliganTask(m_taskAgent), success),
        BasicTasks::SwapPlayerTask(), BasicTasks::BriefTask(),
        BasicTasks::DoUntil(BasicTasks::MulliganTask(m_taskAgent), success),
        BasicTasks::SwapPlayerTask());

    // TODO: Coin for later user
    // m_player2.hand.push_back(new Card());
}

bool GameAgent::MainPhase()
{
    // Ready for main phase
    MainReady();
    // MainMenu return isGameEnd flag
    return MainMenu();
}

void GameAgent::FinalPhase()
{
    TaskMeta meta;
    m_taskAgent.Run(meta, m_player1, m_player2, BasicTasks::GameEndTask());
}

void GameAgent::MainReady()
{
    // MainReady : Draw, ModifyMana, Clear vector `attacked`
    TaskMeta meta;
    m_taskAgent.Run(
        meta, m_player1, m_player2, BasicTasks::DrawTask(1, m_taskAgent),
        BasicTasks::ModifyManaTask(NumMode::ADD, ManaMode::TOTAL, 1),
        BasicTasks::ModifyManaByRef(NumMode::SYNC, ManaMode::EXIST,
                                    m_player1.totalMana));

    m_player1.attacked.clear();
}

bool GameAgent::MainMenu()
{
    // Check before starting main phase
    if (IsGameEnd())
    {
        return true;
    }

    TaskMeta meta;
    m_taskAgent.Run(meta, m_player1, m_player2, BasicTasks::BriefTask());
    // Get Menu Response from GameInterface
    BasicTasks::Requirement(TaskID::SELECT_MENU, m_taskAgent)
        .Interact(m_player1.id, meta);<commit_msg>[ci skip] Update GameAgent - Modify Function Name Run to RunMulti<commit_after>/*************************************************************************
> File Name: GameAgent.cpp
> Project Name: Hearthstone++
> Author: <NAME>
> Purpose: Hearthstone Game Agent
> Created Time: 2017/09/26
> Copyright (c) 2017, <NAME>
*************************************************************************/
#include <Managers/GameAgent.h>
#include <Tasks/BasicTasks/Brief.h>
#include <Tasks/BasicTasks/Combat.h>
#include <Tasks/BasicTasks/Draw.h>
#include <Tasks/BasicTasks/GameEnd.h>
#include <Tasks/BasicTasks/ModifyMana.h>
#include <Tasks/BasicTasks/Mulligan.h>
#include <Tasks/BasicTasks/PlayCard.h>
#include <Tasks/BasicTasks/PlayerSetting.h>
#include <Tasks/BasicTasks/Requirement.h>
#include <Tasks/BasicTasks/Shuffle.h>
#include <Tasks/BasicTasks/SwapPlayer.h>
#include <Tasks/BasicTasks/Wrapper.h>
#include <Tasks/MetaData.h>

#include <random>

namespace Hearthstonepp
{
std::thread GameAgent::StartAgent()
{
    auto flow = [this]() {
        BeginPhase();
        while (true)
        {
            bool isGameEnd = MainPhase();
            if (isGameEnd)
            {
                break;
            }
        }

        FinalPhase();
    };

    return std::thread(std::move(flow));
}

void GameAgent::GetTaskMeta(TaskMeta& meta)
{
    m_taskAgent.Read(meta);
}

void GameAgent::WriteSyncBuffer(TaskMeta&& data, bool sideChannel)
{
    m_taskAgent.Notify(std::move(data), sideChannel);
}

Player& GameAgent::GetPlayer1()
{
    return m_player1;
}

Player& GameAgent::GetPlayer2()
{
    return m_player2;
}

MetaData GameAgent::RunTask(const ITask& task, Player& player1, Player& player2)
{
    return task.Run(player1, player2);
}

TaskAgent& GameAgent::GetTaskAgent()
{
    return m_taskAgent;
}

void GameAgent::BeginPhase()
{
    std::random_device rd;
    std::default_random_engine gen(rd());

    // get random number, zero or one.
    std::uniform_int_distribution<int> bin(0, 1);
    // swap user with 50% probability
    if (bin(gen) == 1)
    {
        BasicTasks::SwapPlayerTask().Run(m_player1, m_player2);
    }

    auto success = [](const TaskMeta& meta) {
        return meta.status == MetaData::MULLIGAN_SUCCESS;
    };

    TaskMeta meta;
    // BeginPhase Task List
    m_taskAgent.RunMulti(
        meta, m_player1, m_player2, BasicTasks::PlayerSettingTask(m_taskAgent),
        BasicTasks::DoBothPlayer(BasicTasks::ShuffleTask()),
        BasicTasks::DoBothPlayer(BasicTasks::DrawTask(NUM_BEGIN_DRAW, m_taskAgent)),
        BasicTasks::BriefTask(),
        BasicTasks::DoUntil(BasicTasks::MulliganTask(m_taskAgent), success),
        BasicTasks::SwapPlayerTask(), BasicTasks::BriefTask(),
        BasicTasks::DoUntil(BasicTasks::MulliganTask(m_taskAgent), success),
        BasicTasks::SwapPlayerTask());

    // TODO: Coin for later user
    // m_player2.hand.push_back(new Card());
}

bool GameAgent::MainPhase()
{
    // Ready for main phase
    MainReady();
    // MainMenu return isGameEnd flag
    return MainMenu();
}

void GameAgent::FinalPhase()
{
    TaskMeta meta;
    m_taskAgent.Run(meta, m_player1, m_player2, BasicTasks::GameEndTask());
}

void GameAgent::MainReady()
{
    // MainReady : Draw, ModifyMana, Clear vector `attacked`
    TaskMeta meta;
    m_taskAgent.RunMulti(
        meta, m_player1, m_player2, BasicTasks::DrawTask(1, m_taskAgent),
        BasicTasks::ModifyManaTask(NumMode::ADD, ManaMode::TOTAL, 1),
        BasicTasks::ModifyManaByRef(NumMode::SYNC, ManaMode::EXIST,
                                    m_player1.totalMana));

    m_player1.attacked.clear();
}

bool GameAgent::MainMenu()
{
    // Check before starting main phase
    if (IsGameEnd())
    {
        return true;
    }

    TaskMeta meta;
    m_taskAgent.Run(meta, m_player1, m_player2, BasicTasks::BriefTask());
    // Get Menu Response from GameInterface
    BasicTasks::Requirement(TaskID::SELECT_MENU, m_taskAgent)
        .Interact(m_player1.id, meta);
{'fraction_non_alphanumeric': 0.16481153554262584, 'fraction_numerical': 0.010371869466228182, 'mean_word_length': 3.7689987937273823, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4901838', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2514, 'n_words': 476}