DarkflameServer/dGame/TradingManager.cpp

286 lines
7.9 KiB
C++
Raw Normal View History

#include "TradingManager.h"
#include "EntityManager.h"
#include "GameMessages.h"
#include "InventoryComponent.h"
#include "../dWorldServer/ObjectIDManager.h"
#include "Game.h"
#include "Logger.h"
#include "Item.h"
#include "Character.h"
#include "CharacterComponent.h"
#include "MissionComponent.h"
#include "eMissionTaskType.h"
TradingManager* TradingManager::m_Address = nullptr;
2022-07-28 13:39:57 +00:00
Trade::Trade(LWOOBJID tradeId, LWOOBJID participantA, LWOOBJID participantB) {
m_TradeId = tradeId;
m_ParticipantA = participantA;
m_ParticipantB = participantB;
}
2022-07-28 13:39:57 +00:00
Trade::~Trade() {
}
2022-07-28 13:39:57 +00:00
LWOOBJID Trade::GetTradeId() const {
return m_TradeId;
}
2022-07-28 13:39:57 +00:00
bool Trade::IsParticipant(LWOOBJID playerId) const {
return m_ParticipantA == playerId || m_ParticipantB == playerId;
}
2022-07-28 13:39:57 +00:00
LWOOBJID Trade::GetParticipantA() const {
return m_ParticipantA;
}
2022-07-28 13:39:57 +00:00
LWOOBJID Trade::GetParticipantB() const {
return m_ParticipantB;
}
2022-07-28 13:39:57 +00:00
Entity* Trade::GetParticipantAEntity() const {
return Game::entityManager->GetEntity(m_ParticipantA);
}
2022-07-28 13:39:57 +00:00
Entity* Trade::GetParticipantBEntity() const {
return Game::entityManager->GetEntity(m_ParticipantB);
}
2022-07-28 13:39:57 +00:00
void Trade::SetCoins(LWOOBJID participant, uint64_t coins) {
if (participant == m_ParticipantA) {
m_CoinsA = coins;
} else if (participant == m_ParticipantB) {
m_CoinsB = coins;
}
}
2022-07-28 13:39:57 +00:00
void Trade::SetItems(LWOOBJID participant, std::vector<TradeItem> items) {
if (participant == m_ParticipantA) {
m_ItemsA = items;
} else if (participant == m_ParticipantB) {
m_ItemsB = items;
}
}
2022-07-28 13:39:57 +00:00
void Trade::SetAccepted(LWOOBJID participant, bool value) {
if (participant == m_ParticipantA) {
m_AcceptedA = !value;
LOG("Accepted from A (%d), B: (%d)", value, m_AcceptedB);
2022-07-28 13:39:57 +00:00
auto* entityB = GetParticipantBEntity();
if (entityB != nullptr) {
GameMessages::SendServerTradeAccept(m_ParticipantB, value, entityB->GetSystemAddress());
}
} else if (participant == m_ParticipantB) {
m_AcceptedB = !value;
LOG("Accepted from B (%d), A: (%d)", value, m_AcceptedA);
2022-07-28 13:39:57 +00:00
auto* entityA = GetParticipantAEntity();
if (entityA != nullptr) {
GameMessages::SendServerTradeAccept(m_ParticipantA, value, entityA->GetSystemAddress());
}
}
if (m_AcceptedA && m_AcceptedB) {
auto* entityB = GetParticipantBEntity();
if (entityB != nullptr) {
GameMessages::SendServerTradeAccept(m_ParticipantB, false, entityB->GetSystemAddress());
} else {
return;
}
auto* entityA = GetParticipantAEntity();
if (entityA != nullptr) {
GameMessages::SendServerTradeAccept(m_ParticipantA, false, entityA->GetSystemAddress());
} else {
return;
}
Complete();
TradingManager::Instance()->CancelTrade(m_TradeId);
2022-07-28 13:39:57 +00:00
}
}
2022-07-28 13:39:57 +00:00
void Trade::Complete() {
auto* entityA = GetParticipantAEntity();
auto* entityB = GetParticipantBEntity();
2022-07-28 13:39:57 +00:00
if (entityA == nullptr || entityB == nullptr) return;
2022-07-28 13:39:57 +00:00
auto* inventoryA = entityA->GetComponent<InventoryComponent>();
auto* inventoryB = entityB->GetComponent<InventoryComponent>();
auto* missionsA = entityA->GetComponent<MissionComponent>();
auto* missionsB = entityB->GetComponent<MissionComponent>();
auto* characterA = entityA->GetCharacter();
auto* characterB = entityB->GetCharacter();
2022-07-28 13:39:57 +00:00
if (inventoryA == nullptr || inventoryB == nullptr || characterA == nullptr || characterB == nullptr || missionsA == nullptr || missionsB == nullptr) return;
// First verify both players have the coins and items requested for the trade.
if (characterA->GetCoins() < m_CoinsA || characterB->GetCoins() < m_CoinsB) {
LOG("Possible coin trade cheating attempt! Aborting trade.");
return;
}
2022-07-28 13:39:57 +00:00
for (const auto& tradeItem : m_ItemsA) {
auto* itemToRemove = inventoryA->FindItemById(tradeItem.itemId);
if (itemToRemove) {
if (itemToRemove->GetCount() < tradeItem.itemCount) {
LOG("Possible cheating attempt from %s in trading!!! Aborting trade", characterA->GetName().c_str());
return;
}
} else {
LOG("Possible cheating attempt from %s in trading due to item not being available!!!", characterA->GetName().c_str());
return;
}
2022-07-28 13:39:57 +00:00
}
2022-07-28 13:39:57 +00:00
for (const auto& tradeItem : m_ItemsB) {
auto* itemToRemove = inventoryB->FindItemById(tradeItem.itemId);
if (itemToRemove) {
if (itemToRemove->GetCount() < tradeItem.itemCount) {
LOG("Possible cheating attempt from %s in trading!!! Aborting trade", characterB->GetName().c_str());
return;
}
} else {
LOG("Possible cheating attempt from %s in trading due to item not being available!!! Aborting trade", characterB->GetName().c_str());
return;
}
2022-07-28 13:39:57 +00:00
}
// Now actually do the trade.
characterA->SetCoins(characterA->GetCoins() - m_CoinsA + m_CoinsB, eLootSourceType::TRADE);
characterB->SetCoins(characterB->GetCoins() - m_CoinsB + m_CoinsA, eLootSourceType::TRADE);
2022-07-28 13:39:57 +00:00
for (const auto& tradeItem : m_ItemsA) {
auto* itemToRemove = inventoryA->FindItemById(tradeItem.itemId);
if (itemToRemove) itemToRemove->SetCount(itemToRemove->GetCount() - tradeItem.itemCount);
missionsA->Progress(eMissionTaskType::GATHER, tradeItem.itemLot, LWOOBJID_EMPTY, "", -tradeItem.itemCount);
inventoryB->AddItem(tradeItem.itemLot, tradeItem.itemCount, eLootSourceType::TRADE);
2022-07-28 13:39:57 +00:00
}
2022-07-28 13:39:57 +00:00
for (const auto& tradeItem : m_ItemsB) {
auto* itemToRemove = inventoryB->FindItemById(tradeItem.itemId);
if (itemToRemove) itemToRemove->SetCount(itemToRemove->GetCount() - tradeItem.itemCount);
missionsB->Progress(eMissionTaskType::GATHER, tradeItem.itemLot, LWOOBJID_EMPTY, "", -tradeItem.itemCount);
inventoryA->AddItem(tradeItem.itemLot, tradeItem.itemCount, eLootSourceType::TRADE);
2022-07-28 13:39:57 +00:00
}
2022-07-28 13:39:57 +00:00
characterA->SaveXMLToDatabase();
characterB->SaveXMLToDatabase();
return;
}
2022-07-28 13:39:57 +00:00
void Trade::Cancel() {
auto* entityA = GetParticipantAEntity();
auto* entityB = GetParticipantBEntity();
2022-07-28 13:39:57 +00:00
if (entityA == nullptr || entityB == nullptr) return;
2022-07-28 13:39:57 +00:00
GameMessages::SendServerTradeCancel(entityA->GetObjectID(), entityA->GetSystemAddress());
GameMessages::SendServerTradeCancel(entityB->GetObjectID(), entityB->GetSystemAddress());
}
2022-07-28 13:39:57 +00:00
void Trade::SendUpdateToOther(LWOOBJID participant) {
Entity* other = nullptr;
Entity* self = nullptr;
uint64_t coins;
std::vector<TradeItem> itemIds;
LOG("Attempting to send trade update");
2022-07-28 13:39:57 +00:00
if (participant == m_ParticipantA) {
other = GetParticipantBEntity();
self = GetParticipantAEntity();
coins = m_CoinsA;
itemIds = m_ItemsA;
} else if (participant == m_ParticipantB) {
other = GetParticipantAEntity();
self = GetParticipantBEntity();
coins = m_CoinsB;
itemIds = m_ItemsB;
} else {
return;
}
2022-07-28 13:39:57 +00:00
if (other == nullptr || self == nullptr) return;
2022-07-28 13:39:57 +00:00
std::vector<TradeItem> items{};
2022-07-28 13:39:57 +00:00
auto* inventoryComponent = self->GetComponent<InventoryComponent>();
2022-07-28 13:39:57 +00:00
if (inventoryComponent == nullptr) return;
2022-07-28 13:39:57 +00:00
for (const auto tradeItem : itemIds) {
auto* item = inventoryComponent->FindItemById(tradeItem.itemId);
2022-07-28 13:39:57 +00:00
if (item == nullptr) return;
2022-07-28 13:39:57 +00:00
if (tradeItem.itemCount > item->GetCount()) return;
2022-07-28 13:39:57 +00:00
items.push_back(tradeItem);
}
LOG("Sending trade update");
2022-07-28 13:39:57 +00:00
GameMessages::SendServerTradeUpdate(other->GetObjectID(), coins, items, other->GetSystemAddress());
}
2022-07-28 13:39:57 +00:00
TradingManager::TradingManager() {
}
2022-07-28 13:39:57 +00:00
TradingManager::~TradingManager() {
for (const auto& pair : trades) {
delete pair.second;
}
2022-07-28 13:39:57 +00:00
trades.clear();
}
2022-07-28 13:39:57 +00:00
Trade* TradingManager::GetTrade(LWOOBJID tradeId) const {
const auto& pair = trades.find(tradeId);
2022-07-28 13:39:57 +00:00
if (pair == trades.end()) return nullptr;
2022-07-28 13:39:57 +00:00
return pair->second;
}
2022-07-28 13:39:57 +00:00
Trade* TradingManager::GetPlayerTrade(LWOOBJID playerId) const {
for (const auto& pair : trades) {
if (pair.second->IsParticipant(playerId)) {
return pair.second;
}
}
return nullptr;
}
2022-07-28 13:39:57 +00:00
void TradingManager::CancelTrade(LWOOBJID tradeId) {
auto* trade = GetTrade(tradeId);
2022-07-28 13:39:57 +00:00
if (trade == nullptr) return;
2022-07-28 13:39:57 +00:00
delete trade;
2022-07-28 13:39:57 +00:00
trades.erase(tradeId);
}
2022-07-28 13:39:57 +00:00
Trade* TradingManager::NewTrade(LWOOBJID participantA, LWOOBJID participantB) {
const LWOOBJID tradeId = ObjectIDManager::Instance()->GenerateObjectID();
2022-07-28 13:39:57 +00:00
auto* trade = new Trade(tradeId, participantA, participantB);
2022-07-28 13:39:57 +00:00
trades[tradeId] = trade;
LOG("Created new trade between (%llu) <-> (%llu)", participantA, participantB);
2022-07-28 13:39:57 +00:00
return trade;
}