crafting order

This commit is contained in:
luis
2026-10-23 01:44:01 -03:00
parent b33068de8f
commit ff76ccb854
3 changed files with 108 additions and 11 deletions
@@ -19,6 +19,7 @@
#include "CharacterDatabase.h"
#include "DatabaseEnv.h"
#include "GameTime.h"
#include "Item.h"
#include "Log.h"
#include "Mail.h"
#include "ObjectAccessor.h"
@@ -45,6 +46,34 @@ namespace
MailSender(MAIL_NORMAL, UI64LIT(0), MAIL_STATIONERY_DEFAULT), MAIL_CHECK_MASK_COPIED);
CharacterDatabase.CommitTransaction(trans);
}
// Returns the reagents a customer escrowed when posting an order. The originals were destroyed at create time, so
// fresh copies are mailed back (reagents for crafting orders are plain trade goods; the exact item instance is not
// meaningful). Currency reagents are not escrowed yet (see HandleCraftingOrderCreate), so only items are returned.
void MailCraftingOrderReagents(ObjectGuid recipient, std::vector<CraftingOrders::OrderReagent> const& reagents,
char const* subject, char const* body)
{
if (reagents.empty() || recipient.IsEmpty())
return;
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
MailDraft draft(subject, body);
for (CraftingOrders::OrderReagent const& reagent : reagents)
{
if (reagent.ItemID <= 0 || !reagent.Quantity)
continue;
if (Item* item = Item::CreateItem(uint32(reagent.ItemID), reagent.Quantity, ItemContext::NONE))
{
item->SaveToDB(trans);
draft.AddItem(item);
}
}
draft.SendMailTo(trans, MailReceiver(ObjectAccessor::FindConnectedPlayer(recipient), recipient.GetCounter()),
MailSender(MAIL_NORMAL, UI64LIT(0), MAIL_STATIONERY_DEFAULT), MAIL_CHECK_MASK_COPIED);
CharacterDatabase.CommitTransaction(trans);
}
}
CraftingOrderMgr& CraftingOrderMgr::Instance()
@@ -188,9 +217,11 @@ void CraftingOrderMgr::Update(uint32 diff)
}
else if (postingExpired)
{
// Order lapsed unclaimed: refund the customer's escrowed tip, then erase.
// Order lapsed unclaimed: refund the customer's escrowed tip and reagents, then erase.
MailCraftingOrderTip(order.CustomerGUID, order.TipAmount,
"Crafting Order Expired", "Your crafting order expired unclaimed. Your tip has been refunded.");
MailCraftingOrderReagents(order.CustomerGUID, order.Reagents,
"Crafting Order Expired", "Your crafting order expired unclaimed. Your reagents are enclosed.");
uint64 const expiredId = order.OrderID;
itr = _orders.erase(itr);
DeleteOrderFromDB(expiredId);
@@ -279,9 +310,11 @@ bool CraftingOrderMgr::RejectOrder(uint64 orderId, ObjectGuid crafter, std::stri
order->ClaimEndDate = 0;
SaveOrderToDB(*order);
// The order is declined and terminal: return the customer's escrowed tip.
// The order is declined and terminal: return the customer's escrowed tip and reagents.
MailCraftingOrderTip(order->CustomerGUID, order->TipAmount,
"Crafting Order Declined", "The crafter declined your order. Your tip has been refunded.");
MailCraftingOrderReagents(order->CustomerGUID, order->Reagents,
"Crafting Order Declined", "The crafter declined your order. Your reagents are enclosed.");
TC_LOG_DEBUG("network", "CraftingOrderMgr: order {} rejected by {} (reason: {})",
orderId, crafter.ToString(), reason.empty() ? "none" : reason);
@@ -295,6 +328,9 @@ bool CraftingOrderMgr::FulfillOrder(uint64 orderId, ObjectGuid crafter)
return false;
order->State = CraftingOrders::OrderState::Fulfilled;
// The reagents were escrowed from the customer when the order was posted and are consumed by this craft, so drop
// them from the record (SaveOrderToDB clears the reagent rows).
order->Reagents.clear();
SaveOrderToDB(*order);
// Release the escrowed tip to the crafter who completed the work.
@@ -311,9 +347,11 @@ bool CraftingOrderMgr::CancelOrder(uint64 orderId, ObjectGuid customer)
if (order->State != CraftingOrders::OrderState::Created)
return false; // can't cancel once a crafter has claimed it
// Refund the customer's escrowed tip before the order is erased.
// Refund the customer's escrowed tip and reagents before the order is erased.
MailCraftingOrderTip(order->CustomerGUID, order->TipAmount,
"Crafting Order Cancelled", "You cancelled your crafting order. Your tip has been refunded.");
MailCraftingOrderReagents(order->CustomerGUID, order->Reagents,
"Crafting Order Cancelled", "You cancelled your crafting order. Your reagents are enclosed.");
RemoveOrder(orderId);
return true;
@@ -92,7 +92,8 @@ public:
void LoadFromDB();
void Update(uint32 diff); // expiry ticker
// Post a new order; returns the new order id (0 on failure). Escrow of tip/reagents is the caller's job (P1).
// Post a new order; returns the new order id (0 on failure). The caller escrows the tip/reagents before calling
// this (HandleCraftingOrderCreate validates possession and removes them); terminal transitions refund them.
uint64 CreateOrder(Player* customer, CraftingOrders::Order order);
bool ClaimOrder(uint64 orderId, ObjectGuid crafter);
bool ReleaseOrder(uint64 orderId, ObjectGuid crafter); // un-claim, back to the pool
@@ -26,6 +26,7 @@
#include "Log.h"
#include "Mail.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "SpellInfo.h"
#include "SpellMgr.h"
@@ -89,15 +90,37 @@ void WorldSession::HandleCraftingOrderCreate(WorldPackets::CraftingOrders::Craft
int64 const now = GameTime::GetGameTime();
order.EndDate = now + 30 * DAY;
// Reagents the customer provided (Vectors[0]). Field semantics are not yet confirmed, so they are stored
// positionally; reagent validation/escrow is handled in a later phase. The wire is consumed byte-exact.
// Reagents the customer provided (Vectors[0]). Today the wire carries item reagents only (item id in Field1,
// count in Field2, optional slot in Extra). They are validated and escrowed up front like the tip; the order holds
// them until it is fulfilled (consumed) or dies (mailed back).
std::unordered_map<uint32, uint32> escrowedItems;
for (WorldPackets::CraftingOrders::CraftingReagentSlot const& slot : packet.Vectors[0])
{
if (!slot.Field2 || !sObjectMgr->GetItemTemplate(slot.Field1))
{
WorldPackets::CraftingOrders::CraftingOrderCreateResult result;
result.Result = WorldPackets::CraftingOrders::CraftingOrderResult::InvalidReagent;
SendPacket(result.Write());
return;
}
CraftingOrders::OrderReagent reagent;
reagent.ItemID = int32(slot.Field1);
reagent.Quantity = slot.Field2;
reagent.Slot = slot.Extra.value_or(0);
order.Reagents.push_back(reagent);
escrowedItems[slot.Field1] += slot.Field2;
}
for (auto const& [itemId, count] : escrowedItems)
{
if (!player->HasItemCount(itemId, count))
{
WorldPackets::CraftingOrders::CraftingOrderCreateResult result;
result.Result = WorldPackets::CraftingOrders::CraftingOrderResult::MissingItem;
SendPacket(result.Write());
return;
}
}
// The tip is escrowed up front (like an auction deposit): the customer must have the gold, and it is held by
@@ -123,9 +146,14 @@ void WorldSession::HandleCraftingOrderCreate(WorldPackets::CraftingOrders::Craft
if (!id)
return;
for (auto const& [itemId, count] : escrowedItems)
player->DestroyItemCount(itemId, count, true);
if (tip)
{
player->ModifyMoney(-int64(tip));
if (tip || !escrowedItems.empty())
{
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
player->SaveInventoryAndGoldToDB(trans);
CharacterDatabase.CommitTransaction(trans);
@@ -218,24 +246,54 @@ void WorldSession::HandleCraftingOrderFulfill(WorldPackets::CraftingOrders::Craf
if (ok)
{
// No crafted item rides the fulfil wire ? derive the recipe's output (SkillLineAbility -> spell ->
// SPELL_EFFECT_CREATE_ITEM) and mail it to the customer, mirroring the client's craft-then-fulfil flow.
// No crafted item rides the fulfil wire ? derive the recipe's output. Modern recipes use
// SPELL_EFFECT_CRAFT_ITEM whose MiscValue is the CraftingDataID (the actual output lives in CraftingData,
// possibly split across quality tiers); legacy recipes still use SPELL_EFFECT_CREATE_ITEM directly.
uint32 outItemId = 0;
int32 outCount = 1;
uint32 craftingDataId = 0;
bool isTradeskill = false;
if (SkillLineAbilityEntry const* ability = sSkillLineAbilityStore.LookupEntry(order->SkillLineAbilityID))
if (SpellInfo const* recipe = sSpellMgr->GetSpellInfo(ability->Spell, DIFFICULTY_NONE))
{
isTradeskill = recipe->HasAttribute(SPELL_ATTR0_IS_TRADESKILL);
for (SpellEffectInfo const& effect : recipe->GetEffects())
if (effect.IsEffect(SPELL_EFFECT_CREATE_ITEM))
{
if (effect.IsEffect(SPELL_EFFECT_CRAFT_ITEM) && effect.MiscValue)
{
craftingDataId = uint32(effect.MiscValue);
break;
}
if (effect.IsEffect(SPELL_EFFECT_CREATE_ITEM) && effect.ItemType)
{
outItemId = effect.ItemType;
outCount = std::max<int32>(1, effect.CalcValue(player));
break;
}
}
}
if (!outItemId && craftingDataId)
{
if (CraftingDataEntry const* craftingData = sCraftingDataStore.LookupEntry(craftingDataId))
{
if (craftingData->CraftedItemID > 0)
outItemId = uint32(craftingData->CraftedItemID);
else
{
std::vector<uint32> const items = sDB2Manager.GetCraftingDataItemIDByCraftingData(craftingData->ID);
if (!items.empty())
outItemId = items.front();
}
}
}
ObjectGuid const customerGuid = order->CustomerGUID;
if (outItemId)
{
if (Item* crafted = Item::CreateItem(outItemId, uint32(outCount), ItemContext::NONE, player))
ItemContext const context = isTradeskill ? ItemContext::Trade_Skill : ItemContext::NONE;
if (Item* crafted = Item::CreateItem(outItemId, uint32(outCount), context, player))
{
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
crafted->SaveToDB(trans);