Finished accepting buy/sell market item offers, few minor fixes.

This commit is contained in:
BeniS 2012-07-30 02:05:32 +12:00
parent e2efbcffbe
commit a914d31afb
5 changed files with 226 additions and 52 deletions

View File

@ -22,6 +22,7 @@ selectedOffer = {}
nameLabel = nil nameLabel = nil
feeLabel = nil feeLabel = nil
balanceLabel = nil
totalPriceEdit = nil totalPriceEdit = nil
piecePriceEdit = nil piecePriceEdit = nil
amountEdit = nil amountEdit = nil
@ -32,6 +33,8 @@ categoryList = nil
subCategoryList = nil subCategoryList = nil
slotFilterList = nil slotFilterList = nil
createOfferButton = nil createOfferButton = nil
buyButton = nil
sellButton = nil
anonymous = nil anonymous = nil
filterButtons = {} filterButtons = {}
@ -97,6 +100,13 @@ local function clearItems()
Market.refreshItemsWidget() Market.refreshItemsWidget()
end end
local function clearOffers()
marketOffers[MarketAction.Buy] = {}
marketOffers[MarketAction.Sell] = {}
buyOfferTable:clearData()
sellOfferTable:clearData()
end
local function clearFilters() local function clearFilters()
for _, filter in pairs(filterButtons) do for _, filter in pairs(filterButtons) do
if filter and filter:isChecked() then if filter and filter:isChecked() then
@ -121,41 +131,97 @@ local function refreshFee()
fee = 0 fee = 0
end end
local function updateOffers(offers) local function addOffer(offer, type)
selectedOffer[MarketAction.Buy] = {} if not offer then
selectedOffer[MarketAction.Sell] = {} return false
end
local id = offer:getId()
local player = offer:getPlayer()
local amount = offer:getAmount()
local price = offer:getPrice()
local timestamp = offer:getTimeStamp()
marketOffers[MarketAction.Buy] = {} if amount < 1 then return false end
marketOffers[MarketAction.Sell] = {} if type == MarketAction.Buy then
local data = {
{['text'] = player, ['width'] = 100},
{['text'] = amount, ['width'] = 60},
{['text'] = price*amount, ['width'] = 90},
{['text'] = price, ['width'] = 80},
{['text'] = string.gsub(os.date('%c', timestamp), " ", " "), ['width'] = 120}
}
buyOfferTable:addRow(data, id)
else
local data = {
{['text'] = player, ['width'] = 100},
{['text'] = amount, ['width'] = 60},
{['text'] = price*amount, ['width'] = 90},
{['text'] = price, ['width'] = 80},
{['text'] = string.gsub(os.date('%c', timestamp), " ", " "), ['width'] = 120}
}
sellOfferTable:addRow(data, id)
end
return true
end
local function mergeOffer(offer)
if not offer then
return false
end
local id = offer:getId()
local type = offer:getType()
local amount = offer:getAmount()
local replaced = false
if type == MarketAction.Buy then
for i = 1, #marketOffers[MarketAction.Buy] do
local o = marketOffers[MarketAction.Buy][i]
-- replace existing offer
if o:isEqual(id) then
marketOffers[MarketAction.Buy][i] = offer
replaced = true
end
end
if not replaced then
table.insert(marketOffers[MarketAction.Buy], offer)
end
else
for i = 1, #marketOffers[MarketAction.Sell] do
local o = marketOffers[MarketAction.Sell][i]
-- replace existing offer
if o:isEqual(id) then
marketOffers[MarketAction.Sell][i] = offer
replaced = true
end
end
if not replaced then
table.insert(marketOffers[MarketAction.Sell], offer)
end
end
return true
end
local function updateOffers(offers)
-- TODO: optimize offer updates later
selectedOffer[MarketAction.Buy] = nil
selectedOffer[MarketAction.Sell] = nil
if not buyOfferTable or not sellOfferTable then if not buyOfferTable or not sellOfferTable then
return return
end end
balanceLabel:setColor('#bbbbbb')
buyOfferTable:clearData() buyOfferTable:clearData()
sellOfferTable:clearData() sellOfferTable:clearData()
balanceLabel:setColor('#bbbbbb')
sellButton:setEnabled(false)
buyButton:setEnabled(false)
for k, offer in pairs(offers) do for k, offer in pairs(offers) do
if offer and offer:getType() == MarketAction.Buy then mergeOffer(offer)
local data = { end
{['text'] = offer:getPlayer(), ['width'] = 100}, for type, offers in pairs(marketOffers) do
{['text'] = offer:getAmount(), ['width'] = 60}, for i = 1, #offers do
{['text'] = offer:getPrice()*offer:getAmount(), ['width'] = 90}, addOffer(offers[i], type)
{['text'] = offer:getPrice(), ['width'] = 80},
{['text'] = string.gsub(os.date('%c', offer:getTimeStamp()), " ", " "), ['width'] = 120}
}
buyOfferTable:addRow(data, offer:getId())
table.insert(marketOffers[MarketAction.Buy], offer)
else
local data = {
{['text'] = offer:getPlayer(), ['width'] = 100},
{['text'] = offer:getAmount(), ['width'] = 60},
{['text'] = offer:getPrice()*offer:getAmount(), ['width'] = 90},
{['text'] = offer:getPrice(), ['width'] = 80},
{['text'] = string.gsub(os.date('%c', offer:getTimeStamp()), " ", " "), ['width'] = 120}
}
sellOfferTable:addRow(data, offer:getId())
table.insert(marketOffers[MarketAction.Sell], offer)
end end
end end
end end
@ -190,8 +256,8 @@ local function updateDetails(itemId, descriptions, purchaseStats, saleStats)
highestPrice = newHigh highestPrice = newHigh
end end
local newLow = stat:getLowestPrice() local newLow = stat:getLowestPrice()
-- ?? getting '4294967295' result from lowest price in 9.60 cipsoft -- ?? getting '0xffffffff' result from lowest price in 9.60 cipsoft
if (lowestPrice == 0 or newLow < lowestPrice) and newLow ~= 4294967295 then if (lowestPrice == 0 or newLow < lowestPrice) and newLow ~= 0xffffffff then
lowestPrice = newLow lowestPrice = newLow
end end
end end
@ -229,8 +295,8 @@ local function updateDetails(itemId, descriptions, purchaseStats, saleStats)
highestPrice = newHigh highestPrice = newHigh
end end
local newLow = stat:getLowestPrice() local newLow = stat:getLowestPrice()
-- ?? getting '4294967295' result from lowest price in 9.60 cipsoft -- ?? getting '0xffffffff' result from lowest price in 9.60 cipsoft
if (lowestPrice == 0 or newLow < lowestPrice) and newLow ~= 4294967295 then if (lowestPrice == 0 or newLow < lowestPrice) and newLow ~= 0xffffffff then
lowestPrice = newLow lowestPrice = newLow
end end
end end
@ -261,6 +327,7 @@ local function updateSelectedItem(newItem)
if Market.isItemSelected() then if Market.isItemSelected() then
selectedItem:setItem(selectedItem.item.ptr) selectedItem:setItem(selectedItem.item.ptr)
nameLabel:setText(selectedItem.item.marketData.name) nameLabel:setText(selectedItem.item.marketData.name)
clearOffers()
Market.enableCreateOffer(true)-- update offer types Market.enableCreateOffer(true)-- update offer types
MarketProtocol.sendMarketBrowse(selectedItem.item.ptr:getId()) -- send browsed msg MarketProtocol.sendMarketBrowse(selectedItem.item.ptr:getId()) -- send browsed msg
@ -330,23 +397,68 @@ local function updateFee(price, amount)
feeLabel:resizeToText() feeLabel:resizeToText()
end end
local function openAmountWindow(callback, type, actionText)
local actionText = actionText or ''
if not Market.isOfferSelected(type) then
return
end
amountWindow = g_ui.createWidget('AmountWindow', rootWidget)
amountWindow:lock()
local spinbox = amountWindow:getChildById('amountSpinBox')
local scrollbar = amountWindow:getChildById('amountScrollBar')
spinbox:setMaximum(selectedOffer[type]:getAmount())
spinbox:setMinimum(1)
spinbox:setValue(1)
scrollbar:setMaximum(selectedOffer[type]:getAmount())
scrollbar:setMinimum(1)
scrollbar:setValue(1)
scrollbar.onValueChange = function(self, value) spinbox:setValue(value) end
spinbox.onValueChange = function(self, value) scrollbar:setValue(value) end
local okButton = amountWindow:getChildById('buttonOk')
if actionText ~= '' then okButton:setText(actionText) end
local okFunc = function()
local counter = selectedOffer[type]:getCounter()
local timestamp = selectedOffer[type]:getTimeStamp()
callback(spinbox:getValue(), timestamp, counter)
okButton:getParent():destroy()
amountWindow = nil
end
local cancelButton = amountWindow:getChildById('buttonCancel')
local cancelFunc = function()
cancelButton:getParent():destroy()
amountWindow = nil
end
amountWindow.onEnter = okFunc
amountWindow.onEscape = cancelFunc
okButton.onClick = okFunc
cancelButton.onClick = cancelFunc
end
local function onSelectSellOffer(table, selectedRow, previousSelectedRow) local function onSelectSellOffer(table, selectedRow, previousSelectedRow)
updateBalance() updateBalance()
for _, offer in pairs(marketOffers[MarketAction.Sell]) do for _, offer in pairs(marketOffers[MarketAction.Sell]) do
if offer:isEqual(selectedRow.ref) then if offer:isEqual(selectedRow.ref) then
selectedOffer[MarketAction.Sell] = offer selectedOffer[MarketAction.Buy] = offer
buyButton:setEnabled(true)
end end
end end
local offer = selectedOffer[MarketAction.Sell] local offer = selectedOffer[MarketAction.Buy]
if offer then if offer then
if offer:getTotalPrice() > information.balance then local price = offer:getPrice()
if price > information.balance then
balanceLabel:setColor('#b22222') balanceLabel:setColor('#b22222')
else else
local slice = (information.balance / 2) local slice = (information.balance / 2)
if (offer:getTotalPrice()/slice) * 100 <= 40 then if (price/slice) * 100 <= 40 then
color = '#008b00' -- green color = '#008b00' -- green
elseif (offer:getTotalPrice()/slice) * 100 <= 70 then elseif (price/slice) * 100 <= 70 then
color = '#eec900' -- yellow color = '#eec900' -- yellow
else else
color = '#ee9a00' -- orange color = '#ee9a00' -- orange
@ -360,7 +472,8 @@ local function onSelectBuyOffer(table, selectedRow, previousSelectedRow)
updateBalance() updateBalance()
for _, offer in pairs(marketOffers[MarketAction.Buy]) do for _, offer in pairs(marketOffers[MarketAction.Buy]) do
if offer:isEqual(selectedRow.ref) then if offer:isEqual(selectedRow.ref) then
selectedOffer[MarketAction.Buy] = offer selectedOffer[MarketAction.Sell] = offer
sellButton:setEnabled(true)
end end
end end
end end
@ -506,6 +619,13 @@ local function initInterface()
offerHistoryPanel = g_ui.loadUI('ui/myoffers/offerhistory.otui') offerHistoryPanel = g_ui.loadUI('ui/myoffers/offerhistory.otui')
offersTabBar:addTab(tr('Offer History'), offerHistoryPanel) offersTabBar:addTab(tr('Offer History'), offerHistoryPanel)
-- setup offers
buyButton = itemOffersPanel:getChildById('buyButton')
buyButton.onClick = function() openAmountWindow(Market.buyMarketOffer, MarketAction.Buy, 'Buy') end
sellButton = itemOffersPanel:getChildById('sellButton')
sellButton.onClick = function() openAmountWindow(Market.sellMarketOffer, MarketAction.Sell, 'Sell') end
-- setup selected item -- setup selected item
nameLabel = marketOffersPanel:getChildById('nameLabel') nameLabel = marketOffersPanel:getChildById('nameLabel')
selectedItem = marketOffersPanel:getChildById('selectedItem') selectedItem = marketOffersPanel:getChildById('selectedItem')
@ -571,6 +691,7 @@ function init()
g_ui.importStyle('ui/general/markettabs.otui') g_ui.importStyle('ui/general/markettabs.otui')
g_ui.importStyle('ui/general/marketbuttons.otui') g_ui.importStyle('ui/general/marketbuttons.otui')
g_ui.importStyle('ui/general/marketcombobox.otui') g_ui.importStyle('ui/general/marketcombobox.otui')
g_ui.importStyle('ui/general/amountwindow.otui')
protocol.initProtocol() protocol.initProtocol()
connect(g_game, { onGameEnd = Market.reset }) connect(g_game, { onGameEnd = Market.reset })
@ -593,6 +714,7 @@ function terminate()
end end
function Market.reset() function Market.reset()
balanceLabel:setColor('#bbbbbb')
categoryList:setCurrentOption(getMarketCategoryName(MarketCategory.First)) categoryList:setCurrentOption(getMarketCategoryName(MarketCategory.First))
Market.clearSelectedItem() Market.clearSelectedItem()
clearFilters() clearFilters()
@ -600,18 +722,17 @@ function Market.reset()
end end
function Market.clearSelectedItem() function Market.clearSelectedItem()
if selectedItem and selectedItem.item.ptr then if Market.isItemSelected() then
Market.resetCreateOffer() Market.resetCreateOffer()
offerTypeList:clearOptions() offerTypeList:clearOptions()
offerTypeList:setText('Please Select') offerTypeList:setText('Please Select')
offerTypeList:setEnabled(false) offerTypeList:setEnabled(false)
updateOffers({}) clearOffers()
radioItemSet:selectWidget(nil) radioItemSet:selectWidget(nil)
nameLabel:setText('No item selected.') nameLabel:setText('No item selected.')
selectedItem:setItem(nil) selectedItem:setItem(nil)
selectedItem = {}
detailsTable:clearData() detailsTable:clearData()
buyStatsTable:clearData() buyStatsTable:clearData()
@ -625,6 +746,10 @@ function Market.isItemSelected()
return selectedItem and not table.empty(selectedItem.item) and selectedItem.item.ptr return selectedItem and not table.empty(selectedItem.item) and selectedItem.item.ptr
end end
function Market.isOfferSelected(type)
return selectedOffer[type] and not selectedOffer[type]:isNull()
end
function Market.depotContains(itemId) function Market.depotContains(itemId)
local count = 0 local count = 0
for i = 1, #information.depotItems do for i = 1, #information.depotItems do
@ -651,12 +776,10 @@ end
function Market.incrementAmount() function Market.incrementAmount()
amountEdit:setValue(amountEdit:getValue() + 1) amountEdit:setValue(amountEdit:getValue() + 1)
-- change total price/piece price according
end end
function Market.decrementAmount() function Market.decrementAmount()
amountEdit:setValue(amountEdit:getValue() - 1) amountEdit:setValue(amountEdit:getValue() - 1)
-- change total price/piece price according
end end
function Market.updateCurrentItems() function Market.updateCurrentItems()
@ -821,6 +944,18 @@ function Market.createNewOffer()
Market.resetCreateOffer() Market.resetCreateOffer()
end end
function Market.buyMarketOffer(amount, timestamp, counter)
if timestamp > 0 and counter > 0 and amount > 0 then
MarketProtocol.sendMarketAcceptOffer(timestamp, counter, amount)
end
end
function Market.sellMarketOffer(amount, timestamp, counter)
if timestamp > 0 and counter > 0 and amount > 0 then
MarketProtocol.sendMarketAcceptOffer(timestamp, counter, amount)
end
end
function Market.onItemBoxChecked(widget) function Market.onItemBoxChecked(widget)
if widget:isChecked() then if widget:isChecked() then
updateSelectedItem(widget.item) updateSelectedItem(widget.item)
@ -828,9 +963,6 @@ function Market.onItemBoxChecked(widget)
end end
function Market.onMarketEnter(depotItems, offers, balance, vocation) function Market.onMarketEnter(depotItems, offers, balance, vocation)
marketOffers[MarketAction.Buy] = {}
marketOffers[MarketAction.Sell] = {}
updateBalance(balance) updateBalance(balance)
information.totalOffers = offers information.totalOffers = offers
if vocation < 0 then if vocation < 0 then

