int TradeShopHandler::helper_trade_shop_buyback(SimulatorThread *sim,
		CharacterServerData *pld, SimulatorQuery *query,
		CreatureInstance *creatureInstance,unsigned int CCSID) {
	InventorySlot *buybackItem = pld->charPtr->inventory.GetItemPtrByCCSID(
			CCSID);
	if (buybackItem == NULL)
		return PrepExt_QueryResponseError(sim->SendBuf, query->ID,
				"Item not found in buyback list.");

	int freeslot = pld->charPtr->inventory.GetFreeSlot(INV_CONTAINER);
	if (freeslot == -1)
		return PrepExt_QueryResponseError(sim->SendBuf, query->ID,
				"No free inventory space.");

	ItemDef *itemPtr = buybackItem->ResolveItemPtr();
	if (itemPtr == NULL)
		return PrepExt_QueryResponseError(sim->SendBuf, query->ID,
				"Item does not exist in database.");

	int cost = itemPtr->mValue * buybackItem->GetStackCount();
	if (creatureInstance->css.copper < cost)
		return PrepExt_QueryResponseError(sim->SendBuf, query->ID, "Not enough coin.");

	InventorySlot newItem;
	newItem.CopyFrom(*buybackItem, false);
	newItem.CCSID = pld->charPtr->inventory.GetCCSID(INV_CONTAINER, freeslot);

	int r = pld->charPtr->inventory.AddItem(INV_CONTAINER, newItem);
	if (r == -1)
		return PrepExt_QueryResponseError(sim->SendBuf, query->ID,
				"Failed to create item.");
	pld->charPtr->pendingChanges++;

	int wpos = 0;
	wpos = RemoveItemUpdate(sim->SendBuf, sim->Aux3, buybackItem);
	wpos += AddItemUpdate(&sim->SendBuf[wpos], sim->Aux3, &newItem);
	wpos += PrepExt_QueryResponseString(&sim->SendBuf[wpos], query->ID, "OK");
	pld->charPtr->inventory.RemItem(buybackItem->CCSID);
	pld->charPtr->pendingChanges++;
	creatureInstance->AdjustCopper(-cost);
	return wpos;
}
int TradeItemsHandler::protected_helper_query_trade_items(SimulatorThread *sim,
		CharacterServerData *pld, SimulatorQuery *query,
		CreatureInstance *creatureInstance) {
	int selfID = creatureInstance->CreatureID;
	int tradeID = creatureInstance->activeLootID;

	ActiveInstance *actInst = creatureInstance->actInst;
	TradeTransaction *tradeData = actInst->tradesys.GetExistingTransaction(
			tradeID);
	if (tradeData == NULL)
		return QueryErrorMsg::TRADENOTFOUND;

	TradePlayerData *pData = tradeData->GetPlayerData(selfID);
	if (pData == NULL)
		return actInst->tradesys.CancelTransaction(selfID, tradeID, sim->SendBuf);

	if (pData->otherPlayerData->tradeWindowOpen == false)
		return QueryErrorMsg::TRADENOTOPENED;

	InventorySlot item;
	pData->itemList.clear();
	for (unsigned int a = 0; a < query->argCount; a++) {
		unsigned long CCSID = strtol(query->args[a].c_str(), NULL, 16);
		InventorySlot *itemPtr = pld->charPtr->inventory.GetItemPtrByCCSID(
				CCSID);
		if (itemPtr == NULL)
			return QueryErrorMsg::INVALIDITEM;

		item.CopyFrom(*itemPtr, false);
		item.CCSID = CCSID;
		pData->itemList.push_back(item);
	}

	CreatureInstance *cInst = pData->otherPlayerData->cInst;
	int wpos = PrepExt_TradeItemOffer(sim->SendBuf, sim->Aux3, selfID, pData->itemList);

	SendToOneSimulator(sim->SendBuf, wpos, cInst->simulatorPtr);
	return PrepExt_QueryResponseString(sim->SendBuf, query->ID, "OK");
}