2012-07-17 16:36:27 +02:00
|
|
|
MarketOffer = {}
|
|
|
|
MarketOffer.__index = MarketOffer
|
|
|
|
|
|
|
|
local OFFER_TIMESTAMP = 1
|
|
|
|
local OFFER_COUNTER = 2
|
|
|
|
|
2012-07-28 15:41:10 +02:00
|
|
|
MarketOffer.new = function(offerId, t, item, amount, price, playerName, state)
|
2012-07-17 16:36:27 +02:00
|
|
|
local offer = {
|
|
|
|
id = {},
|
2012-07-28 15:41:10 +02:00
|
|
|
type = nil,
|
2012-07-17 16:36:27 +02:00
|
|
|
item = 0,
|
|
|
|
amount = 0,
|
|
|
|
price = 0,
|
|
|
|
player = '',
|
|
|
|
state = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if not offerId or type(offerId) ~= 'table' then
|
|
|
|
g_logger.error('MarketOffer.new - invalid offer id provided.')
|
|
|
|
end
|
|
|
|
offer.id = offerId
|
|
|
|
|
2012-07-28 15:41:10 +02:00
|
|
|
t = tonumber(t)
|
|
|
|
if t ~= MarketAction.Buy and t ~= MarketAction.Sell then
|
|
|
|
g_logger.error('MarketOffer.new - invalid type provided.')
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
2012-07-28 15:41:10 +02:00
|
|
|
offer.type = t
|
2012-07-17 16:36:27 +02:00
|
|
|
|
2012-07-19 20:54:24 +02:00
|
|
|
if not item then
|
|
|
|
g_logger.error('MarketOffer.new - invalid item provided.')
|
|
|
|
end
|
|
|
|
offer.item = item
|
|
|
|
|
2012-07-17 16:36:27 +02:00
|
|
|
offer.amount = amount
|
|
|
|
offer.price = price
|
|
|
|
offer.player = playerName
|
|
|
|
|
|
|
|
state = tonumber(state)
|
2012-07-18 01:49:21 +02:00
|
|
|
if state ~= MarketOfferState.Active and state ~= MarketOfferState.Cancelled
|
2012-07-17 16:36:27 +02:00
|
|
|
and state ~= MarketOfferState.Expired and state ~= MarketOfferState.Accepted then
|
|
|
|
g_logger.error('MarketOffer.new - invalid state provided.')
|
|
|
|
end
|
|
|
|
offer.state = state
|
|
|
|
|
|
|
|
setmetatable(offer, MarketOffer)
|
|
|
|
return offer
|
|
|
|
end
|
|
|
|
|
2012-07-27 05:53:42 +02:00
|
|
|
function MarketOffer:isEqual(id)
|
|
|
|
return self.id[OFFER_TIMESTAMP] == id[OFFER_TIMESTAMP] and self.id[OFFER_COUNTER] == id[OFFER_COUNTER]
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
2012-07-27 05:53:42 +02:00
|
|
|
function MarketOffer:isLessThan(id)
|
|
|
|
return self.id[OFFER_TIMESTAMP] <= id[OFFER_TIMESTAMP] and self.id[OFFER_COUNTER] < id[OFFER_COUNTER]
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
2012-07-27 05:53:42 +02:00
|
|
|
function MarketOffer:isNull()
|
2012-07-17 16:36:27 +02:00
|
|
|
return table.empty(self.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sets/Gets
|
|
|
|
|
|
|
|
function MarketOffer:setId(id)
|
|
|
|
if not id or type(id) ~= 'table' then
|
|
|
|
g_logger.error('MarketOffer.setId - invalid id provided.')
|
|
|
|
end
|
|
|
|
self.id = id
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:getId()
|
|
|
|
return self.id
|
|
|
|
end
|
|
|
|
|
2012-07-28 15:41:10 +02:00
|
|
|
function MarketOffer:setType(t)
|
|
|
|
if not t or type(t) ~= 'number' then
|
|
|
|
g_logger.error('MarketOffer.setItem - invalid type provided.')
|
2012-07-19 20:54:24 +02:00
|
|
|
end
|
2012-07-28 15:41:10 +02:00
|
|
|
self.type = type
|
2012-07-19 20:54:24 +02:00
|
|
|
end
|
|
|
|
|
2012-07-28 15:41:10 +02:00
|
|
|
function MarketOffer:getType()
|
|
|
|
return self.type
|
2012-07-19 20:54:24 +02:00
|
|
|
end
|
|
|
|
|
2012-07-17 16:36:27 +02:00
|
|
|
function MarketOffer:setItem(item)
|
2012-07-19 20:54:24 +02:00
|
|
|
if not item or type(item) ~= 'userdata' then
|
2012-07-17 16:36:27 +02:00
|
|
|
g_logger.error('MarketOffer.setItem - invalid item id provided.')
|
|
|
|
end
|
|
|
|
self.item = item
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:getItem()
|
|
|
|
return self.item
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:setAmount(amount)
|
|
|
|
if not amount or type(amount) ~= 'number' then
|
|
|
|
g_logger.error('MarketOffer.setAmount - invalid amount provided.')
|
|
|
|
end
|
|
|
|
self.amount = amount
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:getAmount()
|
|
|
|
return self.amount
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:setPrice(price)
|
|
|
|
if not price or type(price) ~= 'number' then
|
|
|
|
g_logger.error('MarketOffer.setPrice - invalid price provided.')
|
|
|
|
end
|
|
|
|
self.price = price
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:getPrice()
|
|
|
|
return self.price
|
|
|
|
end
|
|
|
|
|
2012-07-28 15:41:10 +02:00
|
|
|
function MarketOffer:getTotalPrice()
|
|
|
|
return self.price * self.amount
|
|
|
|
end
|
|
|
|
|
2012-07-17 16:36:27 +02:00
|
|
|
function MarketOffer:setPlayer(player)
|
|
|
|
if not player or type(player) ~= 'number' then
|
|
|
|
g_logger.error('MarketOffer.setPlayer - invalid player provided.')
|
|
|
|
end
|
|
|
|
self.player = player
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:getPlayer()
|
|
|
|
return self.player
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:setState(state)
|
|
|
|
if not state or type(state) ~= 'number' then
|
|
|
|
g_logger.error('MarketOffer.setState - invalid state provided.')
|
|
|
|
end
|
|
|
|
self.state = state
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:getState()
|
|
|
|
return self.state
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:getTimeStamp()
|
|
|
|
if table.empty(self.id) or #self.id < OFFER_TIMESTAMP then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return self.id[OFFER_TIMESTAMP]
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketOffer:getCounter()
|
|
|
|
if table.empty(self.id) or #self.id < OFFER_COUNTER then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return self.id[OFFER_COUNTER]
|
|
|
|
end
|