Critico
Sexy
- Joined
- Mar 25, 2010
- Messages
- 370
- Reaction score
- 176
CreatureScript
npc_shop.lua
TAG
------------------------------------------------------------------------------------
GLOBALEVENTS
npc_shop.lua
TAG
------------------------------------------------------------------------------------
LIB
Npc_ShopV0.lua
------------------------------------------------------------------------------------
LOGS
create a file.txt and rename to offline_msg.txt
------------------------------------------------------------------------------------
NPC
Npc Auction.xml
auction.lua
------------------------------------------------------------------------------------
QUERY
npc_shop.lua
Lua:
function onLogin(cid)
if getPlayerStorageValue(cid, _config_npc_shop.WithDraw_storage) == -1 then
setPlayerStorageValue(cid, _config_npc_shop.WithDraw_storage , 0)
end
local query = db.getResult("SELECT `id`, `item_id`, `count` FROM `shop_npc` WHERE `date` < ".. os.time() - _config_npc_shop.days_limit*24*60*60 .." and `player_id` = "..getPlayerGUID(cid))
if (query:getID() ~= -1) then
repeat
local item,amount,name = query:getDataInt("item_id"),query:getDataInt("count"), getPlayerName(cid)
local parcel = doCreateItemEx(ITEM_PARCEL)
if isItemStackable(item) or amount == 1 then
doAddContainerItem(parcel, item, amount)
else
for i = 1, amount do
doAddContainerItem(parcel, item, 1)
end
end
doPlayerSendMailByName(name, parcel)
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE,"[Npc Auction] Were returned "..amount.." "..getItemNameById(item).." from your depot.")
doDeleteShopItem(query:getDataInt("id"))
until not query:next()
query:free()
end
local file = io.open(OfflineMessagesConfig.messageSavingDirectory .. getCreatureName(cid) .. '.Gandhi', 'r')
if(not file) then
return true
end
if(not OfflineMessagesConfig.delayBeetwenShowMessageOnLogin) then
for line in file:lines() do
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, line)
end
else
local function sendLine(cid, msgType, filename)
if(not isPlayer(cid)) then -- check player is still online
return
end
local file = io.open(filename, 'r')
if(not file) then
return
end
local first, str = true, ''
for line in file:lines() do
if(first) then
doPlayerSendTextMessage(cid, msgType, line)
first = false
else
str = str .. line .. '\n'
end
end
file:close()
if(str ~= '') then
file = io.open(filename, 'w')
file:write(str)
file:close()
addEvent(sendLine, OfflineMessagesConfig.delayBeetwenShowMessageOnLogin, cid, msgType, filename)
else
os.remove(filename)
end
end
addEvent(sendLine, OfflineMessagesConfig.delayBeetwenShowMessageOnLogin, cid, MESSAGE_STATUS_CONSOLE_ORANGE, OfflineMessagesConfig.messageSavingDirectory .. getCreatureName(cid) .. '.Gandhi')
end
file:close()
return true
end
TAG
Code:
<event type="login" name="MsgNpcShop" event="script" value="npc_shop.lua"/>
------------------------------------------------------------------------------------
GLOBALEVENTS
npc_shop.lua
Lua:
function onThink(cid, interval, lastExecution)
local query = db.getResult("SELECT `id`, `player_id`, `item_id`, `count` FROM `shop_npc` WHERE `date` < "..os.time() - _config_npc_shop.days_limit*24*60*60)
if (query:getID() ~= -1) then
repeat
local item,amount, name = query:getDataInt("item_id"),query:getDataInt("count"), getPlayerByShopID(query:getDataInt("id"))
local player = getPlayerByNameWildcard(name)
if player then
local parcel = doCreateItemEx(ITEM_PARCEL)
if isItemStackable(item) or amount == 1 then
doAddContainerItem(parcel, item, amount)
else
for i = 1, amount do
doAddContainerItem(parcel, item, 1)
end
end
doPlayerSendMailByName(name, parcel)
doPlayerSendTextMessage(player, MESSAGE_STATUS_CONSOLE_ORANGE,"[Npc Auction] Were returned "..amount.." "..getItemNameById(item).." from your depot.")
doDeleteShopItem(query:getDataInt("id"))
end
until not query:next()
query:free()
end
return true
end
TAG
Code:
<globalevent name="ItemsNpcShopt" interval="3600" event="script" value="npc_shop.lua"/>
------------------------------------------------------------------------------------
LIB
Npc_ShopV0.lua
Lua:
_config_npc_shop = {
ShowMyItems_shop = true, -- se vai mostrar os seus proprios items no trade no shop
each_deposit = 1000, -- quanto vai custar para adc item no shop
days_limit = 7, -- limit de dias que o npc vai vender seu item
items_limit = 5, -- max items q vc pode depositar no npc
items_block = {2160,2152,2148}, -- items que o npc n vai poder vender
WithDraw_storage = 725201 -- n mexa
}
function doSetDepositNpc(player, amount, type)
local query = db.getResult("SELECT `value` FROM `player_storage` WHERE `player_id` = ".. player .." AND `key` = ".. _config_npc_shop.WithDraw_storage)
if query:getID() ~= -1 then
return db.executeQuery("UPDATE `player_storage` SET `value` = `value` "..(type == "add" and "+" or "-").." "..amount .." WHERE `player_id` = ".. player .." AND `key` = ".. _config_npc_shop.WithDraw_storage)
end
end
function getDepositNpc(cid)
return getPlayerStorageValue(cid, _config_npc_shop.WithDraw_storage)
end
function getItemByName(name)
local items = io.open("data/items/items.xml", "r"):read("*all")
local get = items:match('name="' .. name ..'"')
if get == nil or get == "" then
return false
end
return true
end
function getItemType(itemid)
local slot, slottypes = "", {"head", "body", "legs", "feet", "shield", "sword","wand", "club", "axe", "distance", "ammunition", "ammo"}
local arq = io.open("data/items/items.xml", "r"):read("*all")
local attributes = arq:match('<item id="' .. itemid .. '".+name="' .. getItemNameById(itemid) ..'">(.-)</item>')
if attributes ~= nil then
for i,x in pairs(slottypes) do
if attributes:find(x) then
slot = x
break
end
end
if slot == "body" then slot = "armor" end
slot = slot == "" and "others" or slot
else
local attributes = arq:match('<item id="' .. itemid .. '".+plural="' .. getItemNameById(itemid) ..'s">(.-)</item>')
if attributes ~= nil then
for i,x in pairs(slottypes) do
if attributes:find(x) then
slot = x
break
end
end
slot = slot == "" and "others" or slot
end
end
return slot
end
function getRowsShopByPlayer(player_id)
local qry,s = db.getResult("SELECT `player_id` FROM `shop_npc` WHERE `player_id` = "..player_id),0
if(qry:getID() ~= -1) then
repeat
s = s + 1
until not(qry:next())
qry:free()
end
return s
end
function isItemInShop(id, amount)
local info = db.getResult("SELECT `item_id`,`count` FROM `shop_npc` WHERE `id` = "..id)
if(info:getID() ~= -1) then
return info:getDataInt("count") < amount and false or true
end
return false
end
function doDeleteShopItem(id)
local info = db.getResult("SELECT `item_id` FROM `shop_npc` WHERE `id` = "..id)
if(info:getID() ~= -1) then
return db.executeQuery("DELETE FROM `shop_npc` WHERE `id` = "..id)
end
return false
end
function doAddItemShop(cid, itemid, amount, type, price)
return db.executeQuery("INSERT INTO `shop_npc` (`player_id`, `item_id`, `count`, `type`, `price`, `date`) VALUES ('".. getPlayerGUID(cid).."', '".. itemid .."', '".. amount .."', '".. type .."', '".. price .."','".. os.time() .."');")
end
function doRemoveShopItem(id, amount)
local info = db.getResult("SELECT `count` FROM `shop_npc` WHERE `id` = "..id)
if(info:getID() ~= -1) then
return db.executeQuery("UPDATE `shop_npc` SET `count` = `count` - "..amount.." WHERE id = "..id)
end
return false
end
function getPlayerByShopID(id)
local qry = db.getResult("SELECT `player_id` FROM `shop_npc` WHERE id = "..id)
if(qry:getID() ~= -1) then
return getPlayerNameByGUID(qry:getDataInt("player_id"))
end
end
---------- offline messages --------------
OfflineMessagesConfig = {
separatorAfterNickname = ',',
messageSavingDirectory = 'data/logs/offline_msg.txt', -- dir with saved messages (folder must exsist!)
delayBeetwenShowMessageOnLogin = 750, -- delay in ms to show received messages on login, to deactivate type 0
messageFormatString = '[%s] %s: %s', -- date, author, message
dateFormatString = '%d.%m.%Y %H:%M:%S' -- date string format
}
------------------------------------------------------------------------------------
LOGS
create a file.txt and rename to offline_msg.txt
------------------------------------------------------------------------------------
NPC
Npc Auction.xml
Code:
<?xml version="1.0"?>
<npc name="Npc Auction" script="data/npc/scripts/auction.lua" walkinterval="0" floorchange="0">
<health now="100" max="100"/>
<look type="325" head="0" body="114" legs="0" feet="0" addons="3"/>
<parameters>
<parameter key="message_greet" value="Hello |PLAYERNAME|. You can {deposit} some items, {withdraw} gps from deposits, {check} your deposits, {trade} for buy items or {remove} your itens from my shop!"/>
</parameters>
</npc>
auction.lua
Lua:
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,msg = NPCHANDLER_CONVbehavior == CONVERSATION_DEFAULT and 0 or cid,string.lower(msg)
local weaponsTypes = {"head", "armor", "legs", "feet", "shield", "sword", "wand", "rod", "club", "axe", "distance", "ammunition", "others", "all"}
if isInArray({'apprise', 'check', 'items', 'item', 'list'}, msg) then
local qry,k,str = db.getResult("SELECT `item_id`, `count`, `price` FROM `shop_npc` WHERE `player_id` = "..getPlayerGUID(cid)),0,"My Items Deposited:\n\n"
if(qry:getID() ~= -1) then
repeat
k = k + 1
str = str..""..qry:getDataInt("count").." "..getItemNameById(qry:getDataInt("item_id")).." - "..qry:getDataInt("price").." Gps "..(tonumber(qry:getDataInt("count")) > 1 and "Each" or "").."\n"
until not (qry:next()) or k >= _config_npc_shop.items_limit
qry:free()
doPlayerPopupFYI(cid, str)
else
selfSay("Currently you do not have items deposited in shop", cid)
end
elseif isInArray({'report', 'withdraw'}, msg) then
if getDepositNpc(cid) <= 0 then
selfSay("Sorry, but your balance is 0.", cid)
else
selfSay("You have "..getDepositNpc(cid).." gps deposited, you want to remove how many gps?", cid)
talkState[talkUser] = 8
end
elseif talkState[talkUser] == 8 then
if tonumber(msg) and tonumber(msg) > 0 and tonumber(msg) <= getDepositNpc(cid) then
setPlayerStorageValue(cid, _config_npc_shop.WithDraw_storage, (getDepositNpc(cid)-tonumber(msg)))
doPlayerAddMoney(cid, tonumber(msg))
selfSay("you withdrew "..msg.." gps, now you have "..getDepositNpc(cid).." of balance.", cid)
talkState[talkUser] = 0
else
selfSay("You can not remove that amount.", cid)
talkState[talkUser] = 0
end
elseif isInArray({'deposit', 'depositar', 'sell', 'selling','vender'}, msg) then
if getRowsShopByPlayer(getPlayerGUID(cid)) < _config_npc_shop.items_limit then
selfSay('Which item you want to put for sale?', cid)
talkState[talkUser] = 2
else
selfSay('Sorry, you can only add '.._config_npc_shop.items_limit..' items per time.', cid)
talkState[talkUser] = 0
end
elseif talkState[talkUser] == 2 then
name,item = msg,getItemByName(msg)
if msg == '' or tonumber(msg) or not item then
talkState[talkUser] = 0
selfSay("This item does not exist.", cid) return true
elseif not isItemMovable(item) or isInArray(_config_npc_shop.items_block, getItemIdByName(name)) then
talkState[talkUser] = 0
selfSay("you can not add this item.", cid) return true
end
item = getItemIdByName(name)
selfSay('how many of that item you want to add to the shop?', cid)
talkState[talkUser] = 3
elseif talkState[talkUser] == 3 then
if tonumber(msg) and tonumber(msg) > 0 and tonumber(msg) <= 100 and getPlayerItemCount(cid, item) >= tonumber(msg) then
count = tonumber(msg)
selfSay('very well, and that each item will cost how much?', cid)
talkState[talkUser] = 4
else
selfSay(getPlayerItemCount(cid, item) < tonumber(msg) and 'Sorry, but you not have '..msg..' items to deposit' or 'enter a valid number from 1 to 100.', cid)
end
elseif talkState[talkUser] == 4 then
if tonumber(msg) and tonumber(msg) > 0 and tonumber(msg) < 9999999 then
price = msg
selfSay('You really want to put {'..count..' '..name..'} per {'..price..'} gps '..(count > 1 and 'each' or '')..' for sale? Will cost '.. _config_npc_shop.each_deposit ..' by deposit, ok? {yes}', cid)
talkState[talkUser] = 6
else
selfSay('choose a valid number.', cid)
end
elseif msgcontains(msg, "yes") and talkState[talkUser] == 6 then
if doPlayerRemoveMoney(cid, _config_npc_shop.each_deposit) then
item,price = getItemIdByName(name),price
if doPlayerRemoveItem(cid, item, count) then
doAddItemShop(cid, item, count, getItemType(item), tonumber(price))
selfSay('Congratulations, the item was successfully added to the list and will be sold to date: '..os.date("%d %B %Y %X ", (os.time() + _config_npc_shop.days_limit*24*60*60))..'.', cid)
talkState[talkUser] = 0
else
selfSay('Sorry, but could not be completed because you do not have the items.', cid)
talkState[talkUser] = 0
end
else
selfSay('Sorry, but you do not have gps sufficient to deposit this item.', cid)
talkState[talkUser] = 0
end
elseif isInArray({'trade', 'comprar', 'buy', 'buying', 'shop'}, msg) then
selfSay('What type of item you want to buy: {head}, {armor}, {legs}, {feet}, {shield}, {sword}, {wand}, {rod}, {club}, {axe}, {distance}, {ammunition}, {others} or {all}?', cid)
talkState[talkUser] = 7
elseif talkState[talkUser] == 7 then
if isInArray(weaponsTypes, msg) then
if msg == "rod" then msg = "wand" end
local qry = msg == "all" and db.getResult("SELECT `id`, `player_id`, `item_id`, `count`, `price` FROM `shop_npc`;") or db.getResult("SELECT `id`, `player_id`, `item_id`, `count`, `price` FROM `shop_npc` WHERE `type` = "..db.escapeString(msg))
local trade,show = {},true
if(qry:getID() ~= -1) then
repeat
if not _config_npc_shop.ShowMyItems_shop and qry:getDataInt("player_id") == getPlayerGUID(cid) then
show = false
end
if show == true then
table.insert(trade,{id= qry:getDataInt("item_id"), buy= qry:getDataInt("price"), db= qry:getDataInt("id"), name= getItemNameById(qry:getDataInt("item_id")), amount = qry:getDataInt("count")})
end
until not(qry:next())
qry:free()
else
selfSay('Sorry, but there is no such item at the moment.', cid) return true
end
local items = {}
for _, item in ipairs(trade) do
items[item.id] = {item_id = item.id, buyPrice = item.buy, changeDb = item.db, subType = item.amount, realName = item.name}
end
local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if items[item].buyPrice ~= 0 and amount <= items[item].subType then
if isItemInShop(items[item].changeDb, amount) then
doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
if isItemStackable(items[item].item_id) then
doPlayerAddItem(cid, items[item].item_id, amount)
else
for i = 1, amount do
doPlayerAddItem(cid, items[item].item_id, 1)
end
end
local player,sell_price = getPlayerByNameWildcard(getPlayerByShopID(items[item].changeDb)),amount*items[item].buyPrice
if(not player) then
doSetDepositNpc(getPlayerGUIDByName(getPlayerByShopID(items[item].changeDb)), sell_price, "add")
local file = io.open(OfflineMessagesConfig.messageSavingDirectory .. getPlayerByShopID(items[item].changeDb) .. '.Gandhi', 'a+')
if(not file) then return true end
local msgfrom, msgoff = "[Npc Auction]",{"Were sold "..amount.." "..getItemNameById(items[item].item_id).." and you received "..sell_price.." gps by items."}
file:write(OfflineMessagesConfig.messageFormatString:format(os.date(OfflineMessagesConfig.dateFormatString), msgfrom, table.concat(msgoff, OfflineMessagesConfig.separatorAfterNickname)) .. '\n')
file:close()
else
setPlayerStorageValue(player,_config_npc_shop.WithDraw_storage, getDepositNpc(player)+sell_price)
doPlayerSendTextMessage(player, MESSAGE_STATUS_CONSOLE_ORANGE,"[Npc Auction] Were sold "..amount.." "..getItemNameById(items[item].item_id).." and you received "..sell_price.." gps by items.")
end
if amount == items[item].subType then
doDeleteShopItem(items[item].changeDb)
else
doRemoveShopItem(items[item].changeDb, amount)
end
selfSay('Here your item, Thanks!', cid)
closeShopWindow(cid)
else
selfSay('desculpe mas este item acabou de ser comprado.', cid)
closeShopWindow(cid)
end
else
selfSay('you can only buy '..items[item].subType..'x of items.', cid)
end
end
openShopWindow(cid, trade, onBuy, onSell)
else
selfSay('do not sell this item type.', cid)
end
elseif isInArray({'retirar', 'remove', 'retire', 'pegar', 'get'}, msg) then
selfSay('What item do you want to remove from my shop? reminding that I return the total amount of the item!', cid)
talkState[talkUser] = 15
elseif talkState[talkUser] == 15 then
name,item = msg,getItemByName(msg)
if msg == '' or tonumber(msg) or not item then
selfSay("This item does not exist.", cid) return true
end
local qry = db.getResult("SELECT `id`, `item_id`, `count` FROM `shop_npc` WHERE `player_id` = "..getPlayerGUID(cid).." and `item_id` = "..getItemIdByName(name))
if(qry:getID() ~= -1) then
local id, item, amount = qry:getDataInt("id"), qry:getDataInt("item_id"), qry:getDataInt("count")
if isItemStackable(item) then
doPlayerAddItem(cid, item, amount)
else
for i = 1, amount do
doPlayerAddItem(cid, item, 1)
end
end
doDeleteShopItem(id)
selfSay("Okay, removed "..amount.." "..name.." from my shop, thanks!", cid)
talkState[talkUser] = 0
else
selfSay("Sorry, but you not have this item!", cid)
end
elseif msg == "no" and talkState[talkUser] >= 1 then
selfSay("okay then...", cid)
talkState[talkUser] = 0
npcHandler:releaseFocus(cid)
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
------------------------------------------------------------------------------------
QUERY
Code:
-- Query to Sqlite:
CREATE TABLE shop_npc (
id INTEGER NOT NULL,
player_id INTEGER NOT NULL,
item_id INTEGER NOT NULL,
count INTEGER NOT NULL,
type VARCHAR( 255 ) NOT NULL,
price INT NOT NULL
DEFAULT 0,
date INTEGER NOT NULL,
PRIMARY KEY ( id )
);
-- Query to MySql:
CREATE TABLE shop_npc (
id INT NOT NULL AUTO_INCREMENT,
player_id INTEGER NOT NULL,
item_id INTEGER NOT NULL,
count INTEGER NOT NULL,
type VARCHAR( 255 ) NOT NULL,
price INT NOT NULL
DEFAULT 0,
date INTEGER NOT NULL,
PRIMARY KEY ( id )
);