Record and send chat message history before game start

This commit is contained in:
Kingofd 2023-10-10 18:59:15 +02:00 committed by GitHub
parent ae79a2a561
commit f9fdd9758b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View file

@ -95,6 +95,7 @@ game::game(wesnothd::server& server, player_connections& player_connections,
, started_(false)
, level_()
, history_()
, chat_history_()
, description_(nullptr)
, current_turn_(0)
, current_side_index_(0)
@ -318,6 +319,7 @@ void game::start_game(player_iterator starter)
update_turn_data();
clear_history();
clear_chat_history();
// Send [observer] tags for all observers that are already in the game.
send_observerjoins();
@ -879,10 +881,10 @@ void game::process_message(simple_wml::document& data, player_iterator user)
simple_wml::node* const message = data.root().child("message");
assert(message);
message->set_attr_dup("sender", user->info().name().c_str());
const simple_wml::string_span& msg = (*message)["message"];
chat_message::truncate_message(msg, *message);
// Save chat as history to be sent to newly joining players
chat_history_.push_back(data.clone());
send_data(data, user);
}
@ -1417,6 +1419,8 @@ bool game::add_player(player_iterator player, bool observer)
send_history(player);
} else {
send_user_list();
// Send the game chat log, regardless if observer or not
send_chat_history(player);
}
const std::string clones = has_same_ip(player);
@ -1748,6 +1752,17 @@ void game::send_history(player_iterator player) const
}
}
void game::send_chat_history(player_iterator player) const
{
if(chat_history_.empty()) {
return;
}
for(auto& h : chat_history_) {
server.send_to_player(player, *h);
}
}
static bool is_invalid_filename_char(char c)
{
return !(isalnum(c) ||
@ -1845,6 +1860,11 @@ void game::clear_history()
history_.clear();
}
void game::clear_chat_history()
{
chat_history_.clear();
}
void game::set_description(simple_wml::node* desc)
{
description_ = desc;

View file

@ -442,6 +442,11 @@ public:
*/
void clear_history();
/**
* Clears the history of recorded chat WML documents.
*/
void clear_chat_history();
/**
* Records a WML document in the game's history.
*
@ -715,6 +720,7 @@ private:
void send_observerjoins(std::optional<player_iterator> player = {});
void send_observerquit(player_iterator observer);
void send_history(player_iterator sock) const;
void send_chat_history(player_iterator sock) const;
/** In case of a host transfer, notify the new host about its status. */
void notify_new_host();
@ -858,6 +864,8 @@ private:
/** Replay data. */
mutable std::vector<std::unique_ptr<simple_wml::document>> history_;
/** Replay chat history data. */
mutable std::vector<std::unique_ptr<simple_wml::document>> chat_history_;
/** Pointer to the game's description in the games_and_users_list_. */
simple_wml::node* description_;