[Corpses] Add corpse entity variables to DB (#4911)

* [Corpses] Add corpse entity variables to DB

- When corpses have entity variables add/remove/modified it will update the database accordingly.
- Corpse loading will pull the database values if they exist.

* Updates per review

* Update version.h

---------

Co-authored-by: Chris Miles <akkadius1@gmail.com>
This commit is contained in:
Fryguy
2025-06-25 16:21:52 -04:00
committed by GitHub
parent dc261bb203
commit 50ae0f8351
7 changed files with 87 additions and 1 deletions
+44
View File
@@ -32,7 +32,9 @@
#include "../common/repositories/character_corpse_items_repository.h"
#include <iostream>
#include "queryserv.h"
#include "../common/json/json.hpp"
using json = nlohmann::json;
extern EntityList entity_list;
extern Zone *zone;
@@ -674,6 +676,21 @@ bool Corpse::Save()
ce.drakkin_tattoo = drakkin_tattoo;
ce.drakkin_details = drakkin_details;
{
json j;
for (const auto& kv : m_EntityVariables) {
j[kv.first] = kv.second;
}
if (!j.empty()) {
ce.entity_variables = j.dump();
} else {
ce.entity_variables = "{}";
}
LogCorpses("Corpse entity_variables: %s", ce.entity_variables.c_str());
}
for (auto &item: m_item_list) {
CharacterCorpseItemEntry e;
@@ -2428,9 +2445,36 @@ Corpse *Corpse::LoadCharacterCorpse(
c->m_become_npc = false;
c->m_consented_guild_id = cc.guild_consent_id;
try {
if (Strings::IsValidJson(cc.entity_variables)) {
json j = json::parse(cc.entity_variables);
for (auto& el : j.items()) {
c->SetEntityVariable(el.key(), el.value().get<std::string>());
}
}
} catch (const std::exception& ex) {
LogError("Failed to parse entity_variables JSON for corpse ID %u: %s", cc.id, ex.what());
}
c->IsRezzed(cc.is_rezzed);
c->UpdateEquipmentLight();
return c;
}
void Corpse::SyncEntityVariablesToCorpseDB()
{
if (!m_is_player_corpse || m_corpse_db_id == 0) {
return;
}
json j;
for (const auto& [key, value] : m_EntityVariables) {
j[key] = value;
}
std::string serialized = j.dump();
CharacterCorpsesRepository::UpdateEntityVariables(database, m_corpse_db_id, serialized);
}