function table_eq(table1, table2)
local avoid_loops = {}
local function recurse(t1, t2)
-- compare value types
if type(t1) ~= type(t2) then return false end
-- Base case: compare simple values
if type(t1) ~= "table" then return t1 == t2 end
-- Now, on to tables.
-- First, let's avoid looping forever.
if avoid_loops[t1] then return avoid_loops[t1] == t2 end
avoid_loops[t1] = t2
-- Copy keys from t2
local t2keys = {}
local t2tablekeys = {}
for k, _ in pairs(t2) do
if type(k) == "table" then table.insert(t2tablekeys, k) end
t2keys[k] = true
end
-- Let's iterate keys from t1
for k1, v1 in pairs(t1) do
local v2 = t2[k1]
if type(k1) == "table" then
-- if key is a table, we need to find an equivalent one.
local ok = false
for i, tk in ipairs(t2tablekeys) do
if table_eq(k1, tk) and recurse(v1, t2[tk]) then
table.remove(t2tablekeys, i)
t2keys[tk] = nil
ok = true
break
end
end
if not ok then return false end
else
-- t1 has a key which t2 doesn't have, fail.
if v2 == nil then return false end
t2keys[k1] = nil
if not recurse(v1, v2) then return false end
end
end
-- if t2 has a key which t1 doesn't have, fail.
if next(t2keys) then return false end
return true
end
return recurse(table1, table2)
end
local sellableI = 0
function marketEdit(itemCheck, tooltipCheck)
if itemCheck and itemCheck ~= nil then
local getTooltip = json.decode(tooltipCheck)
itemCheck:setCustomAttribute("upgradeLevel", getTooltip.itemLevel)
for i,child in pairs(getTooltip.attr) do
if child[2] > 0 then
for _i, _child in pairs(nameAttributes) do
if _child[2] == i then
itemCheck:setCustomAttribute(_i, child[2])
end
end
end
end
end
end
local availableItems
function marketEnter(player, itemCheck, tooltipCheck)
if itemCheck and itemCheck ~= nil then
if table_eq(itemCheck:buildTooltip(json.decode(tooltipCheck).uid), json.decode(tooltipCheck)) then
return true
else
return false
end
else
local sellableItems = {}
local getDepot = player:getDepotChest(1)
local getContainer = player:getSlotItem(CONST_SLOT_BACKPACK)
if not availableItems then
availableItems = {}
for i = 100,30000 do
local itemType = ItemType(i)
if itemType and itemType:isPickupable() then
table.insert(availableItems,itemType:buildTooltip(8999999+i))
end
end
end
if getDepot then
for i,child in pairs(getDepot:getItems(true)) do
sellableI = sellableI + 1
local itemType = ItemType(child:getId())
if itemType then
if table_eq(itemType:buildTooltip(9999999+sellableI),child:buildTooltip(9999999+sellableI)) then
sellableItems[tostring(itemType:getClientId())] = (sellableItems[tostring(itemType:getClientId())] or 0) + child:getCount()
else
if not sellableItems[tostring(9999999+itemType:getClientId())] then sellableItems[tostring(9999999+itemType:getClientId())] = {} end
table.insert(sellableItems[tostring(9999999+itemType:getClientId())], child:buildTooltip(9999999+sellableI))
end
end
end
end
if getContainer then
for i,child in pairs(getContainer:getItems(true)) do
sellableI = sellableI + 1
local itemType = ItemType(child:getId())
if itemType then
if table_eq(itemType:buildTooltip(9999999+sellableI),child:buildTooltip(9999999+sellableI)) then
sellableItems[tostring(itemType:getClientId())] = (sellableItems[tostring(itemType:getClientId())] or 0) + child:getCount()
else
if not sellableItems[tostring(9999999+itemType:getClientId())] then sellableItems[tostring(9999999+itemType:getClientId())] = {} end
table.insert(sellableItems[tostring(9999999+itemType:getClientId())], child:buildTooltip(9999999+sellableI))
end
end
end
end
if player:isUsingOtClient() then
msg = NetworkMessage()
msg:addByte(0xF6)
msg:addU64(player:getBankBalance())
msg:addU64(player:getPremiumPoints())
msg:addByte(player:marketOfferCount())
msg:addString(json.encode(sellableItems))
msg:sendToPlayer(player)
if player:getGroup():getAccess() then
local file,err = io.open("availableItems.txt",'w')
if file then
file:write(json.encode(availableItems))
file:close()
else
print("availableItems file error:", err)
end
end
else
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Market is available only on otclient!")
end
end
return true
end