2012-07-17 16:36:27 +02:00
|
|
|
Market = {}
|
|
|
|
|
2012-07-19 20:54:24 +02:00
|
|
|
g_ui.importStyle('market.otui')
|
|
|
|
g_ui.importStyle('ui/general/markettabs.otui')
|
|
|
|
g_ui.importStyle('ui/general/marketbuttons.otui')
|
2012-07-20 20:20:06 +02:00
|
|
|
g_ui.importStyle('ui/general/marketcombobox.otui')
|
2012-07-19 20:54:24 +02:00
|
|
|
|
2012-07-17 16:36:27 +02:00
|
|
|
local marketWindow
|
2012-07-19 20:54:24 +02:00
|
|
|
local mainTabBar
|
|
|
|
|
|
|
|
local marketOffersPanel
|
|
|
|
local selectionTabBar
|
|
|
|
local browsePanel
|
|
|
|
local searchPanel
|
|
|
|
|
|
|
|
local displaysTabBar
|
|
|
|
local itemOffersPanel
|
|
|
|
local itemDetailsPanel
|
|
|
|
local itemStatsPanel
|
|
|
|
|
|
|
|
local myOffersPanel
|
|
|
|
local offersTabBar
|
|
|
|
local currentOffersPanel
|
|
|
|
local offerHistoryPanel
|
|
|
|
|
|
|
|
local marketOffers = {}
|
2012-07-20 20:20:06 +02:00
|
|
|
local marketItems = {}
|
2012-07-19 20:54:24 +02:00
|
|
|
local depot = {}
|
|
|
|
local information ={}
|
|
|
|
local selectedItem
|
|
|
|
local nameLabel
|
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
local currentItems = {}
|
2012-07-19 20:54:24 +02:00
|
|
|
local itemsPanel
|
2012-07-20 20:20:06 +02:00
|
|
|
local radioItemSet
|
|
|
|
local filterBox
|
|
|
|
|
|
|
|
local function getMarketCategoryName(category)
|
|
|
|
if table.hasKey(MarketCategoryStrings, category) then
|
|
|
|
return MarketCategoryStrings[category]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function getMarketCategoryId(name)
|
|
|
|
local id = table.find(MarketCategoryStrings, name)
|
|
|
|
if id then
|
|
|
|
return id
|
|
|
|
end
|
|
|
|
end
|
2012-07-19 20:54:24 +02:00
|
|
|
|
|
|
|
local function clearSelectedItem()
|
2012-07-20 20:20:06 +02:00
|
|
|
if selectedItem and selectedItem.item.ptr then
|
|
|
|
Market.updateOffers({})
|
|
|
|
radioItemSet:selectWidget(nil)
|
2012-07-19 20:54:24 +02:00
|
|
|
nameLabel:clearText()
|
2012-07-20 20:20:06 +02:00
|
|
|
selectedItem:setItem(nil)
|
|
|
|
selectedItem.item = {}
|
2012-07-19 20:54:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
local function initMarketItems()
|
|
|
|
-- populate all market items
|
|
|
|
marketItems = {}
|
|
|
|
local types = g_things.findThingTypeByAttr(ThingAttrMarket)
|
2012-07-20 20:56:08 +02:00
|
|
|
for idx = 1, #types do
|
|
|
|
local t = types[idx]
|
2012-07-20 20:20:06 +02:00
|
|
|
local newItem = Item.create(t:getId())
|
|
|
|
if newItem then
|
|
|
|
local item = {
|
|
|
|
ptr = newItem,
|
|
|
|
marketData = t:getMarketData()
|
|
|
|
}
|
|
|
|
table.insert(marketItems, item)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-07-19 20:54:24 +02:00
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
local function updateItemsWidget()
|
|
|
|
if table.empty(currentItems) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
itemsPanel = marketWindow:recursiveGetChildById('itemsPanel')
|
2012-07-19 20:54:24 +02:00
|
|
|
local layout = itemsPanel:getLayout()
|
|
|
|
layout:disableUpdates()
|
|
|
|
|
|
|
|
clearSelectedItem()
|
|
|
|
itemsPanel:destroyChildren()
|
|
|
|
|
|
|
|
if radioItemSet then
|
|
|
|
radioItemSet:destroy()
|
|
|
|
end
|
|
|
|
radioItemSet = UIRadioGroup.create()
|
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
for _, item in pairs(currentItems) do
|
|
|
|
local itemBox = g_ui.createWidget('MarketItemBox', itemsPanel)
|
|
|
|
local itemWidget = itemBox:getChildById('item')
|
|
|
|
|
|
|
|
itemBox.item = item
|
|
|
|
itemWidget:setItem(item.ptr)
|
|
|
|
|
|
|
|
radioItemSet:addWidget(itemBox)
|
|
|
|
end
|
2012-07-19 20:54:24 +02:00
|
|
|
|
|
|
|
layout:enableUpdates()
|
|
|
|
layout:update()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function loadDepotItems(depotItems)
|
|
|
|
information.depotItems = {}
|
|
|
|
for _, data in pairs(depotItems) do
|
|
|
|
local item = Item.create(data[1])
|
|
|
|
if not item then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
item:setCount(data[2])
|
|
|
|
local marketData = item:getMarketData()
|
|
|
|
|
|
|
|
if not table.empty(marketData) then
|
|
|
|
local newItem = {
|
|
|
|
ptr = item,
|
|
|
|
marketData = marketData
|
|
|
|
}
|
|
|
|
table.insert(information.depotItems, newItem)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-07-17 16:36:27 +02:00
|
|
|
|
|
|
|
function Market.init()
|
2012-07-19 20:54:24 +02:00
|
|
|
marketWindow = g_ui.createWidget('MarketWindow', rootWidget)
|
|
|
|
marketWindow:hide()
|
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
initMarketItems()
|
2012-07-19 20:54:24 +02:00
|
|
|
|
|
|
|
-- TODO: clean this up into functions
|
|
|
|
-- setup main tabs
|
|
|
|
mainTabBar = marketWindow:getChildById('mainTabBar')
|
|
|
|
mainTabBar:setContentWidget(marketWindow:getChildById('mainTabContent'))
|
|
|
|
|
|
|
|
-- setup 'Market Offer' section tabs
|
|
|
|
marketOffersPanel = g_ui.loadUI('ui/marketoffers.otui')
|
|
|
|
mainTabBar:addTab(tr('Market Offers'), marketOffersPanel)
|
|
|
|
|
|
|
|
selectionTabBar = marketOffersPanel:getChildById('leftTabBar')
|
|
|
|
selectionTabBar:setContentWidget(marketOffersPanel:getChildById('leftTabContent'))
|
|
|
|
|
|
|
|
browsePanel = g_ui.loadUI('ui/marketoffers/browse.otui')
|
|
|
|
selectionTabBar:addTab(tr('Browse'), browsePanel)
|
|
|
|
|
|
|
|
searchPanel = g_ui.loadUI('ui/marketoffers/search.otui')
|
|
|
|
selectionTabBar:addTab(tr('Search'), searchPanel)
|
2012-07-17 16:36:27 +02:00
|
|
|
|
2012-07-19 20:54:24 +02:00
|
|
|
displaysTabBar = marketOffersPanel:getChildById('rightTabBar')
|
|
|
|
displaysTabBar:setContentWidget(marketOffersPanel:getChildById('rightTabContent'))
|
|
|
|
|
|
|
|
itemOffersPanel = g_ui.loadUI('ui/marketoffers/itemoffers.otui')
|
|
|
|
displaysTabBar:addTab(tr('Offers'), itemOffersPanel)
|
|
|
|
|
|
|
|
itemDetailsPanel = g_ui.loadUI('ui/marketoffers/itemdetails.otui')
|
|
|
|
displaysTabBar:addTab(tr('Details'), itemDetailsPanel)
|
|
|
|
|
|
|
|
itemStatsPanel = g_ui.loadUI('ui/marketoffers/itemstats.otui')
|
|
|
|
displaysTabBar:addTab(tr('Statistics'), itemStatsPanel)
|
|
|
|
|
|
|
|
-- setup 'My Offer' section tabs
|
|
|
|
myOffersPanel = g_ui.loadUI('ui/myoffers.otui')
|
|
|
|
mainTabBar:addTab(tr('My Offers'), myOffersPanel)
|
|
|
|
|
|
|
|
offersTabBar = myOffersPanel:getChildById('offersTabBar')
|
|
|
|
offersTabBar:setContentWidget(myOffersPanel:getChildById('offersTabContent'))
|
|
|
|
|
|
|
|
currentOffersPanel = g_ui.loadUI('ui/myoffers/currentoffers.otui')
|
|
|
|
offersTabBar:addTab(tr('Current Offers'), currentOffersPanel)
|
|
|
|
|
|
|
|
offerHistoryPanel = g_ui.loadUI('ui/myoffers/offerhistory.otui')
|
|
|
|
offersTabBar:addTab(tr('Offer History'), offerHistoryPanel)
|
2012-07-20 20:20:06 +02:00
|
|
|
|
|
|
|
nameLabel = marketWindow:recursiveGetChildById('nameLabel')
|
|
|
|
selectedItem = marketWindow:recursiveGetChildById('selectedItem')
|
|
|
|
selectedItem.item = {}
|
|
|
|
|
|
|
|
filterBox = browsePanel:getChildById('filterComboBox')
|
|
|
|
for i = MarketCategory.First, MarketCategory.Last do
|
|
|
|
filterBox:addOption(getMarketCategoryName(i))
|
|
|
|
end
|
|
|
|
filterBox:setCurrentOption(getMarketCategoryName(MarketCategory.First))
|
|
|
|
|
|
|
|
filterBox.onOptionChange = function(combobox, option)
|
|
|
|
Market.loadMarketItems(getMarketCategoryId(option))
|
|
|
|
end
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Market.terminate()
|
2012-07-19 20:54:24 +02:00
|
|
|
if marketWindow then
|
|
|
|
marketWindow:destroy()
|
|
|
|
marketWindow = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
mainTabBar = nil
|
|
|
|
marketOffersPanel = nil
|
|
|
|
selectionTabBar = nil
|
|
|
|
browsePanel = nil
|
|
|
|
searchPanel = nil
|
|
|
|
displaysTabBar = nil
|
|
|
|
itemOffersPanel = nil
|
|
|
|
itemDetailsPanel = nil
|
|
|
|
itemStatsPanel = nil
|
|
|
|
myOffersPanel = nil
|
|
|
|
offersTabBar = nil
|
|
|
|
currentOffersPanel = nil
|
|
|
|
offerHistoryPanel = nil
|
|
|
|
marketOffers = {}
|
2012-07-20 20:20:06 +02:00
|
|
|
marketItems = {}
|
2012-07-19 20:54:24 +02:00
|
|
|
depotItems = {}
|
|
|
|
information = {}
|
2012-07-20 20:20:06 +02:00
|
|
|
currentItems = {}
|
2012-07-19 20:54:24 +02:00
|
|
|
itemsPanel = nil
|
|
|
|
nameLabel = nil
|
2012-07-20 20:20:06 +02:00
|
|
|
radioItemSet = nil
|
2012-07-19 20:54:24 +02:00
|
|
|
selectedItem = nil
|
2012-07-20 20:20:06 +02:00
|
|
|
filterBox = nil
|
2012-07-19 20:54:24 +02:00
|
|
|
|
2012-07-17 16:36:27 +02:00
|
|
|
Market = nil
|
|
|
|
end
|
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
function Market.loadMarketItems(_category)
|
|
|
|
if table.empty(marketItems) then
|
|
|
|
initMarketItems()
|
|
|
|
end
|
|
|
|
|
|
|
|
currentItems = {}
|
|
|
|
for _, item in pairs(marketItems) do
|
|
|
|
-- filter items here
|
|
|
|
local category = item.marketData.category
|
|
|
|
if category == _category or _category == MarketCategory[0] then
|
|
|
|
table.insert(currentItems, item)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
updateItemsWidget()
|
|
|
|
end
|
|
|
|
|
2012-07-19 20:54:24 +02:00
|
|
|
function Market.updateOffers(offers)
|
2012-07-20 20:20:06 +02:00
|
|
|
marketOffers[MarketAction.Buy] = {}
|
|
|
|
marketOffers[MarketAction.Sell] = {}
|
|
|
|
|
|
|
|
local buyOfferList = marketWindow:recursiveGetChildById('buyingList')
|
|
|
|
buyOfferList:destroyChildren()
|
|
|
|
|
|
|
|
local sellOfferList = marketWindow:recursiveGetChildById('sellingList')
|
|
|
|
sellOfferList:destroyChildren()
|
|
|
|
|
2012-07-19 20:54:24 +02:00
|
|
|
for k, offer in pairs(offers) do
|
|
|
|
if offer and offer:getAction() == MarketAction.Buy then
|
2012-07-20 20:20:06 +02:00
|
|
|
local label = g_ui.createWidget('OfferListLabel', buyOfferList)
|
|
|
|
label:setText(offer:getPlayer()..' '..offer:getAmount()..' '..(offer:getPrice()*offer:getAmount())..' '..offer:getPrice()..' '..offer:getTimeStamp())
|
2012-07-19 20:54:24 +02:00
|
|
|
table.insert(marketOffers[MarketAction.Buy], offer)
|
|
|
|
else
|
2012-07-20 20:20:06 +02:00
|
|
|
local label = g_ui.createWidget('OfferListLabel', sellOfferList)
|
|
|
|
label:setText(offer:getPlayer()..' '..offer:getAmount()..' '..(offer:getPrice()*offer:getAmount())..' '..offer:getPrice()..' '..offer:getTimeStamp())
|
2012-07-19 20:54:24 +02:00
|
|
|
table.insert(marketOffers[MarketAction.Sell], offer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Market.updateDetails(itemId, descriptions, purchaseStats, saleStats)
|
2012-07-20 20:20:06 +02:00
|
|
|
if not selectedItem then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
selectedItem.item.details = {
|
|
|
|
serverItemId = itemId,
|
|
|
|
descriptions = descriptions,
|
|
|
|
purchaseStats = purchaseStats,
|
|
|
|
saleStats = saleStats
|
|
|
|
}
|
2012-07-19 20:54:24 +02:00
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
-- TODO: refresh all widget windows
|
2012-07-19 20:54:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Market.updateSelectedItem(newItem)
|
2012-07-20 20:20:06 +02:00
|
|
|
selectedItem.item = newItem
|
|
|
|
if selectedItem and not table.empty(selectedItem.item) then
|
|
|
|
if selectedItem.item.ptr then
|
|
|
|
selectedItem:setItem(selectedItem.item.ptr)
|
|
|
|
nameLabel:setText(selectedItem.item.marketData.name)
|
|
|
|
MarketProtocol.sendMarketBrowse(selectedItem.item.ptr:getId()) -- send sprite id browsed
|
2012-07-19 20:54:24 +02:00
|
|
|
end
|
|
|
|
else
|
2012-07-20 20:20:06 +02:00
|
|
|
selectedItem:setItem(nil)
|
|
|
|
nameLabel:setText(tr('No item selected.'))
|
2012-07-19 20:54:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-17 16:36:27 +02:00
|
|
|
function Market.onMarketEnter(depotItems, offers, balance)
|
2012-07-19 20:54:24 +02:00
|
|
|
if marketWindow:isVisible() then
|
|
|
|
return
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
2012-07-20 20:20:06 +02:00
|
|
|
marketWindow:lock()
|
2012-07-19 20:54:24 +02:00
|
|
|
marketOffers[MarketAction.Buy] = {}
|
|
|
|
marketOffers[MarketAction.Sell] = {}
|
|
|
|
|
|
|
|
information.balance = balance
|
|
|
|
information.totalOffers = offers
|
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
if table.empty(currentItems) then
|
|
|
|
Market.loadMarketItems(MarketCategory.First)
|
|
|
|
end
|
2012-07-19 20:54:24 +02:00
|
|
|
loadDepotItems(depotItems)
|
|
|
|
|
|
|
|
-- TODO: if you are already viewing an item on market enter it must recheck the current item
|
|
|
|
if selectedItem and selectedItem:isChecked() then
|
|
|
|
selectedItem:setChecked(false)
|
|
|
|
selectedItem:setChecked(true)
|
|
|
|
end
|
|
|
|
marketWindow:show()
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Market.onMarketLeave()
|
2012-07-19 20:54:24 +02:00
|
|
|
marketWindow:hide()
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Market.onMarketDetail(itemId, descriptions, purchaseStats, saleStats)
|
2012-07-19 20:54:24 +02:00
|
|
|
Market.updateDetails(itemId, descriptions, purchaseStats, saleStats)
|
|
|
|
|
|
|
|
print('')
|
|
|
|
print('[onMarketDetail]')
|
|
|
|
print('itemId: '..itemId)
|
2012-07-17 16:36:27 +02:00
|
|
|
print('descriptions:')
|
|
|
|
for k, desc in pairs(descriptions) do
|
2012-07-19 20:54:24 +02:00
|
|
|
print(' type: '..desc[1]..' | description: '..desc[2])
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
print('purchaseStats:')
|
|
|
|
for k, stat in pairs(purchaseStats) do
|
2012-07-19 20:54:24 +02:00
|
|
|
print(' transactions: '..stat[1])
|
|
|
|
print(' total price: '..stat[2])
|
|
|
|
print(' highest price: '..stat[3])
|
|
|
|
print(' lowest price: '..stat[4])
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
print('saleStats:')
|
|
|
|
for k, stat in pairs(saleStats) do
|
2012-07-19 20:54:24 +02:00
|
|
|
print(' transactions: '..stat[1])
|
|
|
|
print(' total price: '..stat[2])
|
|
|
|
print(' highest price: '..stat[3])
|
|
|
|
print(' lowest price: '..stat[4])
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Market.onMarketBrowse(offers)
|
2012-07-19 20:54:24 +02:00
|
|
|
Market.updateOffers(offers)
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
2012-07-19 20:54:24 +02:00
|
|
|
|
|
|
|
function Market.onItemBoxChecked(widget)
|
|
|
|
if widget:isChecked() then
|
|
|
|
Market.updateSelectedItem(widget.item)
|
|
|
|
end
|
2012-07-20 20:56:08 +02:00
|
|
|
end
|