From 0561f600316c1d48dd57e93f94b3e0ae10c1d762 Mon Sep 17 00:00:00 2001 From: EmosewaMC <39972741+EmosewaMC@users.noreply.github.com> Date: Wed, 30 Mar 2022 21:58:59 -0700 Subject: [PATCH] Added negative checks --- .../PropertyManagementComponent.cpp | 34 +++++++++++-------- .../dComponents/PropertyManagementComponent.h | 4 ++- dGame/dComponents/PropertyVendorComponent.cpp | 7 ++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/dGame/dComponents/PropertyManagementComponent.cpp b/dGame/dComponents/PropertyManagementComponent.cpp index 431cb800..550d15fe 100644 --- a/dGame/dComponents/PropertyManagementComponent.cpp +++ b/dGame/dComponents/PropertyManagementComponent.cpp @@ -192,31 +192,35 @@ void PropertyManagementComponent::UpdatePropertyDetails(std::string name, std::s OnQueryPropertyData(GetOwner(), UNASSIGNED_SYSTEM_ADDRESS); } -void PropertyManagementComponent::Claim(const LWOOBJID playerId) +bool PropertyManagementComponent::Claim(const LWOOBJID playerId) { if (owner != LWOOBJID_EMPTY) { - return; + return false; } - - SetOwnerId(playerId); - - auto* zone = dZoneManager::Instance()->GetZone(); - - const auto& worldId = zone->GetZoneID(); - const auto zoneId = worldId.GetMapID(); auto* entity = EntityManager::Instance()->GetEntity(playerId); auto* user = entity->GetParentUser(); auto character = entity->GetCharacter(); - if (!character) return; + if (!character) return false; - const auto cloneId = character->GetPropertyCloneID(); + auto* zone = dZoneManager::Instance()->GetZone(); + + const auto& worldId = zone->GetZoneID(); + const auto propertyZoneId = worldId.GetMapID(); + const auto propertyCloneId = worldId.GetCloneID(); + + const auto playerCloneId = character->GetPropertyCloneID(); + + // If we are not on our clone do not allow us to claim the property + if (propertyCloneId != playerCloneId) return false; + + SetOwnerId(playerId); propertyId = ObjectIDManager::GenerateRandomObjectID(); - + auto* insertion = Database::CreatePreppedStmt( "INSERT INTO properties" "(id, owner_id, template_id, clone_id, name, description, rent_amount, rent_due, privacy_option, last_updated, time_claimed, rejection_reason, reputation, zone_id, performance_cost)" @@ -225,9 +229,9 @@ void PropertyManagementComponent::Claim(const LWOOBJID playerId) insertion->setUInt64(1, propertyId); insertion->setUInt64(2, (uint32_t) playerId); insertion->setUInt(3, templateId); - insertion->setUInt64(4, cloneId); + insertion->setUInt64(4, playerCloneId); insertion->setString(5, zone->GetZoneName().c_str()); - insertion->setInt(6, zoneId); + insertion->setInt(6, propertyZoneId); // Try and execute the query, print an error if it fails. try @@ -239,12 +243,14 @@ void PropertyManagementComponent::Claim(const LWOOBJID playerId) Game::logger->Log("PropertyManagementComponent", "Failed to execute query: (%s)!\n", exception.what()); throw exception; + return false; } auto* zoneControlObject = dZoneManager::Instance()->GetZoneControlObject(); for (CppScripts::Script* script : CppScripts::GetEntityScripts(zoneControlObject)) { script->OnZonePropertyRented(zoneControlObject, entity); } + return true; } void PropertyManagementComponent::OnStartBuilding() diff --git a/dGame/dComponents/PropertyManagementComponent.h b/dGame/dComponents/PropertyManagementComponent.h index fd208b63..c37282e9 100644 --- a/dGame/dComponents/PropertyManagementComponent.h +++ b/dGame/dComponents/PropertyManagementComponent.h @@ -101,8 +101,10 @@ public: /** * Makes this property owned by the passed player ID, storing it in the database * @param playerId the ID of the entity that claimed the property + * + * @return If the claim is successful return true. */ - void Claim(LWOOBJID playerId); + bool Claim(LWOOBJID playerId); /** * Event triggered when the owner of the property starts building, will kick other entities out diff --git a/dGame/dComponents/PropertyVendorComponent.cpp b/dGame/dComponents/PropertyVendorComponent.cpp index e6b8fe97..72e8ad64 100644 --- a/dGame/dComponents/PropertyVendorComponent.cpp +++ b/dGame/dComponents/PropertyVendorComponent.cpp @@ -41,13 +41,16 @@ void PropertyVendorComponent::OnBuyFromVendor(Entity* originator, const bool con { if (PropertyManagementComponent::Instance() == nullptr) return; + if (PropertyManagementComponent::Instance()->Claim(originator->GetObjectID()) == false) { + Game::logger->Log("PropertyVendorComponent", "FAILED TO CLAIM PROPERTY. PLAYER ID IS %llu\n", originator->GetObjectID()); + return; + } + GameMessages::SendPropertyRentalResponse(m_Parent->GetObjectID(), 0, 0, 0, 0, originator->GetSystemAddress()); auto* controller = dZoneManager::Instance()->GetZoneControlObject(); controller->OnFireEventServerSide(m_Parent, "propertyRented"); - - PropertyManagementComponent::Instance()->Claim(originator->GetObjectID()); PropertyManagementComponent::Instance()->SetOwner(originator);