• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

On Trade Accept Create Log.txt

Larb

New Member
Joined
Nov 26, 2016
Messages
85
Reaction score
1
i need script if player accept trade by player create log with itens trade and player's name
TFS 1.2
 
Last edited:
Solution
use a recursive container search:
Lua:
function searchContainer(container, items)
    items = items or {}
    for i = container:getSize()-1, 0, -1 do
        local item = container:getItem(i)
        if item:getType():isContainer() then
            searchContainer(item, items)
        else
            items[#items+1] = item
        end
    end
    return items
end
Very nice, added to the script
Lua:
function Player:onTradeAccept(target, item, targetItem)
    file = io.open('data/logs/trade.log',"a")
    file:write(""..os.date("%c")..": "..self:getName().." traded:")
    if item:isContainer() then
        local pitems = searchContainer(item)
        for i = 1, #pitems do
            file:write(string.format(' %s (%s)(%s),'...
use a recursive container search:
Lua:
function searchContainer(container, items)
    items = items or {}
    for i = container:getSize()-1, 0, -1 do
        local item = container:getItem(i)
        if item:getType():isContainer() then
            searchContainer(item, items)
        else
            items[#items+1] = item
        end
    end
    return items
end

Lua:
local items = searchContainer(item)
I trust your judgement but doesn't Lua index from 1 not from 0? Is that why he is getting the error?
EDIT: Try it again now, I think it is my fault
 
if trade bp

Lua Script Error: [Event Interface]
data/events/scripts/player.lua:player@onTradeAccept
data/events/scripts/player.lua:167: attempt to index a nil value
stack traceback:
[C]: in function '__index'
data/events/scripts/player.lua:167: in function <data/events/scripts/pla
yer.lua:160>
 
Sorry my bad
100: Test1 traded book of lies, with Test for brown mushroom, rope, platinum coin, bar of gold, bar of gold, bar of gold, bar of gold, bar of gold, light shovel, crystal coin, energy field rune, addon doll, book of lies,101: [ADM] Gambler traded dwarven shield with Esse Nome for brown mushroom, rope, platinum coin, bar of gold, bar of gold, bar of gold, bar of gold, bar of gold, light shovel, crystal coin, energy field rune, addon doll, book of lies,

@Aled
 
Last edited:
I trust your judgement but doesn't Lua index from 1 not from 0? Is that why he is getting the error?
EDIT: Try it again now, I think it is my fault
you are correct, Lua does, but not C++
the containers are made in C++ (std::deque), which is why indexing starts from 0
 
I made some changes:
Lua:
function Player:onTradeAccept(target, item, targetItem)
    file = io.open('data/logs/trade.log',"a")
    file:write(""..os.date("%c")..": "..self:getName().." traded:")
    if item:isContainer() then
        local pitems = searchContainer(item)
        for i = 1, #pitems do
            file:write(string.format(' %s (%s)(%s),',  pitems[i]:getName(), pitems[i]:getId(), pitems[i]:getCount() > 1 and pitems[i]:getCount()))
        end
    else
        file:write(string.format(' %s (%s)(%s),', item:getName(), item:getId(), item:getCount() > 1 and item:getCount()))
    end
    file:write(" with "..target:getName().." for:")
    if targetItem:isContainer() then
        local titems = searchContainer(targetItem)
        for i = 1, #titems do
            file:write(string.format(' %s (%s)(%s),', titems[i]:getName(), titems[i]:getId(), titems[i]:getCount() > 1 and titems[i]:getCount()))
        end
    else
        file:write(string.format(' %s (%s)(%s).', targetItem:getName(), targetItem:getId(), targetItem:getCount() > 1 and targetItem:getCount()))
    end
    file:write('\n-------------------------\n\n')
    file:close()
    return true
end

Code:
Thu Apr 20 12:12:18 2017: Knight traded: soul orb (5944)(6), with Druid for: giant sword (2393)(false).
-------------------------

Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893)(false).
-------------------------

But someone can help me with this (false) if count is 1, want to looks like:
Code:
Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893).
 
Last edited:
I made some changes:
Lua:
function Player:onTradeAccept(target, item, targetItem)
    file = io.open('data/logs/trade.log',"a")
    file:write(""..os.date("%c")..": "..self:getName().." traded:")
    if item:isContainer() then
        local pitems = searchContainer(item)
        local count = item.getCount()
        local countshow = count > 1
        for i = 1, #pitems do
            file:write(string.format(' %s (%s)(%s),',  pitems[i]:getName(), pitems[i]:getId(), pitems[i]:getCount() > 1 and pitems[i]:getCount()))
        end
    else
        file:write(string.format(' %s (%s)(%s),', item:getName(), item:getId(), item:getCount() > 1 and item:getCount()))
    end
    file:write(" with "..target:getName().." for:")
    if targetItem:isContainer() then
        local titems = searchContainer(targetItem)
        for i = 1, #titems do
            file:write(string.format(' %s (%s)(%s),', titems[i]:getName(), titems[i]:getId(), titems[i]:getCount() > 1 and titems[i]:getCount()))
        end
    else
        file:write(string.format(' %s (%s)(%s).', targetItem:getName(), targetItem:getId(), targetItem:getCount() > 1 and targetItem:getCount()))
    end
    file:write('\n-------------------------\n\n')
    file:close()
    return true
end

Code:
Thu Apr 20 12:12:18 2017: Knight traded: soul orb (5944)(6), with Druid for: giant sword (2393)(false).
-------------------------

Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893)(false).
-------------------------

But someone can help me with this (false) if count is 1, want to looks like:
Code:
Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893).
just use item:getCount() or check > 0 instead of > 1
 