View File

@ -217,12 +217,14 @@ function MarketProtocol.sendMarketCancelOffer(counter)
end end
end end
function MarketProtocol.sendMarketAcceptOffer(counter) function MarketProtocol.sendMarketAcceptOffer(timestamp, counter, amount)
if g_game.getFeature(GamePlayerMarket) then if g_game.getFeature(GamePlayerMarket) then
local msg = OutputMessage.create() local msg = OutputMessage.create()
msg:addU8(ClientOpcodes.ClientMarketAccept) msg:addU8(ClientOpcodes.ClientMarketAccept)
msg:addU32(os.time()) msg:addU32(timestamp)
msg:addU16(counter) msg:addU16(counter)
msg:addU16(amount)
print('sent')
send(msg) send(msg)
else else
g_logger.error('MarketProtocol.sendMarketAcceptOffer does not support the current protocol.') g_logger.error('MarketProtocol.sendMarketAcceptOffer does not support the current protocol.')

View File

@ -0,0 +1,39 @@
AmountWindow < MainWindow
id: amountWindow
!text: tr('Amount')
size: 196 112
Label
!text: tr('Amount:')
width: 64
anchors.left: parent.left
anchors.top: parent.top
margin-top: 2
SpinBox
id: amountSpinBox
anchors.left: prev.right
anchors.right: parent.right
anchors.top: parent.top
HorizontalScrollBar
id: amountScrollBar
anchors.left: parent.left
anchors.right: parent.right
anchors.top: prev.bottom
margin-top: 8
Button
id: buttonOk
!text: tr('Ok')
width: 64
anchors.right: next.left
anchors.bottom: parent.bottom
margin-right: 5
Button
id: buttonCancel
!text: tr('Cancel')
width: 64
anchors.right: parent.right
anchors.bottom: parent.bottom

