• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

GlobalEvent Automatic Znote AAC Shop [TFS 1.0]

Printer

if Printer then print("LUA") end
Senator
Premium User
Joined
Dec 27, 2009
Messages
5,782
Solutions
31
Reaction score
2,284
Location
Sweden?
Add this into globalevents/globalevents.xml
XML:
<globalevent name="Znote Shop" interval="30000" script="znoteShop.lua"/>

And go into globalevents/scripts and create new lua and name it 'znoteShop'
Lua:
function onThink(interval, lastExecution)
    local players = Game.getPlayers()
    if #players == 0 then -- 0 players online, no need to execute the script
        return true
    end

    local player
    for i = 1, #players do
        player = players[i]
        local orderQuery = db.storeQuery('SELECT `id`, `type`, `itemid`, `count` FROM `znote_shop_orders` WHERE `account_id` = '.. player:getAccountId() ..' LIMIT 1;')
        if orderQuery then --Lets check if the players online have orderd something from the shop
            local orderId = result.getDataInt(orderQuery, 'id')
            local orderType = result.getDataInt(orderQuery, 'type')
            local orderItemId = result.getDataInt(orderQuery, 'itemid')
            local orderCount = result.getDataInt(orderQuery, 'count')
            result.free(orderQuery)

            if orderType == 1 then
                if player:addItemEx(Game.createItem(orderItemId, orderCount or 1)) ~= RETURNVALUE_NOERROR or
                    player:getFreeCapacity() < ItemType(orderItemId):getWeight(orderCount or 1) then --Lets check if player have slot or cape left. Else send to player inbox

                    local parcel = player:getInbox():addItem(2596, 1, false, 1)
                    if not parcel then --If not being able to create parcel we stop the script and retry again.
                        print('[ERROR Znote Shop Globalevents] = Error on creating a parcel.')
                        return true
                    end

                    local letter = parcel:addItem(2598, 1, false, 1)
                    letter:setAttribute(ITEM_ATTRIBUTE_TEXT, 'You have received your shop item. Thanks for donating.')
                    parcel:addItem(orderItemId, orderCount or 1, false, 1)
                end
 
                db.query('DELETE FROM `znote_shop_orders` WHERE `id` = '.. orderId ..';')
                player:sendTextMessage(MESSAGE_INFO_DESCR, 'Congratulations! You have received '.. orderCount ..'x '.. ItemType(orderItemId):getName() ..'!')
            end
        end
    end
    return true
end

Enjoy
 
Last edited:
Awesome man! Thanks alot for this release! Good work
 
I had a automatic script like this some time ago, and alot of people complained because this AAC don't have a function to buy items with certain character... So it might be annoying for some people... Specially with custom orders. Anyway, thanks for the awesome job <3
 
I had a automatic script like this some time ago, and alot of people complained because this AAC don't have a function to buy items with certain character... So it might be annoying for some people... Specially with custom orders. Anyway, thanks for the awesome job <3
Well Znote has no option to select a player :p Until it has, i would happily edit the script.
 
Let me guess, your not using TFS 1.x
 
I've updated to the new TFS, but now i get a error:

Code:
Lua Script Error: [GlobalEvent Interface] 
data/globalevents/scripts/shop.lua:onThink
data/globalevents/scripts/shop.lua:12: attempt to call field 'getDataInt' (a nil value)
stack traceback:
    [C]: in function 'getDataInt'
    data/globalevents/scripts/shop.lua:12: in function <data/globalevents/scripts/shop.lua:1>
[Error - GlobalEvents::think] Failed to execute event: Shop
 
You are missing that function from your compat.lua file, open it up and put in getDataInt = result.number, or something along those lines, check the master branch on github
 
This script uses low CPU for few players online and much CPU (one MySQL query for every player! these queries are simple, but they are synchronized and 200 queries may freez OTS for 0.1 sec) for many online, but number of not realized orders does not affect it.
In gesior I use version where it executes 1 query and then search for player online for every order, so many not realized order may slow down script, but searching for player online by name is faast.

Best version (for Znote - when there is no player name in database) would use new TFS 1.1 (maybe 1.0 too) asyncQueries, one 'think event' send queries and let server go on (parse players moves/spells etc.) and another 'think event' wait for mysql response and add items/search player.

I'm working right now on new version of shop.lua script for TFS 1.1 Gesior2012.
It will work without any compat.lua, 100% TFS 1.1 code. Delivers items and containers, add descripion with name of buyer to items that works with custom description (not stackable, not rune, not fluid):
Code:
-- ### 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
I will release it, when I finish Gesior2012 for TFS 1.1 with shop admin.
 
Last edited:
local players = Game.getPlayers()

Game returns null, I'm trying to set it up on OTHire, any ideas?
 
local players = Game.getPlayers()

Game returns null, I'm trying to set it up on OTHire, any ideas?
Maybe what you are looking for is getPlayersOnline() since you are using OTHire and it's not based in TFS 1.x
 
local players = Game.getPlayers()

Game returns null, I'm trying to set it up on OTHire, any ideas?
Just put this in functions.lua
Code:
Game = {}
function Game.getPlayers()
    return getPlayersOnlineList()
end
It might not be built for your server, but that doesn't mean you can't make it work for it either..
You'll just need to make the adjustments on your end to get it working or just edit the script you are trying to implement to use the correct functions :)
 
Guys its nice script but i have problem in TFS 1.2 im waiting waiting waiting and nothing happens :< command !shop works good
 

Similar threads

Back
Top