<commit_before>//
// Copyright (c) 2014 <NAME>
// See LICENSE for more information.
//

#ifndef TWSM_ECON_HPP
#define TWSM_ECON_HPP


#include "ui_main_window.h"

#include <twl/network/network.hpp>

#include <QObject>


namespace Ui
{class main_window;}

namespace twsm
{
	class econ : public QObject
	{
		Q_OBJECT

		Ui::main_window& m_ui;

		std::map<std::size_t, twl::econ_client> m_clients;
		mlk::uint m_current_id{0};

	public:
		econ(Ui::main_window& ui) :
			m_ui{ui}
		{ }

		void update()
		{
			for(auto& a : m_clients)
				a.second.update();
		}

		void init()
		{
			QObject::connect(m_ui.m_cb_ec_servers, SIGNAL(currentIndexChanged(int)), this, SLOT(switch_server(int)));

			QObject::connect(m_ui.m_pb_ec_connect, SIGNAL(clicked()), this, SLOT(connect()));
			QObject::connect(m_ui.m_pb_ec_change_map, SIGNAL(clicked()), this, SLOT(send_map_change()));
			QObject::connect(m_ui.m_pb_ec_reload_map, SIGNAL(clicked()), this, SLOT(send_reload_map()));
			QObject::connect(m_ui.m_pb_ec_start_recording, SIGNAL(clicked()), this, SLOT(send_start_recording()));
			QObject::connect(m_ui.m_pb_ec_stop_recording, SIGNAL(clicked()), this, SLOT(send_stop_recording()));
			QObject::connect(m_ui.m_pb_ec_set_broadcast, SIGNAL(clicked()), this, SLOT(send_broadcast()));
			QObject::connect(m_ui.m_pb_ec_restart_round, SIGNAL(clicked()), this, SLOT(send_restart()));
			QObject::connect(m_ui.m_pb_ec_send_chatmsg, SIGNAL(clicked()), this, SLOT(send_chat()));
			QObject::connect(m_ui.m_pb_ec_send_custom_cmd, SIGNAL(clicked()), this, SLOT(send_custom_cmd()));
			QObject::connect(m_ui.m_pb_ec_shutdown, SIGNAL(clicked()), this, SLOT(send_shutdown()));
		}

		void connect(const mlk::ntw::ip_address& addr, const std::string& passwd)
		{
			auto iter(mlk::cnt::find_in_if([&addr](const decltype(m_clients)::value_type& c){return c.second.address() == addr;}, m_clients));

			// connected, client needs login
			if(iter != std::end(m_clients))
			{std::cout << iter->second.connected() << " : " << iter->second.logged_in() << std::endl;
				if(!iter->second.connected())
					iter->second.connect(addr);

				if(!iter->second.logged_in())
					iter->second.login(passwd);
			}

			// not connected, client needs connection and login
			else
			{
				// create new client
				m_clients.emplace(m_current_id, addr);
				auto& cl(m_clients[m_current_id]);
				cl.on_log_added =
				[this, id = m_current_id](const std::string& str)
				{
					if((m_ui.m_cb_ec_servers->currentIndex() == 0) ||
					   (m_ui.m_cb_ec_servers->currentData().toInt() != static_cast<int>(id)))
						return;

					m_ui.m_te_ec_log->moveCursor(QTextCursor::End);
					m_ui.m_te_ec_log->insertPlainText(str.c_str());
					m_ui.m_te_ec_log->moveCursor(QTextCursor::End);
				};
				cl.on_connection = [this, id = m_current_id]{this->_on_connection(id);};
				cl.on_login = [this, id = m_current_id]{this->_on_login(id);};
				cl.on_connection_timeout = []{std::cout << "timeout" << std::endl;};
				cl.login(passwd);
				++m_current_id;

			}
		}

		void send_command(const twl::econ_command& cmd)
		{
			auto index(m_ui.m_cb_ec_servers->currentIndex());
			if(index == 0)
			{
				// apply on all connected servers
				for(auto& a : m_clients)
					a.second.send_command(cmd);
			}
			else if(index > 0)
			{
				// get id (index != id)
				auto id(m_ui.m_cb_ec_servers->itemData(index).toInt());
				m_clients[id].send_command(cmd);
			}
		}

	private:
		void _on_connection(mlk::uint id)
		{

			for(auto i(1); i < m_ui.m_cb_ec_servers->count(); ++i)
				if(m_ui.m_cb_ec_servers->itemData(i).toInt() == static_cast<int>(id))
				{
					// we were already connected, choose right item
					m_ui.m_cb_ec_servers->setCurrentIndex(i);
					return;
				}


			// new connection
			m_ui.m_cb_ec_servers->addItem(m_clients[id].address().to_string().c_str(), id);
			m_ui.m_cb_ec_servers->setCurrentIndex(m_ui.m_cb_ec_servers->count() - 1);

			std::cout << "connection on id " << id << std::endl;
		}

		void _on_login(mlk::uint id)
		{
			std::cout << "login on id " << id << std::endl;
		}

	private slots:
		void switch_server(int index)
		{
			if(index == 0)
				return;
			auto id(m_ui.m_cb_ec_servers->itemData(index).toInt());

			// switch server, reset ui
			m_ui.m_te_ec_log->setText(m_clients[id].log().c_str());
		}

		void connect()
		{this->connect({QString{m_ui.m_le_ec_host->text() + ":" + m_ui.m_sb_ec_port->text()}.toStdString()}, m_ui.m_le_ec_passwd->text().toStdString());}

		void send_map_change()
		{this->send_command("change_map " + m_ui.m_le_ec_mapname->text().toStdString());}

