David Stone avatar David Stone committed 0a01a50

Moved more code out of the networked client.

Comments (0)

Files changed (16)

 // AI to win a 1v1 battle
-// Copyright (C) 2011 David Stone
+// Copyright (C) 2013 David Stone
 //
 // This file is part of Technical Machine.
 //
 #include "clients/network/invalid_packet.hpp"
 #include "clients/pokemon_lab/client.hpp"
 #include "clients/pokemon_online/client.hpp"
+#include "clients/pokemon_showdown/client.hpp"
 
 using namespace technicalmachine;
 

source/clients/battle.cpp

 #include <boost/filesystem.hpp>
 
 #include "battle_result.hpp"
+#include "client.hpp"
 #include "random_string.hpp"
 
-#include "network/client.hpp"
 #include "network/invalid_simulator_data.hpp"
 #include "network/outmessage.hpp"
 

source/clients/battle.hpp

 class Evaluate;
 class Gender;
 namespace network {
-class Client;
 class OutMessage;
 } // namespace network
 

source/clients/battle_settings.hpp

+// Generic Battle settings
+// Copyright (C) 2013 David Stone
+//
+// This file is part of Technical Machine.
+//
+// Technical Machine is free software: you can redistribute it and / or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef CLIENTS__BATTLE_SETTINGS_HPP_
+#define CLIENTS__BATTLE_SETTINGS_HPP_
+
+namespace technicalmachine {
+
+class BattleSettings {
+	public:
+		virtual bool are_acceptable () const = 0;
+		virtual ~BattleSettings () {}
+};
+
+}	// namespace technicalmachine
+#endif	// CLIENTS__BATTLE_SETTINGS_HPP_

source/clients/battles.cpp

 
 Battle & Battles::handle_begin(uint32_t const battle_id, std::string const & opponent) {
 	auto const it = challenges.find(opponent);
-	if (it == challenges.end())
+	if (it == challenges.end()) {
 		throw NoPendingChallenges(opponent);
+	}
 	Battle & battle = *it->second;
 	active.insert(std::make_pair(battle_id, std::move(it->second)));
 	challenges.erase(opponent);

source/clients/client.cpp

 // sim. Specific functions and data are found in the respective sim's folder.
 
 #include "client.hpp"
+
+#include <ctime>
+#include <iostream>
+
+#include "battle.hpp"
+#include "battle_settings.hpp"
 #include "random_string.hpp"
+
 #include "../settings_file.hpp"
 
 namespace technicalmachine {
 Client::Client(unsigned depth):
 	random_engine(rd()),
 	m_depth(depth) {
+	load_settings(false);
 }
 
 void Client::set_depth(unsigned const new_depth) {
 	return result;
 }
 
-Settings Client::load_settings() {
-	Settings settings;
+Settings Client::load_settings(bool const reload) {
+	if (reload) {
+		m_evaluation_constants.load();
+	}
+	Settings const settings;
 	team_file_name = settings.team_file;
 	time_format = settings.time_format;
 	return settings;
 }
 
-void Client::reload_settings() {
-	m_evaluation_constants.load();
-}
-
 DetailedStats const & Client::detailed() const {
 	return detailed_stats;
 }
 }
 
 Team Client::generate_team() {
+	std::cerr << team_file_name << '\n';
 	return Team(random_engine, team_file_name);
 }
 
 	return ::technicalmachine::random_string(random_engine, size);
 }
 
+void Client::handle_incoming_challenge(std::string const & opponent, BattleSettings const & settings) {
+	constexpr bool challenger = false;
+	handle_finalize_challenge(opponent, settings.are_acceptable(), challenger);
+}
+
 }	// namespace technicalmachine

source/clients/client.hpp

 #ifndef CLIENTS__CLIENT_HPP_
 #define CLIENTS__CLIENT_HPP_
 
