-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathmarketprotocol.lua
More file actions
260 lines (218 loc) · 7.58 KB
/
marketprotocol.lua
File metadata and controls
260 lines (218 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
MarketProtocol = {}
-- private functions
local silent
local protocol
local statistics = runinsandbox('offerstatistic')
local function send(msg)
if protocol and not silent then
protocol:send(msg)
end
end
local function readMarketOffer(msg, action, var)
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()
elseif var == MarketRequest.MyOffers then
else
playerName = msg:getString()
end
return MarketOffer.new({timestamp, counter}, action, Item.create(itemId), amount, price, playerName, state, var)
end
-- parsing protocols
local function parseMarketEnter(protocol, msg)
local balance
if g_game.getClientVersion() >= 981 then
balance = msg:getU64()
else
balance = msg:getU32()
end
local vocation = -1
if g_game.getClientVersion() < 950 then
vocation = msg:getU8() -- get vocation id
end
local offers = msg:getU8()
local depotItems = {}
local depotCount = msg:getU16()
for i = 1, depotCount do
local itemId = msg:getU16() -- item id
local itemCount = msg:getU16() -- item count
depotItems[itemId] = itemCount
end
signalcall(Market.onMarketEnter, depotItems, offers, balance, vocation)
return true
end
local function parseMarketLeave(protocol, msg)
Market.onMarketLeave()
return true
end
local function parseMarketDetail(protocol, msg)
local itemId = msg:getU16()
local descriptions = {}
for i = MarketItemDescription.First, MarketItemDescription.Last do
if msg:peekU16() ~= 0x00 then
table.insert(descriptions, {i, msg:getString()}) -- item descriptions
else
msg:getU16()
end
end
local time = (os.time() / 1000) * statistics.SECONDS_PER_DAY;
local purchaseStats = {}
local count = msg:getU8()
for i=1, count do
local transactions = msg:getU32() -- transaction count
local totalPrice = msg:getU32() -- total price
local highestPrice = msg:getU32() -- highest price
local lowestPrice = msg:getU32() -- lowest price
local tmp = time - statistics.SECONDS_PER_DAY
table.insert(purchaseStats, OfferStatistic.new(tmp, MarketAction.Buy, transactions, totalPrice, highestPrice, lowestPrice))
end
local saleStats = {}
count = msg:getU8()
for i=1, count do
local transactions = msg:getU32() -- transaction count
local totalPrice = msg:getU32() -- total price
local highestPrice = msg:getU32() -- highest price
local lowestPrice = msg:getU32() -- lowest price
local tmp = time - statistics.SECONDS_PER_DAY
table.insert(saleStats, OfferStatistic.new(tmp, MarketAction.Sell, transactions, totalPrice, highestPrice, lowestPrice))
end
signalcall(Market.onMarketDetail, itemId, descriptions, purchaseStats, saleStats)
return true
end
local function parseMarketBrowse(protocol, msg)
local var = msg:getU16()
local offers = {}
local buyOfferCount = msg:getU32()
for i = 1, buyOfferCount do
table.insert(offers, readMarketOffer(msg, MarketAction.Buy, var))
end
local sellOfferCount = msg:getU32()
for i = 1, sellOfferCount do
table.insert(offers, readMarketOffer(msg, MarketAction.Sell, var))
end
signalcall(Market.onMarketBrowse, offers)
return true
end
local function parseMarketResourcesBalance(protocol, msg)
msg:getU8()
local balance = msg:getU64() -- bank
msg:getU8()
msg:getU8()
local money = msg:getU64() -- inventory
signalcall(Market.onMarketResourceBalance, balance, money)
return true
end
-- public functions
function initProtocol()
connect(g_game, { onGameStart = MarketProtocol.registerProtocol,
onGameEnd = MarketProtocol.unregisterProtocol })
-- reloading module
if g_game.isOnline() then
MarketProtocol.registerProtocol()
end
MarketProtocol.silent(false)
end
function terminateProtocol()
disconnect(g_game, { onGameStart = MarketProtocol.registerProtocol,
onGameEnd = MarketProtocol.unregisterProtocol })
-- reloading module
MarketProtocol.unregisterProtocol()
MarketProtocol = nil
end
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)
ProtocolGame.registerOpcode(GameServerOpcodes.GameServerSendResourceBalance, parseMarketResourcesBalance)
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)
ProtocolGame.unregisterOpcode(GameServerOpcodes.GameServerSendResourceBalance, parseMarketResourcesBalance)
end
MarketProtocol.updateProtocol(nil)
end
function MarketProtocol.silent(mode)
silent = mode
end
-- 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.sendMarketBrowseMyOffers()
MarketProtocol.sendMarketBrowse(MarketRequest.MyOffers)
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
function MarketProtocol.sendMarketCancelOffer(timestamp, counter)
if g_game.getFeature(GamePlayerMarket) then
local msg = OutputMessage.create()
msg:addU8(ClientOpcodes.ClientMarketCancel)
msg:addU32(timestamp)
msg:addU16(counter)
send(msg)
else
g_logger.error('MarketProtocol.sendMarketCancelOffer does not support the current protocol.')
end
end
function MarketProtocol.sendMarketAcceptOffer(timestamp, counter, amount)
if g_game.getFeature(GamePlayerMarket) then
local msg = OutputMessage.create()
msg:addU8(ClientOpcodes.ClientMarketAccept)
msg:addU32(timestamp)
msg:addU16(counter)
msg:addU16(amount)
send(msg)
else
g_logger.error('MarketProtocol.sendMarketAcceptOffer does not support the current protocol.')
end
end