Files
DarkflameServer/dGame/dComponents/DonationVendorComponent.cpp
Aaron Kimbrell 2711c69e3e refactor: vendor, donation and trade game messages as structs
Converts the vendor, donation vendor and trade game messages to
NetGameMsgs in VendorMessages.{h,cpp} and TradeMessages.{h,cpp}:
VendorOpenWindow and VendorTransactionResult (SendToClient: the old
functions never broadcast), VendorStatusUpdate, ServerTradeInvite,
ServerTradeInitialReply, ServerTradeFinalReply, ServerTradeAccept,
ServerTradeCancel and ServerTradeUpdate, and the received
RequestVendorStatusUpdate, BuyFromVendor, SellToVendor,
BuybackFromVendor, AddDonationItem, RemoveDonationItem,
ConfirmDonationOnPlayer, CancelDonationOnPlayer, ClientTradeRequest,
ClientTradeCancel, ClientTradeAccept and ClientTradeUpdate. A trade
offer entry (the client's inventory item layout, optional fields and
config block included) is TradeItemEntry, shared by both trade updates.

Selling and buying back move into VendorComponent (SellToVendor,
BuybackFromVendor), adding and confirming donations into
DonationVendorComponent, and VendorComponent builds its own status
update and transaction results. Only the VENDOR component sends its
stock, as before. The received messages are registered in
GameMessageHandler's map, the switch cases and the old functions are
deleted. Handler logic is unchanged.

No wire change and no change in recipients. Verified byte for byte
against a frozen verbatim copy of the old functions over an input grid
(to one client and broadcast), received messages compared with the old
handlers' read sequences (every optional field of a trade entry,
raw and compressed config blocks) and truncated payloads rejected, hand
computed golden bytes, round trips, and a deliberate width mutation made
the tests fail. ConfirmDonationOnPlayer still accepts a message without
its vendor ID, since the old handler read nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:30:53 -05:00

108 lines
4.1 KiB
C++

#include "DonationVendorComponent.h"
#include "Database.h"
#include "CharacterComponent.h"
#include "EconomyLedger.h"
#include "eInventoryType.h"
#include "eMissionTaskType.h"
#include "EntityManager.h"
#include "Game.h"
#include "InventoryComponent.h"
#include "Item.h"
#include "LeaderboardManager.h"
#include "Logger.h"
#include "MissionComponent.h"
#include "User.h"
#include "UserManager.h"
DonationVendorComponent::DonationVendorComponent(Entity* parent, const int32_t componentID) : VendorComponent(parent, componentID) {
//LoadConfigData
m_PercentComplete = 0.0;
m_TotalDonated = 0;
m_TotalRemaining = 0;
// custom attribute to calculate other values
m_Goal = m_Parent->GetVar<int32_t>(u"donationGoal");
if (m_Goal == 0) m_Goal = INT32_MAX;
// Default to the nexus tower jawbox activity and setup settings
m_ActivityId = m_Parent->GetVar<uint32_t>(u"activityID");
if ((m_ActivityId == 0) || (m_ActivityId == 117)) {
m_ActivityId = 117;
m_PercentComplete = 1.0;
m_TotalDonated = INT32_MAX;
m_TotalRemaining = 0;
m_Goal = INT32_MAX;
return;
}
auto donationTotal = Database::Get()->GetDonationTotal(m_ActivityId);
if (donationTotal) m_TotalDonated = donationTotal.value();
m_TotalRemaining = m_Goal - m_TotalDonated;
m_PercentComplete = m_TotalDonated/static_cast<float>(m_Goal);
}
void DonationVendorComponent::SubmitDonation(uint32_t count) {
if (count <= 0 && ((m_TotalDonated + count) > 0)) return;
m_TotalDonated += count;
m_TotalRemaining = m_Goal - m_TotalDonated;
m_PercentComplete = m_TotalDonated/static_cast<float>(m_Goal);
m_DirtyDonationVendor = true;
}
void DonationVendorComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
VendorComponent::Serialize(outBitStream, bIsInitialUpdate);
outBitStream.Write(bIsInitialUpdate || m_DirtyDonationVendor);
if (bIsInitialUpdate || m_DirtyDonationVendor) {
outBitStream.Write(m_PercentComplete);
outBitStream.Write(m_TotalDonated);
outBitStream.Write(m_TotalRemaining);
if (!bIsInitialUpdate) m_DirtyDonationVendor = false;
}
}
void DonationVendorComponent::AddDonationItem(const SystemAddress& sysAddr, LWOOBJID itemObjID, uint32_t count) {
if (GetActivityID() == 0) {
LOG("WARNING: Trying to dontate to a vendor with no activity");
return;
}
User* user = UserManager::Instance()->GetUser(sysAddr);
if (!user) return;
Entity* player = Game::entityManager->GetEntity(user->GetLoggedInChar());
if (!player) return;
auto* characterComponent = player->GetComponent<CharacterComponent>();
if (!characterComponent) return;
auto* inventoryComponent = player->GetComponent<InventoryComponent>();
if (!inventoryComponent) return;
Item* item = inventoryComponent->FindItemById(itemObjID);
if (!item) return;
if (item->GetCount() < count) return;
characterComponent->SetCurrentInteracting(m_Parent->GetObjectID());
inventoryComponent->MoveItemToInventory(item, eInventoryType::DONATION, count, true, false, true);
}
void DonationVendorComponent::ConfirmDonation(Entity& player) {
const auto [inventoryComponent, missionComponent, characterComponent] = player.GetComponentsMut<InventoryComponent, MissionComponent, CharacterComponent>();
if (!inventoryComponent || !missionComponent || !characterComponent || !characterComponent->GetCurrentInteracting()) return;
if (GetActivityID() == 0) {
LOG("WARNING: Trying to dontate to a vendor with no activity");
return;
}
auto* inventory = inventoryComponent->GetInventory(eInventoryType::DONATION);
if (!inventory) return;
auto items = inventory->GetItems();
if (!items.empty()) {
uint32_t count = 0;
for (auto& [itemID, item] : items) {
count += item->GetCount();
EconomyLedger::RecordItemsUnsuppressed(&player, item->GetLot(), -static_cast<int64_t>(item->GetCount()), EconomyLedger::DONATION_SOURCE);
item->RemoveFromInventory();
}
missionComponent->Progress(eMissionTaskType::DONATION, 0, LWOOBJID_EMPTY, "", count);
LeaderboardManager::SaveScore(player.GetObjectID(), GetActivityID(), count);
SubmitDonation(count);
Game::entityManager->SerializeEntity(m_Parent);
}
characterComponent->SetCurrentInteracting(LWOOBJID_EMPTY);
}