		void send_reload_map()<commit_msg>removed some testing output<commit_after>//
// Copyright (c) 2014 <NAME>
// See LICENSE for more information.
//

#ifndef TWSM_ECON_HPP
#define TWSM_ECON_HPP


#include "ui_main_window.h"

#include <twl/network/network.hpp>

#include <QObject>


namespace Ui
{class main_window;}

namespace twsm
{
	class econ : public QObject
	{
		Q_OBJECT

		Ui::main_window& m_ui;

		std::map<std::size_t, twl::econ_client> m_clients;
		mlk::uint m_current_id{0};

	public:
		econ(Ui::main_window& ui) :
			m_ui{ui}
		{ }

		void update()
		{
			for(auto& a : m_clients)
				a.second.update();
		}

		void init()
		{
			QObject::connect(m_ui.m_cb_ec_servers, SIGNAL(currentIndexChanged(int)), this, SLOT(switch_server(int)));

			QObject::connect(m_ui.m_pb_ec_connect, SIGNAL(clicked()), this, SLOT(connect()));
			QObject::connect(m_ui.m_pb_ec_change_map, SIGNAL(clicked()), this, SLOT(send_map_change()));
			QObject::connect(m_ui.m_pb_ec_reload_map, SIGNAL(clicked()), this, SLOT(send_reload_map()));
			QObject::connect(m_ui.m_pb_ec_start_recording, SIGNAL(clicked()), this, SLOT(send_start_recording()));
			QObject::connect(m_ui.m_pb_ec_stop_recording, SIGNAL(clicked()), this, SLOT(send_stop_recording()));
			QObject::connect(m_ui.m_pb_ec_set_broadcast, SIGNAL(clicked()), this, SLOT(send_broadcast()));
			QObject::connect(m_ui.m_pb_ec_restart_round, SIGNAL(clicked()), this, SLOT(send_restart()));
			QObject::connect(m_ui.m_pb_ec_send_chatmsg, SIGNAL(clicked()), this, SLOT(send_chat()));
			QObject::connect(m_ui.m_pb_ec_send_custom_cmd, SIGNAL(clicked()), this, SLOT(send_custom_cmd()));
			QObject::connect(m_ui.m_pb_ec_shutdown, SIGNAL(clicked()), this, SLOT(send_shutdown()));
		}

		void connect(const mlk::ntw::ip_address& addr, const std::string& passwd)
		{
			auto iter(mlk::cnt::find_in_if([&addr](const decltype(m_clients)::value_type& c){return c.second.address() == addr;}, m_clients));

			// connected, client needs login
			if(iter != std::end(m_clients))
			{
				if(!iter->second.connected())
					iter->second.connect(addr);

				if(!iter->second.logged_in())
					iter->second.login(passwd);
			}

			// not connected, client needs connection and login
			else
			{
				// create new client
				m_clients.emplace(m_current_id, addr);
				auto& cl(m_clients[m_current_id]);
				cl.on_log_added =
				[this, id = m_current_id](const std::string& str)
				{
					if((m_ui.m_cb_ec_servers->currentIndex() == 0) ||
					   (m_ui.m_cb_ec_servers->currentData().toInt() != static_cast<int>(id)))
						return;

					m_ui.m_te_ec_log->moveCursor(QTextCursor::End);
					m_ui.m_te_ec_log->insertPlainText(str.c_str());
					m_ui.m_te_ec_log->moveCursor(QTextCursor::End);
				};
				cl.on_connection = [this, id = m_current_id]{this->_on_connection(id);};
				cl.on_login = [this, id = m_current_id]{this->_on_login(id);};
				cl.on_connection_timeout = []{std::cout << "timeout" << std::endl;};
				cl.login(passwd);
				++m_current_id;

			}
		}

		void send_command(const twl::econ_command& cmd)
		{
			auto index(m_ui.m_cb_ec_servers->currentIndex());
			if(index == 0)
			{
				// apply on all connected servers
				for(auto& a : m_clients)
					a.second.send_command(cmd);
			}
			else if(index > 0)
			{
				// get id (index != id)
				auto id(m_ui.m_cb_ec_servers->itemData(index).toInt());
				m_clients[id].send_command(cmd);
			}
		}

	private:
		void _on_connection(mlk::uint id)
		{
			for(auto i(1); i < m_ui.m_cb_ec_servers->count(); ++i)
				if(m_ui.m_cb_ec_servers->itemData(i).toInt() == static_cast<int>(id))
				{
					// we were already connected, choose right item
					m_ui.m_cb_ec_servers->setCurrentIndex(i);
					return;
				}


			// new connection
			m_ui.m_cb_ec_servers->addItem(m_clients[id].address().to_string().c_str(), id);
			m_ui.m_cb_ec_servers->setCurrentIndex(m_ui.m_cb_ec_servers->count() - 1);
		}

		void _on_login(mlk::uint id)
		{

		}

	private slots:
		void switch_server(int index)
		{
			if(index == 0)
				return;
			auto id(m_ui.m_cb_ec_servers->itemData(index).toInt());

			// switch server, reset ui
			m_ui.m_te_ec_log->setText(m_clients[id].log().c_str());
		}

		void connect()
		{this->connect({QString{m_ui.m_le_ec_host->text() + ":" + m_ui.m_sb_ec_port->text()}.toStdString()}, m_ui.m_le_ec_passwd->text().toStdString());}

		void send_map_change()
		{this->send_command("change_map " + m_ui.m_le_ec_mapname->text().toStdString());}

		void send_reload_map()
{'fraction_non_alphanumeric': 0.16976538291279328, 'fraction_numerical': 0.002434705621956618, 'mean_word_length': 4.486945962355798, 'pattern_counts': {'":': 2, '<': 45, '<?xml version=': 0, '>': 73, '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': '5183146', 'n_tokens_mistral': 4106, 'n_tokens_neox': 3569, 'n_words': 504}