implement play time on BB

This commit is contained in:
Martin Michelsen
2022-09-03 21:17:46 -07:00
parent 0180296c49
commit 3e5a961b68
3 changed files with 31 additions and 4 deletions
+25 -2
View File
@@ -209,7 +209,7 @@ PlayerDispDataBBPreview PlayerDispDataBB::to_preview() const {
pre.proportion_x = this->proportion_x;
pre.proportion_y = this->proportion_y;
pre.name = this->name;
pre.play_time = 0; // TODO: Store this somewhere and return it here
pre.play_time = this->play_time;
return pre;
}
@@ -323,6 +323,13 @@ void PlayerBank::save(const string& filename, bool save_to_filesystem) const {
////////////////////////////////////////////////////////////////////////////////
ClientGameData::ClientGameData()
: last_play_time_update(0),
guild_card_number(0),
should_update_play_time(false),
bb_player_index(0),
should_save(true) { }
ClientGameData::~ClientGameData() {
if (!this->bb_username.empty()) {
if (this->account_data.get()) {
@@ -442,6 +449,9 @@ void ClientGameData::load_account_data() {
}
void ClientGameData::save_account_data() const {
if (!this->account_data.get()) {
throw logic_error("save_account_data called when no account data loaded");
}
string filename = this->account_data_filename();
player_files_cache.replace(filename, this->account_data.get(), sizeof(SavedAccountDataBB));
if (this->should_save) {
@@ -453,6 +463,7 @@ void ClientGameData::save_account_data() const {
}
void ClientGameData::load_player_data() {
this->last_play_time_update = now();
string filename = this->player_data_filename();
shared_ptr<SavedPlayerDataBB> data(new SavedPlayerDataBB(
player_files_cache.get_obj_or_load<SavedPlayerDataBB>(filename).obj));
@@ -464,7 +475,19 @@ void ClientGameData::load_player_data() {
player_data_log.info("Loaded player data file %s", filename.c_str());
}
void ClientGameData::save_player_data() const {
void ClientGameData::save_player_data() {
if (!this->player_data.get()) {
throw logic_error("save_player_data called when no player data loaded");
}
if (this->should_update_play_time) {
// This is slightly inaccurate, since fractions of a second are truncated
// off each time we save. I'm lazy, so insert shrug emoji here.
uint64_t t = now();
uint64_t seconds = (t - this->last_play_time_update) / 1000000;
this->player_data->disp.play_time += seconds;
player_data_log.info("Added %" PRIu64 " seconds to play time", seconds);
this->last_play_time_update = t;
}
string filename = this->player_data_filename();
player_files_cache.replace(filename, this->player_data.get(), sizeof(SavedPlayerDataBB));
if (this->should_save) {
+5 -2
View File
@@ -481,9 +481,11 @@ class ClientGameData {
private:
std::shared_ptr<SavedAccountDataBB> account_data;
std::shared_ptr<SavedPlayerDataBB> player_data;
uint64_t last_play_time_update;
public:
uint32_t guild_card_number;
bool should_update_play_time;
// The following fields are not saved, and are only used in certain situations
@@ -500,7 +502,7 @@ public:
std::vector<ItemData> shop_contents;
bool should_save;
ClientGameData() : guild_card_number(0), bb_player_index(0), should_save(true) { }
ClientGameData();
~ClientGameData();
std::shared_ptr<SavedAccountDataBB> account(bool should_load = true);
@@ -519,7 +521,8 @@ public:
void load_account_data();
void save_account_data() const;
void load_player_data();
void save_player_data() const;
// Note: This function is not const because it updates the player's play time.
void save_player_data();
void import_player(const PSOPlayerDataDCPC& pd);
void import_player(const PSOPlayerDataV3& pd);
+1
View File
@@ -129,6 +129,7 @@ void on_login_complete(shared_ptr<ServerState> s, shared_ptr<Client> c) {
if (c->version() == GameVersion::BB) {
// This implicitly loads the client's account and player data
send_complete_player_bb(c);
c->game_data.should_update_play_time = true;
}
send_lobby_list(c, s);