1. David Stone
  2. Technical Machine

Commits

David Stone  committed c10185f

Moved random number generation out of the networked client.

  • Participants
  • Parent commits f00a3b5
  • Branches default

Comments (0)

Files changed (4)

File source/clients/client.cpp

View file
 // sim. Specific functions and data are found in the respective sim's folder.
 
 #include "client.hpp"
-
+#include "random_string.hpp"
 #include "../settings_file.hpp"
 
 namespace technicalmachine {
 
+Client::Client():
+	random_engine(rd()) {
+}
+
 void Client::print_with_time_stamp (std::ostream & stream, std::string const & message) const {
 	stream << time_stamp() + " " + message + "\n";
 }
 
 Settings Client::load_settings() {
 	Settings settings;
+	team_file_name = settings.team_file;
 	time_format = settings.time_format;
 	return settings;
 }
 	return m_evaluation_constants;
 }
 
+Team Client::generate_team() {
+	return Team(random_engine, team_file_name);
+}
+
 void Client::handle_battle_begin(uint32_t battle_id, std::string const & opponent, Party const party) {
 	auto & battle = battles.handle_begin(battle_id, opponent);
 	battle.set_party_if_unknown(party);
 	return battles.first_challenger();
 }
 
+std::string Client::random_string(size_t const size) {
+	return ::technicalmachine::random_string(random_engine, size);
+}
+
 }	// namespace technicalmachine

File source/clients/client.hpp

View file
 #ifndef CLIENTS__CLIENT_HPP_
 #define CLIENTS__CLIENT_HPP_
 
+#include <random>
 #include <string>
 
 #include "battles.hpp"
 		Evaluate const & evaluation_constants() const;
 		virtual ~Client() { }
 	protected:
+		Client();
 		Settings load_settings();
+		Team generate_team();
 		void reload_settings();
 		template<typename ... Args>
 		void handle_challenge_withdrawn(Args && ... args) {
 			return battles.find(std::forward<Args>(args)...);
 		}
 		template<typename Battle, typename ... Args>
-		Battle const & add_pending_challenge(Args && ... args) {
-			return battles.add_pending_challenge<Battle>(std::forward<Args>(args)...);
+		Battle const & add_pending_challenge (Team const & team, std::string const & opponent, Args && ... args) {
+			return battles.add_pending_challenge<Battle>(opponent, rd(), std::forward<Args>(args)..., team);
+		}
+		template<typename Battle, typename ... Args>
+		Battle const & add_pending_challenge (std::string const & opponent, Args && ... args) {
+			return battles.add_pending_challenge<Battle>(opponent, rd(), std::forward<Args>(args)..., team_file_name);
 		}
 		bool challenges_are_queued() const;
 		std::string const & first_challenger() const;
+		std::string random_string(size_t size);
 	private:
 		std::string time_stamp() const;
+		std::random_device rd;
+		std::mt19937 random_engine;
 		Battles battles;
 		DetailedStats detailed_stats;
 		Evaluate m_evaluation_constants;
+		std::string team_file_name;
 		std::string time_format;
 };
 

File source/clients/network/client.cpp

View file
 #include "outmessage.hpp"
 
 #include "../battle.hpp"
-#include "../random_string.hpp"
 
 #include "../../exit_program.hpp"
 #include "../../settings_file.hpp"
 }	// unnamed namespace
 
 Client::Client(unsigned set_depth):
-	random_engine (rd ()),
 	highlights (load_highlights ()),
 	trusted_users (load_trusted_users ()),
 	depth (set_depth)
 
 void Client::load_settings (bool const reloading) {
 	auto settings = ::technicalmachine::Client::load_settings();
-	team_file_name = settings.team_file;
 	chattiness = settings.chattiness;
 	
 	if (!reloading) {
 		port = server.port;
 		current_username = server.username;
 		if (server.password.empty()) {
-			server.password = random_string(random_engine, 31);
+			server.password = random_string(31);
 			settings.write();
 		}
 		current_password = server.password;
 	return current_password;
 }
 
-Team Client::generate_team() {
-	return Team(random_engine, team_file_name);
-}
-
 bool Client::is_highlighted (std::string const & message) const {
 	// Easiest way I've thought of to see if anything in highlights is in the
 	// message is to do a search in the message on each of the elements in

File source/clients/network/client.hpp

View file
 #define CLIENTS__NETWORK__CLIENT_HPP_
 
 #include <cstdint>
-#include <random>
 #include <string>
 #include <vector>
 
 			read_header(msg);
 			io.run();
 		}
-		Team generate_team();
 		virtual void send_keep_alive_message () = 0;
 		std::string const & username() const;
 		std::string const & password() const;
 		void handle_incoming_challenge (std::string const & opponent, GenericBattleSettings const & settings);
 		template<typename Battle, typename ... Args>
 		Battle const & add_pending_challenge (Team const & team, std::string const & opponent, Args && ... args) {
-			return ::technicalmachine::Client::add_pending_challenge<Battle>(opponent, rd(), depth, std::forward<Args>(args)..., team);
+			return ::technicalmachine::Client::add_pending_challenge<Battle>(team, opponent, depth, std::forward<Args>(args)...);
 		}
 		template<typename Battle, typename ... Args>
 		Battle const & add_pending_challenge (std::string const & opponent, Args && ... args) {
-			return ::technicalmachine::Client::add_pending_challenge<Battle>(opponent, rd(), depth, std::forward<Args>(args)..., team_file_name);
+			return ::technicalmachine::Client::add_pending_challenge<Battle>(opponent, depth, std::forward<Args>(args)...);
 		}
 		template<typename Timer>
 		std::unique_ptr<Timer> make_timer() {
 		boost::asio::io_service io;
 		std::string host;
 		std::string port;
-		std::random_device rd;
-		std::mt19937 random_engine;
 		std::unique_ptr<boost::asio::ip::tcp::socket> socket;
 		std::string current_username;
 		std::string current_password;
-		std::string team_file_name;
 		std::vector <std::string> highlights;
 		std::vector <std::string> trusted_users;
 		unsigned chattiness;