local itemWindow = {
{id=2160, subType=0, buy=10000, sell=10000, name="Test1"},
{id=2152, subType=0, buy=100, sell=100, name="Test2"},
{id=2148, subType=0, buy=1, sell=1, name="Test3"},
{id=2173, subType=0, buy=10000, sell=5000, name="Test4"}
}
local items = {}
for _, item in ipairs(itemWindow) do
items[item.id] = {buyPrice = item.buy, sellPrice = item.sell, subType = item.subType, realName = item.name}
end
local function getPlayerMoney(cid)
return ((getPlayerItemCount(cid, 2160) * 10000) +
(getPlayerItemCount(cid, 2152) * 100) +
getPlayerItemCount(cid, 2148))
end
local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if(items[item] == nil) then
selfSay("Ehm.. sorry... this shouldn't be there, I'm not selling it.", cid)
return
end
if(getPlayerMoney(cid) >= amount * items[item].buyPrice) then
local itemz, i = doNpcSellItem(cid, item, amount, subType, ignoreCap, inBackpacks)
if(i < amount) then
if(i == 0) then
selfSay("Sorry, but you don't have space to take it.", cid)
else
selfSay("I've sold some for you, but it seems you can't carry more than this. I won't take more money than necessary.", cid)
doPlayerRemoveMoney(cid, i * items[item].buyPrice)
end
else
selfSay("Thanks for the money!", cid)
doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
end
else
selfSay("Stfu noob, you don't have money.", cid)
end
end
local onSell = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if(items[item] == nil) then
selfSay("Ehm.. sorry... this shouldn't be there, I'm not buying it.", cid)
end
if(subType < 1) then
subType = -1
end
if(doPlayerRemoveItem(cid, item, amount, subType) == TRUE) then
doPlayerAddMoney(cid, items[item].sellPrice * amount)
selfSay("Here you are.", cid)
else
selfSay("No item, no deal.", cid)
end
end
function onCreatureSay(cid, type, msg)
if (msg == "trade") then
if (getPlayerStorageValue(cid, 12345) == -1) then
selfSay("I can\'t trade with you!", cid)
else
openShopWindow(cid, itemWindow, onBuy, onSell)
selfSay("It\'s my offer.", cid)
end
end
end