+#include <cstdint>
 #include <random>
 #include <string>
 
 
 namespace technicalmachine {
 class Settings;
+class Party;
+enum class Result;
+class BattleSettings;
 
 class Client {
 	public:
 		DetailedStats const & detailed() const;
 		Evaluate const & evaluation_constants() const;
 		virtual ~Client() { }
+		void handle_incoming_challenge(std::string const & opponent, BattleSettings const & settings);
 	protected:
 		explicit Client(unsigned depth);
 		void set_depth(unsigned new_depth);
-		Settings load_settings();
+		Settings load_settings(bool reload);
 		Team generate_team();
-		void reload_settings();
 		template<typename ... Args>
 		void handle_challenge_withdrawn(Args && ... args) {
 			return battles.handle_challenge_withdrawn(std::forward<Args>(args)...);
 		}
+		virtual void handle_finalize_challenge(std::string const & opponent, bool accepted, bool challenger) = 0;
 		void handle_battle_begin(uint32_t battle_id, std::string const & opponent, Party party = Party());
 		void handle_battle_end(uint32_t battle_id, Result result);
 		template<typename ... Args>
 		bool challenges_are_queued() const;
 		std::string const & first_challenger() const;
 		std::string random_string(size_t size);
+		virtual void send_battle_challenge(std::string const & opponent) = 0;
 	private:
+		typedef std::mt19937 RandomEngine;
 		std::string time_stamp() const;
 		std::random_device rd;
-		std::mt19937 random_engine;
+		RandomEngine random_engine;
 		Battles battles;
 		DetailedStats detailed_stats;
 		Evaluate m_evaluation_constants;

source/clients/network/battle_settings.hpp

-// Generic Battle settings
-// Copyright (C) 2012 David Stone
-//
-// This file is part of Technical Machine.
-//
-// Technical Machine is free software: you can redistribute it and / or modify
-// it under the terms of the GNU Affero General Public License as
-// published by the Free Software Foundation, either version 3 of the
-// License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-#ifndef NETWORK__BATTLE_SETTINGS_HPP_
-#define NETWORK__BATTLE_SETTINGS_HPP_
-
-namespace technicalmachine {
-namespace network {
-
-class GenericBattleSettings {
-	public:
-		virtual bool are_acceptable () const = 0;
-		virtual ~GenericBattleSettings () {}
-};
-
-}	// namespace network
-}	// namespace technicalmachine
-#endif	// NETWORK__BATTLE_SETTINGS_HPP_

source/clients/network/client.cpp

 #include <boost/filesystem.hpp>
 #include <boost/lexical_cast.hpp>
 
-#include "battle_settings.hpp"
 #include "inmessage.hpp"
 #include "outmessage.hpp"
 
 #include "../battle.hpp"
+#include "../battle_settings.hpp"
 
 #include "../../exit_program.hpp"
 #include "../../settings_file.hpp"
 }
 
 void Client::load_settings (bool const reloading) {
-	auto settings = Base::load_settings();
+	auto settings = reloading ? Base::load_settings(true) : Settings();
 	chattiness = settings.chattiness;
 	
 	if (!reloading) {
 }
 
 
-void Client::handle_incoming_challenge (std::string const & opponent, GenericBattleSettings const & settings) {
+void Client::handle_incoming_challenge (std::string const & opponent, BattleSettings const & settings) {
 	bool const accepted = settings.are_acceptable () and is_trusted (opponent);
 	constexpr bool challenger = false;
 	handle_finalize_challenge (opponent, accepted, challenger);
 	highlights = load_highlights ();
 	trusted_users = load_trusted_users ();
 	load_settings (true);
-	reload_settings();
 }
 
 }	// namespace network

source/clients/network/client.hpp

 #include "../client.hpp"
 
 namespace technicalmachine {
+class BattleSettings;
 class Party;
 enum class Result;
 namespace network {
-class GenericBattleSettings;
 class InMessage;
 class OutMessage;
 
 		std::string const & username() const;
 		std::string const & password() const;
 		void handle_server_message (std::string const & sender, std::string const & message) const;
-		void handle_incoming_challenge (std::string const & opponent, GenericBattleSettings const & settings);
+		void handle_incoming_challenge (std::string const & opponent, BattleSettings const & settings);
 		template<typename Battle, typename ... Args>
 		Battle const & add_pending_challenge(Args && ... args) {
 			return Base::add_pending_challenge<Battle>(std::forward<Args>(args)...);
 		std::unique_ptr<Timer> make_timer() {
 			return std::unique_ptr<Timer>(new Timer(io));
 		}
-		virtual void handle_finalize_challenge (std::string const & opponent, bool accepted, bool challenger) = 0;
 		void handle_private_message (std::string const & sender, std::string const & message);
 	private:
 		bool is_trusted (std::string const & user) const;
 		void handle_part_channel_command (std::string const & request, size_t start);
 		void handle_send_pm_command (std::string const & request, size_t start);
 		void handle_reload_settings_command ();
-		virtual void send_battle_challenge (std::string const & opponent) = 0;
 
 		boost::asio::io_service io;
 		std::string host;

source/clients/pokemon_lab/battle_settings.cpp

 // Pokemon Lab Battle settings
-// Copyright (C) 2012 David Stone
+// Copyright (C) 2013 David Stone
 //
 // This file is part of Technical Machine.
 //
 namespace pl {
 
 BattleSettings::BattleSettings (uint8_t battle_generation, uint32_t battle_active_party_size, uint32_t battle_max_team_length):
-	network::GenericBattleSettings (),
+	::technicalmachine::BattleSettings(),
 	active_party_size (battle_active_party_size),
 	max_team_length (battle_max_team_length),
 	generation (battle_generation) {

source/clients/pokemon_lab/battle_settings.hpp

 // Pokemon Lab Battle settings
-// Copyright (C) 2012 David Stone
+// Copyright (C) 2013 David Stone
 //
 // This file is part of Technical Machine.
 //
 #define POKEMON_LAB__BATTLE_SETTINGS_HPP_
 
 #include <cstdint>
-#include "../network/battle_settings.hpp"
+#include "../battle_settings.hpp"
 
 namespace technicalmachine {
 namespace pl {
 
-class BattleSettings : public network::GenericBattleSettings {
+class BattleSettings : public ::technicalmachine::BattleSettings {
 	public:
-		BattleSettings (uint8_t battle_generation, uint32_t battle_active_party_size, uint32_t battle_max_team_length);
+		BattleSettings(uint8_t battle_generation, uint32_t battle_active_party_size, uint32_t battle_max_team_length);
 		bool are_acceptable () const;
 	private:
 		uint32_t active_party_size;

source/clients/pokemon_online/battle_settings.cpp

 }	// unnamed namespace
 
 BattleSettings::BattleSettings (uint32_t const battle_clauses, uint8_t const battle_mode):
-	network::GenericBattleSettings (),
+	::technicalmachine::BattleSettings(),
 	clauses (battle_clauses),
 	mode (static_cast <Mode> (battle_mode)) {
 }
 
 BattleSettings::BattleSettings (std::vector <uint32_t> const & battle_clauses, uint8_t const battle_mode):
-	network::GenericBattleSettings (),
+	::technicalmachine::BattleSettings(),
 	clauses (vector_to_clauses (battle_clauses)),
 	mode (static_cast <Mode> (battle_mode)) {
 }

source/clients/pokemon_online/battle_settings.hpp

 
 #include <cstdint>
 #include <vector>
-#include "../network/battle_settings.hpp"
+#include "../battle_settings.hpp"
 
 namespace technicalmachine {
 namespace po {
 
 class InMessage;
 
-class BattleSettings : public network::GenericBattleSettings {
+class BattleSettings : public ::technicalmachine::BattleSettings {
 	public:
 		enum Clauses : uint32_t {
 			SLEEP_CLAUSE = 1 << 0,

source/clients/pokemon_showdown/battle_settings.hpp

 #ifndef POKEMON_SHOWDOWN__BATTLE_SETTINGS_HPP_
 #define POKEMON_SHOWDOWN__BATTLE_SETTINGS_HPP_
 
-#include "../network/battle_settings.hpp"
+#include "../battle_settings.hpp"
 
 namespace technicalmachine {
 namespace ps {
 
-class BattleSettings : public network::GenericBattleSettings {
+class BattleSettings : public ::technicalmachine::BattleSettings {
 	public:
 		bool are_acceptable() const override;
 };
 	{
 	boost::filesystem::path team_file = team_file_name;
 	std::vector <boost::filesystem::path> const files = open_directory_and_add_files (team_file);
+	assert(files.size() > 0);
 	std::uniform_int_distribution <size_t> distribution (0, files.size () - 1);
 	team_file = files [distribution (random_engine)];
 	load(team_file.string());
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.