From 722010c0f75b862be7477696725493e8b628566d Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Wed, 15 Nov 2023 22:37:18 -0800 Subject: [PATCH] add $save command --- README.md | 1 + src/ChatCommands.cc | 24 ++++++++++++++++++++++++ src/Client.cc | 12 ++++++++---- src/Client.hh | 1 + 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b5ee541a..5234abac 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,7 @@ Some commands only work on the game server and not on the proxy server. The chat * Blue Burst player commands (game server only) * `$bbchar <1-4>`: Use this command when playing on a non-BB version of PSO. If the username and password are correct, this command converts your current character to BB format and saves it on the server in the given slot. Any character already in that slot is overwritten. * `$edit `: Modifies your character data. If the server does not allow cheat mode anywhere (that is, "CheatModeBehavior" is "Off" in config.json), this command does nothing. + * `$save`: Saves your character, system, and Guild Card data immediately. (By default, your character is saved every 60 seconds while online, and your account and Guild Card data are saved whenever they change.) * Game state commands (game server only) * `$maxlevel `: Sets the maximum level for players to join the current game. (This only applies when joining; if a player joins and then levels up past this level during the game, they are not kicked out, but won't be able to rejoin if they leave.) diff --git a/src/ChatCommands.cc b/src/ChatCommands.cc index 6e28e857..6e89e38a 100644 --- a/src/ChatCommands.cc +++ b/src/ChatCommands.cc @@ -1026,6 +1026,29 @@ static void server_command_convert_char_to_bb(shared_ptr c, const std::s send_get_player_info(c); } +static void server_command_save(shared_ptr c, const std::string&) { + check_version(c, GameVersion::BB); + try { + c->game_data.save_character_file(); + send_text_message(c, "Character data saved"); + } catch (const exception& e) { + send_text_message_printf(c, "Can\'t save character:\n%s", e.what()); + } + try { + c->game_data.save_system_file(); + send_text_message(c, "System data saved"); + } catch (const exception& e) { + send_text_message_printf(c, "Can\'t save system data:\n%s", e.what()); + } + try { + c->game_data.save_guild_card_file(); + send_text_message(c, "Guild Card data saved"); + } catch (const exception& e) { + send_text_message_printf(c, "Can\'t save Guild Cards:\n%s", e.what()); + } + c->reschedule_save_game_data_event(); +} + //////////////////////////////////////////////////////////////////////////////// // Administration commands @@ -1695,6 +1718,7 @@ static const unordered_map chat_commands({ {"$qsync", {server_command_qsync, nullptr}}, {"$quest", {server_command_quest, nullptr}}, {"$rand", {server_command_rand, proxy_command_rand}}, + {"$save", {server_command_save, nullptr}}, {"$saverec", {server_command_saverec, nullptr}}, {"$sc", {server_command_send_client, proxy_command_send_client}}, {"$secid", {server_command_secid, proxy_command_secid}}, diff --git a/src/Client.cc b/src/Client.cc index ce742d88..8fa9e023 100644 --- a/src/Client.cc +++ b/src/Client.cc @@ -179,10 +179,7 @@ Client::Client( this->last_switch_enabled_command.header.subcommand = 0; memset(&this->next_connection_addr, 0, sizeof(this->next_connection_addr)); - if (this->version() == GameVersion::BB) { - struct timeval tv = usecs_to_timeval(60000000); // 1 minute - event_add(this->save_game_data_event.get(), &tv); - } + this->reschedule_save_game_data_event(); this->reschedule_ping_and_timeout_events(); this->log.info("Created"); @@ -199,6 +196,13 @@ Client::~Client() { this->log.info("Deleted"); } +void Client::reschedule_save_game_data_event() { + if (this->version() == GameVersion::BB) { + struct timeval tv = usecs_to_timeval(60000000); // 1 minute + event_add(this->save_game_data_event.get(), &tv); + } +} + void Client::reschedule_ping_and_timeout_events() { struct timeval ping_tv = usecs_to_timeval(30000000); // 30 seconds event_add(this->send_ping_event.get(), &ping_tv); diff --git a/src/Client.hh b/src/Client.hh index 4a3ac866..8a85f659 100644 --- a/src/Client.hh +++ b/src/Client.hh @@ -218,6 +218,7 @@ struct Client : public std::enable_shared_from_this { ServerBehavior server_behavior); ~Client(); + void reschedule_save_game_data_event(); void reschedule_ping_and_timeout_events(); inline GameVersion version() const {