-- ### config ###
-- message send to player by script "type" (types you can check in "global.lua")
shop_msg_type = 19
-- time (in seconds) between connections to sql database by shop script
sql_interval = 30
-- ### end of config ###
sql_comunication_interval = sql_interval * 1000
function onlogin(cid)
if(initshopcomunication == 0) then
local eventserv = addevent(sql_communication, sql_comunication_interval, {})
initshopcomunication = eventserv
end
registercreatureevent(cid, "playerdeath")
return true
end
function sql_communication(parameters)
dofile("./config.lua")
env = assert(luasql.mysql())
con = assert(env:connect(mysqldatabase, mysqluser, mysqlpass, mysqlhost, mysqlport))
result_plr = assert(con:execute("select * from z_ots_comunication where `type` = 'login';"))
todo = result_plr:fetch({}, "a")
while todo do
id = tonumber(todo.id)
action = tostring(todo.action)
delete = tonumber(todo.delete_it)
cid = getplayerbyname(tostring(todo.name))
if isplayer(cid) == true then
local itemtogive_id = tonumber(todo.param1)
local itemtogive_count = tonumber(todo.param2)
local container_id = tonumber(todo.param3)
local container_count = tonumber(todo.param4)
local add_item_type = tostring(todo.param5)
local add_item_name = tostring(todo.param6)
local received_item = 0
local full_weight = 0
if add_item_type == 'container' then
container_weight = getitemweight(container_id, 1)
if isitemrune(itemtogive_id) == true then
items_weight = container_count * getitemweight(itemtogive_id, 1)
else
items_weight = container_count * getitemweight(itemtogive_id, itemtogive_count)
end
full_weight = items_weight + container_weight
else
full_weight = getitemweight(itemtogive_id, itemtogive_count)
if isitemrune(itemtogive_id) == true then
full_weight = getitemweight(itemtogive_id, 1)
else
full_weight = getitemweight(itemtogive_id, itemtogive_count)
end
end
local free_cap = getplayerfreecap(cid)
if full_weight <= free_cap then
if add_item_type == 'container' then
local new_container = docreateitemex(container_id, 1)
local iter = 0
while iter ~= container_count do
doaddcontaineritem(new_container, itemtogive_id, itemtogive_count)
iter = iter + 1
end
received_item = doplayeradditemex(cid, new_container)
else
local new_item = docreateitemex(itemtogive_id, itemtogive_count)
received_item = doplayeradditemex(cid, new_item)
end
if received_item == returnvalue_noerror then
doplayersendtextmessage(cid, shop_msg_type, 'you received >> '.. Add_item_name ..' << from ots shop.')
delete = assert(con:execute("delete from `z_ots_comunication` where `id` = " .. Id .. ";"))
assert(con:execute("update `z_shop_history_item` set `trans_state`='realized', `trans_real`=" .. Os.time() .. " where id = " .. Id .. ";"))
else
doplayersendtextmessage(cid, shop_msg_type, '>> '.. Add_item_name ..' << from ots shop is waiting for you. Please make place for this item in your backpack/hands and wait about '.. Sql_interval ..' seconds to get it.')
end
else
doplayersendtextmessage(cid, shop_msg_type, '>> '.. Add_item_name ..' << from ots shop is waiting for you. It weight is '.. Full_weight ..' oz., you have only '.. Free_cap ..' oz. Free capacity. Put some items in depot and wait about '.. Sql_interval ..' seconds to get it.')
end
end
todo = result_plr:fetch (todo, "a")
end
con:close()
env:close()
local eventserv = addevent(sql_communication, sql_comunication_interval, parameters)
end