• 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!

CreatureEvent Trade logs

Jano

oturhaN
Joined
Feb 21, 2008
Messages
876
Solutions
1
Reaction score
68
Location
Chile
Not much to say, this will create daily logs of every trade in your servers, making easier track items ...

Lua:
local servers =    {[0] = 'server1', [1] = 'server2', [2] = 'server3'}

local function getType(item)
    return (item.type > 0) and item.type or 1
end

Log = {}
Log.__index = Log

function Log.create()
    local t = {}
    setmetatable(t, Log)
    t.file = servers[getConfigValue("worldId")] .. "/" .. os.date("%B-%d-%Y", os.time()) .. ".txt"
    t.str, t.cstr, t.con = '', '', 0
    return t
end

function Log:write()
    local f = io.open("data/logs/trades/" .. self.file, "a+")
    if not f then return false end
    f:write(self.str)
    f:close()
end

function Log:containerString()
    self.cstr = ''
    for i = 1, self.con do
        self.cstr = self.cstr .. '-> '
    end
end

function Log:addContainer()
    self.con = self.con + 1
    self:containerString()
end

function Log:closeContainer()
    self.con = self.con - 1
    self:containerString()
end

function Log:setLine(txt)
    self.str = self.str .. self.cstr .. txt .. '\n'
end

function Log:kill()
    self.file, self.cstr, self.str, self.con = "", "", "", -1
end
    
function onTradeAccept(cid, target, item, targetItem)
    local this = Log.create()
    local name, tname = getCreatureName(cid), getCreatureName(target)
    
    this:setLine("Trade between " .. name .. " and " .. tname .. " || [" .. os.date("%d/%m/%Y %H:%M:%S") .. "]")
    
    local function logging(cid, item)
        this:setLine(getCreatureName(cid) .. " traded:")
        local function scanContainer(cid, uid)
            for k = (getContainerSize(uid) - 1), 0, -1 do
                local tmp = getContainerItem(uid, k)
                this:setLine(getItemNameById(tmp.itemid) .. " x " .. getType(tmp) .. " || itemid: " .. tmp.itemid)
                if isContainer(tmp.uid) then
                    this:addContainer()
                    scanContainer(cid, tmp.uid)
                    this:closeContainer()
                end
            end
        end
        
        this:setLine(getItemNameById(item.itemid) .. " x " .. getType(item) .. " || itemid: " .. item.itemid) 
        if isContainer(item.uid) then
            this:addContainer()
            scanContainer(cid, item.uid)
            this:closeContainer()
        end
    end
    
    logging(cid, item)
    logging(target, targetItem)
    this:setLine("END OF THIS TRADE --------------\n")
    this:write()
    this:kill()
    return true
end

Make you sure of link it to creaturescripts.xml and register it, also make you sure of create the path folders, since this script doesnt make them.
 
I found one that works

Mods

TradeLog.xml
Lua:
<?xml version="1.0" encoding="UTF-8"?>  
<mod name="Trade Log" version="1.0" author="Vodkart" contact="none.com" enabled="yes">  
<config name="TradeLog_func"><![CDATA[
function natural(number)
local n = number - math.floor(number)
return n >= 0.5 and math.ceil(number) or math.floor(number)
end
function containerItemsToString(cont)
   local s = ""
   for _ = 0, getContainerSize(cont) - 1 do
          local item = getContainerItem(cont, _)
          s = s..",".. item.itemid
          if isContainer(item.uid) and getContainerSize(item.uid) > 0 then
                 s = s..":1"..containerItemsToString(item.uid)
          else
                 s = s..":".. natural(getItemWeight(item.uid)/getItemWeightById(item.itemid))
          end
   end
   return s
end
]]></config>
<event type="login" name="TradeRegister" event="script"><![CDATA[
function onLogin(cid)
registerCreatureEvent(cid, "TradeLog")
        return true
        end]]></event>    
<event type="tradeaccept" name="TradeLog" event="script"><![CDATA[
domodlib('TradeLog_func')
function onTradeAccept(cid, target, item, targetItem)
if (not isPlayer(cid)) or (not isPlayer(target)) then return false end
local items1,items2 = ""..item.itemid,""..targetItem.itemid
items1 = isContainer(item.uid) and getContainerSize(item.uid) > 0 and items1..":1"..containerItemsToString(item.uid) or items1 ..":".. natural(getItemWeight(item.uid)/getItemWeightById(item.itemid))
items2 = isContainer(targetItem.uid) and getContainerSize(targetItem.uid) > 0 and items2 ..":1"..containerItemsToString(targetItem.uid) or items2 ..":".. natural(getItemWeight(targetItem.uid)/getItemWeightById(targetItem.itemid))
f = io.open("data/logs/Trades.txt", "a+")
f:write("World: ".. getConfigValue("worldId") .." -  "..getPlayerName(cid).." Traded: [".. items1 .."] for [".. items2 .."] With Player "..getPlayerName(target)..". At " .. os.date("%d %B %Y - %X.", os.time()) .."\n\n----------------------------------------------------------\n")
f:close()
return true
end
]]></event>
</mod>

Author: Vodkart
 
Last edited:
Back
Top