• 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),'...
In events/scripts/player.lua put
Lua:
function Player:onTradeAccept(target, item, targetItem)
    local currenttime = tonumber(os.date("%H%M", os.time()))
    file = io.open('data/logs/tradelog.txt',"w")
    file:write(""..currenttime..": "..player:getName().." traded "..item:getName().." for "..targetItem:getName().." with "..target:getName()...".")
    file:close()
    return true
end
I don't know if it will work and I don't think 0.4 has onTradeAccept
 
Last edited:
In events/scripts/player.lua put
Lua:
function Player:onTradeAccept(target, item, targetItem)
    local currenttime = tonumber(os.date("%H%M", os.time()))
    file = io.open('data/logs/tradelog.txt',"w")
    file:write(""..currenttime..": "..player.." traded "..item.." for "..targetItem.." with "..target..".")
    file:close()
    return true
end
I don't know if it will work and I don't think 0.4 has onTradeAccept

erro

Lua Script Error: [Event Interface]
data/events/scripts/player.lua:player@onTradeAccept
data/events/scripts/player.lua:151: attempt to concatenate local 'target' (a use
rdata value)
stack traceback:
[C]: in function '__concat'
data/events/scripts/player.lua:151: in function <data/events/scripts/pla
yer.lua:148>
 
if it's a container it'll need description of items inside
That's one thing I couldn't be bothered to do
EDIT:
I don't know if this will work or what it will look like:
Lua:
function Player:onTradeAccept(target, item, targetItem)
    local currenttime = tonumber(os.date("%H%M", os.time()))
    file = io.open('data/logs/tradelog.txt',"w")
    file:write(""..currenttime..": "..self:getName().." traded")
    if item:isContainer() then
    local pitems = item:getItemHoldingCount()
        for i, pitems do
            file:write(" "..item:getItem(i):getName()..",")
        end
    else
        file:write(" "..item:getName().."")
    end
    file:write(" with "..target:getName().." for")
    if targetItem:isContainer() then
    local titems = targetItem:getItemHoldingCount()
        for i, titems do
            file:write(" "..targetItem:getItem(i):getName()..",")
        end
    else
        file:write(" "..targetItem:getName()..".")
    end
    file:close()
    return true
end
 
Last edited:
That's one thing I couldn't be bothered to do
EDIT:
I don't know if this will work or what it will look like:
Lua:
function Player:onTradeAccept(target, item, targetItem)
    local currenttime = tonumber(os.date("%H%M", os.time()))
    file = io.open('data/logs/tradelog.txt',"w")
    file:write(""..currenttime..": "..player.." traded")
    if item:isContainer() then
        for i, #item:getItemHoldingCount() do
            file:write(" "..item:getItem(i)..",")
        end
    else
        file:write(" "..item.."")
    end
    file:write(" with "..target.." for")
    if targetItem:isContainer() then
        for i, #targetItem:getItemHoldingCount() do
            file:write(" "..targetItem:getItem(i)..",")
        end
    else
        file:write(" "..targetItem..".")
    end
    file:close()
    return true
end
i have erro 1
[Warning - Events::load] Can not load script: player.lua
data/events/scripts/player.lua:153: '<name>' expected near '#'
removed
for i, #
erro 2
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:player@onTradeAccept
data/events/scripts/player.lua:151: attempt to concatenate global 'player' (a ni
l value)
stack traceback:
[C]: in function '__concat'
data/events/scripts/player.lua:151: in function <data/events/scripts/pla
yer.lua:148>
 
That's one thing I couldn't be bothered to do
EDIT:
I don't know if this will work or what it will look like:
Lua:
function Player:onTradeAccept(target, item, targetItem)
    local currenttime = tonumber(os.date("%H%M", os.time()))
    file = io.open('data/logs/tradelog.txt',"w")
    file:write(""..currenttime..": "..self:getName().." traded")
    if item:isContainer() then
    local pitems = item:getItemHoldingCount()
        for i, #pitems do
            file:write(" "..item:getItem(i):getName()..",")
        end
    else
        file:write(" "..item:getName().."")
    end
    file:write(" with "..target:getName().." for")
    if targetItem:isContainer() then
    local titems = targetItem:getItemHoldingCount()
        for i, #titems do
            file:write(" "..targetItem:getItem(i):getName()..",")
        end
    else
        file:write(" "..targetItem:getName()..".")
    end
    file:close()
    return true
end

Some of my thoughts.
Converting local currenttime to number is unnecessary.
You can use "%c" in os.date() to call date and time at once wich i think will be better than just a time. Remember to not to convert it to number then.
io.file "w" mode will overwrite every previous change. Use "a" instead.
'getItemHoldingCount()' returns just a number not items table.

Keep it up mate!
Cheers
 
'getItemHoldingCount()' returns just a number not items table.
Eeuuuuughhhhhhh thats what you call overthinking (or not reading about functions properly and just whacking them in).. fixed

Thanks a lot

EDIT:
Is there a function that returns the items contained in the container and will it return items in the container within the container?
How would you go about doing that?
 
Eeuuuuughhhhhhh thats what you call overthinking (or not reading about functions properly and just whacking them in).. fixed

Thanks a lot

EDIT:
Is there a function that returns the items contained in the container and will it return items in the container within the container?
How would you go about doing that?
[Warning - Events::load] Can not load script: player.lua
data/events/scripts/player.lua:154: 'in' expected near 'do'

@Aled
 
Eeuuuuughhhhhhh thats what you call overthinking (or not reading about functions properly and just whacking them in).. fixed

Thanks a lot

EDIT:
Is there a function that returns the items contained in the container and will it return items in the container within the container?
How would you go about doing that?

container:getItem(index)
You have to loop over every slot(index) of container.
Remember that indexes starts from 0.
 
Last edited:
Eeuuuuughhhhhhh thats what you call overthinking (or not reading about functions properly and just whacking them in).. fixed

Thanks a lot

EDIT:
Is there a function that returns the items contained in the container and will it return items in the container within the container?
How would you go about doing that?

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)
 
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)
where i add it ?
 
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),',  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
Credits to lay for much better string format:
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:
Solution
@Aled very thanks
1926: Test traded book of lies with Test2 for addon doll.
work fine by trade items if i trade bp have this erro
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:player@onTradeAccept
data/events/scripts/player.lua:166: attempt to call method 'getSize' (a nil valu
e)
stack traceback:
[C]: in function 'getSize'
data/events/scripts/player.lua:166: in function <data/events/scripts/pla
yer.lua:160>
--
Log clean if i make new trade
 
Last edited:
Log ok :D
now
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>
 
Back
Top