[Objects] Convert Add/Delete/Update of Objects to Repositories (#3966)

* [Objects] Convert Add/Delete/Update of Objects to Repositories

- Convert `AddObject()`, `DeleteObject()`, and `UpdateObject()` to repositories.

* Update object_manipulation.cpp

* Update object_manipulation.cpp

* Update object_manipulation.cpp
This commit is contained in:
Alex King 2024-01-12 23:55:46 -05:00 committed by GitHub
parent 8cb15f9357
commit 06e8d258e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 114 deletions

View File

@ -132,9 +132,22 @@ void ObjectManipulation::CommandHandler(Client *c, const Seperator *sep)
) )
); );
const bool object_found = l.empty(); for (const auto& e : l) {
c->Message(
Chat::White,
fmt::format(
"ID: {} Name: {} XYZ: {:.2f}, {:.2f}, {:.2f} Heading: {:.2f}",
e.id,
e.objectname,
e.xpos,
e.ypos,
e.zpos,
e.heading
).c_str()
);
}
if (object_found) { if (!l.empty()) {
c->Message(Chat::White, "An object already exists at this location."); c->Message(Chat::White, "An object already exists at this location.");
return; return;
} }
@ -189,9 +202,13 @@ void ObjectManipulation::CommandHandler(Client *c, const Seperator *sep)
Object *o = entity_list.GetObjectByID(c->GetObjectToolEntityId()); Object *o = entity_list.GetObjectByID(c->GetObjectToolEntityId());
if (!o) {
c->Message(Chat::White, "You do not have a valid selected object.");
return;
}
const uint32 object_id = o->GetDBID(); const uint32 object_id = o->GetDBID();
if (o) {
auto app = new EQApplicationPacket(); auto app = new EQApplicationPacket();
o->CreateDeSpawnPacket(app); o->CreateDeSpawnPacket(app);
entity_list.QueueClients(nullptr, app); entity_list.QueueClients(nullptr, app);
@ -225,58 +242,6 @@ void ObjectManipulation::CommandHandler(Client *c, const Seperator *sep)
).c_str() ).c_str()
); );
} }
return;
}
const auto &e = ObjectRepository::GetWhere(
content_db,
fmt::format(
"id = {} AND zoneid = {} AND version = {} LIMIT 1",
object_id,
zone->GetZoneID(),
zone->GetInstanceVersion()
)
);
if (e[0].type == ObjectTypes::Temporary) {
c->Message(
Chat::White,
fmt::format(
"Object ID {} is a temporarily spawned ground spawn or dropped item, which is not supported with #object. See the 'ground_spawns' table in the database.",
object_id
).c_str()
);
return;
}
const int deleted_object = ObjectRepository::DeleteWhere(
content_db,
fmt::format(
"id = {} AND zoneid = {} AND version = {}",
object_id,
zone->GetZoneID(),
zone->GetInstanceVersion()
)
);
if (deleted_object) {
c->Message(
Chat::White,
fmt::format(
"Successfully deleted Object ID {}.",
object_id
).c_str()
);
} else {
c->Message(
Chat::White,
fmt::format(
"Failed to delete Object ID {}.",
object_id
).c_str()
);
}
} else if (is_edit) { } else if (is_edit) {
Object *o = entity_list.GetObjectByID(c->GetObjectToolEntityId()); Object *o = entity_list.GetObjectByID(c->GetObjectToolEntityId());

View File

@ -30,6 +30,7 @@
#include "../common/repositories/criteria/content_filter_criteria.h" #include "../common/repositories/criteria/content_filter_criteria.h"
#include "../common/events/player_event_logs.h" #include "../common/events/player_event_logs.h"
#include "../common/repositories/ground_spawns_repository.h" #include "../common/repositories/ground_spawns_repository.h"
#include "../common/repositories/object_repository.h"
const char DEFAULT_OBJECT_NAME[] = "IT63_ACTORDEF"; const char DEFAULT_OBJECT_NAME[] = "IT63_ACTORDEF";
@ -669,10 +670,13 @@ bool Object::HandleClick(Client* sender, const ClickObject_Struct* click_object)
return true; return true;
} }
// Add new Zone Object (theoretically only called for items dropped to ground) uint32 ZoneDatabase::AddObject(
uint32 ZoneDatabase::AddObject(uint32 type, uint32 icon, const Object_Struct& object, const EQ::ItemInstance* inst) uint32 type,
uint32 icon,
const Object_Struct& o,
const EQ::ItemInstance* inst
)
{ {
uint32 database_id = 0;
uint32 item_id = 0; uint32 item_id = 0;
int16 charges = 0; int16 charges = 0;
@ -681,35 +685,39 @@ uint32 ZoneDatabase::AddObject(uint32 type, uint32 icon, const Object_Struct& ob
charges = inst->GetCharges(); charges = inst->GetCharges();
} }
// SQL Escape object_name auto e = ObjectRepository::NewEntity();
uint32 len = strlen(object.object_name) * 2 + 1;
auto object_name = new char[len];
DoEscapeString(object_name, object.object_name, strlen(object.object_name));
// Save new record for object e.zoneid = o.zone_id;
std::string query = StringFormat("INSERT INTO object " e.xpos = o.x;
"(zoneid, xpos, ypos, zpos, heading, " e.ypos = o.y;
"itemid, charges, objectname, type, icon) " e.zpos = o.z;
"values (%i, %f, %f, %f, %f, %i, %i, '%s', %i, %i)", e.heading = o.heading;
object.zone_id, object.x, object.y, object.z, object.heading, e.itemid = item_id;
item_id, charges, object_name, type, icon); e.charges = charges;
safe_delete_array(object_name); e.objectname = o.object_name;
auto results = QueryDatabase(query); e.type = type;
if (!results.Success()) { e.icon = icon;
LogError("Unable to insert object: [{}]", results.ErrorMessage().c_str());
e = ObjectRepository::InsertOne(*this, e);
if (!e.id) {
return 0; return 0;
} }
// Save container contents, if container
if (inst && inst->IsType(EQ::item::ItemClassBag)) { if (inst && inst->IsType(EQ::item::ItemClassBag)) {
SaveWorldContainer(object.zone_id, database_id, inst); SaveWorldContainer(o.zone_id, e.id, inst);
} }
return database_id; return e.id;
} }
// Update information about existing object in database void ZoneDatabase::UpdateObject(
void ZoneDatabase::UpdateObject(uint32 id, uint32 type, uint32 icon, const Object_Struct& object, const EQ::ItemInstance* inst) uint32 object_id,
uint32 type,
uint32 icon,
const Object_Struct& o,
const EQ::ItemInstance* inst
)
{ {
uint32 item_id = 0; uint32 item_id = 0;
int16 charges = 0; int16 charges = 0;
@ -720,32 +728,31 @@ void ZoneDatabase::UpdateObject(uint32 id, uint32 type, uint32 icon, const Objec
} }
if (inst && !inst->IsType(EQ::item::ItemClassBag)) { if (inst && !inst->IsType(EQ::item::ItemClassBag)) {
uint32 len = strlen(object.object_name) * 2 + 1; auto e = ObjectRepository::FindOne(*this, object_id);
auto object_name = new char[len];
DoEscapeString(object_name, object.object_name, strlen(object.object_name));
// Save new record for object e.zoneid = o.zone_id;
std::string query = StringFormat( e.xpos = o.x;
"UPDATE object SET " e.ypos = o.y;
"zoneid = %i, xpos = %f, ypos = %f, zpos = %f, heading = %f, " e.zpos = o.z;
"itemid = %i, charges = %i, objectname = '%s', type = %i, icon = %i, " e.heading = o.heading;
"size = %f, tilt_x = %f, tilt_y = %f " e.itemid = item_id;
"WHERE id = %i", e.charges = charges;
object.zone_id, object.x, object.y, object.z, object.heading, e.objectname = o.object_name;
item_id, charges, object_name, type, icon, e.type = type;
object.size, object.tilt_x, object.tilt_y, id e.icon = icon;
); e.size = o.size;
safe_delete_array(object_name); e.tilt_x = o.tilt_x;
auto results = QueryDatabase(query); e.tilt_y = o.tilt_y;
if (!results.Success()) {
LogError("Unable to update object: [{}]", results.ErrorMessage().c_str()); const int updated = ObjectRepository::UpdateOne(*this, e);
if (!updated) {
return; return;
} }
} }
// Save container contents, if container
if (inst && inst->IsType(EQ::item::ItemClassBag)) { if (inst && inst->IsType(EQ::item::ItemClassBag)) {
SaveWorldContainer(object.zone_id, id, inst); SaveWorldContainer(o.zone_id, object_id, inst);
} }
} }
@ -790,17 +797,13 @@ GroundSpawns* ZoneDatabase::LoadGroundSpawns(
return gs; return gs;
} }
void ZoneDatabase::DeleteObject(uint32 id) void ZoneDatabase::DeleteObject(uint32 object_id)
{ {
if (id == 0) { if (!object_id) {
return; return;
} }
std::string query = StringFormat("DELETE FROM object WHERE id = %i", id); ObjectRepository::DeleteOne(*this, object_id);
auto results = QueryDatabase(query);
if (!results.Success()) {
LogError("Unable to delete object: [{}]", results.ErrorMessage().c_str());
}
} }
uint32 Object::GetDBID() uint32 Object::GetDBID()

View File

@ -379,8 +379,8 @@ public:
void SaveWorldContainer(uint32 zone_id, uint32 parent_id, const EQ::ItemInstance* container); void SaveWorldContainer(uint32 zone_id, uint32 parent_id, const EQ::ItemInstance* container);
void DeleteWorldContainer(uint32 parent_id,uint32 zone_id); void DeleteWorldContainer(uint32 parent_id,uint32 zone_id);
uint32 AddObject(uint32 type, uint32 icon, const Object_Struct& object, const EQ::ItemInstance* inst); uint32 AddObject(uint32 type, uint32 icon, const Object_Struct& object, const EQ::ItemInstance* inst);
void UpdateObject(uint32 id, uint32 type, uint32 icon, const Object_Struct& object, const EQ::ItemInstance* inst); void UpdateObject(uint32 object_id, uint32 type, uint32 icon, const Object_Struct& object, const EQ::ItemInstance* inst);
void DeleteObject(uint32 id); void DeleteObject(uint32 object_id);
GroundSpawns* LoadGroundSpawns(uint32 zone_id, int16 instance_version, GroundSpawns* gs); GroundSpawns* LoadGroundSpawns(uint32 zone_id, int16 instance_version, GroundSpawns* gs);
/* Traders */ /* Traders */