2012-07-17 16:36:27 +02:00
|
|
|
MarketProtocol = {}
|
|
|
|
|
|
|
|
-- private functions
|
2012-07-19 20:54:24 +02:00
|
|
|
|
|
|
|
local protocol
|
2012-07-28 15:41:10 +02:00
|
|
|
local statistics = runinsandbox('offerstatistic.lua')
|
2012-07-19 20:54:24 +02:00
|
|
|
|
2012-07-18 01:31:32 +02:00
|
|
|
local function send(msg)
|
2012-07-19 20:54:24 +02:00
|
|
|
if protocol then
|
|
|
|
protocol:send(msg)
|
|
|
|
end
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
2012-07-18 01:31:32 +02:00
|
|
|
local function readMarketOffer(msg, action, var)
|
2012-07-17 16:36:27 +02:00
|
|
|
local timestamp = msg:getU32()
|
|
|
|
local counter = msg:getU16()
|
|
|
|
|
|
|
|
local itemId = 0
|
|
|
|
if var == MarketRequest.MyOffers or var == MarketRequest.MyHistory then
|
|
|
|
itemId = msg:getU16()
|
|
|
|
else
|
|
|
|
itemId = var
|
|
|
|
end
|
|
|
|
|
|
|
|
local amount = msg:getU16()
|
|
|
|
local price = msg:getU32()
|
|
|
|
local playerName
|
|
|
|
local state = MarketOfferState.Active
|
|
|
|
if var == MarketRequest.MyHistory then
|
|
|
|
state = msg:getU8()
|
|
|
|
else
|
|
|
|
playerName = msg:getString()
|
|
|
|
end
|
2012-07-18 01:49:21 +02:00
|
|
|
|
2012-07-19 20:54:24 +02:00
|
|
|
return MarketOffer.new({timestamp, counter}, action, Item.create(itemId), amount, price, playerName, state)
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- parsing protocols
|
2012-07-18 01:31:32 +02:00
|
|
|
local function parseMarketEnter(msg)
|
2012-07-17 16:36:27 +02:00
|
|
|
local balance = msg:getU32()
|
2012-07-23 17:11:53 +02:00
|
|
|
local vocation = -1
|
2012-07-26 08:10:28 +02:00
|
|
|
if g_game.getClientVersion() < 950 then
|
2012-07-23 17:11:53 +02:00
|
|
|
vocation = msg:getU8() -- get vocation id
|
2012-07-19 20:54:24 +02:00
|
|
|
end
|
2012-07-17 16:36:27 +02:00
|
|
|
local offers = msg:getU8()
|
|
|
|
local depotItems = {}
|
|
|
|
|
2012-07-27 05:53:42 +02:00
|
|
|
local depotCount = msg:getU16()
|
|
|
|
for i = 1, depotCount do
|
2012-07-17 16:36:27 +02:00
|
|
|
local itemId = msg:getU16() -- item id
|
|
|
|
local itemCount = msg:getU16() -- item count
|
|
|
|
|
|
|
|
table.insert(depotItems, {itemId, itemCount})
|
|
|
|
end
|
|
|
|
|
2012-07-23 17:11:53 +02:00
|
|
|
signalcall(Market.onMarketEnter, depotItems, offers, balance, vocation)
|
2012-07-19 20:54:24 +02:00
|
|
|
return true
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
2012-07-18 01:31:32 +02:00
|
|
|
local function parseMarketLeave(msg)
|
2012-07-17 16:36:27 +02:00
|
|
|
Market.onMarketLeave()
|
2012-07-19 20:54:24 +02:00
|
|
|
return true
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
2012-07-18 01:31:32 +02:00
|
|
|
local function parseMarketDetail(msg)
|
2012-07-17 16:36:27 +02:00
|
|
|
local itemId = msg:getU16()
|
|
|
|
|
|
|
|
local descriptions = {}
|
|
|
|
for i = MarketItemDescription.First, MarketItemDescription.Last do
|
|
|
|
if msg:peekU16() ~= 0x00 then
|
2012-07-19 20:54:24 +02:00
|
|
|
table.insert(descriptions, {i, msg:getString()}) -- item descriptions
|
2012-07-17 16:36:27 +02:00
|
|
|
else
|
|
|
|
msg:getU16()
|
|
|
|
end
|
|
|
|
end
|
2012-07-28 15:41:10 +02:00
|
|
|
local time = (os.time() / 1000) * statistics.SECONDS_PER_DAY;
|
2012-07-17 16:36:27 +02:00
|
|
|
|
|
|
|
local purchaseStats = {}
|
2012-07-26 08:10:28 +02:00
|
|
|
local count = msg:getU8()
|
2012-07-28 15:41:10 +02:00
|
|
|
for i=1, count do
|
2012-07-17 16:36:27 +02:00
|
|
|
local transactions = msg:getU32() -- transaction count
|
|
|
|
local totalPrice = msg:getU32() -- total price
|
|
|
|
local highestPrice = msg:getU32() -- highest price
|
2012-07-28 15:41:10 +02:00
|
|
|
local lowestPrice = msg:getU32() -- lowest price
|
2012-07-17 16:36:27 +02:00
|
|
|
|
2012-07-28 15:41:10 +02:00
|
|
|
local tmp = time - statistics.SECONDS_PER_DAY
|
|
|
|
table.insert(purchaseStats, OfferStatistic.new(tmp, MarketAction.Buy, transactions, totalPrice, highestPrice, lowestPrice))
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local saleStats = {}
|
2012-07-26 08:10:28 +02:00
|
|
|
count = msg:getU8()
|
2012-07-28 15:41:10 +02:00
|
|
|
for i=1, count do
|
2012-07-17 16:36:27 +02:00
|
|
|
local transactions = msg:getU32() -- transaction count
|
|
|
|
local totalPrice = msg:getU32() -- total price
|
|
|
|
local highestPrice = msg:getU32() -- highest price
|
2012-07-28 15:41:10 +02:00
|
|
|
local lowestPrice = msg:getU32() -- lowest price
|
2012-07-17 16:36:27 +02:00
|
|
|
|
2012-07-28 15:41:10 +02:00
|
|
|
local tmp = time - statistics.SECONDS_PER_DAY
|
|
|
|
table.insert(saleStats, OfferStatistic.new(tmp, MarketAction.Sell, transactions, totalPrice, highestPrice, lowestPrice))
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
signalcall(Market.onMarketDetail, itemId, descriptions, purchaseStats, saleStats)
|
2012-07-19 20:54:24 +02:00
|
|
|
return true
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
2012-07-18 01:31:32 +02:00
|
|
|
local function parseMarketBrowse(msg)
|
2012-07-17 16:36:27 +02:00
|
|
|
local var = msg:getU16()
|
|
|
|
local offers = {}
|
|
|
|
|
2012-07-27 05:53:42 +02:00
|
|
|
local buyOfferCount = msg:getU32()
|
|
|
|
for i = 1, buyOfferCount do
|
2012-07-17 16:36:27 +02:00
|
|
|
table.insert(offers, readMarketOffer(msg, MarketAction.Buy, var))
|
|
|
|
end
|
|
|
|
|
2012-07-27 05:53:42 +02:00
|
|
|
local sellOfferCount = msg:getU32()
|
|
|
|
for i = 1, sellOfferCount do
|
2012-07-17 16:36:27 +02:00
|
|
|
table.insert(offers, readMarketOffer(msg, MarketAction.Sell, var))
|
|
|
|
end
|
|
|
|
|
2012-07-20 20:20:06 +02:00
|
|
|
signalcall(Market.onMarketBrowse, offers)
|
2012-07-19 20:54:24 +02:00
|
|
|
return true
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- public functions
|
2012-07-27 05:53:42 +02:00
|
|
|
function initProtocol()
|
2012-07-19 20:54:24 +02:00
|
|
|
connect(g_game, { onGameStart = MarketProtocol.registerProtocol,
|
|
|
|
onGameEnd = MarketProtocol.unregisterProtocol })
|
|
|
|
|
|
|
|
-- reloading module
|
|
|
|
if g_game.isOnline() then
|
|
|
|
MarketProtocol.updateProtocol(g_game.getProtocolGame())
|
|
|
|
end
|
2012-07-17 16:36:27 +02:00
|
|
|
end
|
|
|
|
|
2012-07-27 05:53:42 +02:00
|
|
|
function terminateProtocol()
|
2012-07-19 20:54:24 +02:00
|
|
|
disconnect(g_game, { onGameStart = MarketProtocol.registerProtocol,
|
|
|
|
onGameEnd = MarketProtocol.unregisterProtocol })
|
2012-07-17 16:36:27 +02:00
|
|
|
|
2012-07-19 20:54:24 +02:00
|
|
|
-- reloading module
|
|
|
|
if not g_game.isOnline() then
|
|
|
|
MarketProtocol.updateProtocol(nil)
|
|
|
|
end
|
2012-07-17 16:36:27 +02:00
|
|
|
MarketProtocol = nil
|
|
|
|
end
|
|
|
|
|
2012-07-19 20:54:24 +02:00
|
|
|
function MarketProtocol.updateProtocol(_protocol)
|
|
|
|
protocol = _protocol
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketProtocol.registerProtocol()
|
|
|
|
if g_game.getFeature(GamePlayerMarket) then
|
|
|
|
ProtocolGame.registerOpcode(GameServerOpcodes.GameServerMarketEnter, parseMarketEnter)
|
|
|
|
ProtocolGame.registerOpcode(GameServerOpcodes.GameServerMarketLeave, parseMarketLeave)
|
|
|
|
ProtocolGame.registerOpcode(GameServerOpcodes.GameServerMarketDetail, parseMarketDetail)
|
|
|
|
ProtocolGame.registerOpcode(GameServerOpcodes.GameServerMarketBrowse, parseMarketBrowse)
|
|
|
|
end
|
|
|
|
MarketProtocol.updateProtocol(g_game.getProtocolGame())
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketProtocol.unregisterProtocol()
|
|
|
|
if g_game.getFeature(GamePlayerMarket) then
|
|
|
|
ProtocolGame.unregisterOpcode(GameServerOpcodes.GameServerMarketEnter, parseMarketEnter)
|
|
|
|
ProtocolGame.unregisterOpcode(GameServerOpcodes.GameServerMarketLeave, parseMarketLeave)
|
|
|
|
ProtocolGame.unregisterOpcode(GameServerOpcodes.GameServerMarketDetail, parseMarketDetail)
|
|
|
|
ProtocolGame.unregisterOpcode(GameServerOpcodes.GameServerMarketBrowse, parseMarketBrowse)
|
|
|
|
end
|
|
|
|
MarketProtocol.updateProtocol(nil)
|
|
|
|
end
|
|
|
|
|
2012-07-17 16:36:27 +02:00
|
|
|
-- sending protocols
|
|
|
|
|
|
|
|
function MarketProtocol.sendMarketLeave()
|
|
|
|
if g_game.getFeature(GamePlayerMarket) then
|
|
|
|
local msg = OutputMessage.create()
|
|
|
|
msg:addU8(ClientOpcodes.ClientMarketLeave)
|
|
|
|
send(msg)
|
|
|
|
else
|
|
|
|
g_logger.error('MarketProtocol.sendMarketLeave does not support the current protocol.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketProtocol.sendMarketBrowse(browseId)
|
|
|
|
if g_game.getFeature(GamePlayerMarket) then
|
|
|
|
local msg = OutputMessage.create()
|
|
|
|
msg:addU8(ClientOpcodes.ClientMarketBrowse)
|
|
|
|
msg:addU16(browseId)
|
|
|
|
send(msg)
|
|
|
|
else
|
|
|
|
g_logger.error('MarketProtocol.sendMarketBrowse does not support the current protocol.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MarketProtocol.sendMarketCreateOffer(type, spriteId, amount, price, anonymous)
|
|
|
|
if g_game.getFeature(GamePlayerMarket) then
|
|
|
|
local msg = OutputMessage.create()
|
|
|
|
msg:addU8(ClientOpcodes.ClientMarketCreate)
|
|
|
|
msg:addU8(type)
|
|
|
|
msg:addU16(spriteId)
|
|
|
|
msg:addU16(amount)
|
|
|
|
msg:addU32(price)
|
|
|
|
msg:addU8(anonymous)
|
|
|
|
send(msg)
|
|
|
|
else
|
|
|
|
g_logger.error('MarketProtocol.sendMarketCreateOffer does not support the current protocol.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-30 10:02:02 +02:00
|
|
|
function MarketProtocol.sendMarketCancelOffer(timestamp, counter)
|
2012-07-17 16:36:27 +02:00
|
|
|
if g_game.getFeature(GamePlayerMarket) then
|
|
|
|
local msg = OutputMessage.create()
|
|
|
|
msg:addU8(ClientOpcodes.ClientMarketCancel)
|
2012-07-30 10:02:02 +02:00
|
|
|
msg:addU32(timestamp)
|
2012-07-17 16:36:27 +02:00
|
|
|
msg:addU16(counter)
|
|
|
|
send(msg)
|
|
|
|
else
|
|
|
|
g_logger.error('MarketProtocol.sendMarketCancelOffer does not support the current protocol.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-29 16:05:32 +02:00
|
|
|
function MarketProtocol.sendMarketAcceptOffer(timestamp, counter, amount)
|
2012-07-17 16:36:27 +02:00
|
|
|
if g_game.getFeature(GamePlayerMarket) then
|
|
|
|
local msg = OutputMessage.create()
|
|
|
|
msg:addU8(ClientOpcodes.ClientMarketAccept)
|
2012-07-29 16:05:32 +02:00
|
|
|
msg:addU32(timestamp)
|
2012-07-17 16:36:27 +02:00
|
|
|
msg:addU16(counter)
|
2012-07-29 16:05:32 +02:00
|
|
|
msg:addU16(amount)
|
2012-07-17 16:36:27 +02:00
|
|
|
send(msg)
|
|
|
|
else
|
|
|
|
g_logger.error('MarketProtocol.sendMarketAcceptOffer does not support the current protocol.')
|
|
|
|
end
|
|
|
|
end
|