mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-15 09:04:10 +00:00
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>
This commit is contained in:
@@ -43,13 +43,13 @@ void AchievementVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
if (costLOT == -1 || !SellsItem(buyer, lot)) {
|
||||
auto* user = UserManager::Instance()->GetUser(buyer->GetSystemAddress());
|
||||
CheatDetection::ReportCheat(user, buyer->GetSystemAddress(), "Attempted to buy item %i from achievement vendor %i that is not purchasable", lot, m_Parent->GetLOT());
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
auto* inventoryComponent = buyer->GetComponent<InventoryComponent>();
|
||||
if (!inventoryComponent) {
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -65,12 +65,12 @@ void AchievementVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
|
||||
const uint32_t altCurrencyCost = itemComp.commendationCost * count;
|
||||
if (inventoryComponent->GetLotCount(costLOT) < altCurrencyCost || !inventoryComponent->RemoveItem(costLOT, altCurrencyCost, eInventoryType::ALL)) {
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
inventoryComponent->AddItem(lot, count, eLootSourceType::VENDOR);
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_SUCCESS);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_SUCCESS);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,18 @@
|
||||
#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
|
||||
@@ -46,3 +59,49 @@ void DonationVendorComponent::Serialize(RakNet::BitStream& outBitStream, bool bI
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,12 @@ public:
|
||||
uint32_t GetActivityID() {return m_ActivityId;};
|
||||
void SubmitDonation(uint32_t count);
|
||||
|
||||
// AddDonationItem from the player at sysAddr: moves count of the item into their donation inventory.
|
||||
void AddDonationItem(const SystemAddress& sysAddr, LWOOBJID itemObjID, uint32_t count);
|
||||
|
||||
// ConfirmDonationOnPlayer: the player donates everything in their donation inventory to this vendor.
|
||||
void ConfirmDonation(Entity& player);
|
||||
|
||||
private:
|
||||
bool m_DirtyDonationVendor = false;
|
||||
float m_PercentComplete = 0.0;
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
#include "eVendorTransactionResult.h"
|
||||
#include "UserManager.h"
|
||||
#include "CheatDetection.h"
|
||||
#include "VendorMessages.h"
|
||||
#include "Item.h"
|
||||
|
||||
VendorComponent::VendorComponent(Entity* parent, const int32_t componentID) : Component(parent, componentID) {
|
||||
m_HasStandardCostItems = false;
|
||||
@@ -31,8 +33,13 @@ void VendorComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitial
|
||||
}
|
||||
|
||||
void VendorComponent::OnUse(Entity* originator) {
|
||||
GameMessages::SendVendorOpenWindow(m_Parent, originator->GetSystemAddress());
|
||||
GameMessages::SendVendorStatusUpdate(m_Parent, originator->GetSystemAddress());
|
||||
GameMessages::VendorOpenWindow openWindow;
|
||||
openWindow.target = m_Parent->GetObjectID();
|
||||
openWindow.SendToClient(originator->GetSystemAddress());
|
||||
|
||||
// Only the entity's VENDOR component sends its stock (achievement and donation vendors never did).
|
||||
auto* vendor = static_cast<VendorComponent*>(m_Parent->GetComponent(eReplicaComponentType::VENDOR));
|
||||
if (vendor) vendor->SendStatusUpdate(originator->GetSystemAddress());
|
||||
}
|
||||
|
||||
void VendorComponent::RefreshInventory(bool isCreation) {
|
||||
@@ -90,7 +97,9 @@ void VendorComponent::RefreshInventory(bool isCreation) {
|
||||
});
|
||||
}
|
||||
Game::entityManager->SerializeEntity(m_Parent);
|
||||
GameMessages::SendVendorStatusUpdate(m_Parent, UNASSIGNED_SYSTEM_ADDRESS);
|
||||
// Only the entity's VENDOR component sends its stock (achievement and donation vendors never did).
|
||||
auto* vendor = static_cast<VendorComponent*>(m_Parent->GetComponent(eReplicaComponentType::VENDOR));
|
||||
if (vendor) vendor->SendStatusUpdate(UNASSIGNED_SYSTEM_ADDRESS);
|
||||
}
|
||||
|
||||
void VendorComponent::SetupConstants() {
|
||||
@@ -142,13 +151,13 @@ void VendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
if (!SellsItem(lot)) {
|
||||
auto* user = UserManager::Instance()->GetUser(buyer->GetSystemAddress());
|
||||
CheatDetection::ReportCheat(user, buyer->GetSystemAddress(), "Attempted to buy item %i from achievement vendor %i that is not purchasable", lot, m_Parent->GetLOT());
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
auto* inventoryComponent = buyer->GetComponent<InventoryComponent>();
|
||||
if (!inventoryComponent) {
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable<CDComponentsRegistryTable>();
|
||||
@@ -160,7 +169,7 @@ void VendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
auto craftingCurrencies = CDItemComponentTable::ParseCraftingCurrencies(itemComp);
|
||||
for (const auto& [crafintCurrencyLOT, crafintCurrencyCount]: craftingCurrencies) {
|
||||
if (inventoryComponent->GetLotCount(crafintCurrencyLOT) < (crafintCurrencyCount * count)) {
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -172,7 +181,7 @@ void VendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -181,14 +190,14 @@ void VendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
|
||||
Character* character = buyer->GetCharacter();
|
||||
if (!character || character->GetCoins() < coinCost) {
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Inventory::IsValidItem(itemComp.currencyLOT)) {
|
||||
const uint32_t altCurrencyCost = std::floor(itemComp.altCurrencyCost * buyScalar) * count;
|
||||
if (inventoryComponent->GetLotCount(itemComp.currencyLOT) < altCurrencyCost) {
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
inventoryComponent->RemoveItem(itemComp.currencyLOT, altCurrencyCost, eInventoryType::ALL);
|
||||
@@ -196,7 +205,7 @@ void VendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
|
||||
|
||||
character->SetCoins(character->GetCoins() - (coinCost), eLootSourceType::VENDOR);
|
||||
inventoryComponent->AddItem(lot, count, eLootSourceType::VENDOR);
|
||||
GameMessages::SendVendorTransactionResult(buyer, buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_SUCCESS);
|
||||
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_SUCCESS);
|
||||
}
|
||||
|
||||
bool VendorComponent::SetupItem(LOT item) {
|
||||
@@ -219,3 +228,90 @@ bool VendorComponent::SetupItem(LOT item) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void VendorComponent::SendStatusUpdate(const SystemAddress& sysAddr, bool bUpdateOnly) {
|
||||
GameMessages::VendorStatusUpdate statusUpdate;
|
||||
statusUpdate.target = m_Parent->GetObjectID();
|
||||
statusUpdate.bUpdateOnly = bUpdateOnly;
|
||||
statusUpdate.inventoryList.reserve(m_Inventory.size());
|
||||
for (const auto& item : m_Inventory) statusUpdate.inventoryList.push_back({ item.lot, item.sortPriority });
|
||||
statusUpdate.Send(sysAddr);
|
||||
}
|
||||
|
||||
void VendorComponent::SendTransactionResult(LWOOBJID target, const SystemAddress& sysAddr, eVendorTransactionResult result) {
|
||||
GameMessages::VendorTransactionResult transactionResult;
|
||||
transactionResult.target = target;
|
||||
transactionResult.iResult = result;
|
||||
transactionResult.SendToClient(sysAddr);
|
||||
}
|
||||
|
||||
void VendorComponent::SellToVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count) {
|
||||
Character* character = player.GetCharacter();
|
||||
if (!character) return;
|
||||
InventoryComponent* inv = static_cast<InventoryComponent*>(player.GetComponent(eReplicaComponentType::INVENTORY));
|
||||
if (!inv) return;
|
||||
|
||||
Item* item = inv->FindItemById(itemObjID);
|
||||
if (!item) return;
|
||||
|
||||
CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable<CDComponentsRegistryTable>();
|
||||
CDItemComponentTable* itemComponentTable = CDClientManager::GetTable<CDItemComponentTable>();
|
||||
|
||||
int itemCompID = compRegistryTable->GetByIDAndType(item->GetLot(), eReplicaComponentType::ITEM);
|
||||
CDItemComponent itemComp = itemComponentTable->GetItemComponentByID(itemCompID);
|
||||
|
||||
// Items with a base value of 0 or max int are special items that should not be sold if they're not sub items
|
||||
if (itemComp.baseValue == 0 || itemComp.baseValue == UINT_MAX) {
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
float sellScalar = GetSellScalar();
|
||||
if (Inventory::IsValidItem(itemComp.currencyLOT)) {
|
||||
const auto altCurrency = static_cast<uint32_t>(itemComp.altCurrencyCost * sellScalar) * count;
|
||||
inv->AddItem(itemComp.currencyLOT, std::floor(altCurrency), eLootSourceType::VENDOR); // Return alt currencies like faction tokens.
|
||||
}
|
||||
|
||||
inv->MoveItemToInventory(item, eInventoryType::VENDOR_BUYBACK, count, true, false, true);
|
||||
character->SetCoins(std::floor(character->GetCoins() + (static_cast<uint32_t>(itemComp.baseValue * sellScalar) * count)), eLootSourceType::VENDOR);
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_SUCCESS);
|
||||
}
|
||||
|
||||
void VendorComponent::BuybackFromVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count) {
|
||||
Character* character = player.GetCharacter();
|
||||
if (!character) return;
|
||||
InventoryComponent* inv = static_cast<InventoryComponent*>(player.GetComponent(eReplicaComponentType::INVENTORY));
|
||||
if (!inv) return;
|
||||
|
||||
Item* item = inv->FindItemById(itemObjID);
|
||||
if (!item) return;
|
||||
|
||||
CDComponentsRegistryTable* compRegistryTable = CDClientManager::GetTable<CDComponentsRegistryTable>();
|
||||
CDItemComponentTable* itemComponentTable = CDClientManager::GetTable<CDItemComponentTable>();
|
||||
|
||||
int itemCompID = compRegistryTable->GetByIDAndType(item->GetLot(), eReplicaComponentType::ITEM);
|
||||
CDItemComponent itemComp = itemComponentTable->GetItemComponentByID(itemCompID);
|
||||
|
||||
float sellScalar = GetSellScalar();
|
||||
|
||||
const auto cost = static_cast<uint32_t>(std::floor(((itemComp.baseValue * sellScalar) * count)));
|
||||
|
||||
if (character->GetCoins() < cost) {
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Inventory::IsValidItem(itemComp.currencyLOT)) {
|
||||
const uint32_t altCurrencyCost = std::floor(itemComp.altCurrencyCost * sellScalar) * count;
|
||||
if (inv->GetLotCount(itemComp.currencyLOT) < altCurrencyCost || !inv->RemoveItem(itemComp.currencyLOT, altCurrencyCost, eInventoryType::ALL)) {
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_FAIL);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//inv->RemoveItem(count, -1, iObjID);
|
||||
inv->MoveItemToInventory(item, Inventory::FindInventoryTypeForLot(item->GetLot()), count, true, false);
|
||||
character->SetCoins(character->GetCoins() - cost, eLootSourceType::VENDOR);
|
||||
//Game::entityManager->SerializeEntity(player); // so inventory updates
|
||||
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include "RakNetTypes.h"
|
||||
#include "eReplicaComponentType.h"
|
||||
|
||||
enum class eVendorTransactionResult : uint32_t;
|
||||
|
||||
struct SoldItem {
|
||||
SoldItem(const LOT lot, const int32_t sortPriority) {
|
||||
this->lot = lot;
|
||||
@@ -49,6 +51,16 @@ public:
|
||||
|
||||
void Buy(Entity* buyer, LOT lot, uint32_t count);
|
||||
|
||||
// SellToVendor / BuybackFromVendor from player (sent from sysAddr), for the item with object ID itemObjID.
|
||||
void SellToVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count);
|
||||
void BuybackFromVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count);
|
||||
|
||||
// Sends this vendor's stock (VendorStatusUpdate) to sysAddr; UNASSIGNED_SYSTEM_ADDRESS broadcasts.
|
||||
void SendStatusUpdate(const SystemAddress& sysAddr, bool bUpdateOnly = false);
|
||||
|
||||
// Sends a VendorTransactionResult targeting target to sysAddr only.
|
||||
static void SendTransactionResult(LWOOBJID target, const SystemAddress& sysAddr, eVendorTransactionResult result);
|
||||
|
||||
private:
|
||||
void HandleMrReeCameras();
|
||||
bool SetupItem(LOT item);
|
||||
|
||||
Reference in New Issue
Block a user