beeki

Trade Log

May 29th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. local servers = {[0] = 'server1', [1] = 'server2', [2] = 'server3'}
  2.  
  3. local function getType(item)
  4.     return (item.type > 0) and item.type or 1
  5. end
  6.  
  7. Log = {}
  8. Log.__index = Log
  9.  
  10. function Log.create()
  11.     local t = {}
  12.     setmetatable(t, Log)
  13.     t.file = servers[getConfigValue("worldId")] .. "/" .. os.date("%B-%d-%Y", os.time()) .. ".txt"
  14.     t.str, t.cstr, t.con = '', '', 0
  15.     return t
  16. end
  17.  
  18. function Log:write()
  19.     local f = io.open("data/logs/trades/" .. self.file, "a+")
  20.     if not f then return false end
  21.     f:write(self.str)
  22.     f:close()
  23. end
  24.  
  25. function Log:containerString()
  26.     self.cstr = ''
  27.     for i = 1, self.con do
  28.         self.cstr = self.cstr .. '-> '
  29.     end
  30. end
  31.  
  32. function Log:addContainer()
  33.     self.con = self.con + 1
  34.     self:containerString()
  35. end
  36.  
  37. function Log:closeContainer()
  38.     self.con = self.con - 1
  39.     self:containerString()
  40. end
  41.  
  42. function Log:setLine(txt)
  43.     self.str = self.str .. self.cstr .. txt .. '\n'
  44. end
  45.  
  46. function Log:kill()
  47.     self.file, self.cstr, self.str, self.con = "", "", "", -1
  48. end
  49.  
  50. function onTradeAccept(cid, target, item, targetItem)
  51.     local this = Log.create()
  52.     local name, tname = getCreatureName(cid), getCreatureName(target)
  53.  
  54.     this:setLine("Trade between " .. name .. " and " .. tname .. " || [" .. os.date("%d/%m/%Y %H:%M:%S") .. "]")
  55.  
  56.     local function logging(cid, item)
  57.         this:setLine(getCreatureName(cid) .. " traded:")
  58.         local function scanContainer(cid, uid)
  59.             for k = (getContainerSize(uid) - 1), 0, -1 do
  60.                 local tmp = getContainerItem(uid, k)
  61.                 this:setLine(getItemNameById(tmp.itemid) .. " x " .. getType(tmp) .. " || itemid: " .. tmp.itemid)
  62.                 if isContainer(tmp.uid) then
  63.                     this:addContainer()
  64.                     scanContainer(cid, tmp.uid)
  65.                     this:closeContainer()
  66.                 end
  67.             end
  68.         end
  69.  
  70.         this:setLine(getItemNameById(item.itemid) .. " x " .. getType(item) .. " || itemid: " .. item.itemid)
  71.         if isContainer(item.uid) then
  72.             this:addContainer()
  73.             scanContainer(cid, item.uid)
  74.             this:closeContainer()
  75.         end
  76.     end
  77.  
  78.     logging(cid, item)
  79.     logging(target, targetItem)
  80.     this:setLine("END OF THIS TRADE --------------\n")
  81.     this:write()
  82.     this:kill()
  83.     return true
  84. end
  85.  
  86. --     <event type="tradeaccept" name="TradeLog" event="script" value="tradelog.lua" />
Advertisement
Add Comment
Please, Sign In to add comment