SHARE
TWEET

auctionsystem.lua

a guest Jun 5th, 2019 92 in 321 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. Offline player to player item trader (Auction System) by vDk
  3. Script version: 1.2a [ -- FIXED CLONE ITEMS BUG -- ]
  4. ]]--
  5. local config = {
  6.     levelRequiredToAdd = 5,
  7.     SendOffersOnlyInPZ = true,
  8.     blocked_items = {2165, 2152, 2148, 2160, 2166, 2167, 2168, 2169, 2202, 2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2213, 2214, 2215, 2343, 2433, 2640, 6132, 6300, 6301, 9932, 9933, 6093, 2123}
  9. }
  10.  
  11. local function updatePlayerBalance(name, value)
  12.     db.query('UPDATE players SET balance=' .. value .. ' WHERE name=' .. db.escapeString(name) .. ' LIMIT 1')
  13. end
  14.  
  15. function onSay(cid, words, param, channel)
  16.     if(param == '') then
  17.         local msg = "Market:\n\n/market buy, ID\n/market remove, ID\n/market add, ItemName, ItemPrice, ItemCount\n\nMore information look in us website!"
  18.         doPlayerPopupFYI(cid, msg)
  19.         return true
  20.     end
  21.  
  22.     local maxOffersPerPlayer = math.floor(getPlayerLevel(cid) / 5)
  23.     local pricePerOffer = 500
  24.  
  25.     local t = string.explode(param, ",")
  26.     if(t[1] == "add") then
  27.         if((not t[2]) or (not t[3]) or (not t[4])) then
  28.             local msg = "/market add, ItemName, ItemPrice, ItemCount"
  29.             doPlayerPopupFYI(cid, msg)
  30.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, msg)
  31.             return true
  32.         end
  33.  
  34.         if(not tonumber(t[3]) or (not tonumber(t[4]))) then
  35.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't set valid price or items count.")
  36.             return true
  37.         end
  38.  
  39.         if(string.len(t[3]) > 7 or (string.len(t[4]) > 3)) then
  40.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This price or item count is too high.")
  41.             return true
  42.         end
  43.  
  44.         local item = getItemIdByName(t[2], false)
  45.         if(not item) then
  46.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Item wich such name does not exists.")
  47.             return true
  48.         end
  49.  
  50.         if(getPlayerLevel(cid) < config.levelRequiredToAdd) then
  51.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't have required (" .. config.levelRequiredToAdd .. ") level.")
  52.             return true
  53.         end
  54.  
  55.         if(isInArray(config.blocked_items, item)) then
  56.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This item is blocked.")
  57.             return true
  58.         end
  59.  
  60.         if(getPlayerItemCount(cid, item) < (tonumber(t[4]))) then
  61.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you don't have this item(s).")
  62.             return true
  63.         end
  64.  
  65.         local check = db.getResult("SELECT `id` FROM `auction_system` WHERE `player` = " .. getPlayerGUID(cid) .. ";")
  66.         if(check:getID() == -1) then
  67.         elseif(check:getRows(true) >= maxOffersPerPlayer) then
  68.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry you can't add more offers (max for your level: " .. maxOffersPerPlayer .. ")")
  69.                 return true
  70.         end
  71.  
  72.         if(config.SendOffersOnlyInPZ) then
  73.             if(not getTilePzInfo(getPlayerPosition(cid))) then
  74.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must be in PZ area when you add offert to database.")
  75.                 return true
  76.             end
  77.         end
  78.  
  79.         if(tonumber(t[4]) < 1 or (tonumber(t[3]) < 1)) then
  80.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You have to type a number higher than 0.")
  81.             return true
  82.         end
  83.  
  84.         if(getPlayerBalance(cid) < 500) then
  85.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You need to have 500GPs in your bank balance to make a offer.")
  86.             return true
  87.         end
  88.         doPlayerSetBalance(cid, getPlayerBalance(cid) - 500)
  89.         updatePlayerBalance(getCreatureByName(cid), getPlayerBalance(cid))
  90.  
  91.         local itemcount, costgp = math.floor(t[4]), math.floor(t[3])
  92.         doPlayerRemoveItem(cid, item, itemcount)
  93.         db.executeQuery("INSERT INTO `auction_system` (`player`, `item_name`, `item_id`, `count`, `cost`, `date`) VALUES (" .. getPlayerGUID(cid) .. ", \"" .. t[2] .. "\", " .. getItemIdByName(t[2]) .. ", " .. itemcount .. ", " .. costgp ..", " .. os.time() .. ")")
  94.         doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You successfully add " .. itemcount .." " .. t[2] .." for " .. costgp .. " gps to offerts database.")
  95.     end
  96.  
  97.     if(t[1] == "buy") then
  98.         if(not tonumber(t[2])) then
  99.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
  100.             return true
  101.         end
  102.  
  103.         local buy = db.getResult("SELECT * FROM `auction_system` WHERE `id` = " .. (tonumber(t[2])) .. ";")
  104.         if(buy:getID() ~= -1) then
  105.             if (getPlayerBalance(cid) < buy:getDataInt("cost")) then
  106.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't have enoguh Bank Balance.")
  107.                 buy:free()
  108.                 return true
  109.             end
  110.  
  111.             if(getPlayerName(cid) == getPlayerNameByGUID(buy:getDataInt("player"))) then
  112.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you can't buy your own items.")
  113.                 buy:free()
  114.                 return true
  115.             end
  116.  
  117.             if(getPlayerFreeCap(cid) < getItemWeightById(buy:getDataInt("item_id"), buy:getDataInt("count")))then
  118.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You try to buy a " .. buy:getDataString("item_name") .. ". It weight " .. getItemWeightById(buy:getDataInt("item_id"), buy:getDataInt("count")) .. " cap oz. and you have only " .. getPlayerFreeCap(cid) .. " oz. free capacity. Put some items to depot and try again.")
  119.                 buy:free()
  120.                 return true
  121.             end
  122.  
  123.             if(isItemStackable((buy:getDataString("item_id")))) then
  124.                 doPlayerAddItem(cid, buy:getDataString("item_id"), buy:getDataInt("count"))
  125.             else
  126.                 for i = 1, buy:getDataInt("count") do
  127.                     doPlayerAddItem(cid, buy:getDataString("item_id"), 1)
  128.                 end
  129.             end
  130.             doPlayerSetBalance(cid, getPlayerBalance(cid) - buy:getDataInt("cost"))
  131.             updatePlayerBalance(getCreatureName(cid), getPlayerBalance(cid))
  132.            
  133.             db.executeQuery("DELETE FROM `auction_system` WHERE `id` = " .. t[2] .. ";")
  134.             doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You bought " .. buy:getDataInt("count") .. " ".. buy:getDataString("item_name") .. " for " .. buy:getDataInt("cost") .. " gps!")
  135.            
  136.             local tid = getPlayerByGUID(buy:getDataInt("player"))
  137.             if(isPlayer(tid)) then
  138.                 doPlayerSetBalance(tid, getPlayerBalance(tid) + buy:getDataInt("cost"))
  139.             else
  140.                 db.executeQuery("UPDATE `players` SET `balance` = `balance` + " .. buy:getDataInt("cost") .. " WHERE `id` = " .. buy:getDataInt("player") .. ";")
  141.             end
  142.  
  143.             buy:free()
  144.         else
  145.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
  146.         end
  147.     end
  148.  
  149.     if(t[1] == "remove") then
  150.         if((not tonumber(t[2]))) then
  151.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
  152.             return true
  153.         end
  154.  
  155.         if(config.SendOffersOnlyInPZ) then
  156.             if(not getTilePzInfo(getPlayerPosition(cid))) then
  157.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must be in PZ area when you remove offerts from database.")
  158.                 return true
  159.             end
  160.         end
  161.  
  162.         local delete = db.getResult("SELECT * FROM `auction_system` WHERE `id` = " .. (tonumber(t[2])) .. ";")
  163.         if(delete:getID() ~= -1) then
  164.             if(getPlayerGUID(cid) == delete:getDataInt("player")) then
  165.                 db.executeQuery("DELETE FROM `auction_system` WHERE `id` = " .. t[2] .. ";")
  166.                 if(isItemStackable(delete:getDataString("item_id"))) then
  167.                     doPlayerAddItem(cid, delete:getDataString("item_id"), delete:getDataInt("count"))
  168.                 else
  169.                     for i = 1, delete:getDataInt("count") do
  170.                         doPlayerAddItem(cid, delete:getDataString("item_id"), 1)
  171.                     end
  172.                 end
  173.  
  174.                 doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Your offert has been deleted from offerts database.")
  175.             else
  176.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This is not your offert!")
  177.             end
  178.            
  179.             delete:free()
  180.         else
  181.             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
  182.         end
  183.     end
  184.     return true
  185. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top