2012-07-24 07:30:08 +02:00
|
|
|
BUY = 1
|
|
|
|
SELL = 2
|
|
|
|
CURRENCY = 'gold'
|
2013-02-15 19:38:57 +01:00
|
|
|
CURRENCY_DECIMAL = false
|
2012-07-24 07:30:08 +02:00
|
|
|
WEIGHT_UNIT = 'oz'
|
|
|
|
LAST_INVENTORY = 10
|
|
|
|
|
|
|
|
npcWindow = nil
|
|
|
|
itemsPanel = nil
|
|
|
|
radioTabs = nil
|
|
|
|
radioItems = nil
|
|
|
|
searchText = nil
|
|
|
|
setupPanel = nil
|
|
|
|
quantity = nil
|
|
|
|
quantityScroll = nil
|
|
|
|
nameLabel = nil
|
|
|
|
priceLabel = nil
|
|
|
|
moneyLabel = nil
|
2013-01-24 17:01:28 +01:00
|
|
|
weightDesc = nil
|
2012-07-24 07:30:08 +02:00
|
|
|
weightLabel = nil
|
2013-01-24 17:01:28 +01:00
|
|
|
capacityDesc = nil
|
2012-07-24 07:30:08 +02:00
|
|
|
capacityLabel = nil
|
|
|
|
tradeButton = nil
|
|
|
|
buyTab = nil
|
|
|
|
sellTab = nil
|
2013-01-27 10:44:15 +01:00
|
|
|
initialized = false
|
2012-07-24 07:30:08 +02:00
|
|
|
|
2013-01-24 17:01:28 +01:00
|
|
|
showWeight = true
|
2012-07-24 07:30:08 +02:00
|
|
|
buyWithBackpack = nil
|
|
|
|
ignoreCapacity = nil
|
|
|
|
ignoreEquipped = nil
|
|
|
|
showAllItems = nil
|
2013-02-15 19:38:57 +01:00
|
|
|
sellAllButton = nil
|
2012-07-24 07:30:08 +02:00
|
|
|
|
2013-02-20 19:41:28 +01:00
|
|
|
playerFreeCapacity = 0
|
|
|
|
playerMoney = 0
|
2012-07-24 07:30:08 +02:00
|
|
|
tradeItems = {}
|
2013-02-20 19:41:28 +01:00
|
|
|
playerItems = {}
|
2012-07-24 07:30:08 +02:00
|
|
|
selectedItem = nil
|
|
|
|
|
2013-01-23 19:55:10 +01:00
|
|
|
cancelNextRelease = nil
|
2013-01-23 10:23:28 +01:00
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function init()
|
2013-01-18 23:39:11 +01:00
|
|
|
npcWindow = g_ui.displayUI('npctrade')
|
2012-07-24 07:30:08 +02:00
|
|
|
npcWindow:setVisible(false)
|
|
|
|
|
|
|
|
itemsPanel = npcWindow:recursiveGetChildById('itemsPanel')
|
|
|
|
searchText = npcWindow:recursiveGetChildById('searchText')
|
|
|
|
|
|
|
|
setupPanel = npcWindow:recursiveGetChildById('setupPanel')
|
|
|
|
quantityScroll = setupPanel:getChildById('quantityScroll')
|
|
|
|
nameLabel = setupPanel:getChildById('name')
|
|
|
|
priceLabel = setupPanel:getChildById('price')
|
|
|
|
moneyLabel = setupPanel:getChildById('money')
|
2013-01-24 17:01:28 +01:00
|
|
|
weightDesc = setupPanel:getChildById('weightDesc')
|
2012-07-24 07:30:08 +02:00
|
|
|
weightLabel = setupPanel:getChildById('weight')
|
2013-01-24 17:01:28 +01:00
|
|
|
capacityDesc = setupPanel:getChildById('capacityDesc')
|
2012-07-24 07:30:08 +02:00
|
|
|
capacityLabel = setupPanel:getChildById('capacity')
|
|
|
|
tradeButton = npcWindow:recursiveGetChildById('tradeButton')
|
|
|
|
|
|
|
|
buyWithBackpack = npcWindow:recursiveGetChildById('buyWithBackpack')
|
|
|
|
ignoreCapacity = npcWindow:recursiveGetChildById('ignoreCapacity')
|
|
|
|
ignoreEquipped = npcWindow:recursiveGetChildById('ignoreEquipped')
|
|
|
|
showAllItems = npcWindow:recursiveGetChildById('showAllItems')
|
2013-02-15 19:38:57 +01:00
|
|
|
sellAllButton = npcWindow:recursiveGetChildById('sellAllButton')
|
2012-07-24 07:30:08 +02:00
|
|
|
|
|
|
|
buyTab = npcWindow:getChildById('buyTab')
|
|
|
|
sellTab = npcWindow:getChildById('sellTab')
|
|
|
|
|
|
|
|
radioTabs = UIRadioGroup.create()
|
|
|
|
radioTabs:addWidget(buyTab)
|
|
|
|
radioTabs:addWidget(sellTab)
|
|
|
|
radioTabs:selectWidget(buyTab)
|
|
|
|
radioTabs.onSelectionChange = onTradeTypeChange
|
|
|
|
|
2013-01-23 19:55:10 +01:00
|
|
|
cancelNextRelease = false
|
2013-01-23 10:23:28 +01:00
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
if g_game.isOnline() then
|
|
|
|
playerFreeCapacity = g_game.getLocalPlayer():getFreeCapacity()
|
|
|
|
end
|
|
|
|
|
|
|
|
connect(g_game, { onGameEnd = hide,
|
|
|
|
onOpenNpcTrade = onOpenNpcTrade,
|
|
|
|
onCloseNpcTrade = onCloseNpcTrade,
|
|
|
|
onPlayerGoods = onPlayerGoods } )
|
|
|
|
|
|
|
|
connect(LocalPlayer, { onFreeCapacityChange = onFreeCapacityChange,
|
|
|
|
onInventoryChange = onInventoryChange } )
|
2013-01-27 10:44:15 +01:00
|
|
|
|
|
|
|
initialized = true
|
2012-07-24 07:30:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function terminate()
|
2013-01-27 10:44:15 +01:00
|
|
|
initialized = false
|
2012-07-24 07:30:08 +02:00
|
|
|
npcWindow:destroy()
|
|
|
|
|
|
|
|
disconnect(g_game, { onGameEnd = hide,
|
|
|
|
onOpenNpcTrade = onOpenNpcTrade,
|
|
|
|
onCloseNpcTrade = onCloseNpcTrade,
|
|
|
|
onPlayerGoods = onPlayerGoods } )
|
|
|
|
|
|
|
|
disconnect(LocalPlayer, { onFreeCapacityChange = onFreeCapacityChange,
|
|
|
|
onInventoryChange = onInventoryChange } )
|
|
|
|
end
|
|
|
|
|
|
|
|
function show()
|
|
|
|
if g_game.isOnline() then
|
|
|
|
if #tradeItems[BUY] > 0 then
|
|
|
|
radioTabs:selectWidget(buyTab)
|
|
|
|
else
|
|
|
|
radioTabs:selectWidget(sellTab)
|
|
|
|
end
|
|
|
|
|
|
|
|
npcWindow:show()
|
|
|
|
npcWindow:raise()
|
|
|
|
npcWindow:focus()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function hide()
|
|
|
|
npcWindow:hide()
|
|
|
|
end
|
|
|
|
|
|
|
|
function onItemBoxChecked(widget)
|
|
|
|
if widget:isChecked() then
|
|
|
|
local item = widget.item
|
|
|
|
selectedItem = item
|
|
|
|
refreshItem(item)
|
|
|
|
tradeButton:enable()
|
2013-02-20 01:34:06 +01:00
|
|
|
|
|
|
|
if getCurrentTradeType() == SELL then
|
|
|
|
quantityScroll:setValue(quantityScroll:getMaximum())
|
|
|
|
end
|
2012-07-24 07:30:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function onQuantityValueChange(quantity)
|
2013-01-06 16:04:49 +01:00
|
|
|
if selectedItem then
|
2012-07-24 07:30:08 +02:00
|
|
|
weightLabel:setText(string.format('%.2f', selectedItem.weight*quantity) .. ' ' .. WEIGHT_UNIT)
|
2013-02-15 19:38:57 +01:00
|
|
|
priceLabel:setText(formatCurrency(getItemPrice(selectedItem)))
|
2012-07-24 07:30:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function onTradeTypeChange(radioTabs, selected, deselected)
|
|
|
|
tradeButton:setText(selected:getText())
|
|
|
|
selected:setOn(true)
|
|
|
|
deselected:setOn(false)
|
|
|
|
|
|
|
|
local currentTradeType = getCurrentTradeType()
|
|
|
|
buyWithBackpack:setVisible(currentTradeType == BUY)
|
|
|
|
ignoreCapacity:setVisible(currentTradeType == BUY)
|
|
|
|
ignoreEquipped:setVisible(currentTradeType == SELL)
|
|
|
|
showAllItems:setVisible(currentTradeType == SELL)
|
2013-03-12 04:18:47 +01:00
|
|
|
sellAllButton:setVisible(currentTradeType == SELL)
|
2012-07-24 07:30:08 +02:00
|
|
|
|
|
|
|
refreshTradeItems()
|
|
|
|
refreshPlayerGoods()
|
|
|
|
end
|
|
|
|
|
|
|
|
function onTradeClick()
|
|
|
|
if getCurrentTradeType() == BUY then
|
|
|
|
g_game.buyItem(selectedItem.ptr, quantityScroll:getValue(), ignoreCapacity:isChecked(), buyWithBackpack:isChecked())
|
|
|
|
else
|
|
|
|
g_game.sellItem(selectedItem.ptr, quantityScroll:getValue(), ignoreEquipped:isChecked())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function onSearchTextChange()
|
|
|
|
refreshPlayerGoods()
|
|
|
|
end
|
|
|
|
|
|
|
|
function itemPopup(self, mousePosition, mouseButton)
|
2013-01-23 19:55:10 +01:00
|
|
|
if cancelNextRelease then
|
|
|
|
cancelNextRelease = false
|
2013-01-23 10:23:28 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
if mouseButton == MouseRightButton then
|
|
|
|
local menu = g_ui.createWidget('PopupMenu')
|
2015-04-19 13:56:03 +02:00
|
|
|
menu:setGameMenu(true)
|
2012-07-24 07:30:08 +02:00
|
|
|
menu:addOption(tr('Look'), function() return g_game.inspectNpcTrade(self:getItem()) end)
|
|
|
|
menu:display(mousePosition)
|
|
|
|
return true
|
2014-01-18 15:09:26 +01:00
|
|
|
elseif ((g_mouse.isPressed(MouseLeftButton) and mouseButton == MouseRightButton)
|
2013-01-23 10:23:28 +01:00
|
|
|
or (g_mouse.isPressed(MouseRightButton) and mouseButton == MouseLeftButton)) then
|
2013-01-23 19:55:10 +01:00
|
|
|
cancelNextRelease = true
|
2013-01-23 10:23:28 +01:00
|
|
|
g_game.inspectNpcTrade(self:getItem())
|
|
|
|
return true
|
2012-07-24 07:30:08 +02:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function onBuyWithBackpackChange()
|
|
|
|
if selectedItem then
|
|
|
|
refreshItem(selectedItem)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function onIgnoreCapacityChange()
|
|
|
|
refreshPlayerGoods()
|
|
|
|
end
|
|
|
|
|
|
|
|
function onIgnoreEquippedChange()
|
|
|
|
refreshPlayerGoods()
|
|
|
|
end
|
|
|
|
|
|
|
|
function onShowAllItemsChange()
|
|
|
|
refreshPlayerGoods()
|
|
|
|
end
|
|
|
|
|
2013-02-15 19:38:57 +01:00
|
|
|
function setCurrency(currency, decimal)
|
2012-07-24 07:30:08 +02:00
|
|
|
CURRENCY = currency
|
2013-02-15 19:38:57 +01:00
|
|
|
CURRENCY_DECIMAL = decimal
|
2012-07-24 07:30:08 +02:00
|
|
|
end
|
|
|
|
|
2013-01-24 17:01:28 +01:00
|
|
|
function setShowWeight(state)
|
|
|
|
showWeight = state
|
|
|
|
weightDesc:setVisible(state)
|
|
|
|
weightLabel:setVisible(state)
|
|
|
|
end
|
|
|
|
|
|
|
|
function setShowYourCapacity(state)
|
|
|
|
capacityDesc:setVisible(state)
|
|
|
|
capacityLabel:setVisible(state)
|
|
|
|
ignoreCapacity:setVisible(state)
|
2012-07-24 07:30:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function clearSelectedItem()
|
2012-04-29 11:08:55 +02:00
|
|
|
nameLabel:clearText()
|
|
|
|
weightLabel:clearText()
|
|
|
|
priceLabel:clearText()
|
|
|
|
tradeButton:disable()
|
2013-02-20 01:34:06 +01:00
|
|
|
quantityScroll:setMinimum(0)
|
|
|
|
quantityScroll:setMaximum(0)
|
2012-04-29 11:08:55 +02:00
|
|
|
if selectedItem then
|
|
|
|
radioItems:selectWidget(nil)
|
|
|
|
selectedItem = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function getCurrentTradeType()
|
2012-04-29 11:08:55 +02:00
|
|
|
if tradeButton:getText() == tr('Buy') then
|
|
|
|
return BUY
|
|
|
|
else
|
|
|
|
return SELL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-06 16:04:49 +01:00
|
|
|
function getItemPrice(item, single)
|
|
|
|
local amount = 1
|
|
|
|
local single = single or false
|
|
|
|
if not single then
|
|
|
|
amount = quantityScroll:getValue()
|
|
|
|
end
|
2012-04-29 11:08:55 +02:00
|
|
|
if getCurrentTradeType() == BUY then
|
|
|
|
if buyWithBackpack:isChecked() then
|
|
|
|
if item.ptr:isStackable() then
|
2013-01-06 16:04:49 +01:00
|
|
|
return item.price*amount + 20
|
2012-04-29 11:08:55 +02:00
|
|
|
else
|
2013-01-06 16:04:49 +01:00
|
|
|
return item.price*amount + math.ceil(amount/20)*20
|
2012-04-29 11:08:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-01-06 16:04:49 +01:00
|
|
|
return item.price*amount
|
2012-04-29 11:08:55 +02:00
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function getSellQuantity(item)
|
2013-02-15 19:38:57 +01:00
|
|
|
if not item or not playerItems[item:getId()] then return 0 end
|
2012-04-29 11:08:55 +02:00
|
|
|
local removeAmount = 0
|
|
|
|
if ignoreEquipped:isChecked() then
|
|
|
|
local localPlayer = g_game.getLocalPlayer()
|
|
|
|
for i=1,LAST_INVENTORY do
|
|
|
|
local inventoryItem = localPlayer:getInventoryItem(i)
|
2013-02-15 19:38:57 +01:00
|
|
|
if inventoryItem and inventoryItem:getId() == item:getId() then
|
2012-04-29 11:08:55 +02:00
|
|
|
removeAmount = removeAmount + inventoryItem:getCount()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-15 19:38:57 +01:00
|
|
|
return playerItems[item:getId()] - removeAmount
|
2012-04-29 11:08:55 +02:00
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function canTradeItem(item)
|
2012-04-29 11:08:55 +02:00
|
|
|
if getCurrentTradeType() == BUY then
|
2013-01-06 16:04:49 +01:00
|
|
|
return (ignoreCapacity:isChecked() or (not ignoreCapacity:isChecked() and playerFreeCapacity >= item.weight)) and playerMoney >= getItemPrice(item, true)
|
2012-04-29 11:08:55 +02:00
|
|
|
else
|
2013-02-15 19:38:57 +01:00
|
|
|
return getSellQuantity(item.ptr) > 0
|
2012-04-29 11:08:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function refreshItem(item)
|
2012-04-29 11:08:55 +02:00
|
|
|
nameLabel:setText(item.name)
|
|
|
|
weightLabel:setText(string.format('%.2f', item.weight) .. ' ' .. WEIGHT_UNIT)
|
2013-02-15 19:38:57 +01:00
|
|
|
priceLabel:setText(formatCurrency(getItemPrice(item)))
|
2012-04-29 11:08:55 +02:00
|
|
|
|
|
|
|
if getCurrentTradeType() == BUY then
|
|
|
|
local capacityMaxCount = math.floor(playerFreeCapacity / item.weight)
|
|
|
|
if ignoreCapacity:isChecked() then
|
2013-02-15 19:38:57 +01:00
|
|
|
capacityMaxCount = 65535
|
2012-04-29 11:08:55 +02:00
|
|
|
end
|
2013-01-06 16:04:49 +01:00
|
|
|
local priceMaxCount = math.floor(playerMoney / getItemPrice(item, true))
|
2013-02-15 19:38:57 +01:00
|
|
|
local finalCount = math.max(0, math.min(getMaxAmount(), math.min(priceMaxCount, capacityMaxCount)))
|
2013-02-20 01:34:06 +01:00
|
|
|
quantityScroll:setMinimum(1)
|
2013-01-06 16:04:49 +01:00
|
|
|
quantityScroll:setMaximum(finalCount)
|
2012-04-29 11:08:55 +02:00
|
|
|
else
|
2013-02-20 01:34:06 +01:00
|
|
|
quantityScroll:setMinimum(1)
|
2013-02-15 19:38:57 +01:00
|
|
|
quantityScroll:setMaximum(math.max(0, math.min(getMaxAmount(), getSellQuantity(item.ptr))))
|
2012-04-29 11:08:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
setupPanel:enable()
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function refreshTradeItems()
|
2012-04-29 11:08:55 +02:00
|
|
|
local layout = itemsPanel:getLayout()
|
|
|
|
layout:disableUpdates()
|
|
|
|
|
|
|
|
clearSelectedItem()
|
|
|
|
|
|
|
|
searchText:clearText()
|
|
|
|
setupPanel:disable()
|
|
|
|
itemsPanel:destroyChildren()
|
|
|
|
|
|
|
|
if radioItems then
|
|
|
|
radioItems:destroy()
|
|
|
|
end
|
2012-06-26 00:13:30 +02:00
|
|
|
radioItems = UIRadioGroup.create()
|
2012-04-29 11:08:55 +02:00
|
|
|
|
|
|
|
local currentTradeItems = tradeItems[getCurrentTradeType()]
|
|
|
|
for key,item in pairs(currentTradeItems) do
|
2012-06-26 00:13:30 +02:00
|
|
|
local itemBox = g_ui.createWidget('NPCItemBox', itemsPanel)
|
2012-04-29 11:08:55 +02:00
|
|
|
itemBox.item = item
|
2012-04-30 08:34:29 +02:00
|
|
|
|
2012-07-04 22:36:58 +02:00
|
|
|
local text = ''
|
2012-04-30 00:43:23 +02:00
|
|
|
local name = item.name
|
2012-07-04 22:36:58 +02:00
|
|
|
text = text .. name
|
2013-01-24 17:01:28 +01:00
|
|
|
if showWeight then
|
2012-07-04 22:36:58 +02:00
|
|
|
local weight = string.format('%.2f', item.weight) .. ' ' .. WEIGHT_UNIT
|
|
|
|
text = text .. '\n' .. weight
|
|
|
|
end
|
2013-02-15 19:38:57 +01:00
|
|
|
local price = formatCurrency(item.price)
|
2012-07-04 22:36:58 +02:00
|
|
|
text = text .. '\n' .. price
|
|
|
|
itemBox:setText(text)
|
2012-04-29 11:08:55 +02:00
|
|
|
|
2012-04-30 00:43:23 +02:00
|
|
|
local itemWidget = itemBox:getChildById('item')
|
|
|
|
itemWidget:setItem(item.ptr)
|
2012-07-24 07:30:08 +02:00
|
|
|
itemWidget.onMouseRelease = itemPopup
|
2012-04-29 11:08:55 +02:00
|
|
|
|
|
|
|
radioItems:addWidget(itemBox)
|
|
|
|
end
|
|
|
|
|
|
|
|
layout:enableUpdates()
|
|
|
|
layout:update()
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function refreshPlayerGoods()
|
2013-01-27 10:44:15 +01:00
|
|
|
if not initialized then return end
|
2013-01-26 21:40:03 +01:00
|
|
|
|
2013-02-20 12:19:30 +01:00
|
|
|
checkSellAllTooltip()
|
|
|
|
|
2013-02-15 19:38:57 +01:00
|
|
|
moneyLabel:setText(formatCurrency(playerMoney))
|
2012-04-29 11:08:55 +02:00
|
|
|
capacityLabel:setText(string.format('%.2f', playerFreeCapacity) .. ' ' .. WEIGHT_UNIT)
|
|
|
|
|
|
|
|
local currentTradeType = getCurrentTradeType()
|
2012-04-30 23:57:17 +02:00
|
|
|
local searchFilter = searchText:getText():lower()
|
2012-04-29 11:08:55 +02:00
|
|
|
local foundSelectedItem = false
|
|
|
|
|
|
|
|
local items = itemsPanel:getChildCount()
|
|
|
|
for i=1,items do
|
|
|
|
local itemWidget = itemsPanel:getChildByIndex(i)
|
|
|
|
local item = itemWidget.item
|
|
|
|
|
|
|
|
local canTrade = canTradeItem(item)
|
2013-01-23 10:23:28 +01:00
|
|
|
itemWidget:setOn(canTrade)
|
2013-02-20 19:41:28 +01:00
|
|
|
itemWidget:setEnabled(canTrade)
|
2012-04-29 11:08:55 +02:00
|
|
|
|
2012-04-30 23:57:17 +02:00
|
|
|
local searchCondition = (searchFilter == '') or (searchFilter ~= '' and string.find(item.name:lower(), searchFilter) ~= nil)
|
2012-04-29 11:08:55 +02:00
|
|
|
local showAllItemsCondition = (currentTradeType == BUY) or (showAllItems:isChecked()) or (currentTradeType == SELL and not showAllItems:isChecked() and canTrade)
|
|
|
|
itemWidget:setVisible(searchCondition and showAllItemsCondition)
|
|
|
|
|
|
|
|
if selectedItem == item and itemWidget:isEnabled() and itemWidget:isVisible() then
|
|
|
|
foundSelectedItem = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not foundSelectedItem then
|
|
|
|
clearSelectedItem()
|
|
|
|
end
|
|
|
|
|
|
|
|
if selectedItem then
|
|
|
|
refreshItem(selectedItem)
|
|
|
|
end
|
|
|
|
end
|
2012-04-25 01:04:37 +02:00
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function onOpenNpcTrade(items)
|
2012-04-30 02:53:46 +02:00
|
|
|
tradeItems[BUY] = {}
|
|
|
|
tradeItems[SELL] = {}
|
|
|
|
|
|
|
|
for key,item in pairs(items) do
|
2012-05-13 17:05:17 +02:00
|
|
|
if item[4] > 0 then
|
2013-01-09 06:26:56 +01:00
|
|
|
local newItem = {}
|
|
|
|
newItem.ptr = item[1]
|
|
|
|
newItem.name = item[2]
|
|
|
|
newItem.weight = item[3] / 100
|
2012-04-30 02:53:46 +02:00
|
|
|
newItem.price = item[4]
|
|
|
|
table.insert(tradeItems[BUY], newItem)
|
2013-01-09 06:26:56 +01:00
|
|
|
end
|
2013-01-24 17:01:28 +01:00
|
|
|
|
2013-01-09 06:26:56 +01:00
|
|
|
if item[5] > 0 then
|
|
|
|
local newItem = {}
|
|
|
|
newItem.ptr = item[1]
|
|
|
|
newItem.name = item[2]
|
|
|
|
newItem.weight = item[3] / 100
|
2012-04-30 02:53:46 +02:00
|
|
|
newItem.price = item[5]
|
|
|
|
table.insert(tradeItems[SELL], newItem)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
refreshTradeItems()
|
2012-07-24 07:30:08 +02:00
|
|
|
addEvent(show) -- player goods has not been parsed yet
|
2012-04-30 02:53:46 +02:00
|
|
|
end
|
|
|
|
|
2012-07-30 12:59:08 +02:00
|
|
|
function closeNpcTrade()
|
|
|
|
g_game.closeNpcTrade()
|
|
|
|
hide()
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function onCloseNpcTrade()
|
|
|
|
hide()
|
2012-04-30 02:53:46 +02:00
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function onPlayerGoods(money, items)
|
2012-04-30 02:53:46 +02:00
|
|
|
playerMoney = money
|
|
|
|
|
|
|
|
playerItems = {}
|
|
|
|
for key,item in pairs(items) do
|
|
|
|
local id = item[1]:getId()
|
|
|
|
if not playerItems[id] then
|
|
|
|
playerItems[id] = item[2]
|
|
|
|
else
|
|
|
|
playerItems[id] = playerItems[id] + item[2]
|
|
|
|
end
|
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2012-04-30 02:53:46 +02:00
|
|
|
refreshPlayerGoods()
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function onFreeCapacityChange(localPlayer, freeCapacity, oldFreeCapacity)
|
2012-04-30 02:53:46 +02:00
|
|
|
playerFreeCapacity = freeCapacity
|
|
|
|
|
|
|
|
if npcWindow:isVisible() then
|
|
|
|
refreshPlayerGoods()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-06 16:04:49 +01:00
|
|
|
function onInventoryChange(inventory, item, oldItem)
|
2013-02-20 16:34:06 +01:00
|
|
|
refreshPlayerGoods()
|
2012-04-30 02:53:46 +02:00
|
|
|
end
|
2013-02-15 19:38:57 +01:00
|
|
|
|
2013-02-20 11:08:03 +01:00
|
|
|
function getTradeItemData(id, type)
|
|
|
|
if table.empty(tradeItems[type]) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if type then
|
|
|
|
for key,item in pairs(tradeItems[type]) do
|
|
|
|
if item.ptr and item.ptr:getId() == id then
|
|
|
|
return item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for _,items in pairs(tradeItems) do
|
|
|
|
for key,item in pairs(items) do
|
|
|
|
if item.ptr and item.ptr:getId() == id then
|
|
|
|
return item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2013-02-20 12:19:30 +01:00
|
|
|
function checkSellAllTooltip()
|
|
|
|
sellAllButton:setEnabled(true)
|
|
|
|
sellAllButton:removeTooltip()
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-02-20 12:19:30 +01:00
|
|
|
local total = 0
|
|
|
|
local info = ''
|
|
|
|
local first = true
|
|
|
|
|
|
|
|
for key, amount in pairs(playerItems) do
|
2013-02-20 12:28:45 +01:00
|
|
|
local data = getTradeItemData(key, SELL)
|
|
|
|
if data then
|
|
|
|
amount = getSellQuantity(data.ptr)
|
|
|
|
if amount > 0 then
|
|
|
|
if data and amount > 0 then
|
|
|
|
info = info..(not first and "\n" or "")..
|
|
|
|
amount.." "..
|
|
|
|
data.name.." ("..
|
|
|
|
data.price*amount.." gold)"
|
|
|
|
|
|
|
|
total = total+(data.price*amount)
|
|
|
|
if first then first = false end
|
|
|
|
end
|
2013-02-20 12:19:30 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if info ~= '' then
|
|
|
|
info = info.."\nTotal: "..total.." gold"
|
|
|
|
sellAllButton:setTooltip(info)
|
|
|
|
else
|
|
|
|
sellAllButton:setEnabled(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-15 19:38:57 +01:00
|
|
|
function formatCurrency(amount)
|
|
|
|
if CURRENCY_DECIMAL then
|
|
|
|
return string.format("%.02f", amount/100.0) .. ' ' .. CURRENCY
|
|
|
|
else
|
|
|
|
return amount .. ' ' .. CURRENCY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function getMaxAmount()
|
|
|
|
if getCurrentTradeType() == SELL and g_game.getFeature(GameDoubleShopSellAmount) then
|
|
|
|
return 10000
|
|
|
|
end
|
|
|
|
return 100
|
|
|
|
end
|
|
|
|
|
|
|
|
function sellAll()
|
|
|
|
for itemid,item in pairs(playerItems) do
|
|
|
|
local item = Item.create(itemid)
|
|
|
|
local amount = getSellQuantity(item)
|
|
|
|
if amount > 0 then
|
|
|
|
g_game.sellItem(item, amount, ignoreEquipped:isChecked())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|