-- ### CONFIG ###
-- message send to player by script "type" (types you can check in "global.lua")
SHOP_MSG_TYPE = 36
-- ### END OF CONFIG ###
function onThink(interval)
local deliveryInterval = interval / 1000
local resultId = db.storeQuery("SELECT * FROM z_ots_comunication")
if(resultId) then
repeat
local id = result.getNumber(resultId, "id")
local player = Player(result.getString(resultId, "name"))
if(player) then
local addItemType = result.getString(resultId, "param5")
if(addItemType == 'item' or addItemType == 'container') then
local itemId = result.getNumber(resultId, "param1")
local itemCount = result.getNumber(resultId, "param2")
local containerId = result.getNumber(resultId, "param3")
local containerCount = result.getNumber(resultId, "param4")
local addItemName = result.getString(resultId, "param6")
local itemReceivedStatus = 0
local itemItemType = ItemType(itemId)
local containerItemType = ItemType(containerId)
local itemsWeight = 0
if(itemItemType:isRune() or itemItemType:isFluidContainer() or itemItemType:getCharges() > 0) then
itemsWeight = itemItemType:getWeight(1)
else
itemsWeight = itemItemType:getWeight(itemCount)
end
if(addItemType == 'container') then
itemsWeight = itemsWeight * containerCount + containerItemType:getWeight(1)
end
local freeCapacity = player:getFreeCapacity()
if(itemsWeight <= freeCapacity) then
local itemForPlayer = 0
if(addItemType == 'container') then
itemForPlayer = Container(doCreateItemEx(containerId, 1))
itemForPlayer:setAttribute("description", containerItemType:getDescription() .. "\nBought by " .. player:getName() .. ".")
local innerContainerItem = 0
for i = 1, containerCount do
innerContainerItem = Item(doCreateItemEx(itemId, itemCount))
if(not itemItemType:isRune() and not itemItemType:isFluidContainer() and not itemItemType:isStackable()) then
innerContainerItem:setAttribute("description", itemItemType:getDescription() .. "\nBought by " .. player:getName() .. ".")
end
itemForPlayer:addItemEx(innerContainerItem)
end
else
itemForPlayer = Item(doCreateItemEx(itemId, itemCount))
if(not itemItemType:isRune() and not itemItemType:isFluidContainer() and not itemItemType:isStackable()) then
itemForPlayer:setAttribute("description", itemItemType:getDescription() .. "\nBought by " .. player:getName() .. ".")
end
end
itemReceivedStatus = player:addItemEx(itemForPlayer, false)
if(type(itemReceivedStatus) == "number" and itemReceivedStatus == 0) then
player:sendTextMessage(SHOP_MSG_TYPE, 'You received >> '.. addItemName ..' << from OTS shop.')
db.query("DELETE FROM `z_ots_comunication` WHERE `id` = " .. id .. ";")
db.query("UPDATE `z_shop_history_item` SET `trans_state`='realized', `trans_real`=" .. os.time() .. " WHERE id = " .. id .. ";")
else
player:sendTextMessage(SHOP_MSG_TYPE, '>> '.. addItemName ..' << from OTS shop is waiting for you. Please make place for this item in your backpack/hands and wait about '.. deliveryInterval ..' seconds to get it.')
end
else
player:sendTextMessage(SHOP_MSG_TYPE, '>> '.. addItemName ..' << from OTS shop is waiting for you. It weight is '.. (itemsWeight / 100) ..' oz., you have only '.. (freeCapacity / 100) ..' oz. free capacity. Put some items in depot and wait about '.. deliveryInterval ..' seconds to get it.')
end
end
end
until not result.next(resultId)
result.free(resultId)
end
return true
end