simplify sending of [turn] data

1) We remove the syncmp_handler class which was a
   bit too complicated for its rather simple task
2) We move network related stuff out of playsingle_controller
This commit is contained in:
gfgtdf 2024-03-01 01:54:54 +01:00
parent 4b287691ef
commit 69fc479cfa
8 changed files with 25 additions and 96 deletions

View file

@ -290,6 +290,11 @@ public:
*/
void update_gui_to_player(const int team_index, const bool observe = false);
/// Sends replay [command]s to the server
virtual void send_actions() { }
/// Reads and executes replay [command]s from the server
virtual void receive_actions() { }
virtual bool is_networked_mp() const { return false; }
virtual void send_to_wesnothd(const config&, const std::string& = "unknown") const { }
virtual bool receive_from_wesnothd(config&) const { return false; }

View file

@ -46,6 +46,9 @@ playmp_controller::playmp_controller(const config& level, saved_game& state_of_g
: playsingle_controller(level, state_of_game, mp_info && mp_info->skip_replay)
, network_processing_stopped_(false)
, blindfold_(*gui_, mp_info && mp_info->skip_replay_blindfolded)
, replay_sender_(*resources::recorder)
, network_reader_([this](config& cfg) { return receive_from_wesnothd(cfg); })
, turn_data_(replay_sender_, network_reader_)
, mp_info_(mp_info)
{
// upgrade hotkey handler to the mp (network enabled) version
@ -420,7 +423,7 @@ void playmp_controller::surrender(int side_number)
turn_data_.send_data();
}
void playmp_controller::pull_remote_choice()
void playmp_controller::receive_actions()
{
turn_info::PROCESS_DATA_RESULT res = turn_data_.sync_network();
assert(res != turn_info::PROCESS_END_TURN);
@ -435,7 +438,7 @@ void playmp_controller::pull_remote_choice()
}
}
void playmp_controller::send_user_choice()
void playmp_controller::send_actions()
{
turn_data_.send_data();
}

View file

@ -20,7 +20,7 @@
#include "syncmp_handler.hpp"
struct mp_game_metadata;
class playmp_controller : public playsingle_controller, public syncmp_handler
class playmp_controller : public playsingle_controller
{
public:
playmp_controller(const config& level, saved_game& state_of_game, mp_game_metadata* mp_info);
@ -28,8 +28,8 @@ public:
void maybe_linger() override;
void process_oos(const std::string& err_msg) const override;
void pull_remote_choice() override;
void send_user_choice() override;
void receive_actions() override;
void send_actions() override;
void surrender(int side_number);
class hotkey_handler;
@ -66,5 +66,13 @@ protected:
blindfold blindfold_;
private:
void process_network_data(bool chat_only = false);
/// Helper to send our actions to the server
/// Used by turn_data_
replay_network_sender replay_sender_;
/// Used by turn_data_
playturn_network_adapter network_reader_;
/// Helper to read and execute (in particular replay data/ user actions ) messsages from the server
turn_info turn_data_;
mp_game_metadata* mp_info_;
};

View file

@ -67,9 +67,6 @@ static lg::log_domain log_enginerefac("enginerefac");
playsingle_controller::playsingle_controller(const config& level, saved_game& state_of_game, bool skip_replay)
: play_controller(level, state_of_game, skip_replay, true) // start faded
, cursor_setter_(cursor::NORMAL)
, replay_sender_(*resources::recorder)
, network_reader_([this](config& cfg) { return receive_from_wesnothd(cfg); })
, turn_data_(replay_sender_, network_reader_)
, end_turn_requested_(false)
, ai_fallback_(false)
, replay_controller_()
@ -650,7 +647,7 @@ void playsingle_controller::play_ai_turn()
}
undo_stack().clear();
turn_data_.send_data();
send_actions();
try {
try {
@ -672,7 +669,7 @@ void playsingle_controller::play_ai_turn()
require_end_turn();
}
turn_data_.sync_network();
send_actions();
gui_->recalculate_minimap();
gui_->invalidate_unit();
gui_->invalidate_game_status();

View file

@ -101,13 +101,6 @@ protected:
const cursor::setter cursor_setter_;
/// Helper to send our actions to the server
/// Used by turn_data_
replay_network_sender replay_sender_;
/// Used by turn_data_
playturn_network_adapter network_reader_;
/// Helper to read and execute (in particular replay data/ user actions ) messsages from the server
turn_info turn_data_;
/// true iff the user has pressed the end turn button this turn.
/// (or wants to end linger mode, which is implemented via the same button)
bool end_turn_requested_;

View file

@ -227,14 +227,14 @@ int synced_context::get_unit_id_diff()
void synced_context::pull_remote_user_input()
{
syncmp_registry::pull_remote_choice();
resources::controller->receive_actions();
}
// TODO: this is now also used for normal actions, maybe it should be renamed.
void synced_context::send_user_choice()
{
assert(undo_blocked());
syncmp_registry::send_user_choice();
resources::controller->send_actions();
}
std::shared_ptr<randomness::rng> synced_context::get_rng_for_action()

View file

@ -14,53 +14,3 @@
*/
#include "syncmp_handler.hpp"
#include <cassert>
#include <algorithm>
syncmp_handler::syncmp_handler()
{
syncmp_registry::add_handler(this);
}
syncmp_handler::~syncmp_handler()
{
syncmp_registry::remove_handler(this);
}
std::vector<syncmp_handler*>& syncmp_registry::handlers()
{
//using pointer in order to prevent destruction at program end. Although in this simple case it shouldn't matter.
static handler_list* handlers_ = new handler_list();
return *handlers_;
}
void syncmp_registry::remove_handler(syncmp_handler* handler)
{
handler_list::iterator elem = std::find(handlers().begin(), handlers().end(), handler);
assert(elem != handlers().end());
handlers().erase(elem);
}
void syncmp_registry::add_handler(syncmp_handler* handler)
{
handler_list::iterator elem = std::find(handlers().begin(), handlers().end(), handler);
assert(elem == handlers().end());
handlers().push_back(handler);
}
void syncmp_registry::pull_remote_choice()
{
for(syncmp_handler* phandler : handlers())
{
phandler->pull_remote_choice();
}
}
void syncmp_registry::send_user_choice()
{
for(syncmp_handler* phandler : handlers())
{
phandler->send_user_choice();
}
}

View file

@ -15,30 +15,3 @@
#pragma once
#include<vector>
/*
Automatically registrates itself in the registry in the constructor.
*/
class syncmp_handler
{
public:
syncmp_handler();
virtual void pull_remote_choice() = 0;
virtual void send_user_choice() = 0;
virtual ~syncmp_handler();
};
class syncmp_registry
{
public:
//called by get_user_choice while waiting for a remote user choice.
static void pull_remote_choice();
//called when get_user_choice was called and the client wants to send the choice to the other clients immideately
static void send_user_choice();
private:
friend class syncmp_handler;
typedef std::vector<syncmp_handler*> handler_list;
static void remove_handler(syncmp_handler* handler);
static void add_handler(syncmp_handler* handler);
static handler_list& handlers();
};