I made some changes:
Lua:
function Player:onTradeAccept(target, item, targetItem)
    file = io.open('data/logs/trade.log',"a")
    file:write(""..os.date("%c")..": "..self:getName().." traded:")
    if item:isContainer() then
        local pitems = searchContainer(item)
        for i = 1, #pitems do
            file:write(string.format(' %s (%s)(%s),',  pitems[i]:getName(), pitems[i]:getId(), pitems[i]:getCount() > 1 and pitems[i]:getCount()))
        end
    else
        file:write(string.format(' %s (%s)(%s),', item:getName(), item:getId(), item:getCount() > 1 and item:getCount()))
    end
    file:write(" with "..target:getName().." for:")
    if targetItem:isContainer() then
        local titems = searchContainer(targetItem)
        for i = 1, #titems do
            file:write(string.format(' %s (%s)(%s),', titems[i]:getName(), titems[i]:getId(), titems[i]:getCount() > 1 and titems[i]:getCount()))
        end
    else
        file:write(string.format(' %s (%s)(%s).', targetItem:getName(), targetItem:getId(), targetItem:getCount() > 1 and targetItem:getCount()))
    end
    file:write('\n-------------------------\n\n')
    file:close()
    return true
end

Code:
Thu Apr 20 12:12:18 2017: Knight traded: soul orb (5944)(6), with Druid for: giant sword (2393)(false).
-------------------------

Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893)(false).
-------------------------

But someone can help me with this (false) if count is 1, want to looks like:
Code:
Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893).
just use item:getCount() or check > 0 instead of > 1

Awesome guys thanks for the insight, added to the 'best answer' post for everyone else's benefit
 
I made some changes:
Lua:
function Player:onTradeAccept(target, item, targetItem)
    file = io.open('data/logs/trade.log',"a")
    file:write(""..os.date("%c")..": "..self:getName().." traded:")
    if item:isContainer() then
        local pitems = searchContainer(item)
        for i = 1, #pitems do
            file:write(string.format(' %s (%s)(%s),',  pitems[i]:getName(), pitems[i]:getId(), pitems[i]:getCount() > 1 and pitems[i]:getCount()))
        end
    else
        file:write(string.format(' %s (%s)(%s),', item:getName(), item:getId(), item:getCount() > 1 and item:getCount()))
    end
    file:write(" with "..target:getName().." for:")
    if targetItem:isContainer() then
        local titems = searchContainer(targetItem)
        for i = 1, #titems do
            file:write(string.format(' %s (%s)(%s),', titems[i]:getName(), titems[i]:getId(), titems[i]:getCount() > 1 and titems[i]:getCount()))
        end
    else
        file:write(string.format(' %s (%s)(%s).', targetItem:getName(), targetItem:getId(), targetItem:getCount() > 1 and targetItem:getCount()))
    end
    file:write('\n-------------------------\n\n')
    file:close()
    return true
end

Code:
Thu Apr 20 12:12:18 2017: Knight traded: soul orb (5944)(6), with Druid for: giant sword (2393)(false).
-------------------------

Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893)(false).
-------------------------

But someone can help me with this (false) if count is 1, want to looks like:
Code:
Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893).
just use item:getCount() or check > 0 instead of > 1
Awesome guys thanks for the insight, added to the 'best answer' post for everyone else's benefit

I've tried this code as I was looking for something similar, however, when I try to trade two different items which would be one without any container and other is a container, when the last player accepts the deal, it crashes. Let me explain better.

For instance, If I have a gold coin and I trade with someone else for a backpack which has another gold coin inside. I will send a trade request with my single gold coin, once I get the target request I will be the first one to accept the trade and the target last one. This is when the crash produces. If the items on both sides are no-container, it works. If both of the items are containers, it works as well. And if items are no-container vs container, the last one to accept has to be the person with the no-container item in order to avoid the crash.

Quite weird but I cannot really understand what's going on. Seems like after
Lua:
if item:isContainer() then
it crashes.
 
Back
Top