View File

@ -101,7 +101,7 @@ Panel
margin-top: 3 margin-top: 3
width: 75 width: 75
minimum: 1 minimum: 1
maximum: 99999999 maximum: 999999999
$disabled: $disabled:
color: #aaaaaa44 color: #aaaaaa44
@ -122,7 +122,7 @@ Panel
margin-top: 3 margin-top: 3
width: 75 width: 75
minimum: 1 minimum: 1
maximum: 99999999 maximum: 999999999
$disabled: $disabled:
color: #aaaaaa44 color: #aaaaaa44
@ -150,7 +150,7 @@ Panel
margin-left: 3 margin-left: 3
width: 55 width: 55
minimum: 1 minimum: 1
maximum: 999999 maximum: 64000
NextButton NextButton
id: nextAmountButton id: nextAmountButton
@ -166,7 +166,6 @@ Panel
anchors.left: prev.right anchors.left: prev.right
margin-left: 7 margin-left: 7
width: 90 width: 90
//@onClick: g_game.closeNpcTrade()
CheckBox CheckBox
id: anonymousCheckBox id: anonymousCheckBox

View File

@ -40,12 +40,13 @@ Panel
margin: 1 margin: 1
Button Button
id: buyButton
!text: tr('Buy Now') !text: tr('Buy Now')
anchors.right: parent.right anchors.right: parent.right
anchors.bottom: next.bottom anchors.bottom: next.bottom
margin-right: 6 margin-right: 6
width: 80 width: 80
//@onClick: g_game.closeNpcTrade() enabled: false
Label Label
!text: tr('Sell Offers') !text: tr('Sell Offers')
@ -93,13 +94,14 @@ Panel
pixels-scroll: true pixels-scroll: true
Button Button
id: sellButton
!text: tr('Sell Now') !text: tr('Sell Now')
anchors.right: parent.right anchors.right: parent.right
anchors.top: prev.bottom anchors.top: prev.bottom
margin-top: 5 margin-top: 5
margin-right: 6 margin-right: 6
width: 80 width: 80
//@onClick: g_game.closeNpcTrade() enabled: false
Label Label
!text: tr('Buy Offers') !text: tr('Buy Offers')