Good idea, this script never seen and very useful
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
function creatureSayCallback(cid, type, msg)
if(not npcHandler:isFocused(cid)) then
return false
end
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
local config = {
storage = 1234,
conversion = 0.9
}
local trades = {
{id=2457, buy=800, sell=100, name='steel helmet'},
{id=2471, buy=800, sell=100, name='golden helmet'}
}
local items = {}
for i = #trades, 1, -1 do
v = trades[i]
if getPlayerStorageValue(cid, config.storage) == -1 then
price = v.buy
else
price = math.floor(v.buy * config.conversion)
v.buy = math.floor(v.buy * config.conversion)
end
items[v.id] = {item_id = v.id, buyPrice = price, sellPrice = v.sell, subType = 0, realName = v.name}
end
local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if doPlayerRemoveMoney(cid, amount * items[item].buyPrice) then
selfSay('You have bought '..amount..' '..items[item].realName..' for '..items[item].buyPrice * amount..' gold coins.', cid)
doPlayerAddItem(cid, item, amount)
else
selfSay('You have not enough money.', cid)
end
end
local onSell = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if doPlayerRemoveItem(cid, item, amount) then
selfSay('You sold '..amount..' '..items[item].realName..' for '..items[item].sellPrice * amount..' gold coins.', cid)
doPlayerAddMoney(cid, amount * items[item].sellPrice)
end
end
if msgcontains(msg, 'trade') then
selfSay('It\'s my offert.', cid)
return openShopWindow(cid, trades, onBuy, onSell)
end
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
function changePriceTradeItems(cid, trades, conversion, storage, value, message)
if value == nil then value = 1 end
if message == nil then message = 'It\'s my offert.' end
local items = {}
for i = #trades, 1, -1 do
v = trades[i]
if getPlayerStorageValue(cid, storage) == value then
price = math.floor(v.buy * conversion)
v.buy = math.floor(v.buy * conversion)
else
price = v.buy
end
items[v.id] = {item_id = v.id, buyPrice = price, sellPrice = v.sell, subType = 0, realName = v.name}
end
local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if doPlayerRemoveMoney(cid, amount * items[item].buyPrice) then
selfSay('You have bought '..amount..' '..items[item].realName..' for '..items[item].buyPrice * amount..' gold coins.', cid)
doPlayerAddItem(cid, item, amount)
else
selfSay('You have not enough money.', cid)
end
end
local onSell = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if doPlayerRemoveItem(cid, item, amount) then
selfSay('You sold '..amount..' '..items[item].realName..' for '..items[item].sellPrice * amount..' gold coins.', cid)
doPlayerAddMoney(cid, amount * items[item].sellPrice)
end
end
selfSay(message, cid)
return openShopWindow(cid, trades, onBuy, onSell)
end
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
function creatureSayCallback(cid, type, msg)
if(not npcHandler:isFocused(cid)) then
return false
end
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
storage = 1234
conversion = 0.9
local trades = {
{id=2457, buy=800, sell=100, name='steel helmet'},
{id=2471, buy=800, sell=100, name='golden helmet'}
}
if msgcontains(msg, 'trade') then
return changePriceTradeItems(cid, trades, conversion, storage)
end
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
if(doPlayerRemoveItem(cid, itemid, amount, subType)) then
local msg = self.npcHandler:getMessage(MESSAGE_SOLD)
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, self.npcHandler:parseMessage(msg, parseInfo))
if getPlayerStorageValue(cid, 20500) == -1 then
doPlayerAddMoney(cid, amount * shopItem.sell)
elseif getPlayerStorageValue(cid, 20500) == 1 then
doPlayerAddMoney(cid, amount * shopItem.sell * 1.2)
end
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
self.npcHandler.talkStart[cid] = os.time()
else
self.npcHandler.talkStart = os.time()
end
return true
end